Skip to content
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

Fix #2212: Avoid imports in the wrong namespace #2225

Merged
merged 2 commits into from
Apr 11, 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
48 changes: 22 additions & 26 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
previous
}

def selection(imp: ImportInfo, name: Name) =
if (imp.sym.isCompleting) {
ctx.warning(i"cyclic ${imp.sym}, ignored", tree.pos)
NoType
} else if (unimported.nonEmpty && unimported.contains(imp.site.termSymbol))
NoType
else {
val pre = imp.site
val denot = pre.member(name).accessibleFrom(pre)(refctx)
// Pass refctx so that any errors are reported in the context of the
// reference instead of the
if (reallyExists(denot)) pre.select(name, denot) else NoType
}

/** The type representing a named import with enclosing name when imported
* from given `site` and `selectors`.
*/
Expand All @@ -193,25 +207,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
found
}

def selection(name: Name) =
if (imp.sym.isCompleting) {
ctx.warning(i"cyclic ${imp.sym}, ignored", tree.pos)
NoType
}
else if (unimported.nonEmpty && unimported.contains(imp.site.termSymbol))
NoType
else {
// Pass refctx so that any errors are reported in the context of the
// reference instead of the
checkUnambiguous(selectionType(imp.site, name, tree.pos)(refctx))
}
def unambiguousSelection(name: Name) =
checkUnambiguous(selection(imp, name))

selector match {
case Thicket(fromId :: Ident(Name) :: _) =>
val Ident(from) = fromId
selection(if (name.isTypeName) from.toTypeName else from)
unambiguousSelection(if (name.isTypeName) from.toTypeName else from)
case Ident(Name) =>
selection(name)
unambiguousSelection(name)
case _ =>
recur(rest)
}
Expand All @@ -224,18 +228,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
/** The type representing a wildcard import with enclosing name when imported
* from given import info
*/
def wildImportRef(imp: ImportInfo)(implicit ctx: Context): Type = {
if (imp.isWildcardImport) {
val pre = imp.site
if (!unimported.contains(pre.termSymbol) &&
!imp.excluded.contains(name.toTermName) &&
name != nme.CONSTRUCTOR) {
val denot = pre.member(name).accessibleFrom(pre)(refctx)
if (reallyExists(denot)) return pre.select(name, denot)
}
}
NoType
}
def wildImportRef(imp: ImportInfo)(implicit ctx: Context): Type =
if (imp.isWildcardImport && !imp.excluded.contains(name.toTermName) && name != nme.CONSTRUCTOR)
selection(imp, name)
else NoType

/** Is (some alternative of) the given predenotation `denot`
* defined in current compilation unit?
Expand Down
19 changes: 19 additions & 0 deletions tests/pos/i2212.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package object squants {
type Time = squants.time.Time
}
package squants.time {
class Time
object Time { def x = 2 }
}
package squants.velocity {
import squants.time._ // <-- imports `Time` value
import squants.Time // <-- imports type alias
object Velocity { Time.x }
}

import scala.math.BigDecimal.RoundingMode
import scala.math.BigDecimal.RoundingMode.RoundingMode

object Money {
def foo(round: RoundingMode = RoundingMode.HALF_EVEN): Int = ???
}
5 changes: 2 additions & 3 deletions tests/run/t5857.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ object Test {
b
}

// whatever it is, it should be less than, say, 250ms
// whatever it is, it should be less than, say, 1000ms
// if `max` involves traversal, it takes over 5 seconds on a 3.2GHz i7 CPU
//println(exectime)
assert(exectime < 250, exectime)
assert(exectime < 1000, exectime)
}

}