Skip to content

Commit 8f63854

Browse files
committed
Improve error message in overloading resolution
Improve error message when trying to do overloading resolution of alternatives introduces by refinements. As i8736 shows, it's not so simple to identify overloaded members introduced by refinements, since this can arise through intersection and de-aliasing. So our best bet is to make the error message clearer at the point of overloading resolution.
1 parent ce30a1c commit 8f63854

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,6 @@ class Typer extends Namer
17791779
tpt1.tpe.typeSymbol == defn.PolyFunctionClass && rsym.name == nme.apply
17801780
if (!polymorphicRefinementAllowed && rsym.info.isInstanceOf[PolyType] && rsym.allOverriddenSymbols.isEmpty)
17811781
report.error(PolymorphicMethodMissingTypeInParent(rsym, tpt1.symbol), refinement.srcPos)
1782-
17831782
val member = refineCls.info.member(rsym.name)
17841783
if (member.isOverloaded)
17851784
report.error(OverloadInRefinement(rsym), refinement.srcPos)
@@ -3124,20 +3123,16 @@ class Typer extends Namer
31243123
}
31253124
}
31263125
case ambiAlts =>
3127-
if (tree.tpe.isErroneous || pt.isErroneous) tree.withType(UnspecifiedErrorType)
3128-
else {
3126+
if tree.tpe.isErroneous || pt.isErroneous then tree.withType(UnspecifiedErrorType)
3127+
else
31293128
val remainingDenots = altDenots.filter(denot => ambiAlts.contains(altRef(denot)))
31303129
val addendum =
3131-
if ambiAlts.toSet.size != ambiAlts.size then
3132-
// Several variants have the same signature. This can happen for structural
3133-
// type selections. See i8736.scala
3134-
"""|
3135-
|
3136-
|Note: this happens because two or more alternatives have the same erasure,
3137-
| so they cannot be distinguished by overloading resolution""".stripMargin
3130+
if ambiAlts.exists(!_.symbol.exists) then
3131+
i"""|
3132+
|
3133+
|Note: Overloaded definitions introduced by refinements cannot be resolved"""
31383134
else ""
31393135
errorTree(tree, AmbiguousOverload(tree, remainingDenots, pt, addendum))
3140-
}
31413136
}
31423137
}
31433138

tests/neg/i11544.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import language.reflectiveCalls
2+
3+
object Foo {
4+
val m: ({ def foo(str: String, int: Int): Int }){ def foo(arg: Int): Int } = ???
5+
val m1 = m
6+
val n = m1.foo(23) // error
7+
}

tests/neg/i8736.check

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
| (k: ("z" : String)): Boolean
1818
| all match arguments (("z" : String))
1919
|
20-
| Note: this happens because two or more alternatives have the same erasure,
21-
| so they cannot be distinguished by overloading resolution
20+
| Note: Overloaded definitions introduced by refinements cannot be resolved
2221

2322
longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)