Skip to content

Commit 594043f

Browse files
authored
Merge pull request #14493 from griggt/fix-14491
2 parents 1476a30 + 541a348 commit 594043f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ class ReplDriver(settings: Array[String],
254254
def extractTopLevelImports(ctx: Context): List[tpd.Import] =
255255
unfusedPhases(using ctx).collectFirst { case phase: CollectTopLevelImports => phase.imports }.get
256256

257+
def contextWithNewImports(ctx: Context, imports: List[tpd.Import]): Context =
258+
if imports.isEmpty then ctx
259+
else
260+
imports.foldLeft(ctx.fresh.setNewScope)((ctx, imp) =>
261+
ctx.importContext(imp, imp.symbol(using ctx)))
262+
257263
implicit val state = {
258264
val state0 = newRun(istate, parsed.reporter)
259265
state0.copy(context = state0.context.withSource(parsed.source))
@@ -269,7 +275,10 @@ class ReplDriver(settings: Array[String],
269275
var allImports = newState.imports
270276
if (newImports.nonEmpty)
271277
allImports += (newState.objectIndex -> newImports)
272-
val newStateWithImports = newState.copy(imports = allImports)
278+
val newStateWithImports = newState.copy(
279+
imports = allImports,
280+
context = contextWithNewImports(newState.context, newImports)
281+
)
273282

274283
val warnings = newState.context.reporter
275284
.removeBufferedMessages(using newState.context)

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,27 @@ class ReplCompilerTests extends ReplTest:
241241
assertEquals(List("// defined class C"), lines())
242242
}
243243

244+
@Test def i14491 =
245+
initially {
246+
run("import language.experimental.fewerBraces")
247+
} andThen {
248+
run("""|val x = Seq(7,8,9).apply:
249+
| 1
250+
|""".stripMargin)
251+
assertEquals("val x: Int = 8", storedOutput().trim)
252+
}
253+
initially {
254+
run("""|import language.experimental.fewerBraces
255+
|import language.experimental.fewerBraces as _
256+
|""".stripMargin)
257+
} andThen {
258+
run("""|val x = Seq(7,8,9).apply:
259+
| 1
260+
|""".stripMargin)
261+
assert("expected error if fewerBraces is unimported",
262+
lines().exists(_.contains("missing arguments for method apply")))
263+
}
264+
244265
object ReplCompilerTests:
245266

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

0 commit comments

Comments
 (0)