Skip to content

Commit e52aea4

Browse files
authored
Resolve name when named imp is behind wild imps (#21888)
When a named import (such as `import bug.util.List`) is defined before two clashing wildcard imports (`import bug.util.*; import java.util.*`) the name "List" should resolve to it, rather than a resolution error being emitted. This was due to the fact that `findRefRecur` didn't return the precedence at which it found that import, `checkImportAlternatives` used the `prevPrec` to `checkNewOrShadowed`. Now we check against the entire `foundResult`, allowing an early named import to be picked over later wildcard imports. Fixes #18529 (Replaces #21871)
2 parents 47f7d14 + 2d2b2ad commit e52aea4

File tree

7 files changed

+46
-0
lines changed

7 files changed

+46
-0
lines changed

Diff for: compiler/src/dotty/tools/dotc/typer/Typer.scala

+6
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
268268
// special cases: definitions beat imports, and named imports beat
269269
// wildcard imports, provided both are in contexts with same scope
270270
found
271+
else if newPrec == WildImport && ctx.outersIterator.exists: ctx =>
272+
ctx.isImportContext && namedImportRef(ctx.importInfo.uncheckedNN).exists
273+
then
274+
// Don't let two ambiguous wildcard imports rule over
275+
// a winning named import. See pos/i18529.
276+
found
271277
else
272278
if !scala2pkg && !previous.isError && !found.isError then
273279
fail(AmbiguousReference(name, newPrec, prevPrec, prevCtx,

Diff for: tests/pos/i18529/JCode1.java

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package bug.code;
2+
3+
import bug.util.List;
4+
import bug.util.*;
5+
import java.util.*;
6+
7+
public class JCode1 {
8+
public void m1(List<Integer> xs) { return; }
9+
}

Diff for: tests/pos/i18529/JCode2.java

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package bug.code;
2+
3+
import bug.util.*;
4+
import bug.util.List;
5+
import java.util.*;
6+
7+
public class JCode2 {
8+
public void m1(List<Integer> xs) { return; }
9+
}

Diff for: tests/pos/i18529/List.java

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package bug.util;
2+
3+
public final class List<E> {}

Diff for: tests/pos/i18529/SCode1.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package bug.code
2+
3+
import bug.util.List
4+
import bug.util.*
5+
import java.util.*
6+
7+
class SCode1 {
8+
def work(xs: List[Int]): Unit = {}
9+
}

Diff for: tests/pos/i18529/SCode2.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package bug.code
2+
3+
import bug.util.*
4+
import bug.util.List
5+
import java.util.*
6+
7+
class SCode2 {
8+
def work(xs: List[Int]): Unit = {}
9+
}

Diff for: tests/pos/i18529/Test.scala

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Test

0 commit comments

Comments
 (0)