Skip to content

Commit 7e2627c

Browse files
committed
Avoid over-eager eta-expansion
For neg/typedapply.scala we had an infinite sequence of eta-expansions, followed by `.apply` selections.
1 parent 87c5ac0 commit 7e2627c

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,13 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
280280
case _ => false
281281
}
282282

283+
/** Is tree a Function or Closure node, possibly preceded by definitions? (currently unused) */
284+
def isClosure(tree: Tree)(implicit ctx: Context): Boolean = unsplice(tree) match {
285+
case _: Function | _: Closure => true
286+
case Block(_, expr) => isClosure(expr)
287+
case _ => false
288+
}
289+
283290
/** Is `tree` an implicit function or closure, possibly nested in a block? */
284291
def isImplicitClosure(tree: Tree)(implicit ctx: Context): Boolean = unsplice(tree) match {
285292
case Function((param: untpd.ValDef) :: _, _) => param.mods.is(Implicit)
@@ -470,6 +477,12 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
470477
case _ => false
471478
}
472479

480+
/** Is tree a compiler-generated `.apply` node? */
481+
def isSyntheticApply(tree: Tree): Boolean = tree match {
482+
case Select(qual, nme.apply) => tree.pos.end == qual.pos.end
483+
case _ => false
484+
}
485+
473486
/** Strips layers of `.asInstanceOf[T]` / `_.$asInstanceOf[T]()` from an expression */
474487
def stripCast(tree: Tree)(implicit ctx: Context): Tree = {
475488
def isCast(sel: Tree) = sel.symbol == defn.Any_asInstanceOf

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,12 +2012,16 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
20122012
else -1 // no eta expansion in this case
20132013
}
20142014

2015-
if (arity >= 0 && !tree.symbol.isConstructor && !ctx.mode.is(Mode.Pattern))
2015+
if (wtp.isImplicit)
2016+
err.typeMismatch(tree, pt)
2017+
else if (arity >= 0 &&
2018+
!tree.symbol.isConstructor &&
2019+
!ctx.mode.is(Mode.Pattern) &&
2020+
!isApplyProto(pt) &&
2021+
!isSyntheticApply(tree))
20162022
typed(etaExpand(tree, wtp, arity), pt)
20172023
else if (wtp.paramInfos.isEmpty)
20182024
adaptInterpolated(tpd.Apply(tree, Nil), pt, EmptyTree)
2019-
else if (wtp.isImplicit)
2020-
err.typeMismatch(tree, pt)
20212025
else
20222026
missingArgs
20232027
case _ =>

tests/neg/typedapply.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ object typedapply {
66

77
foo[Int, String, String](1, "abc") // error: wrong number of type parameters
88

9+
def fuz(x: Int, y: String) = (x, y)
10+
11+
val x1 = fuz.curried // OK
12+
val x2: Int => String => (Int, String) = x1
13+
914
def bar(x: Int) = x
1015

11-
bar[Int](1) // error: does not take parameters
16+
bar[Int](1) // error: does not take type parameters
17+
18+
bar.baz // error: baz is not a member of Int => Int
19+
1220

1321
}
22+
23+

0 commit comments

Comments
 (0)