Skip to content

Commit

Permalink
Fix scala#10709: Add missing level check before inlining
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
nicolasstucki committed Dec 14, 2020
1 parent 3c18f6b commit a9580f2
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 31 deletions.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,8 @@ class Typer extends Namer
else typedExpr(ddef.rhs, tpt1.tpe.widenExpr)(using rhsCtx))

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

Expand Down Expand Up @@ -3293,7 +3295,7 @@ class Typer extends Namer
}
else if (methPart(tree).symbol.isAllOf(Inline | Deferred) && !Inliner.inInlineMethod) then
errorTree(tree, i"Deferred inline ${methPart(tree).symbol.showLocated} cannot be invoked")
else if (Inliner.isInlineable(tree) && !suppressInline) {
else if (Inliner.isInlineable(tree) && !suppressInline && StagingContext.level == 0) {
tree.tpe <:< wildApprox(pt)
val errorCount = ctx.reporter.errorCount
val meth = methPart(tree).symbol
Expand Down
10 changes: 10 additions & 0 deletions tests/neg-macros/i10709/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.quoted._
import scala.compiletime.summonInline

object Invalid {
inline def apply[A, B]: Any = ${ invalidImpl[A, B] }

def invalidImpl[A, B](using qctx: Quotes, tpeA: Type[A], tpeB: Type[B]): Expr[Any] = {
'{summonInline[B <:< A]}
}
}
6 changes: 6 additions & 0 deletions tests/neg-macros/i10709/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class A
class B extends A

def test: Unit =
println(Invalid[A, B]) // compiles as expected
println(Invalid[B, A]) // error
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import scala.quoted._

object PowerMacro {
def power2(x: Expr[Double])(using Quotes) = '{
inline def power(x: Double, n: Long): Double =
inline def power(x: Double, n: Long): Double = // error
if (n == 0) 1.0
else if (n % 2 == 0) { val y = x * x; power(y, n / 2) }
else x * power(x, n - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object PowerMacro {
else '{ $x * ${powerCode(x, n - 1)} }

def power2(x: Expr[Double])(using Quotes) = '{
inline def power(x: Double): Double = ${powerCode('x, 2)}
inline def power(x: Double): Double = ${powerCode('x, 2)} // error
power($x)
}
}
14 changes: 0 additions & 14 deletions tests/run-macros/i4803e/App_2.scala

This file was deleted.

14 changes: 0 additions & 14 deletions tests/run-macros/i4803f/App_2.scala

This file was deleted.

0 comments on commit a9580f2

Please sign in to comment.