Skip to content

Commit 1656850

Browse files
oderskyMasseGuillaume
authored andcommitted
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 3a03e24 commit 1656850

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
@@ -73,6 +73,13 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
7373
*/
7474
private var importedFromRoot: Set[Symbol] = Set()
7575

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

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

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

288+
if (foundUnderScala2.exists && (foundUnderScala2 ne rawType)) {
289+
ctx.migrationWarning(
290+
ex"""Name resolution will change.
291+
| currently selected : $foundUnderScala2
292+
| in the future, without -language:Scala2: $rawType""", tree.pos)
293+
rawType = foundUnderScala2
294+
}
295+
275296
val ownType =
276297
if (rawType.exists)
277298
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)