From 089e42f8212c84e4756b00aa75d18a6d139fd940 Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Fri, 15 Nov 2024 13:57:29 +0100 Subject: [PATCH 1/2] Handle old given syntax where identifier and type are seperated by new line --- .../src/dotty/tools/dotc/parsing/Parsers.scala | 14 +++++++++++++- tests/pos/i21768.scala | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i21768.scala diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index cd727ba8ac72..926e748b85c7 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -1010,13 +1010,25 @@ object Parsers { skipParams() lookahead.isColon && { + lookahead.nextToken() !sourceVersion.isAtLeast(`3.6`) || { // in the new given syntax, a `:` at EOL after an identifier represents a single identifier given // Example: // given C: // def f = ... - lookahead.nextToken() !lookahead.isAfterLineEnd + } || { + // Support for for pre-3.6 syntax where type is put on the next line + // Examples: + // given namedGiven: + // X[T] with {} + // given otherGiven: + // X[T] = new X[T]{} + lookahead.isIdent && { + lookahead.nextToken() + skipParams() + lookahead.token == WITH || lookahead.token == EQUALS + } } } diff --git a/tests/pos/i21768.scala b/tests/pos/i21768.scala new file mode 100644 index 000000000000..85066d0cb8a6 --- /dev/null +++ b/tests/pos/i21768.scala @@ -0,0 +1,12 @@ + +trait Foo[T]: + def foo(v: T): Unit + +given myFooOfInt: + Foo[Int] with + def foo(v: Int): Unit = ??? + +given myFooOfLong: + Foo[Long] = new Foo[Long] { + def foo(v: Long): Unit = ??? + } From a87a18912002a2d29439b83dfe1f61270c892026 Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Sun, 17 Nov 2024 22:43:46 +0100 Subject: [PATCH 2/2] Delay `.nextToken` computation as per review comment --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 926e748b85c7..7a55690dcc21 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -1010,12 +1010,12 @@ object Parsers { skipParams() lookahead.isColon && { - lookahead.nextToken() !sourceVersion.isAtLeast(`3.6`) || { // in the new given syntax, a `:` at EOL after an identifier represents a single identifier given // Example: // given C: // def f = ... + lookahead.nextToken() !lookahead.isAfterLineEnd } || { // Support for for pre-3.6 syntax where type is put on the next line