Skip to content

Commit

Permalink
Resolve name when named imp is behind wild imps (#21888)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
odersky authored Dec 17, 2024
2 parents 47f7d14 + 2d2b2ad commit e52aea4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
// special cases: definitions beat imports, and named imports beat
// wildcard imports, provided both are in contexts with same scope
found
else if newPrec == WildImport && ctx.outersIterator.exists: ctx =>
ctx.isImportContext && namedImportRef(ctx.importInfo.uncheckedNN).exists
then
// Don't let two ambiguous wildcard imports rule over
// a winning named import. See pos/i18529.
found
else
if !scala2pkg && !previous.isError && !found.isError then
fail(AmbiguousReference(name, newPrec, prevPrec, prevCtx,
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i18529/JCode1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bug.code;

import bug.util.List;
import bug.util.*;
import java.util.*;

public class JCode1 {
public void m1(List<Integer> xs) { return; }
}
9 changes: 9 additions & 0 deletions tests/pos/i18529/JCode2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bug.code;

import bug.util.*;
import bug.util.List;
import java.util.*;

public class JCode2 {
public void m1(List<Integer> xs) { return; }
}
3 changes: 3 additions & 0 deletions tests/pos/i18529/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package bug.util;

public final class List<E> {}
9 changes: 9 additions & 0 deletions tests/pos/i18529/SCode1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bug.code

import bug.util.List
import bug.util.*
import java.util.*

class SCode1 {
def work(xs: List[Int]): Unit = {}
}
9 changes: 9 additions & 0 deletions tests/pos/i18529/SCode2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bug.code

import bug.util.*
import bug.util.List
import java.util.*

class SCode2 {
def work(xs: List[Int]): Unit = {}
}
1 change: 1 addition & 0 deletions tests/pos/i18529/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Test

0 comments on commit e52aea4

Please sign in to comment.