@@ -623,9 +623,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
623623
624624 object Apply extends ApplyModule :
625625 def apply (fun : Term , args : List [Term ]): Apply =
626+ xCheckMacroAssert(fun.tpe.widen.isInstanceOf [dotc.core.Types .MethodType ], " Expected `fun.tpe` to widen into a `MethodType`" )
626627 xCheckMacroValidExprs(args)
627628 withDefaultPos(tpd.Apply (fun, args))
628629 def copy (original : Tree )(fun : Term , args : List [Term ]): Apply =
630+ xCheckMacroAssert(fun.tpe.widen.isInstanceOf [dotc.core.Types .MethodType ], " Expected `fun.tpe` to widen into a `MethodType`" )
629631 xCheckMacroValidExprs(args)
630632 tpd.cpy.Apply (original)(fun, args)
631633 def unapply (x : Apply ): (Term , List [Term ]) =
@@ -664,8 +666,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
664666
665667 object TypeApply extends TypeApplyModule :
666668 def apply (fun : Term , args : List [TypeTree ]): TypeApply =
669+ xCheckMacroAssert(fun.tpe.widen.isInstanceOf [dotc.core.Types .PolyType ], " Expected `fun.tpe` to widen into a `PolyType`" )
667670 withDefaultPos(tpd.TypeApply (fun, args))
668671 def copy (original : Tree )(fun : Term , args : List [TypeTree ]): TypeApply =
672+ xCheckMacroAssert(fun.tpe.widen.isInstanceOf [dotc.core.Types .PolyType ], " Expected `fun.tpe` to widen into a `PolyType`" )
669673 tpd.cpy.TypeApply (original)(fun, args)
670674 def unapply (x : TypeApply ): (Term , List [TypeTree ]) =
671675 (x.fun, x.args)
@@ -789,7 +793,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
789793
790794 object Block extends BlockModule :
791795 def apply (stats : List [Statement ], expr : Term ): Block =
792- xCheckMacroBlockOwners(withDefaultPos(tpd.Block (stats, expr)))
796+ xCheckMacroBlockOwners(withDefaultPos(tpd.Block (stats, xCheckMacroValidExpr( expr) )))
793797 def copy (original : Tree )(stats : List [Statement ], expr : Term ): Block =
794798 xCheckMacroBlockOwners(tpd.cpy.Block (original)(stats, expr))
795799 def unapply (x : Block ): (List [Statement ], Term ) =
@@ -3082,13 +3086,22 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
30823086 if xCheckMacro then termOpt.foreach(xCheckMacroValidExpr)
30833087 termOpt
30843088 private def xCheckMacroValidExpr (term : Term ): term.type =
3085- if xCheckMacro then
3086- assert(! term.tpe.widenDealias.isInstanceOf [dotc.core.Types .MethodicType ],
3089+ xCheckMacroAssert(! term.tpe.widenDealias.isInstanceOf [dotc.core.Types .MethodicType ],
30873090 " Reference to a method must be eta-expanded before it is used as an expression: " + term.show)
30883091 term
30893092
30903093 private inline def xCheckMacroAssert (inline cond : Boolean , inline msg : String ): Unit =
3091- assert(! xCheckMacro || cond, msg)
3094+ if xCheckMacro && ! cond then
3095+ xCheckMacroAssertFail(msg)
3096+
3097+ private def xCheckMacroAssertFail (msg : String ): Unit =
3098+ val error = new AssertionError (msg)
3099+ if ! yDebugMacro then
3100+ // start stack trace at the place where the user called the reflection method
3101+ error.setStackTrace(
3102+ error.getStackTrace
3103+ .dropWhile(_.getClassName().startsWith(" scala.quoted.runtime.impl" )))
3104+ throw error
30923105
30933106 object Printer extends PrinterModule :
30943107
0 commit comments