Skip to content

Commit 69ba0fe

Browse files
committed
Fix #8866: Inline calls inserted by macros
1 parent fe1b3ae commit 69ba0fe

File tree

7 files changed

+67
-1
lines changed

7 files changed

+67
-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

0 commit comments

Comments
 (0)