Skip to content

Improve error message in overloading resolution #11553

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 2 commits into from
Feb 28, 2021
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
17 changes: 6 additions & 11 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,6 @@ class Typer extends Namer
tpt1.tpe.typeSymbol == defn.PolyFunctionClass && rsym.name == nme.apply
if (!polymorphicRefinementAllowed && rsym.info.isInstanceOf[PolyType] && rsym.allOverriddenSymbols.isEmpty)
report.error(PolymorphicMethodMissingTypeInParent(rsym, tpt1.symbol), refinement.srcPos)

val member = refineCls.info.member(rsym.name)
if (member.isOverloaded)
report.error(OverloadInRefinement(rsym), refinement.srcPos)
Expand Down Expand Up @@ -3124,20 +3123,16 @@ class Typer extends Namer
}
}
case ambiAlts =>
if (tree.tpe.isErroneous || pt.isErroneous) tree.withType(UnspecifiedErrorType)
else {
if tree.tpe.isErroneous || pt.isErroneous then tree.withType(UnspecifiedErrorType)
else
val remainingDenots = altDenots.filter(denot => ambiAlts.contains(altRef(denot)))
val addendum =
if ambiAlts.toSet.size != ambiAlts.size then
// Several variants have the same signature. This can happen for structural
// type selections. See i8736.scala
"""|
|
|Note: this happens because two or more alternatives have the same erasure,
| so they cannot be distinguished by overloading resolution""".stripMargin
if ambiAlts.exists(!_.symbol.exists) then
i"""|
|
|Note: Overloaded definitions introduced by refinements cannot be resolved"""
else ""
errorTree(tree, AmbiguousOverload(tree, remainingDenots, pt, addendum))
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/neg/i11544.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- [E051] Reference Error: tests/neg/i11544.scala:6:13 -----------------------------------------------------------------
6 | val n = m1.foo(23) // error
| ^^^^^^
| Ambiguous overload. The overloaded alternatives of method (str: String, int: Int): Int with types
| (str: String, int: Int): Int
| (arg: Int): Int
| both match arguments ((23 : Int))
|
| Note: Overloaded definitions introduced by refinements cannot be resolved

longer explanation available when compiling with `-explain`
7 changes: 7 additions & 0 deletions tests/neg/i11544.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import language.reflectiveCalls

object Foo {
val m: ({ def foo(str: String, int: Int): Int }){ def foo(arg: Int): Int } = ???
val m1 = m
val n = m1.foo(23) // error
}
3 changes: 1 addition & 2 deletions tests/neg/i8736.check
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
| (k: ("z" : String)): Boolean
| all match arguments (("z" : String))
|
| Note: this happens because two or more alternatives have the same erasure,
| so they cannot be distinguished by overloading resolution
| Note: Overloaded definitions introduced by refinements cannot be resolved

longer explanation available when compiling with `-explain`