@@ -264,7 +264,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
264
264
object DefDef extends DefDefModule :
265
265
def apply (symbol : Symbol , rhsFn : List [List [Tree ]] => Option [Term ]): DefDef =
266
266
withDefaultPos(tpd.DefDef (symbol.asTerm, prefss =>
267
- yCheckedOwners(rhsFn(prefss), symbol).getOrElse(tpd.EmptyTree )
267
+ yCheckedOwners(yCheckValidExpr( rhsFn(prefss) ), symbol).getOrElse(tpd.EmptyTree )
268
268
))
269
269
def copy (original : Tree )(name : String , paramss : List [ParamClause ], tpt : TypeTree , rhs : Option [Term ]): DefDef =
270
270
tpd.cpy.DefDef (original)(name.toTermName, paramss, tpt, yCheckedOwners(rhs, original.symbol).getOrElse(tpd.EmptyTree ))
@@ -293,9 +293,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
293
293
294
294
object ValDef extends ValDefModule :
295
295
def apply (symbol : Symbol , rhs : Option [Term ]): ValDef =
296
- tpd.ValDef (symbol.asTerm, yCheckedOwners(rhs, symbol).getOrElse(tpd.EmptyTree ))
296
+ tpd.ValDef (symbol.asTerm, yCheckedOwners(yCheckValidExpr( rhs) , symbol).getOrElse(tpd.EmptyTree ))
297
297
def copy (original : Tree )(name : String , tpt : TypeTree , rhs : Option [Term ]): ValDef =
298
- tpd.cpy.ValDef (original)(name.toTermName, tpt, yCheckedOwners(rhs, original.symbol).getOrElse(tpd.EmptyTree ))
298
+ tpd.cpy.ValDef (original)(name.toTermName, tpt, yCheckedOwners(yCheckValidExpr( rhs) , original.symbol).getOrElse(tpd.EmptyTree ))
299
299
def unapply (vdef : ValDef ): (String , TypeTree , Option [Term ]) =
300
300
(vdef.name.toString, vdef.tpt, optional(vdef.rhs))
301
301
@@ -568,9 +568,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
568
568
569
569
object NamedArg extends NamedArgModule :
570
570
def apply (name : String , arg : Term ): NamedArg =
571
- withDefaultPos(tpd.NamedArg (name.toTermName, arg))
571
+ withDefaultPos(tpd.NamedArg (name.toTermName, yCheckValidExpr( arg) ))
572
572
def copy (original : Tree )(name : String , arg : Term ): NamedArg =
573
- tpd.cpy.NamedArg (original)(name.toTermName, arg)
573
+ tpd.cpy.NamedArg (original)(name.toTermName, yCheckValidExpr( arg) )
574
574
def unapply (x : NamedArg ): (String , Term ) =
575
575
(x.name.toString, x.value)
576
576
end NamedArg
@@ -592,8 +592,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
592
592
593
593
object Apply extends ApplyModule :
594
594
def apply (fun : Term , args : List [Term ]): Apply =
595
+ yCheckValidExprs(args)
595
596
withDefaultPos(tpd.Apply (fun, args))
596
597
def copy (original : Tree )(fun : Term , args : List [Term ]): Apply =
598
+ yCheckValidExprs(args)
597
599
tpd.cpy.Apply (original)(fun, args)
598
600
def unapply (x : Apply ): (Term , List [Term ]) =
599
601
(x.fun, x.args)
@@ -665,9 +667,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
665
667
666
668
object Typed extends TypedModule :
667
669
def apply (expr : Term , tpt : TypeTree ): Typed =
668
- withDefaultPos(tpd.Typed (expr, tpt))
670
+ withDefaultPos(tpd.Typed (yCheckValidExpr( expr) , tpt))
669
671
def copy (original : Tree )(expr : Term , tpt : TypeTree ): Typed =
670
- tpd.cpy.Typed (original)(expr, tpt)
672
+ tpd.cpy.Typed (original)(yCheckValidExpr( expr) , tpt)
671
673
def unapply (x : Typed ): (Term , TypeTree ) =
672
674
(x.expr, x.tpt)
673
675
end Typed
@@ -689,9 +691,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
689
691
690
692
object Assign extends AssignModule :
691
693
def apply (lhs : Term , rhs : Term ): Assign =
692
- withDefaultPos(tpd.Assign (lhs, rhs))
694
+ withDefaultPos(tpd.Assign (lhs, yCheckValidExpr( rhs) ))
693
695
def copy (original : Tree )(lhs : Term , rhs : Term ): Assign =
694
- tpd.cpy.Assign (original)(lhs, rhs)
696
+ tpd.cpy.Assign (original)(lhs, yCheckValidExpr( rhs) )
695
697
def unapply (x : Assign ): (Term , Term ) =
696
698
(x.lhs, x.rhs)
697
699
end Assign
@@ -754,7 +756,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
754
756
object Lambda extends LambdaModule :
755
757
def apply (owner : Symbol , tpe : MethodType , rhsFn : (Symbol , List [Tree ]) => Tree ): Block =
756
758
val meth = dotc.core.Symbols .newSymbol(owner, nme.ANON_FUN , Synthetic | Method , tpe)
757
- tpd.Closure (meth, tss => yCheckedOwners(rhsFn(meth, tss.head.map(withDefaultPos)), meth))
759
+ tpd.Closure (meth, tss => yCheckedOwners(yCheckValidExpr( rhsFn(meth, tss.head.map(withDefaultPos) )), meth))
758
760
759
761
def unapply (tree : Block ): Option [(List [ValDef ], Term )] = tree match {
760
762
case Block ((ddef @ DefDef (_, TermParamClause (params) :: Nil , _, Some (body))) :: Nil , Closure (meth, _))
@@ -774,9 +776,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
774
776
775
777
object If extends IfModule :
776
778
def apply (cond : Term , thenp : Term , elsep : Term ): If =
777
- withDefaultPos(tpd.If (cond, thenp, elsep))
779
+ withDefaultPos(tpd.If (yCheckValidExpr( cond), yCheckValidExpr( thenp), yCheckValidExpr( elsep) ))
778
780
def copy (original : Tree )(cond : Term , thenp : Term , elsep : Term ): If =
779
- tpd.cpy.If (original)(cond, thenp, elsep)
781
+ tpd.cpy.If (original)(yCheckValidExpr( cond), yCheckValidExpr( thenp), yCheckValidExpr( elsep) )
780
782
def unapply (tree : If ): (Term , Term , Term ) =
781
783
(tree.cond, tree.thenp, tree.elsep)
782
784
end If
@@ -800,10 +802,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
800
802
801
803
object Match extends MatchModule :
802
804
def apply (selector : Term , cases : List [CaseDef ]): Match =
803
- withDefaultPos(tpd.Match (selector, cases))
805
+ withDefaultPos(tpd.Match (yCheckValidExpr( selector) , cases))
804
806
805
807
def copy (original : Tree )(selector : Term , cases : List [CaseDef ]): Match =
806
- tpd.cpy.Match (original)(selector, cases)
808
+ tpd.cpy.Match (original)(yCheckValidExpr( selector) , cases)
807
809
808
810
def unapply (x : Match ): (Term , List [CaseDef ]) =
809
811
(x.scrutinee, x.cases)
@@ -850,9 +852,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
850
852
851
853
object Try extends TryModule :
852
854
def apply (expr : Term , cases : List [CaseDef ], finalizer : Option [Term ]): Try =
853
- withDefaultPos(tpd.Try (expr, cases, finalizer.getOrElse(tpd.EmptyTree )))
855
+ withDefaultPos(tpd.Try (yCheckValidExpr( expr) , cases, finalizer.getOrElse(tpd.EmptyTree )))
854
856
def copy (original : Tree )(expr : Term , cases : List [CaseDef ], finalizer : Option [Term ]): Try =
855
- tpd.cpy.Try (original)(expr, cases, finalizer.getOrElse(tpd.EmptyTree ))
857
+ tpd.cpy.Try (original)(yCheckValidExpr( expr) , cases, finalizer.getOrElse(tpd.EmptyTree ))
856
858
def unapply (x : Try ): (Term , List [CaseDef ], Option [Term ]) =
857
859
(x.body, x.cases, optional(x.finalizer))
858
860
end Try
@@ -875,9 +877,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
875
877
876
878
object Return extends ReturnModule :
877
879
def apply (expr : Term , from : Symbol ): Return =
878
- withDefaultPos(tpd.Return (expr, from))
880
+ withDefaultPos(tpd.Return (yCheckValidExpr( expr) , from))
879
881
def copy (original : Tree )(expr : Term , from : Symbol ): Return =
880
- tpd.cpy.Return (original)(expr, tpd.ref(from))
882
+ tpd.cpy.Return (original)(yCheckValidExpr( expr) , tpd.ref(from))
881
883
def unapply (x : Return ): (Term , Symbol ) =
882
884
(x.expr, x.from.symbol)
883
885
end Return
@@ -899,8 +901,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
899
901
900
902
object Repeated extends RepeatedModule :
901
903
def apply (elems : List [Term ], elemtpt : TypeTree ): Repeated =
904
+ yCheckValidExprs(elems)
902
905
withDefaultPos(tpd.SeqLiteral (elems, elemtpt))
903
906
def copy (original : Tree )(elems : List [Term ], elemtpt : TypeTree ): Repeated =
907
+ yCheckValidExprs(elems)
904
908
tpd.cpy.SeqLiteral (original)(elems, elemtpt)
905
909
def unapply (x : Repeated ): (List [Term ], TypeTree ) =
906
910
(x.elems, x.elemtpt)
@@ -923,9 +927,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
923
927
924
928
object Inlined extends InlinedModule :
925
929
def apply (call : Option [Tree ], bindings : List [Definition ], expansion : Term ): Inlined =
926
- withDefaultPos(tpd.Inlined (call.getOrElse(tpd.EmptyTree ), bindings.map { case b : tpd.MemberDef => b }, expansion))
930
+ withDefaultPos(tpd.Inlined (call.getOrElse(tpd.EmptyTree ), bindings.map { case b : tpd.MemberDef => b }, yCheckValidExpr( expansion) ))
927
931
def copy (original : Tree )(call : Option [Tree ], bindings : List [Definition ], expansion : Term ): Inlined =
928
- tpd.cpy.Inlined (original)(call.getOrElse(tpd.EmptyTree ), bindings.asInstanceOf [List [tpd.MemberDef ]], expansion)
932
+ tpd.cpy.Inlined (original)(call.getOrElse(tpd.EmptyTree ), bindings.asInstanceOf [List [tpd.MemberDef ]], yCheckValidExpr( expansion) )
929
933
def unapply (x : Inlined ): (Option [Tree /* Term | TypeTree */ ], List [Definition ], Term ) =
930
934
(optional(x.call), x.bindings, x.body)
931
935
end Inlined
@@ -978,9 +982,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
978
982
979
983
object While extends WhileModule :
980
984
def apply (cond : Term , body : Term ): While =
981
- withDefaultPos(tpd.WhileDo (cond, body))
985
+ withDefaultPos(tpd.WhileDo (yCheckValidExpr( cond), yCheckValidExpr( body) ))
982
986
def copy (original : Tree )(cond : Term , body : Term ): While =
983
- tpd.cpy.WhileDo (original)(cond, body)
987
+ tpd.cpy.WhileDo (original)(yCheckValidExpr( cond), yCheckValidExpr( body) )
984
988
def unapply (x : While ): (Term , Term ) =
985
989
(x.cond, x.body)
986
990
end While
@@ -2830,6 +2834,18 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2830
2834
case _ => traverseChildren(t)
2831
2835
}.traverse(tree)
2832
2836
2837
+ private def yCheckValidExprs (terms : List [Term ]): terms.type =
2838
+ if yCheck then terms.foreach(yCheckValidExpr)
2839
+ terms
2840
+ private def yCheckValidExpr (termOpt : Option [Term ]): termOpt.type =
2841
+ if yCheck then termOpt.foreach(yCheckValidExpr)
2842
+ termOpt
2843
+ private def yCheckValidExpr (term : Term ): term.type =
2844
+ if yCheck then
2845
+ assert(! term.tpe.widenDealias.isInstanceOf [dotc.core.Types .MethodicType ],
2846
+ " Reference to a method must be eta-expanded before it is used as an expression: " + term.show)
2847
+ term
2848
+
2833
2849
object Printer extends PrinterModule :
2834
2850
2835
2851
lazy val TreeCode : Printer [Tree ] = new Printer [Tree ]:
@@ -2873,6 +2889,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
2873
2889
Extractors .showConstant(using QuotesImpl .this )(const)
2874
2890
2875
2891
end Printer
2892
+
2876
2893
end reflect
2877
2894
2878
2895
def unpickleExpr [T ](pickled : String | List [String ], typeHole : (Int , Seq [Any ]) => scala.quoted.Type [? ], termHole : (Int , Seq [Any ], scala.quoted.Quotes ) => scala.quoted.Expr [? ]): scala.quoted.Expr [T ] =
0 commit comments