Skip to content

Commit

Permalink
Add a hint for using global language import with REPL (#22220)
Browse files Browse the repository at this point in the history
Fix #16250
  • Loading branch information
noti0na1 authored Dec 17, 2024
2 parents 6ae9548 + c79b212 commit 3f184c7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3706,7 +3706,15 @@ object Parsers {
in.languageImportContext = in.languageImportContext.importContext(imp, NoSymbol)
for case ImportSelector(id @ Ident(imported), EmptyTree, _) <- selectors do
if Feature.handleGlobalLanguageImport(prefix, imported) && !outermost then
syntaxError(em"this language import is only allowed at the toplevel", id.span)
val desc =
if ctx.mode.is(Mode.Interactive) then
"not allowed in the REPL"
else "only allowed at the toplevel"
val hint =
if ctx.mode.is(Mode.Interactive) then
f"\nTo use this language feature, include the flag `-language:$prefix.$imported` when starting the REPL"
else ""
syntaxError(em"this language import is $desc$hint", id.span)
if allSourceVersionNames.contains(imported) && prefix.isEmpty then
if !outermost then
syntaxError(em"source version import is only allowed at the toplevel", id.span)
Expand Down
18 changes: 18 additions & 0 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,24 @@ class ReplCompilerTests extends ReplTest:
assertTrue(all.head.startsWith("-- [E103] Syntax Error"))
assertTrue(all.exists(_.trim().startsWith("| Illegal start of statement: this modifier is not allowed here")))

@Test def `i16250a`: Unit = initially:
val hints = List(
"this language import is not allowed in the REPL",
"To use this language feature, include the flag `-language:experimental.captureChecking` when starting the REPL"
)
run("import language.experimental.captureChecking")
val all = lines()
assertTrue(hints.forall(hint => all.exists(_.contains(hint))))

@Test def `i16250b`: Unit = initially:
val hints = List(
"this language import is not allowed in the REPL",
"To use this language feature, include the flag `-language:experimental.pureFunctions` when starting the REPL"
)
run("import language.experimental.pureFunctions")
val all = lines()
assertTrue(hints.forall(hint => all.exists(_.contains(hint))))

object ReplCompilerTests:

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

0 comments on commit 3f184c7

Please sign in to comment.