From 7b48e9dffb06bf7eb577b6629131e8bbf24e7b3a Mon Sep 17 00:00:00 2001 From: i10416 Date: Sun, 22 Oct 2023 15:15:43 +0900 Subject: [PATCH 1/2] fix(#18265): crash on extension method without body This commit fixes a bug that causes Scala compiler to crash due to position error when an extension method followed by a newline has neither type annotation nor `= `. This commit is based on https://github.com/lampepfl/dotty/pull/18445. Co-Authored-By: @hamzaremmal [Cherry-picked caf30e3d1186ed306ba5c8ef93b148ad7db6dc54] --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 2 ++ tests/neg/i18265.check | 6 ++++++ tests/neg/i18265.scala | 5 +++++ 3 files changed, 13 insertions(+) create mode 100644 tests/neg/i18265.check create mode 100644 tests/neg/i18265.scala diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 66e001403d6a..6add504a98cb 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -2607,6 +2607,8 @@ object Parsers { parents match { case parent :: Nil if !in.isNestedStart => reposition(if (parent.isType) ensureApplied(wrapNew(parent)) else parent) + case tkn if in.token == INDENT => + New(templateBodyOpt(emptyConstructor, parents, Nil)) case _ => New(reposition(templateBodyOpt(emptyConstructor, parents, Nil))) } diff --git a/tests/neg/i18265.check b/tests/neg/i18265.check new file mode 100644 index 000000000000..fc9f044f4e83 --- /dev/null +++ b/tests/neg/i18265.check @@ -0,0 +1,6 @@ +-- [E019] Syntax Error: tests/neg/i18265.scala:5:13 -------------------------------------------------------------------- +5 | def twice // error + | ^ + | Missing return type + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/i18265.scala b/tests/neg/i18265.scala new file mode 100644 index 000000000000..5cc67b7bd182 --- /dev/null +++ b/tests/neg/i18265.scala @@ -0,0 +1,5 @@ +trait Foo + +val foo = new Foo: + extension (s: String) + def twice // error From 45faafb1051da700e8cf4746a6567e6b1f8afdec Mon Sep 17 00:00:00 2001 From: i10416 Date: Sun, 22 Oct 2023 17:03:07 +0900 Subject: [PATCH 2/2] maybefix: update semanticdb expected output [Cherry-picked 07761aec094af62c4ed7621a0362167cbe66eb1b] --- tests/semanticdb/expect/StructuralTypes.expect.scala | 4 ++-- tests/semanticdb/metac.expect | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/semanticdb/expect/StructuralTypes.expect.scala b/tests/semanticdb/expect/StructuralTypes.expect.scala index 96c7181d6f10..98aaca95f880 100644 --- a/tests/semanticdb/expect/StructuralTypes.expect.scala +++ b/tests/semanticdb/expect/StructuralTypes.expect.scala @@ -16,7 +16,7 @@ object StructuralTypes/*<-example::StructuralTypes.*/: val V/*<-example::StructuralTypes.V.*/: Object/*->java::lang::Object#*/ { def scalameta/*<-local4*/: String/*->scala::Predef.String#*/ - } = /*<-local6*/new: - def scalameta/*<-local5*/ = "4.0" + } = new: + /*<-local6*/def scalameta/*<-local5*/ = "4.0" V/*->example::StructuralTypes.V.*/.scalameta/*->scala::reflect::Selectable#selectDynamic().*/ end StructuralTypes/*->example::StructuralTypes.*/ \ No newline at end of file diff --git a/tests/semanticdb/metac.expect b/tests/semanticdb/metac.expect index e05a645c0141..00aa8673f015 100644 --- a/tests/semanticdb/metac.expect +++ b/tests/semanticdb/metac.expect @@ -3472,7 +3472,7 @@ Occurrences: [16:9..16:15): Object -> java/lang/Object# [17:8..17:17): scalameta <- local4 [17:19..17:25): String -> scala/Predef.String# -[18:6..18:6): <- local6 +[19:4..19:4): <- local6 [19:8..19:17): scalameta <- local5 [20:2..20:3): V -> example/StructuralTypes.V. [20:4..20:13): scalameta -> scala/reflect/Selectable#selectDynamic().