From 9b225c1c524882ae6164b192d98697c9e91a8a2a Mon Sep 17 00:00:00 2001 From: Tom Grigg Date: Thu, 29 Apr 2021 16:28:27 -0700 Subject: [PATCH] Don't re-add root imports to context in REPLFrontEnd The root context for REPL compiler runs is initialized by ReplCompiler#newRun, which contains special handling to: 1. first add the default root imports (java.lang._, scala._, Predef._) 2. then import each previous REPL wrapper in order (incl. its imports) The result is the innermost context imports the most recent REPL wrapper and the (nearly) outermost contexts contain the default imports. Before this commit, the default root imports were also being added to the (innermost) context in REPLFrontEnd#runOn, resulting in REPL definitions being shadowed by those in java.lang._, scala._ and Predef._ --- .../src/dotty/tools/repl/ReplFrontEnd.scala | 3 +-- compiler/test-resources/repl/i11146 | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 compiler/test-resources/repl/i11146 diff --git a/compiler/src/dotty/tools/repl/ReplFrontEnd.scala b/compiler/src/dotty/tools/repl/ReplFrontEnd.scala index 59df49e853cb..4eb983e7f674 100644 --- a/compiler/src/dotty/tools/repl/ReplFrontEnd.scala +++ b/compiler/src/dotty/tools/repl/ReplFrontEnd.scala @@ -4,7 +4,6 @@ package repl import dotc.typer.FrontEnd import dotc.CompilationUnit import dotc.core.Contexts._ -import dotc.typer.ImportInfo.withRootImports /** A customized `FrontEnd` for the REPL * @@ -19,7 +18,7 @@ private[repl] class REPLFrontEnd extends FrontEnd { override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] = { assert(units.size == 1) // REPl runs one compilation unit at a time val unit = units.head - val unitContext = ctx.fresh.setCompilationUnit(unit).withRootImports + val unitContext = ctx.fresh.setCompilationUnit(unit) enterSyms(using unitContext) typeCheck(using unitContext) List(unit) diff --git a/compiler/test-resources/repl/i11146 b/compiler/test-resources/repl/i11146 new file mode 100644 index 000000000000..1a6b7d414eeb --- /dev/null +++ b/compiler/test-resources/repl/i11146 @@ -0,0 +1,25 @@ +scala> class Appendable { def foo = println("Appendable.foo") } +// defined class Appendable + +scala> (new Appendable).foo +Appendable.foo + +scala> def assert(x: Boolean) = println(if x then "not asserted" else "asserted") +def assert(x: Boolean): Unit + +scala> assert(false) +asserted + +scala> class Option; object Option { val baz = 42 } +// defined class Option +// defined object Option + +scala> Option.baz +val res0: Int = 42 + +scala> object fs2 { class Stream[T] { override def toString = "fs2.Stream(..)" }; object Stream { def apply[T](x: T) = new Stream[T] }} +// defined object fs2 + +scala> import fs2.Stream +scala> Stream(1) +val res1: fs2.Stream[Int] = fs2.Stream(..)