Skip to content

Commit 40675e6

Browse files
committed
Fix #2212: Avoid imports in the wrong namespace
Don't issue an error if when considering a named import that refers to a valoe or type which does not exist. Instead, disregard the import an continue.
1 parent 14fde01 commit 40675e6

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

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

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
178178
previous
179179
}
180180

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

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

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

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

tests/pos/i2212.scala

Lines changed: 19 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)