Skip to content

Commit 7bbf9f0

Browse files
committed
Improve overloading resolution if expected type is not FunProto
If expected type is a selection proto or similar we picked all alternatives that were compatible with it. But if the expected type was meant to be used after extension method expansion, then no alternative would be classified as compatible. We now drop all method alternatives in this case. If only one alternative remains, we pick that one. Fixes #14279
1 parent 5587767 commit 7bbf9f0

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,12 +1939,12 @@ trait Applications extends Compatibility {
19391939

19401940
record("resolveOverloaded.FunProto", alts.length)
19411941
val alts1 = narrowBySize(alts)
1942-
//report.log(i"narrowed by size: ${alts1.map(_.symbol.showDcl)}%, %")
1942+
overload.println(i"narrowed by size: ${alts1.map(_.symbol.showDcl)}%, %")
19431943
if isDetermined(alts1) then alts1
19441944
else
19451945
record("resolveOverloaded.narrowedBySize", alts1.length)
19461946
val alts2 = narrowByShapes(alts1)
1947-
//report.log(i"narrowed by shape: ${alts2.map(_.symbol.showDcl)}%, %")
1947+
overload.println(i"narrowed by shape: ${alts2.map(_.symbol.showDcl)}%, %")
19481948
if isDetermined(alts2) then alts2
19491949
else
19501950
record("resolveOverloaded.narrowedByShape", alts2.length)
@@ -1977,8 +1977,14 @@ trait Applications extends Compatibility {
19771977
* new java.io.ObjectOutputStream(f)
19781978
*/
19791979
pt match {
1980-
case SAMType(mtp) => narrowByTypes(alts, mtp.paramInfos, mtp.resultType)
1981-
case _ => compat
1980+
case SAMType(mtp) =>
1981+
narrowByTypes(alts, mtp.paramInfos, mtp.resultType)
1982+
case _ =>
1983+
// pick any alternatives that are not methods since these might be convertible
1984+
// to the expected type, or be used as extension method arguments.
1985+
val convertible = alts.filterNot(alt =>
1986+
normalize(alt, IgnoredProto(pt)).widenSingleton.isInstanceOf[MethodType])
1987+
if convertible.length == 1 then convertible else compat
19821988
}
19831989
else compat
19841990
}

tests/pos/i14729.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Foo():
2+
def normal: Unit = println("normal")
3+
4+
extension (f: Foo)
5+
def ext: Unit = println("ext")
6+
7+
object Bar:
8+
def makeFoo[A]: Foo = Foo()
9+
def makeFoo[A](s: String): Foo = Foo()
10+
11+
def tests: Unit =
12+
Bar.makeFoo[Int].ext // error
13+
14+
(Bar.makeFoo[Int]).ext // error
15+
16+
val foo = Bar.makeFoo[Int]
17+
foo.ext // ok
18+
19+
Bar.makeFoo[Int].normal // ok

0 commit comments

Comments
 (0)