Skip to content

Commit 3f184c7

Browse files
authored
Add a hint for using global language import with REPL (#22220)
Fix #16250
2 parents 6ae9548 + c79b212 commit 3f184c7

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+9-1
Original file line numberDiff line numberDiff line change
@@ -3706,7 +3706,15 @@ object Parsers {
37063706
in.languageImportContext = in.languageImportContext.importContext(imp, NoSymbol)
37073707
for case ImportSelector(id @ Ident(imported), EmptyTree, _) <- selectors do
37083708
if Feature.handleGlobalLanguageImport(prefix, imported) && !outermost then
3709-
syntaxError(em"this language import is only allowed at the toplevel", id.span)
3709+
val desc =
3710+
if ctx.mode.is(Mode.Interactive) then
3711+
"not allowed in the REPL"
3712+
else "only allowed at the toplevel"
3713+
val hint =
3714+
if ctx.mode.is(Mode.Interactive) then
3715+
f"\nTo use this language feature, include the flag `-language:$prefix.$imported` when starting the REPL"
3716+
else ""
3717+
syntaxError(em"this language import is $desc$hint", id.span)
37103718
if allSourceVersionNames.contains(imported) && prefix.isEmpty then
37113719
if !outermost then
37123720
syntaxError(em"source version import is only allowed at the toplevel", id.span)

Diff for: compiler/test/dotty/tools/repl/ReplCompilerTests.scala

+18
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,24 @@ class ReplCompilerTests extends ReplTest:
493493
assertTrue(all.head.startsWith("-- [E103] Syntax Error"))
494494
assertTrue(all.exists(_.trim().startsWith("| Illegal start of statement: this modifier is not allowed here")))
495495

496+
@Test def `i16250a`: Unit = initially:
497+
val hints = List(
498+
"this language import is not allowed in the REPL",
499+
"To use this language feature, include the flag `-language:experimental.captureChecking` when starting the REPL"
500+
)
501+
run("import language.experimental.captureChecking")
502+
val all = lines()
503+
assertTrue(hints.forall(hint => all.exists(_.contains(hint))))
504+
505+
@Test def `i16250b`: Unit = initially:
506+
val hints = List(
507+
"this language import is not allowed in the REPL",
508+
"To use this language feature, include the flag `-language:experimental.pureFunctions` when starting the REPL"
509+
)
510+
run("import language.experimental.pureFunctions")
511+
val all = lines()
512+
assertTrue(hints.forall(hint => all.exists(_.contains(hint))))
513+
496514
object ReplCompilerTests:
497515

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

0 commit comments

Comments
 (0)