Skip to content

Commit

Permalink
Add missing error messages to asserts in QuotesImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Oct 29, 2024
1 parent dd37f07 commit 39d45dc
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
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 @@ -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
18 changes: 18 additions & 0 deletions tests/neg/i20946.check
Original file line number Diff line number Diff line change
@@ -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) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
---------------------------------------------------------------------------------------------------------------------
10 changes: 10 additions & 0 deletions tests/neg/i20946/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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)

'{()}
}

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

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

18 changes: 18 additions & 0 deletions tests/neg/i20946a.check
Original file line number Diff line number Diff line change
@@ -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 <none>
| 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) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
---------------------------------------------------------------------------------------------------------------------
10 changes: 10 additions & 0 deletions tests/neg/i20946a/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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)

'{()}
}

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

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

0 comments on commit 39d45dc

Please sign in to comment.