From 99afc892839ca6209786ccd0dbca2544633be2e8 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 26 Aug 2016 18:29:17 +0200 Subject: 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 --- src/dotty/tools/dotc/typer/Typer.scala | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'src/dotty/tools/dotc/typer/Typer.scala') diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 52470ba87..5fbb395ba 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -72,6 +72,13 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit */ private var importedFromRoot: Set[Symbol] = Set() + /** Temporary data item for single call to typed ident: + * This symbol would be found under Scala2 mode, but is not + * in dotty (because dotty conforms to spec section 2 + * wrt to package member resolution but scalac doe not). + */ + private var foundUnderScala2: Type = _ + def newLikeThis: Typer = new Typer /** 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 else curOwner.thisType.select(name, defDenot) if (!(curOwner is Package) || isDefinedInCurrentUnit(defDenot)) return checkNewOrShadowed(found, definition) // no need to go further out, we found highest prec entry - else if (defDenot.symbol is Package) - return checkNewOrShadowed(previous orElse found, packageClause) - else if (prevPrec < packageClause) - return findRef(found, packageClause, ctx)(outer) + else { + if (ctx.scala2Mode) + foundUnderScala2 = checkNewOrShadowed(found, definition) + if (defDenot.symbol is Package) + return checkNewOrShadowed(previous orElse found, packageClause) + else if (prevPrec < packageClause) + return findRef(found, packageClause, ctx)(outer) + } } } val curImport = ctx.importInfo @@ -267,10 +278,20 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit val saved = importedFromRoot importedFromRoot = Set.empty - val rawType = + foundUnderScala2 = NoType + + var rawType = try findRef(NoType, BindingPrec.nothingBound, NoContext) finally importedFromRoot = saved + if (foundUnderScala2.exists && (foundUnderScala2 ne rawType)) { + ctx.migrationWarning( + ex"""Name resolution will change. + | currently selected : $foundUnderScala2 + | in the future, without -language:Scala2: $rawType""", tree.pos) + rawType = foundUnderScala2 + } + val ownType = if (rawType.exists) ensureAccessible(rawType, superAccess = false, tree.pos) -- cgit v1.2.3