From 472de2565c8f6f8dff7884d5898b53a7c85b333a Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Mon, 18 Nov 2024 10:34:32 +0100 Subject: [PATCH] Handle old given syntax where identifier and type are seperated by new line (#21957) Fixes #21768 Fixes usages of `with {...}` and `= new {}` declarations presented in tests. [Cherry-picked 7644ecd6a4933a1750c527a16007f5890c45d0e7] --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 12 ++++++++++++ tests/pos/i21768.scala | 12 ++++++++++++ 2 files changed, 24 insertions(+) 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 98dd5ff0f3f4..807755af7ab0 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -1001,6 +1001,18 @@ object Parsers { // 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 = ??? + }