Skip to content

Commit 42c361c

Browse files
Add error hint on local inline def used in quotes (#16572)
Closes #16532
2 parents 431f05b + 7fd1cbc commit 42c361c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,16 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
264264
if (!tp.isInstanceOf[ThisType]) sym.show
265265
else if (sym.is(ModuleClass)) sym.sourceModule.show
266266
else i"${sym.name}.this"
267+
val hint =
268+
if sym.is(Inline) && levelOf(sym) < level then
269+
"\n\n" +
270+
"Hint: Staged references to inline definition in quotes are only inlined after the quote is spliced into level 0 code by a macro. " +
271+
"Try moving this inline definition in a statically accessible location such as an object (this definition can be private)."
272+
else ""
267273
report.error(
268274
em"""access to $symStr from wrong staging level:
269275
| - the definition is at level ${levelOf(sym)},
270-
| - but the access is at level $level.""", pos)
276+
| - but the access is at level $level.$hint""", pos)
271277
tp
272278
}
273279

tests/neg-macros/i16532.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Error: tests/neg-macros/i16532.scala:7:13 ---------------------------------------------------------------------------
2+
7 | val x2 = recurseII($a, $b) // error
3+
| ^^^^^^^^^
4+
|access to method recurseII from wrong staging level:
5+
| - the definition is at level 0,
6+
| - but the access is at level 1.
7+
|
8+
|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).

tests/neg-macros/i16532.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted.*
2+
3+
def power0Impl(a: Expr[Int], b: Expr[Int])(using Quotes): Expr[Int] =
4+
inline def recurseII(a:Int, n:Int): Int = ???
5+
6+
'{
7+
val x2 = recurseII($a, $b) // error
8+
x2
9+
}

0 commit comments

Comments
 (0)