Skip to content

Commit 39d45dc

Browse files
committed
Add missing error messages to asserts in QuotesImpl
1 parent dd37f07 commit 39d45dc

File tree

7 files changed

+70
-2
lines changed

7 files changed

+70
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -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.check

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
-- Error: tests/neg/i20946/Test_2.scala:5:29 ---------------------------------------------------------------------------
3+
5 | macroWithAssertFailing[Int](123) // error
4+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| Exception occurred while executing macro expansion.
6+
| java.lang.AssertionError: assertion failed: expected a term symbol but received class Int
7+
| at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
8+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Ref$.apply(QuotesImpl.scala:475)
9+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Ref$.apply(QuotesImpl.scala:474)
10+
| at Macro_1$package$.macroWithAssertFailingImpl(Macro_1.scala:6)
11+
|
12+
|---------------------------------------------------------------------------------------------------------------------
13+
|Inline stack trace
14+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15+
|This location contains code that was inlined from Test_2.scala:1
16+
1 |inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) }
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
---------------------------------------------------------------------------------------------------------------------

tests/neg/i20946/Macro_1.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted.*
2+
3+
def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = {
4+
import quotes.reflect.*
5+
6+
Ref(TypeRepr.of[T].typeSymbol)
7+
8+
'{()}
9+
}
10+

tests/neg/i20946/Test_2.scala

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

tests/neg/i20946a.check

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
-- Error: tests/neg/i20946a/Test_2.scala:5:29 --------------------------------------------------------------------------
3+
5 | macroWithAssertFailing[Int](123) // error
4+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| Exception occurred while executing macro expansion.
6+
| java.lang.AssertionError: assertion failed: Expected a type symbol, but got val <none>
7+
| at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
8+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeIdent$.apply(QuotesImpl.scala:1165)
9+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeIdent$.apply(QuotesImpl.scala:1164)
10+
| at Macro_1$package$.macroWithAssertFailingImpl(Macro_1.scala:6)
11+
|
12+
|---------------------------------------------------------------------------------------------------------------------
13+
|Inline stack trace
14+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15+
|This location contains code that was inlined from Test_2.scala:1
16+
1 |inline def macroWithAssertFailing[T](t: T): Unit = ${ macroWithAssertFailingImpl[T]('t) }
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
---------------------------------------------------------------------------------------------------------------------

tests/neg/i20946a/Macro_1.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted.*
2+
3+
def macroWithAssertFailingImpl[T: Type](t: Expr[T])(using Quotes): Expr[Unit] = {
4+
import quotes.reflect.*
5+
6+
TypeIdent(t.asTerm.symbol)
7+
8+
'{()}
9+
}
10+

tests/neg/i20946a/Test_2.scala

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

0 commit comments

Comments
 (0)