Skip to content

Commit 99afc89

Browse files
committed
Accommodate Scala2 name resolution scheme
Scala2 does not conform to spec Section 2, where it says: Bindings of different kinds have a precedence defined on them: 1. Definitions and declarations that are local, inherited, or made available by a package clause and also defined in the same compilation unit as the reference, have highest precedence. 2. Explicit imports have next highest precedence. 3. Wildcard imports have next highest precedence. 4. Definitions made available by a package clause, but not also defined in the same compilation unit as the reference, have lowest precedence. In fact Scala 2, merges (1) and (4) into highest precedence. This commit simulates the Scala2 behavior under -language:Scala2, but gives a migration warning. For the naming-resolution test case we get: dotc *.scala -language:Scala2 -migration callsite.scala:9: migration warning: Name resolution will change. currently selected : naming.resolution.Files in the future, without -language:Scala2: java.nio.file.Files' where Files is a type in package object package which is an alias of java.util.stream.Stream[java.nio.file.Path] Files' is a class in package file def gimmeFiles: Files = Files.list(Paths.get(".")) ^ one warning found
1 parent 1a538af commit 99afc89

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
7272
*/
7373
private var importedFromRoot: Set[Symbol] = Set()
7474

75+
/** Temporary data item for single call to typed ident:
76+
* This symbol would be found under Scala2 mode, but is not
77+
* in dotty (because dotty conforms to spec section 2
78+
* wrt to package member resolution but scalac doe not).
79+
*/
80+
private var foundUnderScala2: Type = _
81+
7582
def newLikeThis: Typer = new Typer
7683

7784
/** Attribute an identifier consisting of a simple name or wildcard
@@ -228,10 +235,14 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
228235
else curOwner.thisType.select(name, defDenot)
229236
if (!(curOwner is Package) || isDefinedInCurrentUnit(defDenot))
230237
return checkNewOrShadowed(found, definition) // no need to go further out, we found highest prec entry
231-
else if (defDenot.symbol is Package)
232-
return checkNewOrShadowed(previous orElse found, packageClause)
233-
else if (prevPrec < packageClause)
234-
return findRef(found, packageClause, ctx)(outer)
238+
else {
239+
if (ctx.scala2Mode)
240+
foundUnderScala2 = checkNewOrShadowed(found, definition)
241+
if (defDenot.symbol is Package)
242+
return checkNewOrShadowed(previous orElse found, packageClause)
243+
else if (prevPrec < packageClause)
244+
return findRef(found, packageClause, ctx)(outer)
245+
}
235246
}
236247
}
237248
val curImport = ctx.importInfo
@@ -267,10 +278,20 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
267278
val saved = importedFromRoot
268279
importedFromRoot = Set.empty
269280

270-
val rawType =
281+
foundUnderScala2 = NoType
282+
283+
var rawType =
271284
try findRef(NoType, BindingPrec.nothingBound, NoContext)
272285
finally importedFromRoot = saved
273286

287+
if (foundUnderScala2.exists && (foundUnderScala2 ne rawType)) {
288+
ctx.migrationWarning(
289+
ex"""Name resolution will change.
290+
| currently selected : $foundUnderScala2
291+
| in the future, without -language:Scala2: $rawType""", tree.pos)
292+
rawType = foundUnderScala2
293+
}
294+
274295
val ownType =
275296
if (rawType.exists)
276297
ensureAccessible(rawType, superAccess = false, tree.pos)

tests/disabled/not-representable/naming-resolution/callsite.scala

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/disabled/not-representable/naming-resolution/compiler.error

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/disabled/not-representable/naming-resolution/package.scala

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)