Skip to content

Commit eed4f2f

Browse files
authored
Merge pull request #1901 from dotty-staging/change-predef-import
Adopt scala's scheme for root import hiding
2 parents 36237cb + eb10637 commit eed4f2f

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ class Definitions {
747747
else if (ctx.settings.YnoPredef.value) StaticRootImportFns
748748
else StaticRootImportFns ++ PredefImportFns
749749

750+
lazy val ShadowableImportNames = Set("Predef", "DottyPredef").map(_.toTermName)
750751
lazy val RootImportTypes = RootImportFns.map(_())
751752

752753
/** Modules whose members are in the default namespace and their module classes */

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ object ImportInfo {
1515
val selectors = untpd.Ident(nme.WILDCARD) :: Nil
1616
def expr = tpd.Ident(refFn())
1717
def imp = tpd.Import(expr, selectors)
18-
new ImportInfo(imp.symbol, selectors, isRootImport = true)
18+
new ImportInfo(imp.symbol, selectors, None, isRootImport = true)
1919
}
2020
}
2121

2222
/** Info relating to an import clause
23-
* @param sym The import symbol defined by the clause
24-
* @param selectors The selector clauses
25-
* @param rootImport true if this is one of the implicit imports of scala, java.lang
26-
* or Predef in the start context, false otherwise.
23+
* @param sym The import symbol defined by the clause
24+
* @param selectors The selector clauses
25+
* @param symNameOpt Optionally, the name of the import symbol. None for root imports.
26+
* Defined for all explicit imports from ident or select nodes.
27+
* @param isRootImport true if this is one of the implicit imports of scala, java.lang,
28+
* scala.Predef or dotty.DottyPredef in the start context, false otherwise.
2729
*/
28-
class ImportInfo(symf: => Symbol, val selectors: List[untpd.Tree], val isRootImport: Boolean = false)(implicit ctx: Context) {
30+
class ImportInfo(symf: => Symbol, val selectors: List[untpd.Tree],
31+
symNameOpt: Option[TermName], val isRootImport: Boolean = false)(implicit ctx: Context) {
2932

3033
lazy val sym = symf
3134

@@ -105,11 +108,11 @@ class ImportInfo(symf: => Symbol, val selectors: List[untpd.Tree], val isRootImp
105108
*/
106109
lazy val unimported: Symbol = {
107110
lazy val sym = site.termSymbol
108-
val hasMaskingSelector = selectors exists {
109-
case Thicket(_ :: Ident(nme.WILDCARD) :: Nil) => true
110-
case _ => false
111+
def maybeShadowsRoot = symNameOpt match {
112+
case Some(symName) => defn.ShadowableImportNames.contains(symName)
113+
case None => false
111114
}
112-
if (hasMaskingSelector && defn.RootImportTypes.exists(_.symbol == sym)) sym
115+
if (maybeShadowsRoot && defn.RootImportTypes.exists(_.symbol == sym)) sym
113116
else NoSymbol
114117
}
115118

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,13 @@ class Namer { typer: Typer =>
373373
}
374374

375375
/** A new context that summarizes an import statement */
376-
def importContext(sym: Symbol, selectors: List[Tree])(implicit ctx: Context) =
377-
ctx.fresh.setImportInfo(new ImportInfo(sym, selectors))
376+
def importContext(imp: Import, sym: Symbol)(implicit ctx: Context) = {
377+
val impNameOpt = imp.expr match {
378+
case ref: RefTree => Some(ref.name.asTermName)
379+
case _ => None
380+
}
381+
ctx.fresh.setImportInfo(new ImportInfo(sym, imp.selectors, impNameOpt))
382+
}
378383

379384
/** A new context for the interior of a class */
380385
def inClassContext(selfInfo: DotClass /* Should be Type | Symbol*/)(implicit ctx: Context): Context = {
@@ -423,7 +428,7 @@ class Namer { typer: Typer =>
423428
setDocstring(pkg, stat)
424429
ctx
425430
case imp: Import =>
426-
importContext(createSymbol(imp), imp.selectors)
431+
importContext(imp, createSymbol(imp))
427432
case mdef: DefTree =>
428433
val sym = enterSymbol(createSymbol(mdef))
429434
setDocstring(sym, origStat)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
15881588
case (imp: untpd.Import) :: rest =>
15891589
val imp1 = typed(imp)
15901590
buf += imp1
1591-
traverse(rest)(importContext(imp1.symbol, imp.selectors))
1591+
traverse(rest)(importContext(imp, imp1.symbol))
15921592
case (mdef: untpd.DefTree) :: rest =>
15931593
mdef.removeAttachment(ExpandedTree) match {
15941594
case Some(xtree) =>

0 commit comments

Comments
 (0)