Skip to content

Commit 3affebe

Browse files
committed
Refined handling of imports
Three changes: 1) Named imports beat wildcard imports in same scope, no matter what the relative order is 2) Named/named imports and wildcard/wildcard imports in the same scope of the same name but different symbols give an error 3) An error is not reported if two imported termRefs or typeRefs are equal wrt =:=.
1 parent 607033b commit 3affebe

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,12 @@ class Typer extends Namer with Applications with Implicits {
224224
* does properly shadow the new one from an outer context.
225225
*/
226226
def checkNewOrShadowed(found: Type, newPrec: Int): Type =
227-
if (!previous.exists || (previous == found)) found
228-
else if (newPrec == definition && (prevCtx.scope eq ctx.scope)) {
229-
// special case: definitions beat imports if both are in contexts with same scope
227+
if (!previous.exists || (previous =:= found)) found
228+
else if ((prevCtx.scope eq ctx.scope) &&
229+
(newPrec == definition ||
230+
newPrec == namedImport && prevPrec == wildImport)) {
231+
// special cases: definitions beat imports, and named imports beat
232+
// wildcard imports, provided both are in contexts with same scope
230233
found
231234
}
232235
else {
@@ -312,11 +315,14 @@ class Typer extends Namer with Applications with Implicits {
312315
}
313316
val curImport = ctx.importInfo
314317
if (curImport != null && curImport.isRootImport && previous.exists) return previous
315-
if (prevPrec < namedImport && (curImport ne outer.importInfo) && !curImport.sym.isCompleting) {
318+
// would import of kind `prec` be not shadowed by a nested higher-precedence definition?
319+
def isPossibleImport(prec: Int) =
320+
prevPrec < prec || prevPrec == prec && (prevCtx.scope eq ctx.scope)
321+
if (isPossibleImport(namedImport) && (curImport ne outer.importInfo) && !curImport.sym.isCompleting) {
316322
val namedImp = namedImportRef(curImport.site, curImport.selectors)
317323
if (namedImp.exists)
318324
return findRef(checkNewOrShadowed(namedImp, namedImport), namedImport, ctx)(outer)
319-
if (prevPrec < wildImport) {
325+
if (isPossibleImport(wildImport)) {
320326
val wildImp = wildImportRef(curImport)
321327
if (wildImp.exists)
322328
return findRef(checkNewOrShadowed(wildImp, wildImport), wildImport, ctx)(outer)

0 commit comments

Comments
 (0)