Skip to content

Avoid stale import context during REPL parsing #14493

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
Feb 16, 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
11 changes: 10 additions & 1 deletion compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ class ReplDriver(settings: Array[String],
def extractTopLevelImports(ctx: Context): List[tpd.Import] =
unfusedPhases(using ctx).collectFirst { case phase: CollectTopLevelImports => phase.imports }.get

def contextWithNewImports(ctx: Context, imports: List[tpd.Import]): Context =
if imports.isEmpty then ctx
else
imports.foldLeft(ctx.fresh.setNewScope)((ctx, imp) =>
ctx.importContext(imp, imp.symbol(using ctx)))

implicit val state = {
val state0 = newRun(istate, parsed.reporter)
state0.copy(context = state0.context.withSource(parsed.source))
Expand All @@ -269,7 +275,10 @@ class ReplDriver(settings: Array[String],
var allImports = newState.imports
if (newImports.nonEmpty)
allImports += (newState.objectIndex -> newImports)
val newStateWithImports = newState.copy(imports = allImports)
val newStateWithImports = newState.copy(
imports = allImports,
context = contextWithNewImports(newState.context, newImports)
Comment on lines +278 to +280
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm on the fence as to whether this is the appropriate place to fix this.

)

val warnings = newState.context.reporter
.removeBufferedMessages(using newState.context)
Expand Down
21 changes: 21 additions & 0 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,27 @@ class ReplCompilerTests extends ReplTest:
assertEquals(List("// defined class C"), lines())
}

@Test def i14491 =
initially {
run("import language.experimental.fewerBraces")
} andThen {
run("""|val x = Seq(7,8,9).apply:
| 1
|""".stripMargin)
assertEquals("val x: Int = 8", storedOutput().trim)
}
initially {
run("""|import language.experimental.fewerBraces
|import language.experimental.fewerBraces as _
|""".stripMargin)
} andThen {
run("""|val x = Seq(7,8,9).apply:
| 1
|""".stripMargin)
assert("expected error if fewerBraces is unimported",
lines().exists(_.contains("missing arguments for method apply")))
}

object ReplCompilerTests:

private val pattern = Pattern.compile("\\r[\\n]?|\\n");
Expand Down