Skip to content

Commit

Permalink
[CFE/kernel] Fix crash on TypeParameter.declaration when compiling co…
Browse files Browse the repository at this point in the history
…nstructor 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]>
  • Loading branch information
jensjoha authored and Commit Queue committed Nov 14, 2024
1 parent 72534f1 commit a00d45f
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/front_end/lib/src/source/source_constructor_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ class DeclaredSourceConstructorBuilder
@override
Builder get parent => declarationBuilder;

@override
bool get supportsTypeParameters => false;

@override
// Coverage-ignore(suite): Not run.
Name get memberName => _memberName.name;
Expand Down
6 changes: 5 additions & 1 deletion pkg/front_end/lib/src/source/source_function_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ abstract class SourceFunctionBuilderImpl extends SourceMemberBuilderImpl
@override
bool get isNative => nativeMethodName != null;

bool get supportsTypeParameters => true;

void buildFunction() {
function.asyncMarker = asyncModifier;
function.body = body;
Expand All @@ -330,7 +332,9 @@ abstract class SourceFunctionBuilderImpl extends SourceMemberBuilderImpl
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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>() {}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const int x = 42;

class Foo {
Foo<@x x>() {}
}
1 change: 1 addition & 0 deletions pkg/front_end/testcases/textual_outline.status
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a00d45f

Please sign in to comment.