Skip to content

Commit 0bece40

Browse files
Merge pull request #13588 from dotty-staging/fix-13558
Prioritize ambiguous errors in extension method search
2 parents f5e7485 + 6dd09bf commit 0bece40

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3163,7 +3163,10 @@ class Typer extends Namer
31633163
if !app.isEmpty && !nestedCtx.reporter.hasErrors then
31643164
nestedCtx.typerState.commit()
31653165
return app
3166-
for err <- nestedCtx.reporter.allErrors.take(1) do
3166+
val errs = nestedCtx.reporter.allErrors
3167+
val remembered = // report AmbiguousReferences as priority, otherwise last error
3168+
(errs.filter(_.msg.isInstanceOf[AmbiguousReference]) ++ errs).take(1)
3169+
for err <- remembered do
31673170
rememberSearchFailure(qual,
31683171
SearchFailure(app.withType(FailedExtension(app, selectionProto, err.msg))))
31693172
catch case ex: TypeError => nestedFailure(ex)

tests/neg/i13558.check

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- [E008] Not Found Error: tests/neg/i13558.scala:23:14 ----------------------------------------------------------------
2+
23 | println(a.id) // error
3+
| ^^^^
4+
| value id is not a member of testcode.A.
5+
| An extension method was tried, but could not be fully constructed:
6+
|
7+
| testcode.ExtensionA.id(a) failed with
8+
|
9+
| Reference to id is ambiguous,
10+
| it is both imported by import testcode.ExtensionB._
11+
| and imported subsequently by import testcode.ExtensionA._
12+
-- [E008] Not Found Error: tests/neg/i13558.scala:29:14 ----------------------------------------------------------------
13+
29 | println(a.id) // error
14+
| ^^^^
15+
| value id is not a member of testcode.A.
16+
| An extension method was tried, but could not be fully constructed:
17+
|
18+
| testcode.ExtensionB.id(a) failed with
19+
|
20+
| Reference to id is ambiguous,
21+
| it is both imported by import testcode.ExtensionA._
22+
| and imported subsequently by import testcode.ExtensionB._

tests/neg/i13558.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package testcode
2+
3+
class A
4+
5+
class B
6+
7+
object ExtensionA {
8+
extension (self: A) {
9+
def id = "A"
10+
}
11+
}
12+
object ExtensionB {
13+
extension (self: B) {
14+
def id = "B"
15+
}
16+
}
17+
18+
object Main {
19+
def main1(args: Array[String]): Unit = {
20+
import ExtensionB._
21+
import ExtensionA._
22+
val a = A()
23+
println(a.id) // error
24+
}
25+
def main2(args: Array[String]): Unit = {
26+
import ExtensionA._
27+
import ExtensionB._
28+
val a = A()
29+
println(a.id) // error
30+
}
31+
}

0 commit comments

Comments
 (0)