Skip to content

Commit 5ea0084

Browse files
committed
Use yCheckValidExpr for other expressions
1 parent 04dba31 commit 5ea0084

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

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

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
264264
object DefDef extends DefDefModule:
265265
def apply(symbol: Symbol, rhsFn: List[List[Tree]] => Option[Term]): DefDef =
266266
withDefaultPos(tpd.DefDef(symbol.asTerm, prefss =>
267-
yCheckedOwners(rhsFn(prefss), symbol).getOrElse(tpd.EmptyTree)
267+
yCheckedOwners(yCheckValidExpr(rhsFn(prefss)), symbol).getOrElse(tpd.EmptyTree)
268268
))
269269
def copy(original: Tree)(name: String, paramss: List[ParamClause], tpt: TypeTree, rhs: Option[Term]): DefDef =
270270
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
293293

294294
object ValDef extends ValDefModule:
295295
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))
297297
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))
299299
def unapply(vdef: ValDef): (String, TypeTree, Option[Term]) =
300300
(vdef.name.toString, vdef.tpt, optional(vdef.rhs))
301301

@@ -568,9 +568,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
568568

569569
object NamedArg extends NamedArgModule:
570570
def apply(name: String, arg: Term): NamedArg =
571-
withDefaultPos(tpd.NamedArg(name.toTermName, arg))
571+
withDefaultPos(tpd.NamedArg(name.toTermName, yCheckValidExpr(arg)))
572572
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))
574574
def unapply(x: NamedArg): (String, Term) =
575575
(x.name.toString, x.value)
576576
end NamedArg
@@ -592,15 +592,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
592592

593593
object Apply extends ApplyModule:
594594
def apply(fun: Term, args: List[Term]): Apply =
595-
yCheckArgs(args)
595+
yCheckValidExprs(args)
596596
withDefaultPos(tpd.Apply(fun, args))
597597
def copy(original: Tree)(fun: Term, args: List[Term]): Apply =
598-
yCheckArgs(args)
598+
yCheckValidExprs(args)
599599
tpd.cpy.Apply(original)(fun, args)
600600
def unapply(x: Apply): (Term, List[Term]) =
601601
(x.fun, x.args)
602-
private def yCheckArgs(args: List[Term]): Unit =
603-
if yCheck then args.foreach(yCheckValidExpr)
604602
end Apply
605603

606604
given ApplyMethods: ApplyMethods with
@@ -669,9 +667,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
669667

670668
object Typed extends TypedModule:
671669
def apply(expr: Term, tpt: TypeTree): Typed =
672-
withDefaultPos(tpd.Typed(expr, tpt))
670+
withDefaultPos(tpd.Typed(yCheckValidExpr(expr), tpt))
673671
def copy(original: Tree)(expr: Term, tpt: TypeTree): Typed =
674-
tpd.cpy.Typed(original)(expr, tpt)
672+
tpd.cpy.Typed(original)(yCheckValidExpr(expr), tpt)
675673
def unapply(x: Typed): (Term, TypeTree) =
676674
(x.expr, x.tpt)
677675
end Typed
@@ -693,9 +691,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
693691

694692
object Assign extends AssignModule:
695693
def apply(lhs: Term, rhs: Term): Assign =
696-
withDefaultPos(tpd.Assign(lhs, rhs))
694+
withDefaultPos(tpd.Assign(lhs, yCheckValidExpr(rhs)))
697695
def copy(original: Tree)(lhs: Term, rhs: Term): Assign =
698-
tpd.cpy.Assign(original)(lhs, rhs)
696+
tpd.cpy.Assign(original)(lhs, yCheckValidExpr(rhs))
699697
def unapply(x: Assign): (Term, Term) =
700698
(x.lhs, x.rhs)
701699
end Assign
@@ -758,7 +756,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
758756
object Lambda extends LambdaModule:
759757
def apply(owner: Symbol, tpe: MethodType, rhsFn: (Symbol, List[Tree]) => Tree): Block =
760758
val meth = dotc.core.Symbols.newSymbol(owner, nme.ANON_FUN, Synthetic | Method, tpe)
761-
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))
762760

