Skip to content

Commit 97c386f

Browse files
Merge pull request #7536 from dotty-staging/split-eta-expansion-from-seal
Split eta-expansion from seal
2 parents 8830888 + c109b9b commit 97c386f

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
237237
def Term_underlyingArgument(self: Term)(given Context): Term = self.underlyingArgument
238238
def Term_underlying(self: Term)(given Context): Term = self.underlying
239239

240+
def Term_etaExpand(term: Term): Term = term.tpe.widen match {
241+
case mtpe: Types.MethodType if !mtpe.isParamDependent =>
242+
val closureResType = mtpe.resType match {
243+
case t: Types.MethodType => t.toFunctionType()
244+
case t => t
245+
}
246+
val closureTpe = Types.MethodType(mtpe.paramNames, mtpe.paramInfos, closureResType)
247+
val closureMethod = ctx.newSymbol(ctx.owner, nme.ANON_FUN, Synthetic | Method, closureTpe)
248+
tpd.Closure(closureMethod, tss => Term_etaExpand(new tpd.TreeOps(term).appliedToArgs(tss.head)))
249+
case _ => term
250+
}
251+
240252
type Ref = tpd.RefTree
241253

242254
def matchRef(tree: Tree)(given Context): Option[Ref] = tree match {
@@ -1591,19 +1603,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
15911603
PickledQuotes.quotedTypeToTree(self)
15921604

15931605
/** Convert `Term` to an `quoted.Expr[Any]` */
1594-
def QuotedExpr_seal(self: Term)(given ctx: Context): scala.quoted.Expr[Any] = {
1595-
def etaExpand(term: Term): Term = term.tpe.widen match {
1596-
case mtpe: Types.MethodType if !mtpe.isParamDependent =>
1597-
val closureResType = mtpe.resType match {
1598-
case t: Types.MethodType => t.toFunctionType()
1599-
case t => t
1600-
}
1601-
val closureTpe = Types.MethodType(mtpe.paramNames, mtpe.paramInfos, closureResType)
1602-
val closureMethod = ctx.newSymbol(ctx.owner, nme.ANON_FUN, Synthetic | Method, closureTpe)
1603-
tpd.Closure(closureMethod, tss => etaExpand(new tpd.TreeOps(term).appliedToArgs(tss.head)))
1604-
case _ => term
1605-
}
1606-
new scala.internal.quoted.TastyTreeExpr(etaExpand(self), compilerId)
1606+
def QuotedExpr_seal(self: Term)(given ctx: Context): scala.quoted.Expr[Any] = self.tpe.widen match {
1607+
case _: Types.MethodType | _: Types.PolyType => throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.")
1608+
case _ => new scala.internal.quoted.TastyTreeExpr(self, compilerId)
16071609
}
16081610

16091611
/** Checked cast to a `quoted.Expr[U]` */

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ trait CompilerInterface {
283283
def Term_tpe(self: Term)(given ctx: Context): Type
284284
def Term_underlyingArgument(self: Term)(given ctx: Context): Term
285285
def Term_underlying(self: Term)(given ctx: Context): Term
286+
def Term_etaExpand(term: Term): Term
286287

287288
/** Tree representing a reference to definition */
288289
type Ref <: Term

library/src/scala/tasty/reflect/TreeOps.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ trait TreeOps extends Core {
172172
def tpe(given ctx: Context): Type = internal.Term_tpe(self)
173173
def underlyingArgument(given ctx: Context): Term = internal.Term_underlyingArgument(self)
174174
def underlying(given ctx: Context): Term = internal.Term_underlying(self)
175+
def etaExpand(given ctx: Context): Term = internal.Term_etaExpand(self)
175176

176177
/** A unary apply node with given argument: `tree(arg)` */
177178
def appliedTo(arg: Term)(given ctx: Context): Term =

tests/run-macros/tasty-seal-method/quoted_1.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object Asserts {
1414
fn.tpe.widen match {
1515
case IsMethodType(_) =>
1616
args.size match {
17-
case 0 => Expr.betaReduce(fn.seal.cast[() => Int])()
18-
case 1 => Expr.betaReduce(fn.seal.cast[Int => Int])('{0})
19-
case 2 => Expr.betaReduce(fn.seal.cast[(Int, Int) => Int])('{0}, '{0})
20-
case 3 => Expr.betaReduce(fn.seal.cast[(Int, Int, Int) => Int])('{0}, '{0}, '{0})
17+
case 0 => Expr.betaReduce(fn.etaExpand.seal.cast[() => Int])()
18+
case 1 => Expr.betaReduce(fn.etaExpand.seal.cast[Int => Int])('{0})
19+
case 2 => Expr.betaReduce(fn.etaExpand.seal.cast[(Int, Int) => Int])('{0}, '{0})
20+
case 3 => Expr.betaReduce(fn.etaExpand.seal.cast[(Int, Int, Int) => Int])('{0}, '{0}, '{0})
2121
}
2222
}
2323
case _ => x
@@ -35,10 +35,10 @@ object Asserts {
3535
case Apply(fn, args) =>
3636
val pre = rec(fn)
3737
args.size match {
38-
case 0 => Expr.betaReduce(pre.seal.cast[() => Any])().unseal
39-
case 1 => Expr.betaReduce(pre.seal.cast[Int => Any])('{0}).unseal
40-
case 2 => Expr.betaReduce(pre.seal.cast[(Int, Int) => Any])('{0}, '{0}).unseal
41-
case 3 => Expr.betaReduce(pre.seal.cast[(Int, Int, Int) => Any])('{0}, '{0}, '{0}).unseal
38+
case 0 => Expr.betaReduce(pre.etaExpand.seal.cast[() => Any])().unseal
39+
case 1 => Expr.betaReduce(pre.etaExpand.seal.cast[Int => Any])('{0}).unseal
40+
case 2 => Expr.betaReduce(pre.etaExpand.seal.cast[(Int, Int) => Any])('{0}, '{0}).unseal
41+
case 3 => Expr.betaReduce(pre.etaExpand.seal.cast[(Int, Int, Int) => Any])('{0}, '{0}, '{0}).unseal
4242
}
4343
case _ => term
4444
}

0 commit comments

Comments
 (0)