Skip to content

Commit 10a1300

Browse files
authored
Merge pull request #2225 from dotty-staging/fix-#2192
Fix #2212: Avoid imports in the wrong namespace
2 parents d63191d + 552d856 commit 10a1300

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

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

+22-26
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
179179
previous
180180
}
181181

182+
def selection(imp: ImportInfo, name: Name) =
183+
if (imp.sym.isCompleting) {
184+
ctx.warning(i"cyclic ${imp.sym}, ignored", tree.pos)
185+
NoType
186+
} else if (unimported.nonEmpty && unimported.contains(imp.site.termSymbol))
187+
NoType
188+
else {
189+
val pre = imp.site
190+
val denot = pre.member(name).accessibleFrom(pre)(refctx)
191+
// Pass refctx so that any errors are reported in the context of the
192+
// reference instead of the
193+
if (reallyExists(denot)) pre.select(name, denot) else NoType
194+
}
195+
182196
/** The type representing a named import with enclosing name when imported
183197
* from given `site` and `selectors`.
184198
*/
@@ -194,25 +208,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
194208
found
195209
}
196210

197-
def selection(name: Name) =
198-
if (imp.sym.isCompleting) {
199-
ctx.warning(i"cyclic ${imp.sym}, ignored", tree.pos)
200-
NoType
201-
}
202-
else if (unimported.nonEmpty && unimported.contains(imp.site.termSymbol))
203-
NoType
204-
else {
205-
// Pass refctx so that any errors are reported in the context of the
206-
// reference instead of the
207-
checkUnambiguous(selectionType(imp.site, name, tree.pos)(refctx))
208-
}
211+
def unambiguousSelection(name: Name) =
212+
checkUnambiguous(selection(imp, name))
209213

210214
selector match {
211215
case Thicket(fromId :: Ident(Name) :: _) =>
212216
val Ident(from) = fromId
213-
selection(if (name.isTypeName) from.toTypeName else from)
217+
unambiguousSelection(if (name.isTypeName) from.toTypeName else from)
214218
case Ident(Name) =>
215-
selection(name)
219+
unambiguousSelection(name)
216220
case _ =>
217221
recur(rest)
218222
}
@@ -225,18 +229,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
225229
/** The type representing a wildcard import with enclosing name when imported
226230
* from given import info
227231
*/
228-
def wildImportRef(imp: ImportInfo)(implicit ctx: Context): Type = {
229-
if (imp.isWildcardImport) {
230-
val pre = imp.site
231-
if (!unimported.contains(pre.termSymbol) &&
232-
!imp.excluded.contains(name.toTermName) &&
233-
name != nme.CONSTRUCTOR) {
234-
val denot = pre.member(name).accessibleFrom(pre)(refctx)
235-
if (reallyExists(denot)) return pre.select(name, denot)
236-
}
237-
}
238-
NoType
239-
}
232+
def wildImportRef(imp: ImportInfo)(implicit ctx: Context): Type =
233+
if (imp.isWildcardImport && !imp.excluded.contains(name.toTermName) && name != nme.CONSTRUCTOR)
234+
selection(imp, name)
235+
else NoType
240236

241237
/** Is (some alternative of) the given predenotation `denot`
242238
* defined in current compilation unit?

tests/pos/i2212.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package object squants {
2+
type Time = squants.time.Time
3+
}
4+
package squants.time {
5+
class Time
6+
object Time { def x = 2 }
7+
}
8+
package squants.velocity {
9+
import squants.time._ // <-- imports `Time` value
10+
import squants.Time // <-- imports type alias
11+
object Velocity { Time.x }
12+
}
13+
14+
import scala.math.BigDecimal.RoundingMode
15+
import scala.math.BigDecimal.RoundingMode.RoundingMode
16+
17+
object Money {
18+
def foo(round: RoundingMode = RoundingMode.HALF_EVEN): Int = ???
19+
}

tests/run/t5857.scala

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ object Test {
3636
b
3737
}
3838

39-
// whatever it is, it should be less than, say, 250ms
39+
// whatever it is, it should be less than, say, 1000ms
4040
// if `max` involves traversal, it takes over 5 seconds on a 3.2GHz i7 CPU
4141
//println(exectime)
42-
assert(exectime < 250, exectime)
42+
assert(exectime < 1000, exectime)
4343
}
44-
4544
}

0 commit comments

Comments
 (0)