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 REPL shadowing bug #16389

Merged
merged 1 commit into from
Nov 23, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
if (sym.isImport)
sym.infoOrCompleter match {
case info: Namer#Completer => return info.original.show
case info: ImportType => return s"import $info.expr.show"
case info: ImportType => return s"import ${info.expr.show}"
dwijnand marked this conversation as resolved.
Show resolved Hide resolved
case _ =>
}
def name =
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/repl/ReplCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class ReplCompiler extends Compiler:
}

val rootCtx = super.rootContext.fresh
.setOwner(defn.EmptyPackageClass)
.withRootImports
.fresh.setOwner(defn.EmptyPackageClass): Context
(state.validObjectIndexes).foldLeft(rootCtx)((ctx, id) =>
importPreviousRun(id)(using ctx))
}
Expand Down
14 changes: 14 additions & 0 deletions compiler/test/dotty/tools/repl/ShadowingBatchTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ class ShadowingBatchTests extends ErrorMessagesTest:
ictx.setSetting(classpath, classpath.value + File.pathSeparator + dir.jpath.toAbsolutePath)
}

@Test def io =
val lib = """|package io.foo
|
|object Bar {
| def baz: Int = 42
|}
|""".stripMargin
val app = """|object Main:
| def main(args: Array[String]): Unit =
| println(io.foo.Bar.baz)
|""".stripMargin
checkMessages(lib).expectNoErrors
checkMessages(app).expectNoErrors

@Test def file =
checkMessages("class C(val c: Int)").expectNoErrors
checkMessages("object rsline1 {\n def line1 = new C().c\n}").expect { (_, msgs) =>
Expand Down
17 changes: 17 additions & 0 deletions compiler/test/dotty/tools/repl/ShadowingTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ class ShadowingTests extends ReplTest(options = ShadowingTests.options):
Files.delete(file)
end compileShadowed

@Test def io = shadowedScriptedTest(name = "io",
shadowed = """|package io.foo
|
|object Bar {
| def baz: Int = 42
|}
|""".stripMargin,
script = """|scala> io.foo.Bar.baz
|val res0: Int = 42
|""".stripMargin
)

@Test def i7635 = shadowedScriptedTest(name = "<i7635>",
shadowed = "class C(val c: Int)",
script =
Expand Down Expand Up @@ -129,6 +141,11 @@ class ShadowingTests extends ReplTest(options = ShadowingTests.options):
ShadowingTests.createSubDir("util")
testScript(name = "<shadow-subdir-util>",
"""|scala> import util.Try
|-- [E008] Not Found Error: -----------------------------------------------------
|1 | import util.Try
| | ^^^
| | value Try is not a member of util
|1 error found
|
|scala> object util { class Try { override def toString = "you've gotta try!" } }
|// defined object util
Expand Down