Skip to content

Commit f50bf57

Browse files
committed
Fix #2856: Prioritize root package members over root imports
The fix is more complex than I would like (see comment in source code). If someone can reproduce the error locally without this commit and rack it down this would help enormously.
1 parent a92652c commit f50bf57

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,27 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
243243
if (ctx.scope == null) previous
244244
else {
245245
var result: Type = NoType
246+
246247
val curOwner = ctx.owner
247-
if ((ctx.scope ne lastCtx.scope) || (curOwner ne lastCtx.owner)) {
248+
249+
// Can this scope contain new definitions? This is usually the first
250+
// context where either the scope or the owner changes wrt the
251+
// context immediately nested in it. But for package contexts, it's
252+
// the opposite: the last context before the package changes. This distinction
253+
// is made so that top-level imports following a package clause are
254+
// logically nested in that package clause. Finally, for the root package
255+
// we switch back to the original test. This means that rop-level packages in
256+
// the root package take priority over root imports. For instance,
257+
// a top-level io package takes priority over scala.io.
258+
// It would be nice if we could drop all this complication, and
259+
// always use the second condition. Unfortunately compileStdLib breaks
260+
// with an error on CI which I cannot replicate locally (not even
261+
// with the exact list of files given).
262+
val isNewDefScope =
263+
if (curOwner.is(Package) && !curOwner.isRoot) curOwner ne ctx.outer.owner
264+
else (ctx.scope ne lastCtx.scope) || (curOwner ne lastCtx.owner)
265+
266+
if (isNewDefScope) {
248267
val defDenot = ctx.denotNamed(name)
249268
if (qualifies(defDenot)) {
250269
val found =

0 commit comments

Comments
 (0)