Skip to content

Commit 6c375b5

Browse files
authored
Merge pull request #11553 from dotty-staging/fix-11544
Improve error message in overloading resolution
2 parents 07d9dd2 + f1a6bdf commit 6c375b5

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-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.check

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- [E051] Reference Error: tests/neg/i11544.scala:6:13 -----------------------------------------------------------------
2+
6 | val n = m1.foo(23) // error
3+
| ^^^^^^
4+
| Ambiguous overload. The overloaded alternatives of method (str: String, int: Int): Int with types
5+
| (str: String, int: Int): Int
6+
| (arg: Int): Int
7+
| both match arguments ((23 : Int))
8+
|
9+
| Note: Overloaded definitions introduced by refinements cannot be resolved
10+
11+
longer explanation available when compiling with `-explain`

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)