Skip to content

Adopt scala's scheme for root import hiding #1901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ class Definitions {
else if (ctx.settings.YnoPredef.value) StaticRootImportFns
else StaticRootImportFns ++ PredefImportFns

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

/** Modules whose members are in the default namespace and their module classes */
Expand Down
23 changes: 13 additions & 10 deletions compiler/src/dotty/tools/dotc/typer/ImportInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ object ImportInfo {
val selectors = untpd.Ident(nme.WILDCARD) :: Nil
def expr = tpd.Ident(refFn())
def imp = tpd.Import(expr, selectors)
new ImportInfo(imp.symbol, selectors, isRootImport = true)
new ImportInfo(imp.symbol, selectors, None, isRootImport = true)
}
}

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could put a if (isRootImport) assert(symNameOpt.isEmpty) here to enforce what the documentation says.

lazy val sym = symf

Expand Down Expand Up @@ -105,11 +108,11 @@ class ImportInfo(symf: => Symbol, val selectors: List[untpd.Tree], val isRootImp
*/
lazy val unimported: Symbol = {
lazy val sym = site.termSymbol
val hasMaskingSelector = selectors exists {
case Thicket(_ :: Ident(nme.WILDCARD) :: Nil) => true
case _ => false
def maybeShadowsRoot = symNameOpt match {
case Some(symName) => defn.ShadowableImportNames.contains(symName)
case None => false
}
if (hasMaskingSelector && defn.RootImportTypes.exists(_.symbol == sym)) sym
if (maybeShadowsRoot && defn.RootImportTypes.exists(_.symbol == sym)) sym
else NoSymbol
}

Expand Down
11 changes: 8 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,13 @@ class Namer { typer: Typer =>
}

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

/** A new context for the interior of a class */
def inClassContext(selfInfo: DotClass /* Should be Type | Symbol*/)(implicit ctx: Context): Context = {
Expand Down Expand Up @@ -423,7 +428,7 @@ class Namer { typer: Typer =>
setDocstring(pkg, stat)
ctx
case imp: Import =>
importContext(createSymbol(imp), imp.selectors)
importContext(imp, createSymbol(imp))
case mdef: DefTree =>
val sym = enterSymbol(createSymbol(mdef))
setDocstring(sym, origStat)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
case (imp: untpd.Import) :: rest =>
val imp1 = typed(imp)
buf += imp1
traverse(rest)(importContext(imp1.symbol, imp.selectors))
traverse(rest)(importContext(imp, imp1.symbol))
case (mdef: untpd.DefTree) :: rest =>
mdef.removeAttachment(ExpandedTree) match {
case Some(xtree) =>
Expand Down