@@ -625,9 +625,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
625625
626626 object Apply extends ApplyModule :
627627 def apply (fun : Term , args : List [Term ]): Apply =
628+ xCheckMacroAssert(fun.tpe.widen.isInstanceOf [dotc.core.Types .MethodType ], " Expected `fun.tpe` to widen into a `MethodType`" )
628629 xCheckMacroValidExprs(args)
629630 withDefaultPos(tpd.Apply (fun, args))
630631 def copy (original : Tree )(fun : Term , args : List [Term ]): Apply =
632+ xCheckMacroAssert(fun.tpe.widen.isInstanceOf [dotc.core.Types .MethodType ], " Expected `fun.tpe` to widen into a `MethodType`" )
631633 xCheckMacroValidExprs(args)
632634 tpd.cpy.Apply (original)(fun, args)
633635 def unapply (x : Apply ): (Term , List [Term ]) =
@@ -666,8 +668,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
666668
667669 object TypeApply extends TypeApplyModule :
668670 def apply (fun : Term , args : List [TypeTree ]): TypeApply =
671+ xCheckMacroAssert(fun.tpe.widen.isInstanceOf [dotc.core.Types .PolyType ], " Expected `fun.tpe` to widen into a `PolyType`" )
669672 withDefaultPos(tpd.TypeApply (fun, args))
670673 def copy (original : Tree )(fun : Term , args : List [TypeTree ]): TypeApply =
674+ xCheckMacroAssert(fun.tpe.widen.isInstanceOf [dotc.core.Types .PolyType ], " Expected `fun.tpe` to widen into a `PolyType`" )
671675 tpd.cpy.TypeApply (original)(fun, args)
672676 def unapply (x : TypeApply ): (Term , List [TypeTree ]) =
673677 (x.fun, x.args)
@@ -791,7 +795,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
791795
792796 object Block extends BlockModule :
793797 def apply (stats : List [Statement ], expr : Term ): Block =
794- xCheckMacroBlockOwners(withDefaultPos(tpd.Block (stats, expr)))
798+ xCheckMacroBlockOwners(withDefaultPos(tpd.Block (stats, xCheckMacroValidExpr( expr) )))
795799 def copy (original : Tree )(stats : List [Statement ], expr : Term ): Block =
796800 xCheckMacroBlockOwners(tpd.cpy.Block (original)(stats, expr))
797801 def unapply (x : Block ): (List [Statement ], Term ) =
@@ -3095,13 +3099,22 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
30953099 if xCheckMacro then termOpt.foreach(xCheckMacroValidExpr)
30963100 termOpt
30973101 private def xCheckMacroValidExpr (term : Term ): term.type =
3098- if xCheckMacro then
3099- assert(! term.tpe.widenDealias.isInstanceOf [dotc.core.Types .MethodicType ],
3102+ xCheckMacroAssert(! term.tpe.widenDealias.isInstanceOf [dotc.core.Types .MethodicType ],
31003103 " Reference to a method must be eta-expanded before it is used as an expression: " + term.show)
31013104 term
31023105
31033106 private inline def xCheckMacroAssert (inline cond : Boolean , inline msg : String ): Unit =
3104- assert(! xCheckMacro || cond, msg)
3107+ if xCheckMacro && ! cond then
3108+ xCheckMacroAssertFail(msg)
3109+
3110+ private def xCheckMacroAssertFail (msg : String ): Unit =
3111+ val error = new AssertionError (msg)
3112+ if ! yDebugMacro then
3113+ // start stack trace at the place where the user called the reflection method
3114+ error.setStackTrace(
3115+ error.getStackTrace
3116+ .dropWhile(_.getClassName().startsWith(" scala.quoted.runtime.impl" )))
3117+ throw error
31053118
31063119 object Printer extends PrinterModule :
31073120
0 commit comments