Skip to content

Commit b47f0f2

Browse files
authored
Add missing error messages to asserts in QuotesImpl (#21852)
closes #20946
2 parents 01288d2 + 014d0db commit b47f0f2

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
301301

302302
object DefDef extends DefDefModule:
303303
def apply(symbol: Symbol, rhsFn: List[List[Tree]] => Option[Term]): DefDef =
304-
xCheckMacroAssert(symbol.isTerm, s"expected a term symbol but received $symbol")
304+
xCheckMacroAssert(symbol.isTerm, s"expected a term symbol, but received $symbol")
305305
xCheckMacroAssert(symbol.flags.is(Flags.Method), "expected a symbol with `Method` flag set")
306306
withDefaultPos(tpd.DefDef(symbol.asTerm, prefss =>
307307
xCheckedMacroOwners(xCheckMacroValidExpr(rhsFn(prefss)), symbol).getOrElse(tpd.EmptyTree)
@@ -472,7 +472,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
472472
def term(tp: TermRef): Ref =
473473
withDefaultPos(tpd.ref(tp).asInstanceOf[tpd.RefTree])
474474
def apply(sym: Symbol): Ref =
475-
assert(sym.isTerm)
475+
assert(sym.isTerm, s"expected a term symbol, but received $sym")
476476
val refTree = tpd.ref(sym) match
477477
case t @ tpd.This(ident) => // not a RefTree, so we need to work around this - issue #19732
478478
// 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
11281128
def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeTree =
11291129
tp.asInstanceOf[TypeImpl].typeTree
11301130
def ref(sym: Symbol): TypeTree =
1131-
assert(sym.isType, "Expected a type symbol, but got " + sym)
1131+
assert(sym.isType, s"Expected a type symbol, but got $sym")
11321132
tpd.ref(sym)
11331133
end TypeTree
11341134

@@ -1162,7 +1162,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
11621162

11631163
object TypeIdent extends TypeIdentModule:
11641164
def apply(sym: Symbol): TypeTree =
1165-
assert(sym.isType)
1165+
assert(sym.isType, s"Expected a type symbol, but got $sym")
11661166
withDefaultPos(tpd.ref(sym).asInstanceOf[tpd.TypeTree])
11671167
def copy(original: Tree)(name: String): TypeIdent =
11681168
tpd.cpy.Ident(original)(name.toTypeName)

tests/neg/i20946/Macro_1.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted.*
2+
3+
def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = {
4+
import quotes.reflect.*
5+
6+
try
7+
Ref(TypeRepr.of[T].typeSymbol)
8+
catch
9+
case ex: Throwable =>
10+
if ex.getMessage().contains("expected a term symbol, but received ") then
11+
throw ex
12+
13+
'{()}
14+
}

tests/neg/i20946/Test_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) }
2+
3+
@main
4+
def run =
5+
macroWithAssertFailing[Int](123) // error

tests/neg/i20946a/Macro_1.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted.*
2+
3+
def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = {
4+
import quotes.reflect.*
5+
6+
try
7+
TypeIdent(t.asTerm.symbol)
8+
catch
9+
case ex: Throwable =>
10+
if ex.getMessage().contains("Expected a type symbol, but got ") then
11+
throw ex
12+
13+
'{()}
14+
}

tests/neg/i20946a/Test_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) }
2+
3+
@main
4+
def run =
5+
macroWithAssertFailing[Int](123) // error

0 commit comments

Comments
 (0)