Skip to content

Commit 412a45a

Browse files
committed
Fix REPL shadowing bug
1 parent 298ff3f commit 412a45a

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
10601060
if (sym.isImport)
10611061
sym.infoOrCompleter match {
10621062
case info: Namer#Completer => return info.original.show
1063-
case info: ImportType => return s"import $info.expr.show"
1063+
case info: ImportType => return s"import ${info.expr.show}"
10641064
case _ =>
10651065
}
10661066
def name =

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
439439
if (curImport.nn.unimported ne NoSymbol) unimported += curImport.nn.unimported
440440
if (curOwner.is(Package) && curImport != null && curImport.isRootImport && previous.exists)
441441
previous // no more conflicts possible in this case
442-
else if (isPossibleImport(NamedImport) && (curImport nen outer.importInfo)) {
442+
else if isPossibleImport(NamedImport) &&
443+
((curImport nen outer.importInfo)
444+
|| curImport != null && curImport.site.termSymbol.name.isReplWrapperName)
445+
then
443446
val namedImp = namedImportRef(curImport.uncheckedNN)
444447
if (namedImp.exists)
445448
recurAndCheckNewOrShadowed(namedImp, NamedImport, ctx)(using outer)
@@ -456,7 +459,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
456459
updateUnimported()
457460
loop(ctx)(using outer)
458461
}
459-
}
460462
else loop(ctx)(using outer)
461463
}
462464
}

compiler/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class ReplCompiler extends Compiler:
6262
}
6363

6464
val rootCtx = super.rootContext.fresh
65-
.setOwner(defn.EmptyPackageClass)
6665
.withRootImports
6766
(state.validObjectIndexes).foldLeft(rootCtx)((ctx, id) =>
6867
importPreviousRun(id)(using ctx))

compiler/test/dotty/tools/repl/ShadowingBatchTests.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ class ShadowingBatchTests extends ErrorMessagesTest:
3232
ictx.setSetting(classpath, classpath.value + File.pathSeparator + dir.jpath.toAbsolutePath)
3333
}
3434

35+
@Test def io =
36+
val lib = """|package io.foo
37+
|
38+
|object Bar {
39+
| def baz: Int = 42
40+
|}
41+
|""".stripMargin
42+
val app = """|object Main:
43+
| def main(args: Array[String]): Unit =
44+
| println(io.foo.Bar.baz)
45+
|""".stripMargin
46+
checkMessages(lib).expectNoErrors
47+
checkMessages(app).expectNoErrors
48+
3549
@Test def file =
3650
checkMessages("class C(val c: Int)").expectNoErrors
3751
checkMessages("object rsline1 {\n def line1 = new C().c\n}").expect { (_, msgs) =>

compiler/test/dotty/tools/repl/ShadowingTests.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ class ShadowingTests extends ReplTest(options = ShadowingTests.options):
7676
Files.delete(file)
7777
end compileShadowed
7878

79+
@Test def io = shadowedScriptedTest(name = "io",
80+
shadowed = """|package io.foo
81+
|
82+
|object Bar {
83+
| def baz: Int = 42
84+
|}
85+
|""".stripMargin,
86+
script = """|scala> io.foo.Bar.baz
87+
|val res0: Int = 42
88+
|""".stripMargin
89+
)
90+
7991
@Test def i7635 = shadowedScriptedTest(name = "<i7635>",
8092
shadowed = "class C(val c: Int)",
8193
script =
@@ -97,7 +109,7 @@ class ShadowingTests extends ReplTest(options = ShadowingTests.options):
97109
|""".stripMargin
98110
)
99111

100-
@Test def `shadow subdirectories on classpath` =
112+
@Test def shadowSubDir =
101113
// NB: Tests of shadowing of subdirectories on the classpath are only valid
102114
// when the subdirectories exist prior to initialization of the REPL driver.
103115
// In the tests below this is enforced by the call to `testScript` which
@@ -129,6 +141,11 @@ class ShadowingTests extends ReplTest(options = ShadowingTests.options):
129141
ShadowingTests.createSubDir("util")
130142
testScript(name = "<shadow-subdir-util>",
131143
"""|scala> import util.Try
144+
|-- [E008] Not Found Error: -----------------------------------------------------
145+
|1 | import util.Try
146+
| | ^^^
147+
| | value Try is not a member of util
148+
|1 error found
132149
|
133150
|scala> object util { class Try { override def toString = "you've gotta try!" } }
134151
|// defined object util

0 commit comments

Comments
 (0)