Skip to content

Prioritize ambiguous errors in extension method search #13588

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
Sep 29, 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
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3150,7 +3150,10 @@ class Typer extends Namer
if !app.isEmpty && !nestedCtx.reporter.hasErrors then
nestedCtx.typerState.commit()
return app
for err <- nestedCtx.reporter.allErrors.take(1) do
val errs = nestedCtx.reporter.allErrors
val remembered = // report AmbiguousReferences as priority, otherwise last error
(errs.filter(_.msg.isInstanceOf[AmbiguousReference]) ++ errs).take(1)
for err <- remembered do
rememberSearchFailure(qual,
SearchFailure(app.withType(FailedExtension(app, selectionProto, err.msg))))
catch case ex: TypeError => nestedFailure(ex)
Expand Down
22 changes: 22 additions & 0 deletions tests/neg/i13558.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- [E008] Not Found Error: tests/neg/i13558.scala:23:14 ----------------------------------------------------------------
23 | println(a.id) // error
| ^^^^
| value id is not a member of testcode.A.
| An extension method was tried, but could not be fully constructed:
|
| testcode.ExtensionA.id(a) failed with
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 would be nice if we issued

     id(a)     failed with

instead, but it turns out to be devilishly complicated to do so. So I decided it's not worth going down that rabbit hole since a fix would likely make code that's already super complicated even less understandable.

|
| Reference to id is ambiguous,
| it is both imported by import testcode.ExtensionB._
| and imported subsequently by import testcode.ExtensionA._
-- [E008] Not Found Error: tests/neg/i13558.scala:29:14 ----------------------------------------------------------------
29 | println(a.id) // error
| ^^^^
| value id is not a member of testcode.A.
| An extension method was tried, but could not be fully constructed:
|
| testcode.ExtensionB.id(a) failed with
|
| Reference to id is ambiguous,
| it is both imported by import testcode.ExtensionA._
| and imported subsequently by import testcode.ExtensionB._
31 changes: 31 additions & 0 deletions tests/neg/i13558.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package testcode

class A

class B

object ExtensionA {
extension (self: A) {
def id = "A"
}
}
object ExtensionB {
extension (self: B) {
def id = "B"
}
}

object Main {
def main1(args: Array[String]): Unit = {
import ExtensionB._
import ExtensionA._
val a = A()
println(a.id) // error
}
def main2(args: Array[String]): Unit = {
import ExtensionA._
import ExtensionB._
val a = A()
println(a.id) // error
}
}