From 2e004b8ccf71557652d6f6331735442decb3754d Mon Sep 17 00:00:00 2001 From: adampauls Date: Sat, 12 Feb 2022 16:18:09 -0800 Subject: [PATCH] Fix check. --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 5 +++-- compiler/src/dotty/tools/dotc/parsing/Scanners.scala | 2 -- compiler/src/dotty/tools/dotc/parsing/Tokens.scala | 2 +- compiler/src/dotty/tools/repl/Main.scala | 1 + compiler/test/dotty/tools/vulpix/ParallelTesting.scala | 6 +++++- tests/neg/arg-eof.scala | 3 +++ tests/neg/i1679.scala | 2 +- tests/neg/t1625.scala | 4 ++-- tests/neg/trailing-comma-pattern.check | 6 ++++++ tests/neg/trailing-comma-pattern.scala | 3 +++ tests/neg/trailing-comma-pattern2.check | 10 ++++++++++ tests/neg/trailing-comma-pattern2.scala | 3 +++ tests/pos/trailing-comma-pattern.scala | 3 +++ 13 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 tests/neg/arg-eof.scala create mode 100644 tests/neg/trailing-comma-pattern.check create mode 100644 tests/neg/trailing-comma-pattern.scala create mode 100644 tests/neg/trailing-comma-pattern2.check create mode 100644 tests/neg/trailing-comma-pattern2.scala create mode 100644 tests/pos/trailing-comma-pattern.scala diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 3ffef90057ef..776af3a24931 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -881,7 +881,8 @@ object Parsers { val next = in.lookahead.token next == LBRACKET || next == LPAREN - /** Is current ident a `*`, and is it followed by a `)` or `, )`? */ + /** Is current ident a `*`, and is it followed by a `)`, `, )`, `,EOF`? The latter two are not + syntactically valid, but we need to include them here for error recovery. */ def followingIsVararg(): Boolean = in.isIdent(nme.raw.STAR) && { val lookahead = in.LookaheadScanner() @@ -890,7 +891,7 @@ object Parsers { || lookahead.token == COMMA && { lookahead.nextToken() - lookahead.token == RPAREN + lookahead.token == RPAREN || lookahead.token == EOF } } diff --git a/compiler/src/dotty/tools/dotc/parsing/Scanners.scala b/compiler/src/dotty/tools/dotc/parsing/Scanners.scala index 1fee0f52f770..d6ef2646eacf 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Scanners.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Scanners.scala @@ -659,8 +659,6 @@ object Scanners { && (token == RPAREN || token == RBRACKET || token == RBRACE || token == OUTDENT) then () /* skip the trailing comma */ - else if token == EOF then // e.g. when the REPL is parsing "val List(x, y, _*," - () /* skip the trailing comma */ else reset() case END => diff --git a/compiler/src/dotty/tools/dotc/parsing/Tokens.scala b/compiler/src/dotty/tools/dotc/parsing/Tokens.scala index 55f428cef5a4..5c29bace1e7d 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Tokens.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Tokens.scala @@ -285,7 +285,7 @@ object Tokens extends TokensCommon { final val endMarkerTokens = identifierTokens | BitSet(IF, WHILE, FOR, MATCH, TRY, NEW, THROW, GIVEN, VAL, THIS) - final val skipStopTokens = BitSet(SEMI, NEWLINE, NEWLINES, RBRACE, RPAREN, RBRACKET, OUTDENT) + final val skipStopTokens = BitSet(SEMI, NEWLINE, NEWLINES, RBRACE, RPAREN, RBRACKET, OUTDENT, COMMA) final val softModifierNames = Set(nme.inline, nme.opaque, nme.open, nme.transparent, nme.infix) } diff --git a/compiler/src/dotty/tools/repl/Main.scala b/compiler/src/dotty/tools/repl/Main.scala index 127ccd10b467..7eb906edc586 100644 --- a/compiler/src/dotty/tools/repl/Main.scala +++ b/compiler/src/dotty/tools/repl/Main.scala @@ -1,6 +1,7 @@ package dotty.tools.repl /** Main entry point to the REPL */ +// To test, run bin/scala object Main { def main(args: Array[String]): Unit = new ReplDriver(args).tryRunning diff --git a/compiler/test/dotty/tools/vulpix/ParallelTesting.scala b/compiler/test/dotty/tools/vulpix/ParallelTesting.scala index bd30e7fff98e..f265a1d31af6 100644 --- a/compiler/test/dotty/tools/vulpix/ParallelTesting.scala +++ b/compiler/test/dotty/tools/vulpix/ParallelTesting.scala @@ -825,7 +825,11 @@ trait ParallelTesting extends RunnerOrchestration { self => def sawDiagnostic(d: Diagnostic): Unit = d.pos.nonInlined match case srcpos if srcpos.exists => - val key = s"${relativize(srcpos.source.file.toString)}:${srcpos.line + 1}" + // If an annotated // error is placed on the last line of the file, EOF errors can show up past the last. + // Without this adjustment, it's not possible to annotate // error on errors whose position is EOF + // in some cases. + val adjustedLine = if srcpos.point == srcpos.source.length && srcpos.point > 0 then srcpos.source.offsetToLine(srcpos.point - 1) else srcpos.line + val key = s"${relativize(srcpos.source.file.toString)}:${adjustedLine + 1}" if !seenAt(key) then unexpected += key case srcpos => if !seenAt("nopos") then unpositioned += relativize(srcpos.source.file.toString) diff --git a/tests/neg/arg-eof.scala b/tests/neg/arg-eof.scala new file mode 100644 index 000000000000..0195ae7577d1 --- /dev/null +++ b/tests/neg/arg-eof.scala @@ -0,0 +1,3 @@ +object Test: + case class Widget(name: String, other: Int = 5) + Widget(name = "foo", // error // error diff --git a/tests/neg/i1679.scala b/tests/neg/i1679.scala index 08a33bb15596..cadeb85dc8db 100644 --- a/tests/neg/i1679.scala +++ b/tests/neg/i1679.scala @@ -1,5 +1,5 @@ class A[T] object o { // Testing compiler crash, this test should be modified when named type argument are completely implemented - val x: A[T=Int, T=Int] = ??? // error: ']' expected, but '=' found + val x: A[T=Int, T=Int] = ??? // error: ']' expected, but '=' found // error } diff --git a/tests/neg/t1625.scala b/tests/neg/t1625.scala index e98ce985238e..41929f0ab476 100644 --- a/tests/neg/t1625.scala +++ b/tests/neg/t1625.scala @@ -1,3 +1,3 @@ trait T3 { - def foo(x: String*, y: String*, c: String*): Int // error: an identifier expected, but ',' found -} \ No newline at end of file + def foo(x: String*, y: String*, c: String*): Int // error: an identifier expected, but ',' found // error: an identifier expected, but ',' found +} diff --git a/tests/neg/trailing-comma-pattern.check b/tests/neg/trailing-comma-pattern.check new file mode 100644 index 000000000000..15a8739de631 --- /dev/null +++ b/tests/neg/trailing-comma-pattern.check @@ -0,0 +1,6 @@ +-- [E032] Syntax Error: tests/neg/trailing-comma-pattern.scala:4:0 ----------------------------------------------------- +4 | + |^ + |pattern expected + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/trailing-comma-pattern.scala b/tests/neg/trailing-comma-pattern.scala new file mode 100644 index 000000000000..28da312952cc --- /dev/null +++ b/tests/neg/trailing-comma-pattern.scala @@ -0,0 +1,3 @@ +object Test: + val List(x, y, _*, +// error diff --git a/tests/neg/trailing-comma-pattern2.check b/tests/neg/trailing-comma-pattern2.check new file mode 100644 index 000000000000..42db4ea00623 --- /dev/null +++ b/tests/neg/trailing-comma-pattern2.check @@ -0,0 +1,10 @@ +-- [E032] Syntax Error: tests/neg/trailing-comma-pattern2.scala:2:21 --------------------------------------------------- +2 | val List(x, y, _*, ) // error + | ^ + | pattern expected + | + | longer explanation available when compiling with `-explain` +-- [E040] Syntax Error: tests/neg/trailing-comma-pattern2.scala:4:0 ---------------------------------------------------- +4 | + |^ + |'=' expected, but unindent found diff --git a/tests/neg/trailing-comma-pattern2.scala b/tests/neg/trailing-comma-pattern2.scala new file mode 100644 index 000000000000..5ac2ce966cf7 --- /dev/null +++ b/tests/neg/trailing-comma-pattern2.scala @@ -0,0 +1,3 @@ +object Test: + val List(x, y, _*, ) // error +// error diff --git a/tests/pos/trailing-comma-pattern.scala b/tests/pos/trailing-comma-pattern.scala new file mode 100644 index 000000000000..878b3af7748a --- /dev/null +++ b/tests/pos/trailing-comma-pattern.scala @@ -0,0 +1,3 @@ +object Test: + val List(x, y, _*, + ) = List(1, 2, 3)