diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index e8524a193e5a..ef2eacf42225 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -301,7 +301,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler object DefDef extends DefDefModule: def apply(symbol: Symbol, rhsFn: List[List[Tree]] => Option[Term]): DefDef = - xCheckMacroAssert(symbol.isTerm, s"expected a term symbol but received $symbol") + xCheckMacroAssert(symbol.isTerm, s"expected a term symbol, but received $symbol") xCheckMacroAssert(symbol.flags.is(Flags.Method), "expected a symbol with `Method` flag set") withDefaultPos(tpd.DefDef(symbol.asTerm, prefss => xCheckedMacroOwners(xCheckMacroValidExpr(rhsFn(prefss)), symbol).getOrElse(tpd.EmptyTree) @@ -472,7 +472,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler def term(tp: TermRef): Ref = withDefaultPos(tpd.ref(tp).asInstanceOf[tpd.RefTree]) def apply(sym: Symbol): Ref = - assert(sym.isTerm) + assert(sym.isTerm, s"expected a term symbol, but received $sym") val refTree = tpd.ref(sym) match case t @ tpd.This(ident) => // not a RefTree, so we need to work around this - issue #19732 // ident in `This` can be a TypeIdent of sym, so we manually prepare the ref here, @@ -1128,7 +1128,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeTree = tp.asInstanceOf[TypeImpl].typeTree def ref(sym: Symbol): TypeTree = - assert(sym.isType, "Expected a type symbol, but got " + sym) + assert(sym.isType, s"Expected a type symbol, but got $sym") tpd.ref(sym) end TypeTree @@ -1162,7 +1162,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler object TypeIdent extends TypeIdentModule: def apply(sym: Symbol): TypeTree = - assert(sym.isType) + assert(sym.isType, s"Expected a type symbol, but got $sym") withDefaultPos(tpd.ref(sym).asInstanceOf[tpd.TypeTree]) def copy(original: Tree)(name: String): TypeIdent = tpd.cpy.Ident(original)(name.toTypeName) diff --git a/tests/neg/i20946/Macro_1.scala b/tests/neg/i20946/Macro_1.scala new file mode 100644 index 000000000000..f598f5d278ce --- /dev/null +++ b/tests/neg/i20946/Macro_1.scala @@ -0,0 +1,14 @@ +import scala.quoted.* + +def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = { + import quotes.reflect.* + + try + Ref(TypeRepr.of[T].typeSymbol) + catch + case ex: Throwable => + if ex.getMessage().contains("expected a term symbol, but received ") then + throw ex + + '{()} +} diff --git a/tests/neg/i20946/Test_2.scala b/tests/neg/i20946/Test_2.scala new file mode 100644 index 000000000000..80ae0a95fa4b --- /dev/null +++ b/tests/neg/i20946/Test_2.scala @@ -0,0 +1,5 @@ +inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) } + +@main +def run = + macroWithAssertFailing[Int](123) // error diff --git a/tests/neg/i20946a/Macro_1.scala b/tests/neg/i20946a/Macro_1.scala new file mode 100644 index 000000000000..b3603fe91b10 --- /dev/null +++ b/tests/neg/i20946a/Macro_1.scala @@ -0,0 +1,14 @@ +import scala.quoted.* + +def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = { + import quotes.reflect.* + + try + TypeIdent(t.asTerm.symbol) + catch + case ex: Throwable => + if ex.getMessage().contains("Expected a type symbol, but got ") then + throw ex + + '{()} +} diff --git a/tests/neg/i20946a/Test_2.scala b/tests/neg/i20946a/Test_2.scala new file mode 100644 index 000000000000..80ae0a95fa4b --- /dev/null +++ b/tests/neg/i20946a/Test_2.scala @@ -0,0 +1,5 @@ +inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) } + +@main +def run = + macroWithAssertFailing[Int](123) // error