Skip to content

Commit a00d45f

Browse files
jensjohaCommit Queue
authored andcommitted
[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 <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 72534f1 commit a00d45f

9 files changed

+119
-1
lines changed

pkg/front_end/lib/src/source/source_constructor_builder.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ class DeclaredSourceConstructorBuilder
465465
@override
466466
Builder get parent => declarationBuilder;
467467

468+
@override
469+
bool get supportsTypeParameters => false;
470+
468471
@override
469472
// Coverage-ignore(suite): Not run.
470473
Name get memberName => _memberName.name;

pkg/front_end/lib/src/source/source_function_builder.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ abstract class SourceFunctionBuilderImpl extends SourceMemberBuilderImpl
312312
@override
313313
bool get isNative => nativeMethodName != null;
314314

315+
bool get supportsTypeParameters => true;
316+
315317
void buildFunction() {
316318
function.asyncMarker = asyncModifier;
317319
function.body = body;
@@ -330,7 +332,9 @@ abstract class SourceFunctionBuilderImpl extends SourceMemberBuilderImpl
330332
if (typeParameters != null) {
331333
for (NominalParameterBuilder t in typeParameters!) {
332334
TypeParameter parameter = t.parameter;
333-
function.typeParameters.add(parameter);
335+
if (supportsTypeParameters) {
336+
function.typeParameters.add(parameter);
337+
}
334338
if (needsCheckVisitor != null) {
335339
if (parameter.bound.accept(needsCheckVisitor)) {
336340
parameter.isCovariantByClass = true;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
const int x = 42;
6+
7+
class Foo {
8+
Foo<@x x>() {}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:6: Error: Constructors can't have type parameters.
6+
// Try removing the type parameters.
7+
// Foo<@x x>() {}
8+
// ^^^^^^
9+
//
10+
// 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.
11+
// Foo<@x x>() {}
12+
// ^
13+
//
14+
import self as self;
15+
import "dart:core" as core;
16+
17+
class Foo extends core::Object {
18+
constructor •() → self::Foo
19+
: super core::Object::•() {}
20+
}
21+
static const field core::int x = #C1;
22+
23+
constants {
24+
#C1 = 42
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:6: Error: Constructors can't have type parameters.
6+
// Try removing the type parameters.
7+
// Foo<@x x>() {}
8+
// ^^^^^^
9+
//
10+
// 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.
11+
// Foo<@x x>() {}
12+
// ^
13+
//
14+
import self as self;
15+
import "dart:core" as core;
16+
17+
class Foo extends core::Object {
18+
constructor •() → self::Foo
19+
: super core::Object::•() {}
20+
}
21+
static const field core::int x = #C1;
22+
23+
constants {
24+
#C1 = 42
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:6: Error: Constructors can't have type parameters.
6+
// Try removing the type parameters.
7+
// Foo<@x x>() {}
8+
// ^^^^^^
9+
//
10+
// 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.
11+
// Foo<@x x>() {}
12+
// ^
13+
//
14+
import self as self;
15+
import "dart:core" as core;
16+
17+
class Foo extends core::Object {
18+
constructor •() → self::Foo
19+
;
20+
}
21+
static const field core::int x = 42;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/regress/annotation_on_type_parameter_name_clash_on_constructor.dart:8:6: Error: Constructors can't have type parameters.
6+
// Try removing the type parameters.
7+
// Foo<@x x>() {}
8+
// ^^^^^^
9+
//
10+
// 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.
11+
// Foo<@x x>() {}
12+
// ^
13+
//
14+
import self as self;
15+
import "dart:core" as core;
16+
17+
class Foo extends core::Object {
18+
constructor •() → self::Foo
19+
: super core::Object::•() {}
20+
}
21+
static const field core::int x = #C1;
22+
23+
constants {
24+
#C1 = 42
25+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const int x = 42;
2+
3+
class Foo {
4+
Foo<@x x>() {}
5+
}

pkg/front_end/testcases/textual_outline.status

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
# These tests have errors in them which isn't reported by the parser but the
99
# formatter still sees as syntax errors and thus won't format.
10+
regress/annotation_on_type_parameter_name_clash_on_constructor: FormatterCrash
1011
extension_types/const_constructor_body: FormatterCrash
1112
extension_types/field_access: FormatterCrash
1213
extension_types/issue52119: FormatterCrash

0 commit comments

Comments
 (0)