763761
def unapply(tree: Block): Option[(List[ValDef], Term)] = tree match {
764762
case Block((ddef @ DefDef(_, TermParamClause(params) :: Nil, _, Some(body))) :: Nil, Closure(meth, _))
@@ -778,9 +776,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
778776

779777
object If extends IfModule:
780778
def apply(cond: Term, thenp: Term, elsep: Term): If =
781-
withDefaultPos(tpd.If(cond, thenp, elsep))
779+
withDefaultPos(tpd.If(yCheckValidExpr(cond), yCheckValidExpr(thenp), yCheckValidExpr(elsep)))
782780
def copy(original: Tree)(cond: Term, thenp: Term, elsep: Term): If =
783-
tpd.cpy.If(original)(cond, thenp, elsep)
781+
tpd.cpy.If(original)(yCheckValidExpr(cond), yCheckValidExpr(thenp), yCheckValidExpr(elsep))
784782
def unapply(tree: If): (Term, Term, Term) =
785783
(tree.cond, tree.thenp, tree.elsep)
786784
end If
@@ -804,10 +802,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
804802

805803
object Match extends MatchModule:
806804
def apply(selector: Term, cases: List[CaseDef]): Match =
807-
withDefaultPos(tpd.Match(selector, cases))
805+
withDefaultPos(tpd.Match(yCheckValidExpr(selector), cases))
808806

809807
def copy(original: Tree)(selector: Term, cases: List[CaseDef]): Match =
810-
tpd.cpy.Match(original)(selector, cases)
808+
tpd.cpy.Match(original)(yCheckValidExpr(selector), cases)
811809

812810
def unapply(x: Match): (Term, List[CaseDef]) =
813811
(x.scrutinee, x.cases)
@@ -854,9 +852,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
854852

855853
object Try extends TryModule:
856854
def apply(expr: Term, cases: List[CaseDef], finalizer: Option[Term]): Try =
857-
withDefaultPos(tpd.Try(expr, cases, finalizer.getOrElse(tpd.EmptyTree)))
855+
withDefaultPos(tpd.Try(yCheckValidExpr(expr), cases, finalizer.getOrElse(tpd.EmptyTree)))
858856
def copy(original: Tree)(expr: Term, cases: List[CaseDef], finalizer: Option[Term]): Try =
859-
tpd.cpy.Try(original)(expr, cases, finalizer.getOrElse(tpd.EmptyTree))
857+
tpd.cpy.Try(original)(yCheckValidExpr(expr), cases, finalizer.getOrElse(tpd.EmptyTree))
860858
def unapply(x: Try): (Term, List[CaseDef], Option[Term]) =
861859
(x.body, x.cases, optional(x.finalizer))
862860
end Try
@@ -879,9 +877,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
879877

880878
object Return extends ReturnModule:
881879
def apply(expr: Term, from: Symbol): Return =
882-
withDefaultPos(tpd.Return(expr, from))
880+
withDefaultPos(tpd.Return(yCheckValidExpr(expr), from))
883881
def copy(original: Tree)(expr: Term, from: Symbol): Return =
884-
tpd.cpy.Return(original)(expr, tpd.ref(from))
882+
tpd.cpy.Return(original)(yCheckValidExpr(expr), tpd.ref(from))
885883
def unapply(x: Return): (Term, Symbol) =
886884
(x.expr, x.from.symbol)
887885
end Return
@@ -903,8 +901,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
903901

904902
object Repeated extends RepeatedModule:
905903
def apply(elems: List[Term], elemtpt: TypeTree): Repeated =
904+
yCheckValidExprs(elems)
906905
withDefaultPos(tpd.SeqLiteral(elems, elemtpt))
907906
def copy(original: Tree)(elems: List[Term], elemtpt: TypeTree): Repeated =
907+
yCheckValidExprs(elems)
908908
tpd.cpy.SeqLiteral(original)(elems, elemtpt)
909909
def unapply(x: Repeated): (List[Term], TypeTree) =
910910
(x.elems, x.elemtpt)
@@ -927,9 +927,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
927927

928928
object Inlined extends InlinedModule:
929929
def apply(call: Option[Tree], bindings: List[Definition], expansion: Term): Inlined =
930-
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)))
931931
def copy(original: Tree)(call: Option[Tree], bindings: List[Definition], expansion: Term): Inlined =
932-
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))
933933
def unapply(x: Inlined): (Option[Tree /* Term | TypeTree */], List[Definition], Term) =
934934
(optional(x.call), x.bindings, x.body)
935935
end Inlined
@@ -982,9 +982,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
982982

983983
object While extends WhileModule:
984984
def apply(cond: Term, body: Term): While =
985-
withDefaultPos(tpd.WhileDo(cond, body))
985+
withDefaultPos(tpd.WhileDo(yCheckValidExpr(cond), yCheckValidExpr(body)))
986986
def copy(original: Tree)(cond: Term, body: Term): While =
987-
tpd.cpy.WhileDo(original)(cond, body)
987+
tpd.cpy.WhileDo(original)(yCheckValidExpr(cond), yCheckValidExpr(body))
988988
def unapply(x: While): (Term, Term) =
989989
(x.cond, x.body)
990990
end While
@@ -2834,6 +2834,18 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
28342834
case _ => traverseChildren(t)
28352835
}.traverse(tree)
28362836

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+
28372849
object Printer extends PrinterModule:
28382850

28392851
lazy val TreeCode: Printer[Tree] = new Printer[Tree]:
@@ -2878,11 +2890,6 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
28782890

28792891
end Printer
28802892

2881-
private def yCheckValidExpr(term: Term): Unit =
2882-
if yCheck then
2883-
assert(!term.tpe.widenDealias.isInstanceOf[dotc.core.Types.MethodicType],
2884-
"Reference to a method must be eta-expanded before it is used as an expression: " + term.show)
2885-
28862893
end reflect
28872894

28882895
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

Comments
 (0)