@@ -625,9 +625,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
625
625
626
626
object Apply extends ApplyModule :
627
627
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`" )
628
629
xCheckMacroValidExprs(args)
629
630
withDefaultPos(tpd.Apply (fun, args))
630
631
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`" )
631
633
xCheckMacroValidExprs(args)
632
634
tpd.cpy.Apply (original)(fun, args)
633
635
def unapply (x : Apply ): (Term , List [Term ]) =
@@ -666,8 +668,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
666
668
667
669
object TypeApply extends TypeApplyModule :
668
670
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`" )
669
672
withDefaultPos(tpd.TypeApply (fun, args))
670
673
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`" )
671
675
tpd.cpy.TypeApply (original)(fun, args)
672
676
def unapply (x : TypeApply ): (Term , List [TypeTree ]) =
673
677
(x.fun, x.args)
@@ -791,7 +795,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
791
795
792
796
object Block extends BlockModule :
793
797
def apply (stats : List [Statement ], expr : Term ): Block =
794
- xCheckMacroBlockOwners(withDefaultPos(tpd.Block (stats, expr)))
798
+ xCheckMacroBlockOwners(withDefaultPos(tpd.Block (stats, xCheckMacroValidExpr( expr) )))
795
799
def copy (original : Tree )(stats : List [Statement ], expr : Term ): Block =
796
800
xCheckMacroBlockOwners(tpd.cpy.Block (original)(stats, expr))
797
801
def unapply (x : Block ): (List [Statement ], Term ) =
@@ -3095,13 +3099,22 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
3095
3099
if xCheckMacro then termOpt.foreach(xCheckMacroValidExpr)
3096
3100
termOpt
3097
3101
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 ],
3100
3103
" Reference to a method must be eta-expanded before it is used as an expression: " + term.show)
3101
3104
term
3102
3105
3103
3106
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
3105
3118
3106
3119
object Printer extends PrinterModule :
3107
3120
0 commit comments