Skip to content

Commit 5c01725

Browse files
authored
Fix "Colon as operator part at beginning of line in REPL" (scala#24942)
Attempts to fix http://github.com/scala/scala3/issues/24142, vibe coded Seems like a straightforward regex fix that doesn't require a claude explanation
1 parent 74ab8e5 commit 5c01725

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

repl/src/dotty/tools/repl/ParseResult.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ case object Help extends Command {
155155

156156
object ParseResult {
157157

158-
@sharable private val CommandExtract = """(:[\S]+)\s*(.*)""".r
158+
// Commands must start with ":" followed by a letter (e.g. :help, :load)
159+
// This ensures operators like :: or :+ are treated as Scala code, not commands
160+
@sharable private val CommandExtract = """(:[a-zA-Z]\S*)\s*(.*)""".r
159161

160162
private def parseStats(using Context): List[untpd.Tree] = {
161163
val parser = new Parser(ctx.source)

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,28 @@ class ReplCompilerTests extends ReplTest:
520520
@Test def `i22844b regression colon arrow eol`: Unit = contextually:
521521
assertTrue(ParseResult.isIncomplete("List(42).map: x =>"))
522522

523+
// i24142: Colon operator at line beginning should be treated as Scala code, not REPL command
524+
@Test def `i24142 cons operator`: Unit = initially:
525+
run("::(1, Nil)")
526+
assertEquals("val res0: ::[Int] = List(1)", storedOutput().trim)
527+
528+
@Test def `i24142 symbolic identifier starting with colon`: Unit = initially:
529+
// Test a custom identifier starting with :
530+
run("val `:test` = 42")
531+
assertEquals("val :test: Int = 42", storedOutput().trim)
532+
533+
@Test def `i24142 colon as infix`: Unit = initially:
534+
run("1 :: 2 :: Nil")
535+
assertEquals("val res0: List[Int] = List(1, 2)", storedOutput().trim)
536+
537+
@Test def `i24142 commands still work`: Unit = initially:
538+
run(":help")
539+
assertTrue(storedOutput().contains("The REPL has several commands available"))
540+
541+
@Test def `i24142 abbreviated commands still work`: Unit = initially:
542+
run(":he")
543+
assertTrue(storedOutput().contains("The REPL has several commands available"))
544+
523545
object ReplCompilerTests:
524546

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

0 commit comments

Comments
 (0)