Skip to content

Commit 62fe541

Browse files
committed
New strategy for polymorphic overloads with apply members
Instead of doing overload selection directly on the result of the PolyType which contains unbounded paramrefs, we now first call `wildApprox` to get rid of the paramrefs. The resulting behavior is different from what Scala 2 does, but it makes polymorphic overloads with apply members behave more like polymorphic overloads with a parameter list, e.g given: def a(x: Any): Any def a[T <: Int]: T => T a(1) def b(x: Any): Any def b[T <: Int](x: T): T b(1) in both cases the second overload will be selected.
1 parent 743ab1f commit 62fe541

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
15211521
if (tryApply(alt)) {
15221522
val qual = alt.widen match {
15231523
case pt: PolyType =>
1524-
pt.resultType
1524+
wildApprox(pt.resultType)
15251525
case _ =>
15261526
alt
15271527
}

tests/neg/poly-overloads.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ class A {
88
def foo2[T]: T => String = ???
99

1010
foo2(1): String // ok
11-
foo2("") // error, because T isn't instantiated before overloading resolution is done
11+
foo2("") // ok, unlike Scala 2
1212

1313
def foo3(x: Any): Any = x
1414
def foo3[T <: Int]: T => T = x => x
1515

1616
val a = foo3(1) // ok
17-
val b: Int = a // error because the non-apply alternative was chosen, like in Scala 2
17+
val b: Int = a // ok, unlike Scala 2 which prefers the first overload
1818

1919
def foo4(x: Any): Any = x
2020
def foo4[T >: Int]: T => T = x => x
2121

22-
val c = foo4(1) // ok
23-
val d: Int = c // ok
22+
val c = foo4(1) // error, unlike Scala 2 this is ambiguous
2423
}

0 commit comments

Comments
 (0)