Skip to content

Commit 36cc65a

Browse files
author
Aggelos Biboudis
authored
Merge pull request #3981 from dotty-staging/fix-#3945
Fix #3945: Fix eta expansion for partially applied methods
2 parents 3365ed3 + 27f390b commit 36cc65a

File tree

5 files changed

+77
-18
lines changed

5 files changed

+77
-18
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -3286,7 +3286,7 @@ object Types {
32863286
* `owningTree` and `owner` are used to determine whether a type-variable can be instantiated
32873287
* at some given point. See `Inferencing#interpolateUndetVars`.
32883288
*/
3289-
final class TypeVar(val origin: TypeParamRef, creatorState: TyperState, val bindingTree: untpd.Tree, val owner: Symbol) extends CachedProxyType with ValueType {
3289+
final class TypeVar(val origin: TypeParamRef, creatorState: TyperState, var bindingTree: untpd.Tree, val owner: Symbol) extends CachedProxyType with ValueType {
32903290

32913291
/** The permanent instance type of the variable, or NoType is none is given yet */
32923292
private[this] var myInst: Type = NoType

compiler/src/dotty/tools/dotc/typer/EtaExpansion.scala

+29-13
Original file line numberDiff line numberDiff line change
@@ -167,37 +167,53 @@ object EtaExpansion extends LiftImpure {
167167
*
168168
* { val xs = es; expr }
169169
*
170-
* If xarity matches the number of parameters in `mt`, the eta-expansion is
170+
* The result of the eta-expansion is either (1)
171171
*
172172
* { val xs = es; (x1, ..., xn) => expr(x1, ..., xn) }
173173
*
174-
* Note that the function value's parameters are untyped, hence the type will
175-
* be supplied by the environment (or if missing be supplied by the target
176-
* method as a fallback). On the other hand, if `xarity` is different from
177-
* the number of parameters in `mt`, then we cannot propagate parameter types
178-
* from the expected type, and we fallback to using the method's original
179-
* parameter types instead.
174+
* or (2)
180175
*
181-
* In either case, the result is an untyped tree, with `es` and `expr` as typed splices.
176+
* { val xs = es; (x1: T1, ..., xn: Tn) => expr(x1, ..., xn) }
177+
*
178+
* or (3)
179+
*
180+
* { val xs = es; (x1: T1, ..., xn: Tn) => expr(x1, ..., xn) _ }
181+
*
182+
* where `T1, ..., Tn` are the paremeter types of the expanded method.
183+
*
184+
* Case (3) applies if the method is curried, i.e. its result type is again a method
185+
* type. Case (2) applies if the expected arity of the function type `xarity` differs
186+
* from the number of parameters in `mt`. Case (1) applies if `mt` is uncurried
187+
* and its number of parameters equals `xarity`. In this case we can always infer
188+
* the parameter types later from the callee even if parameter types could not be
189+
* inferred from the expected type. Hence, we lose nothing by omitting parameter types
190+
* in the eta expansion. On the other hand omitting these parameters keeps the possibility
191+
* open that different parameters are inferred from the expected type, so we keep
192+
* more options open.
193+
*
194+
* In each case, the result is an untyped tree, with `es` and `expr` as typed splices.
195+
*
196+
* F[V](x) ==> (x => F[X])
182197
*/
183198
def etaExpand(tree: Tree, mt: MethodType, xarity: Int)(implicit ctx: Context): untpd.Tree = {
184199
import untpd._
185200
assert(!ctx.isAfterTyper)
186201
val defs = new mutable.ListBuffer[tpd.Tree]
187202
val lifted: Tree = TypedSplice(liftApp(defs, tree))
203+
val isLastApplication = mt.resultType match {
204+
case rt: MethodType => rt.isImplicitMethod
205+
case _ => true
206+
}
188207
val paramTypes: List[Tree] =
189-
if (mt.paramInfos.length == xarity) mt.paramInfos map (_ => TypeTree())
208+
if (isLastApplication && mt.paramInfos.length == xarity) mt.paramInfos map (_ => TypeTree())
190209
else mt.paramInfos map TypeTree
191210
val params = (mt.paramNames, paramTypes).zipped.map((name, tpe) =>
192211
ValDef(name, tpe, EmptyTree).withFlags(Synthetic | Param).withPos(tree.pos.startPos))
193212
var ids: List[Tree] = mt.paramNames map (name => Ident(name).withPos(tree.pos.startPos))
194213
if (mt.paramInfos.nonEmpty && mt.paramInfos.last.isRepeatedParam)
195214
ids = ids.init :+ repeated(ids.last)
196215
var body: Tree = Apply(lifted, ids)
197-
mt.resultType match {
198-
case rt: MethodType if !rt.isImplicitMethod => body = PostfixOp(body, Ident(nme.WILDCARD))
199-
case _ =>
200-
}
216+
if (!isLastApplication) body = PostfixOp(body, Ident(nme.WILDCARD))
201217
val fn = untpd.Function(params, body)
202218
if (defs.nonEmpty) untpd.Block(defs.toList map (untpd.TypedSplice(_)), fn) else fn
203219
}

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

+15-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ object Inferencing {
166166
case Apply(fn, _) => boundVars(fn, acc)
167167
case TypeApply(fn, targs) =>
168168
val tvars = targs.tpes.collect {
169-
case tvar: TypeVar if !tvar.isInstantiated => tvar
169+
case tvar: TypeVar if !tvar.isInstantiated && targs.contains(tvar.bindingTree) => tvar
170170
}
171171
boundVars(fn, acc ::: tvars)
172172
case Select(pre, _) => boundVars(pre, acc)
@@ -394,6 +394,20 @@ trait Inferencing { this: Typer =>
394394
}
395395
if (constraint.uninstVars exists qualifies) interpolate()
396396
}
397+
398+
/** The uninstantiated type variables introduced somehwere in `tree` */
399+
def uninstBoundVars(tree: Tree)(implicit ctx: Context): List[TypeVar] = {
400+
val buf = new mutable.ListBuffer[TypeVar]
401+
tree.foreachSubTree {
402+
case TypeApply(_, args) =>
403+
args.tpes.foreach {
404+
case tv: TypeVar if !tv.isInstantiated && tree.contains(tv.bindingTree) => buf += tv
405+
case _ =>
406+
}
407+
case _ =>
408+
}
409+
buf.toList
410+
}
397411
}
398412

399413
/** An enumeration controlling the degree of forcing in "is-dully-defined" checks. */

compiler/src/dotty/tools/dotc/typer/Typer.scala

+13-3
Original file line numberDiff line numberDiff line change
@@ -1970,7 +1970,7 @@ class Typer extends Namer
19701970
if (!tree.denot.isOverloaded) {
19711971
// for overloaded trees: resolve overloading before simplifying
19721972
if (tree.isDef) interpolateUndetVars(tree, tree.symbol, pt)
1973-
else if (!tree.tpe.widen.isInstanceOf[LambdaType]) interpolateUndetVars(tree, NoSymbol, pt)
1973+
else if (!tree.tpe.widen.isInstanceOf[MethodOrPoly]) interpolateUndetVars(tree, NoSymbol, pt)
19741974
tree.overwriteType(tree.tpe.simplified)
19751975
}
19761976
adaptInterpolated(tree, pt)
@@ -2227,8 +2227,18 @@ class Typer extends Namer
22272227
if (arity >= 0 &&
22282228
!tree.symbol.isConstructor &&
22292229
!ctx.mode.is(Mode.Pattern) &&
2230-
!(isSyntheticApply(tree) && !isExpandableApply))
2231-
typed(etaExpand(tree, wtp, arity), pt)
2230+
!(isSyntheticApply(tree) && !isExpandableApply)) {
2231+
// Eta expansion interacts in tricky ways with type variable instantiation
2232+
// because it can extend the region where type variables are bound (and therefore may not
2233+
// be interpolated). To avoid premature interpolations, we need to extend the
2234+
// bindingTree of variables as we go along. Test case in pos/i3945.scala.
2235+
val boundtvs = uninstBoundVars(tree)
2236+
val uexpanded = etaExpand(tree, wtp, arity)
2237+
boundtvs.foreach(_.bindingTree = uexpanded) // make boundtvs point to uexpanded so that they are _not_ interpolated
2238+
val texpanded = typedUnadapted(uexpanded, pt)
2239+
boundtvs.foreach(_.bindingTree = texpanded) // make boundtvs point to texpanded so that they _can_ be interpolated
2240+
adapt(texpanded, pt)
2241+
}
22322242
else if (wtp.paramInfos.isEmpty && isAutoApplied(tree.symbol))
22332243
adaptInterpolated(tpd.Apply(tree, Nil), pt)
22342244
else if (wtp.isImplicitMethod)

tests/pos/i3945.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
object Test {
2+
def int[A](k: String => A)(s: String)(x: Int): A = ???
3+
4+
// composing directly: ok in scalac, now also in dotc
5+
val c: (String => String) => (String) => (Int) => (Int) => String = (int[Int => String](_)).compose(int[String](_))
6+
7+
// unwrapping composition: ok in scalac, ok in dotc
8+
val q: (String => Int => String) => (String) => (Int) => (Int => String) = int[Int => String]
9+
val p: (String => String) => (String) => (Int) => String = int
10+
val c2: (String => String) => (String) => (Int) => (Int) => String = q.compose(p)
11+
12+
class B
13+
class C extends B
14+
implicit def iC: C => Unit = ???
15+
16+
// making sure A is not instantiated before implicit search
17+
def f[A](k: String => A)(s: String)(x: Int)(implicit y: A => Unit): A = ???
18+
val r: (String => C) => (String) => (Int) => B = f
19+
}

0 commit comments

Comments
 (0)