Skip to content

Commit

Permalink
Add missing error messages to asserts in QuotesImpl (#21852)
Browse files Browse the repository at this point in the history
closes #20946
  • Loading branch information
jchyb authored Nov 4, 2024
2 parents 01288d2 + 014d0db commit b47f0f2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
8 changes: 4 additions & 4 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions tests/neg/i20946/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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

'{()}
}
5 changes: 5 additions & 0 deletions tests/neg/i20946/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) }

@main
def run =
macroWithAssertFailing[Int](123) // error
14 changes: 14 additions & 0 deletions tests/neg/i20946a/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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

'{()}
}
5 changes: 5 additions & 0 deletions tests/neg/i20946a/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) }

@main
def run =
macroWithAssertFailing[Int](123) // error

0 comments on commit b47f0f2

Please sign in to comment.