From 39d45dc82df20306bdd32ee4ea72f494c6ab6ff1 Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Tue, 29 Oct 2024 10:31:49 +0100 Subject: [PATCH 1/2] 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 + From 014d0dbb4df6ee6b93aad7692a35996c962cded6 Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Fri, 1 Nov 2024 10:28:32 +0100 Subject: [PATCH 2/2] Change tests for i20946 to only check the assert message in code --- .../scala/quoted/runtime/impl/QuotesImpl.scala | 6 +++--- tests/neg/i20946.check | 18 ------------------ tests/neg/i20946/Macro_1.scala | 8 ++++++-- tests/neg/i20946/Test_2.scala | 1 - tests/neg/i20946a.check | 18 ------------------ tests/neg/i20946a/Macro_1.scala | 8 ++++++-- tests/neg/i20946a/Test_2.scala | 1 - 7 files changed, 15 insertions(+), 45 deletions(-) delete mode 100644 tests/neg/i20946.check delete mode 100644 tests/neg/i20946a.check diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index abda4aa191a9..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, s"expected a term symbol but received $sym") + 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 diff --git a/tests/neg/i20946.check b/tests/neg/i20946.check deleted file mode 100644 index acce8bf4852d..000000000000 --- a/tests/neg/i20946.check +++ /dev/null @@ -1,18 +0,0 @@ - --- 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 index 0f2bb8416a0c..f598f5d278ce 100644 --- a/tests/neg/i20946/Macro_1.scala +++ b/tests/neg/i20946/Macro_1.scala @@ -3,8 +3,12 @@ import scala.quoted.* def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = { import quotes.reflect.* - Ref(TypeRepr.of[T].typeSymbol) + 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 index 79a02ff1a5db..80ae0a95fa4b 100644 --- a/tests/neg/i20946/Test_2.scala +++ b/tests/neg/i20946/Test_2.scala @@ -3,4 +3,3 @@ inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl @main def run = macroWithAssertFailing[Int](123) // error - diff --git a/tests/neg/i20946a.check b/tests/neg/i20946a.check deleted file mode 100644 index f279a60a4798..000000000000 --- a/tests/neg/i20946a.check +++ /dev/null @@ -1,18 +0,0 @@ - --- 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 index c0e9e6eec116..b3603fe91b10 100644 --- a/tests/neg/i20946a/Macro_1.scala +++ b/tests/neg/i20946a/Macro_1.scala @@ -3,8 +3,12 @@ import scala.quoted.* def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = { import quotes.reflect.* - TypeIdent(t.asTerm.symbol) + 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 index 79a02ff1a5db..80ae0a95fa4b 100644 --- a/tests/neg/i20946a/Test_2.scala +++ b/tests/neg/i20946a/Test_2.scala @@ -3,4 +3,3 @@ inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl @main def run = macroWithAssertFailing[Int](123) // error -