Skip to content

Fix #11146: Don't re-add root imports to context in REPLFrontEnd #12303

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

Merged
merged 1 commit into from
May 1, 2021

Conversation

griggt
Copy link
Contributor

@griggt griggt commented Apr 30, 2021

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._

The context before this PR as seen by the frontend would look something like the below (this is after two previous lines of REPL input.) Note the undesirable imports of java.lang._, scala._ and Predef._ in the innermost contexts.

Context(
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@619c93ca, import = import Predef.{...}, implicits = ...
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@619c93ca, import = import scala.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@619c93ca, import = import java.lang.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@619c93ca, import = import rs$line$2.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@619c93ca, import = import rs$line$2.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@619c93ca, import = import rs$line$2.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@619c93ca, import = import rs$line$1.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@3b2c4a8b, import = import rs$line$1.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@3b2c4a8b, import = import Predef.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@7f2c57fe, import = import Predef.{...}, implicits = ...
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@7f2c57fe, import = import scala.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@7f2c57fe, import = import java.lang.{...}
  owner = module class <root>, scope = dotty.tools.dotc.core.Scopes$MutableScope@7f2c57fe, import =
  owner = val <none>, scope = dotty.tools.dotc.core.Scopes$MutableScope@43935e9c, import =
  owner = val <none>, scope = null, import =
  owner = val <none>, scope = null, import =
  owner = val <none>, scope = null, import =
  owner = val <none>, scope = null, import =
  owner = val <none>, scope = null, import = )

Fixes #11146
Fixes #9720

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._
Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for figuring that out!

@smarter smarter merged commit c6debb7 into scala:master May 1, 2021
@griggt griggt deleted the fix-11146 branch May 1, 2021 14:49
@Kordyjan Kordyjan added this to the 3.0.1 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Definitions in java.lang._ shadow definitions in REPL REPL imports not overriding predef
3 participants