Skip to content

Commit d1a0ea5

Browse files
committed
Fix #8866: Inline calls inserted by macros
1 parent ad309fa commit d1a0ea5

File tree

9 files changed

+123
-1
lines changed

9 files changed

+123
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,8 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
12371237
case res: Apply if res.symbol == defn.InternalQuoted_exprSplice
12381238
&& level == 0
12391239
&& !suppressInline =>
1240-
expandMacro(res.args.head, tree.span)
1240+
val expanded = expandMacro(res.args.head, tree.span)
1241+
typedExpr(expanded) // Inline calls and constant fold code generated by the macro
12411242
case res => res
12421243
}
12431244

tests/pos-macros/i8866/Macro_1.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import scala.quoted._
2+
3+
object OtherMacro {
4+
5+
def impl(using qctx: QuoteContext): Expr[Int] =
6+
'{ 42 }
7+
8+
inline def apply = ${ OtherMacro.impl }
9+
10+
}
11+
12+
object Macro {
13+
14+
def impl(using qctx: QuoteContext): Expr[Int] = {
15+
import qctx.tasty._
16+
17+
let(
18+
Select.unique(
19+
'{ OtherMacro }.unseal,
20+
"apply"
21+
)
22+
)(identity).seal.cast[Int]
23+
}
24+
25+
inline def apply = ${ Macro.impl }
26+
27+
}

tests/pos-macros/i8866/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val a = Macro.apply

tests/pos-macros/i8866b/Macro_1.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.quoted._
2+
3+
object Other {
4+
inline def apply = 5
5+
}
6+
7+
object Macro {
8+
9+
def impl(using qctx: QuoteContext): Expr[Int] = {
10+
import qctx.tasty._
11+
12+
let(
13+
Select.unique(
14+
'{ Other }.unseal,
15+
"apply"
16+
)
17+
)(identity).seal.cast[Int]
18+
19+
}
20+
21+
inline def apply = ${ Macro.impl }
22+
23+
}

tests/pos-macros/i8866b/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val a = Macro.apply

tests/pos-macros/i8866c/Macro_1.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.quoted._
2+
3+
def f(xs: Boolean*): Unit = ???
4+
5+
def mcrImpl(using QuoteContext): Expr[Unit] =
6+
val func: Expr[Seq[Boolean] => Unit] =
7+
'{(esx: Seq[Boolean]) => f(esx: _*)}
8+
val trees: Expr[Seq[Boolean]] = '{Seq(true)}
9+
Expr.betaReduce('{ $func($trees) })
10+
end mcrImpl
11+
12+
inline def mcr: Unit = ${ mcrImpl }

tests/pos-macros/i8866c/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val x = mcr

tests/pos-macros/i9687/Macro_1.scala

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package x
2+
3+
import scala.quoted._
4+
5+
object FastPath {
6+
7+
inline def sum(x:Int):Int =
8+
x + 1
9+
10+
}
11+
12+
object SlowPath {
13+
14+
def sum(x:Int):Int =
15+
x + 1
16+
17+
}
18+
19+
object X {
20+
21+
inline def transform[A](inline x:A):A = ${
22+
transformImpl[A]('x)
23+
}
24+
25+
def transformImpl[A:Type](x:Expr[A])(using qctx: QuoteContext):Expr[A] = {
26+
import qctx.tasty._
27+
val slowPath = '{ SlowPath }.unseal
28+
val fastPath = '{ FastPath }.unseal
29+
val transformer = new TreeMap() {
30+
override def transformTerm(term:Term)(using ctx:Context):Term = {
31+
term match
32+
case Apply(sel@Select(o,m),args) =>
33+
if ( o.tpe =:= slowPath.tpe && m=="sum" )
34+
Apply(Select.unique(fastPath,"sum"), args)
35+
else
36+
super.transformTerm(term)
37+
case _ => super.transformTerm(term)
38+
}
39+
}
40+
val r = transformer.transformTerm(x.unseal).seal.cast[A]
41+
println(s"result: ${r.show}")
42+
r
43+
}
44+
}

tests/pos-macros/i9687/Test_2.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package x
2+
3+
4+
object Main {
5+
6+
7+
def main(args:Array[String]):Unit =
8+
val r = X.transform{
9+
SlowPath.sum(1)
10+
}
11+
12+
}

0 commit comments

Comments
 (0)