Skip to content

Commit a9580f2

Browse files
committed
Fix #10709: Add missing level check before inlining
If an a call to an inline method is within a quote, this call must not be inlined. * Delay inlining within quotes. * Disallow `inline def` within quotes. Same as `inline def` in `inline def`.
1 parent 3c18f6b commit a9580f2

File tree

7 files changed

+21
-31
lines changed

7 files changed

+21
-31
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,8 @@ class Typer extends Namer
19951995
else typedExpr(ddef.rhs, tpt1.tpe.widenExpr)(using rhsCtx))
19961996

19971997
if sym.isInlineMethod then
1998+
if StagingContext.level > 0 then
1999+
report.error("inline def cannot be within quotes", sym.sourcePos)
19982000
val rhsToInline = PrepareInlineable.wrapRHS(ddef, tpt1, rhs1)
19992001
PrepareInlineable.registerInlineInfo(sym, rhsToInline)
20002002

@@ -3293,7 +3295,7 @@ class Typer extends Namer
32933295
}
32943296
else if (methPart(tree).symbol.isAllOf(Inline | Deferred) && !Inliner.inInlineMethod) then
32953297
errorTree(tree, i"Deferred inline ${methPart(tree).symbol.showLocated} cannot be invoked")
3296-
else if (Inliner.isInlineable(tree) && !suppressInline) {
3298+
else if (Inliner.isInlineable(tree) && !suppressInline && StagingContext.level == 0) {
32973299
tree.tpe <:< wildApprox(pt)
32983300
val errorCount = ctx.reporter.errorCount
32993301
val meth = methPart(tree).symbol

tests/neg-macros/i10709/Macro_1.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted._
2+
import scala.compiletime.summonInline
3+
4+
object Invalid {
5+
inline def apply[A, B]: Any = ${ invalidImpl[A, B] }
6+
7+
def invalidImpl[A, B](using qctx: Quotes, tpeA: Type[A], tpeB: Type[B]): Expr[Any] = {
8+
'{summonInline[B <:< A]}
9+
}
10+
}

tests/neg-macros/i10709/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class A
2+
class B extends A
3+
4+
def test: Unit =
5+
println(Invalid[A, B]) // compiles as expected
6+
println(Invalid[B, A]) // error

tests/run-macros/i4803e/Macro_1.scala renamed to tests/neg-macros/i4803e.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted._
22

33
object PowerMacro {
44
def power2(x: Expr[Double])(using Quotes) = '{
5-
inline def power(x: Double, n: Long): Double =
5+
inline def power(x: Double, n: Long): Double = // error
66
if (n == 0) 1.0
77
else if (n % 2 == 0) { val y = x * x; power(y, n / 2) }
88
else x * power(x, n - 1)

tests/run-macros/i4803f/Macro_1.scala renamed to tests/neg-macros/i4803f.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object PowerMacro {
77
else '{ $x * ${powerCode(x, n - 1)} }
88

99
def power2(x: Expr[Double])(using Quotes) = '{
10-
inline def power(x: Double): Double = ${powerCode('x, 2)}
10+
inline def power(x: Double): Double = ${powerCode('x, 2)} // error
1111
power($x)
1212
}
1313
}

tests/run-macros/i4803e/App_2.scala

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/run-macros/i4803f/App_2.scala

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)