[CFE/kernel] Fix crash on TypeParameter.declaration when compiling constructor with annotation on type parameter with name clash
Change-Id: Ic56cfc8f4eaafe3a6d25371b6ae5deb452651543
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395001
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
diff --git a/pkg/front_end/lib/src/source/source_constructor_builder.dart b/pkg/front_end/lib/src/source/source_constructor_builder.dart
index aae9585..7a0efd0 100644
--- a/pkg/front_end/lib/src/source/source_constructor_builder.dart
+++ b/pkg/front_end/lib/src/source/source_constructor_builder.dart
@@ -466,6 +466,9 @@
Builder get parent => declarationBuilder;
@override
+ bool get supportsTypeParameters => false;
+
+ @override
// Coverage-ignore(suite): Not run.
Name get memberName => _memberName.name;
diff --git a/pkg/front_end/lib/src/source/source_function_builder.dart b/pkg/front_end/lib/src/source/source_function_builder.dart
index 1ac8f69..8ce0e15 100644
--- a/pkg/front_end/lib/src/source/source_function_builder.dart
+++ b/pkg/front_end/lib/src/source/source_function_builder.dart
@@ -312,6 +312,8 @@
@override
bool get isNative => nativeMethodName != null;
+ bool get supportsTypeParameters => true;
+
void buildFunction() {
function.asyncMarker = asyncModifier;
function.body = body;
@@ -330,7 +332,9 @@
if (typeParameters != null) {
for (NominalParameterBuilder t in typeParameters!) {
TypeParameter parameter = t.parameter;
- function.typeParameters.add(parameter);
+ if (supportsTypeParameters) {
+ function.typeParameters.add(parameter);
+ }
if (needsCheckVisitor != null) {
if (parameter.bound.accept(needsCheckVisitor)) {
parameter.isCovariantByClass = true;
diff --git a/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart
new file mode 100644
index 0000000..8ad1ed6
--- /dev/null
+++ b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart
@@ -0,0 +1,9 @@
+// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+const int x = 42;
+
+class Foo {
+ Foo<@x x>() {}
+}
diff --git a/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.expect b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.expect
new file mode 100644
index 0000000..b4480ac
--- /dev/null
+++ b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.expect
@@ -0,0 +1,25 @@
+library;
+//
+// Problems in library:
+//
+// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:6: Error: Constructors can't have type parameters.
+// Try removing the type parameters.
+// Foo<@x x>() {}
+// ^^^^^^
+//
+// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
+// Foo<@x x>() {}
+// ^
+//
+import self as self;
+import "dart:core" as core;
+
+class Foo extends core::Object {
+ constructor •() → self::Foo
+ : super core::Object::•() {}
+}
+static const field core::int x = #C1;
+
+constants {
+ #C1 = 42
+}
diff --git a/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.modular.expect b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.modular.expect
new file mode 100644
index 0000000..b4480ac
--- /dev/null
+++ b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.modular.expect
@@ -0,0 +1,25 @@
+library;
+//
+// Problems in library:
+//
+// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:6: Error: Constructors can't have type parameters.
+// Try removing the type parameters.
+// Foo<@x x>() {}
+// ^^^^^^
+//
+// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
+// Foo<@x x>() {}
+// ^
+//
+import self as self;
+import "dart:core" as core;
+
+class Foo extends core::Object {
+ constructor •() → self::Foo
+ : super core::Object::•() {}
+}
+static const field core::int x = #C1;
+
+constants {
+ #C1 = 42
+}
diff --git a/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.outline.expect b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.outline.expect
new file mode 100644
index 0000000..9e78531
--- /dev/null
+++ b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.outline.expect
@@ -0,0 +1,21 @@
+library;
+//
+// Problems in library:
+//
+// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:6: Error: Constructors can't have type parameters.
+// Try removing the type parameters.
+// Foo<@x x>() {}
+// ^^^^^^
+//
+// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
+// Foo<@x x>() {}
+// ^
+//
+import self as self;
+import "dart:core" as core;
+
+class Foo extends core::Object {
+ constructor •() → self::Foo
+ ;
+}
+static const field core::int x = 42;
diff --git a/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.transformed.expect b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.transformed.expect
new file mode 100644
index 0000000..b4480ac
--- /dev/null
+++ b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.strong.transformed.expect
@@ -0,0 +1,25 @@
+library;
+//
+// Problems in library:
+//
+// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:6: Error: Constructors can't have type parameters.
+// Try removing the type parameters.
+// Foo<@x x>() {}
+// ^^^^^^
+//
+// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
+// Foo<@x x>() {}
+// ^
+//
+import self as self;
+import "dart:core" as core;
+
+class Foo extends core::Object {
+ constructor •() → self::Foo
+ : super core::Object::•() {}
+}
+static const field core::int x = #C1;
+
+constants {
+ #C1 = 42
+}
diff --git a/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.textual_outline.expect b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.textual_outline.expect
new file mode 100644
index 0000000..7c6b6ae
--- /dev/null
+++ b/pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart.textual_outline.expect
@@ -0,0 +1,5 @@
+const int x = 42;
+
+class Foo {
+ Foo<@x x>() {}
+}
diff --git a/pkg/front_end/testcases/textual_outline.status b/pkg/front_end/testcases/textual_outline.status
index b9bf65d..b668ef7 100644
--- a/pkg/front_end/testcases/textual_outline.status
+++ b/pkg/front_end/testcases/textual_outline.status
@@ -7,6 +7,7 @@
# These tests have errors in them which isn't reported by the parser but the
# formatter still sees as syntax errors and thus won't format.
+regress/annotation_on_type_parameter_name_clash_on_constructor: FormatterCrash
extension_types/const_constructor_body: FormatterCrash
extension_types/field_access: FormatterCrash
extension_types/issue52119: FormatterCrash