Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing error messages to asserts in QuotesImpl #21852

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading