Skip to content

Improve overloading resolution if expected type is not FunProto #14733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1939,12 +1939,12 @@ trait Applications extends Compatibility {

record("resolveOverloaded.FunProto", alts.length)
val alts1 = narrowBySize(alts)
//report.log(i"narrowed by size: ${alts1.map(_.symbol.showDcl)}%, %")
overload.println(i"narrowed by size: ${alts1.map(_.symbol.showDcl)}%, %")
if isDetermined(alts1) then alts1
else
record("resolveOverloaded.narrowedBySize", alts1.length)
val alts2 = narrowByShapes(alts1)
//report.log(i"narrowed by shape: ${alts2.map(_.symbol.showDcl)}%, %")
overload.println(i"narrowed by shape: ${alts2.map(_.symbol.showDcl)}%, %")
if isDetermined(alts2) then alts2
else
record("resolveOverloaded.narrowedByShape", alts2.length)
Expand Down Expand Up @@ -1977,8 +1977,14 @@ trait Applications extends Compatibility {
* new java.io.ObjectOutputStream(f)
*/
pt match {
case SAMType(mtp) => narrowByTypes(alts, mtp.paramInfos, mtp.resultType)
case _ => compat
case SAMType(mtp) =>
narrowByTypes(alts, mtp.paramInfos, mtp.resultType)
case _ =>
// pick any alternatives that are not methods since these might be convertible
// to the expected type, or be used as extension method arguments.
val convertible = alts.filterNot(alt =>
normalize(alt, IgnoredProto(pt)).widenSingleton.isInstanceOf[MethodType])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is using normalize really needed here or could we do:

Suggested change
normalize(alt, IgnoredProto(pt)).widenSingleton.isInstanceOf[MethodType])
alt.widenSingleton.isInstanceOf[MethodOrPoly])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's needed for skipping using clauses

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. normalize will create fresh type variables for PolyTypes, they'll be gc'ed eventually but if we wanted to avoid creating them in the first place we could perhaps have a stripPolyAndImplicit method, or add a flag to normalize for situations where we don't keep around its result.

if convertible.length == 1 then convertible else compat
}
else compat
}
Expand Down
19 changes: 19 additions & 0 deletions tests/pos/i14729.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Foo():
def normal: Unit = println("normal")

extension (f: Foo)
def ext: Unit = println("ext")

object Bar:
def makeFoo[A]: Foo = Foo()
def makeFoo[A](s: String): Foo = Foo()

def tests: Unit =
Bar.makeFoo[Int].ext // error

(Bar.makeFoo[Int]).ext // error

val foo = Bar.makeFoo[Int]
foo.ext // ok

Bar.makeFoo[Int].normal // ok