Skip to content

Commit 67c9abb

Browse files
authored
Merge pull request #6756 from dotty-staging/poly-apply
Handle polymorphic overloads with apply members
2 parents e1b8f74 + 62fe541 commit 67c9abb

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,15 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
15181518

15191519
/** Replace each alternative by its apply members where necessary */
15201520
def applyMembers(alt: TermRef): List[TermRef] =
1521-
if (tryApply(alt)) alt.member(nme.apply).alternatives.map(TermRef(alt, nme.apply, _))
1521+
if (tryApply(alt)) {
1522+
val qual = alt.widen match {
1523+
case pt: PolyType =>
1524+
wildApprox(pt.resultType)
1525+
case _ =>
1526+
alt
1527+
}
1528+
qual.member(nme.apply).alternatives.map(TermRef(alt, nme.apply, _))
1529+
}
15221530
else alt :: Nil
15231531

15241532
/** Fall back from an apply method to its original alternative */

tests/neg/poly-overloads.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class A {
2+
def foo1(x: Int): Int = x
3+
def foo1[T]: String => T = ???
4+
5+
foo1("") // ok
6+
7+
def foo2(x: Int): Int = x
8+
def foo2[T]: T => String = ???
9+
10+
foo2(1): String // ok
11+
foo2("") // ok, unlike Scala 2
12+
13+
def foo3(x: Any): Any = x
14+
def foo3[T <: Int]: T => T = x => x
15+
16+
val a = foo3(1) // ok
17+
val b: Int = a // ok, unlike Scala 2 which prefers the first overload
18+
19+
def foo4(x: Any): Any = x
20+
def foo4[T >: Int]: T => T = x => x
21+
22+
val c = foo4(1) // error, unlike Scala 2 this is ambiguous
23+
}

0 commit comments

Comments
 (0)