From 39d45dc82df20306bdd32ee4ea72f494c6ab6ff1 Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Tue, 29 Oct 2024 10:31:49 +0100 Subject: [PATCH] Add missing error messages to asserts in QuotesImpl --- .../scala/quoted/runtime/impl/QuotesImpl.scala | 4 ++-- tests/neg/i20946.check | 18 ++++++++++++++++++ tests/neg/i20946/Macro_1.scala | 10 ++++++++++ tests/neg/i20946/Test_2.scala | 6 ++++++ tests/neg/i20946a.check | 18 ++++++++++++++++++ tests/neg/i20946a/Macro_1.scala | 10 ++++++++++ tests/neg/i20946a/Test_2.scala | 6 ++++++ 7 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tests/neg/i20946.check create mode 100644 tests/neg/i20946/Macro_1.scala create mode 100644 tests/neg/i20946/Test_2.scala create mode 100644 tests/neg/i20946a.check create mode 100644 tests/neg/i20946a/Macro_1.scala create mode 100644 tests/neg/i20946a/Test_2.scala diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index e8524a193e5a..abda4aa191a9 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -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, @@ -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.check b/tests/neg/i20946.check new file mode 100644 index 000000000000..acce8bf4852d --- /dev/null +++ b/tests/neg/i20946.check @@ -0,0 +1,18 @@ + +-- Error: tests/neg/i20946/Test_2.scala:5:29 --------------------------------------------------------------------------- +5 | macroWithAssertFailing[Int](123) // error + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | Exception occurred while executing macro expansion. + | java.lang.AssertionError: assertion failed: expected a term symbol but received class Int + | at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) + | at scala.quoted.runtime.impl.QuotesImpl$reflect$Ref$.apply(QuotesImpl.scala:475) + | at scala.quoted.runtime.impl.QuotesImpl$reflect$Ref$.apply(QuotesImpl.scala:474) + | at Macro_1$package$.macroWithAssertFailingImpl(Macro_1.scala:6) + | + |--------------------------------------------------------------------------------------------------------------------- + |Inline stack trace + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + |This location contains code that was inlined from Test_2.scala:1 +1 |inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + --------------------------------------------------------------------------------------------------------------------- diff --git a/tests/neg/i20946/Macro_1.scala b/tests/neg/i20946/Macro_1.scala new file mode 100644 index 000000000000..0f2bb8416a0c --- /dev/null +++ b/tests/neg/i20946/Macro_1.scala @@ -0,0 +1,10 @@ +import scala.quoted.* + +def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = { + import quotes.reflect.* + + Ref(TypeRepr.of[T].typeSymbol) + + '{()} +} + diff --git a/tests/neg/i20946/Test_2.scala b/tests/neg/i20946/Test_2.scala new file mode 100644 index 000000000000..79a02ff1a5db --- /dev/null +++ b/tests/neg/i20946/Test_2.scala @@ -0,0 +1,6 @@ +inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) } + +@main +def run = + macroWithAssertFailing[Int](123) // error + diff --git a/tests/neg/i20946a.check b/tests/neg/i20946a.check new file mode 100644 index 000000000000..f279a60a4798 --- /dev/null +++ b/tests/neg/i20946a.check @@ -0,0 +1,18 @@ + +-- Error: tests/neg/i20946a/Test_2.scala:5:29 -------------------------------------------------------------------------- +5 | macroWithAssertFailing[Int](123) // error + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | Exception occurred while executing macro expansion. + | java.lang.AssertionError: assertion failed: Expected a type symbol, but got val + | at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) + | at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeIdent$.apply(QuotesImpl.scala:1165) + | at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeIdent$.apply(QuotesImpl.scala:1164) + | at Macro_1$package$.macroWithAssertFailingImpl(Macro_1.scala:6) + | + |--------------------------------------------------------------------------------------------------------------------- + |Inline stack trace + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + |This location contains code that was inlined from Test_2.scala:1 +1 |inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + --------------------------------------------------------------------------------------------------------------------- diff --git a/tests/neg/i20946a/Macro_1.scala b/tests/neg/i20946a/Macro_1.scala new file mode 100644 index 000000000000..c0e9e6eec116 --- /dev/null +++ b/tests/neg/i20946a/Macro_1.scala @@ -0,0 +1,10 @@ +import scala.quoted.* + +def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = { + import quotes.reflect.* + + TypeIdent(t.asTerm.symbol) + + '{()} +} + diff --git a/tests/neg/i20946a/Test_2.scala b/tests/neg/i20946a/Test_2.scala new file mode 100644 index 000000000000..79a02ff1a5db --- /dev/null +++ b/tests/neg/i20946a/Test_2.scala @@ -0,0 +1,6 @@ +inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) } + +@main +def run = + macroWithAssertFailing[Int](123) // error +