Skip to content

Commit

Permalink
Add error hint on local inline def used in quotes (#16572)
Browse files Browse the repository at this point in the history
Closes #16532
  • Loading branch information
nicolasstucki authored Dec 23, 2022
2 parents 431f05b + 7fd1cbc commit 42c361c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,16 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
if (!tp.isInstanceOf[ThisType]) sym.show
else if (sym.is(ModuleClass)) sym.sourceModule.show
else i"${sym.name}.this"
val hint =
if sym.is(Inline) && levelOf(sym) < level then
"\n\n" +
"Hint: Staged references to inline definition in quotes are only inlined after the quote is spliced into level 0 code by a macro. " +
"Try moving this inline definition in a statically accessible location such as an object (this definition can be private)."
else ""
report.error(
em"""access to $symStr from wrong staging level:
| - the definition is at level ${levelOf(sym)},
| - but the access is at level $level.""", pos)
| - but the access is at level $level.$hint""", pos)
tp
}

Expand Down
8 changes: 8 additions & 0 deletions tests/neg-macros/i16532.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Error: tests/neg-macros/i16532.scala:7:13 ---------------------------------------------------------------------------
7 | val x2 = recurseII($a, $b) // error
| ^^^^^^^^^
|access to method recurseII from wrong staging level:
| - the definition is at level 0,
| - but the access is at level 1.
|
|Hint: Staged references to inline definition in quotes are only inlined after the quote is spliced into level 0 code by a macro. Try moving this inline definition in a statically accessible location such as an object (this definition can be private).
9 changes: 9 additions & 0 deletions tests/neg-macros/i16532.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted.*

def power0Impl(a: Expr[Int], b: Expr[Int])(using Quotes): Expr[Int] =
inline def recurseII(a:Int, n:Int): Int = ???

'{
val x2 = recurseII($a, $b) // error
x2
}

0 comments on commit 42c361c

Please sign in to comment.