Skip to content

Commit

Permalink
Merge pull request #13588 from dotty-staging/fix-13558
Browse files Browse the repository at this point in the history
Prioritize ambiguous errors in extension method search
  • Loading branch information
anatoliykmetyuk committed Sep 29, 2021
2 parents f5e7485 + 6dd09bf commit 0bece40
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Expand Up @@ -3163,7 +3163,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
@@ -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
|
| 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
@@ -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
}
}

0 comments on commit 0bece40

Please sign in to comment.