Skip to content

Commit 36f9133

Browse files
committed
Fix import disabling
It was broken before, since it worked only on wildcard imports.
1 parent 3469039 commit 36f9133

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ object Contexts {
216216
else if (isNonEmptyScopeContext) scope.implicitDecls
217217
else Nil
218218
val outerImplicits =
219-
if (isImportContext && importInfo.hiddenRoot.exists)
220-
outer.implicits exclude importInfo.hiddenRoot
219+
if (isImportContext && importInfo.unimported.exists)
220+
outer.implicits exclude importInfo.unimported
221221
else
222222
outer.implicits
223223
if (implicitRefs.isEmpty) outerImplicits

compiler/src/dotty/tools/dotc/typer/ImportInfo.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,22 @@ class ImportInfo(symf: => Symbol, val selectors: List[untpd.Tree], val isRootImp
9595
/** The root import symbol hidden by this symbol, or NoSymbol if no such symbol is hidden.
9696
* Note: this computation needs to work even for un-initialized import infos, and
9797
* is not allowed to force initialization.
98+
*
99+
* TODO: Once we have fully bootstrapped, I would prefer if we expressed
100+
* unimport with an `override` modifier, and generalized it to all imports.
101+
* I believe this would be more transparent than the curren set of conditions. E.g.
102+
*
103+
* override import Predef.{any2stringAdd => _, StringAdd => _, _} // disables String +
104+
* override import java.lang.{} // disables all imports
98105
*/
99-
lazy val hiddenRoot: Symbol = {
100-
val sym = site.termSymbol
101-
def hasMaskingSelector = selectors exists {
106+
lazy val unimported: Symbol = {
107+
lazy val sym = site.termSymbol
108+
val hasMaskingSelector = selectors exists {
102109
case Thicket(_ :: Ident(nme.WILDCARD) :: Nil) => true
103110
case _ => false
104111
}
105-
if ((defn.RootImportTypes exists (_.symbol == sym)) && hasMaskingSelector) sym else NoSymbol
112+
if (hasMaskingSelector && defn.RootImportTypes.exists(_.symbol == sym)) sym
113+
else NoSymbol
106114
}
107115

108116
override def toString = {

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
7474
* Note: It would be more proper to move importedFromRoot into typedIdent.
7575
* We should check that this has no performance degradation, however.
7676
*/
77-
private var importedFromRoot: Set[Symbol] = Set()
77+
private var unimported: Set[Symbol] = Set()
7878

7979
/** Temporary data item for single call to typed ident:
8080
* This symbol would be found under Scala2 mode, but is not
@@ -102,15 +102,6 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
102102
*/
103103
def error(msg: => Message, pos: Position) = ctx.error(msg, pos)
104104

105-
/** Is this import a root import that has been shadowed by an explicit
106-
* import in the same program?
107-
*/
108-
def isDisabled(imp: ImportInfo, site: Type): Boolean = {
109-
if (imp.isRootImport && (importedFromRoot contains site.termSymbol)) return true
110-
if (imp.hiddenRoot.exists) importedFromRoot += imp.hiddenRoot
111-
false
112-
}
113-
114105
/** Does this identifier appear as a constructor of a pattern? */
115106
def isPatternConstr =
116107
if (ctx.mode.isExpr && (ctx.outer.mode is Mode.Pattern))
@@ -201,7 +192,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
201192
}
202193

203194
def selection(name: Name) =
204-
if (imp.sym.isCompleting) {
195+
if (unimported.contains(imp.site.termSymbol))
196+
NoType
197+
else if (imp.sym.isCompleting) {
205198
ctx.warning(i"cyclic ${imp.sym}, ignored", tree.pos)
206199
NoType
207200
}
@@ -232,7 +225,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
232225
def wildImportRef(imp: ImportInfo)(implicit ctx: Context): Type = {
233226
if (imp.isWildcardImport) {
234227
val pre = imp.site
235-
if (!isDisabled(imp, pre) && !(imp.excluded contains name.toTermName) && name != nme.CONSTRUCTOR) {
228+
if (!unimported.contains(pre.termSymbol) &&
229+
!imp.excluded.contains(name.toTermName) &&
230+
name != nme.CONSTRUCTOR) {
236231
val denot = pre.member(name).accessibleFrom(pre)(refctx)
237232
if (reallyExists(denot)) return pre.select(name, denot)
238233
}
@@ -289,6 +284,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
289284
if (result.exists) result
290285
else { // find import
291286
val curImport = ctx.importInfo
287+
def updateUnimported() =
288+
if (curImport.unimported.exists) unimported += curImport.unimported
292289
if (ctx.owner.is(Package) && curImport != null && curImport.isRootImport && previous.exists)
293290
previous // no more conflicts possible in this case
294291
else if (isPossibleImport(namedImport) && (curImport ne outer.importInfo)) {
@@ -299,8 +296,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
299296
val wildImp = wildImportRef(curImport)
300297
if (wildImp.exists)
301298
findRef(checkNewOrShadowed(wildImp, wildImport), wildImport, ctx)(outer)
302-
else loop(outer)
303-
} else loop(outer)
299+
else {
300+
updateUnimported()
301+
loop(outer)
302+
}
303+
}
304+
else {
305+
updateUnimported()
306+
loop(outer)
307+
}
304308
}
305309
else loop(outer)
306310
}
@@ -321,9 +325,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
321325
}
322326

323327
val rawType = {
324-
val saved1 = importedFromRoot
328+
val saved1 = unimported
325329
val saved2 = foundUnderScala2
326-
importedFromRoot = Set.empty
330+
unimported = Set.empty
327331
foundUnderScala2 = NoType
328332
try {
329333
var found = findRef(NoType, BindingPrec.nothingBound, NoContext)
@@ -337,7 +341,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
337341
found
338342
}
339343
finally {
340-
importedFromRoot = saved1
344+
unimported = saved1
341345
foundUnderScala2 = saved2
342346
}
343347
}

0 commit comments

Comments
 (0)