diff --git a/compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala b/compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala index 8ded18fa9036..1d0ed035df09 100644 --- a/compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala +++ b/compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala @@ -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 } diff --git a/tests/neg-macros/i16532.check b/tests/neg-macros/i16532.check new file mode 100644 index 000000000000..45dc9d07dcaf --- /dev/null +++ b/tests/neg-macros/i16532.check @@ -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). diff --git a/tests/neg-macros/i16532.scala b/tests/neg-macros/i16532.scala new file mode 100644 index 000000000000..d1edfdd80088 --- /dev/null +++ b/tests/neg-macros/i16532.scala @@ -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 + }