Skip to content

Commit 059661d

Browse files
committed
Fix import disabling
It was broken before, since it worked only on wildcard imports.
1 parent 81bf077 commit 059661d

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

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

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 = {

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))
@@ -196,7 +187,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
196187
}
197188

198189
def selection(name: Name) =
199-
if (imp.sym.isCompleting) {
190+
if (unimported.contains(imp.site.termSymbol))
191+
NoType
192+
else if (imp.sym.isCompleting) {
200193
ctx.warning(i"cyclic ${imp.sym}, ignored", tree.pos)
201194
NoType
202195
}
@@ -227,7 +220,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
227220
def wildImportRef(imp: ImportInfo)(implicit ctx: Context): Type = {
228221
if (imp.isWildcardImport) {
229222
val pre = imp.site
230-
if (!isDisabled(imp, pre) && !(imp.excluded contains name.toTermName) && name != nme.CONSTRUCTOR) {
223+
if (!unimported.contains(pre.termSymbol) &&
224+
!imp.excluded.contains(name.toTermName) &&
225+
name != nme.CONSTRUCTOR) {
231226
val denot = pre.member(name).accessibleFrom(pre)(refctx)
232227
if (reallyExists(denot)) return pre.select(name, denot)
233228
}
@@ -284,6 +279,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
284279
if (result.exists) result
285280
else { // find import
286281
val curImport = ctx.importInfo
282+
def updateUnimported() =
283+
if (curImport.unimported.exists) unimported += curImport.unimported
287284
if (ctx.owner.is(Package) && curImport != null && curImport.isRootImport && previous.exists)
288285
previous // no more conflicts possible in this case
289286
else if (isPossibleImport(namedImport) && (curImport ne outer.importInfo)) {
@@ -294,8 +291,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
294291
val wildImp = wildImportRef(curImport)
295292
if (wildImp.exists)
296293
findRef(checkNewOrShadowed(wildImp, wildImport), wildImport, ctx)(outer)
297-
else loop(outer)
298-
} else loop(outer)
294+
else {
295+
updateUnimported()
296+
loop(outer)
297+
}
298+
}
299+
else {
300+
updateUnimported()
301+
loop(outer)
302+
}
299303
}
300304
else loop(outer)
301305
}
@@ -316,9 +320,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
316320
}
317321

318322
val rawType = {
319-
val saved1 = importedFromRoot
323+
val saved1 = unimported
320324
val saved2 = foundUnderScala2
321-
importedFromRoot = Set.empty
325+
unimported = Set.empty
322326
foundUnderScala2 = NoType
323327
try {
324328
var found = findRef(NoType, BindingPrec.nothingBound, NoContext)
@@ -332,7 +336,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
332336
found
333337
}
334338
finally {
335-
importedFromRoot = saved1
339+
unimported = saved1
336340
foundUnderScala2 = saved2
337341
}
338342
}

0 commit comments

Comments
 (0)