Skip to content

Commit aea99e7

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Augment. Report AUGMENTATION_TYPE_PARAMETER_NAME
Change-Id: Icd455e26ec11f2daa9907e996df2706bd8f7d9f0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373321 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 8e4c2d6 commit aea99e7

File tree

7 files changed

+128
-1
lines changed

7 files changed

+128
-1
lines changed

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_BOUND:
215215
status: noFix
216216
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_COUNT:
217217
status: noFix
218+
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_NAME:
219+
status: needsFix
220+
notes: |-
221+
We could rename the type parameter, this is purely local operation.
218222
CompileTimeErrorCode.AUGMENTATION_WITHOUT_DECLARATION:
219223
status: noFix
220224
CompileTimeErrorCode.AUGMENTATION_WITHOUT_IMPORT:

pkg/analyzer/lib/src/error/codes.g.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
277277
"parameters.",
278278
);
279279

280+
static const CompileTimeErrorCode AUGMENTATION_TYPE_PARAMETER_NAME =
281+
CompileTimeErrorCode(
282+
'AUGMENTATION_TYPE_PARAMETER_NAME',
283+
"The augmentation type parameter must have the same name as the "
284+
"corresponding type parameter of the declaration.",
285+
correctionMessage:
286+
"Try changing the augmentation to match the declaration type "
287+
"parameters.",
288+
);
289+
280290
static const CompileTimeErrorCode AUGMENTATION_WITHOUT_DECLARATION =
281291
CompileTimeErrorCode(
282292
'AUGMENTATION_WITHOUT_DECLARATION',

pkg/analyzer/lib/src/error/error_code_values.g.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const List<ErrorCode> errorCodeValues = [
6969
CompileTimeErrorCode.AUGMENTATION_OF_DIFFERENT_DECLARATION_KIND,
7070
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_BOUND,
7171
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_COUNT,
72+
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_NAME,
7273
CompileTimeErrorCode.AUGMENTATION_WITHOUT_DECLARATION,
7374
CompileTimeErrorCode.AUGMENTATION_WITHOUT_IMPORT,
7475
CompileTimeErrorCode.AUGMENTATION_WITHOUT_LIBRARY,

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,15 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
17071707
for (var index = 0; index < declarationCount; index++) {
17081708
var ofDeclaration = declarationTypeParameters[index];
17091709
var ofAugmentation = typeParameters[index];
1710+
1711+
if (ofAugmentation.name.lexeme != ofDeclaration.name) {
1712+
errorReporter.atToken(
1713+
ofAugmentation.name,
1714+
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_NAME,
1715+
);
1716+
continue;
1717+
}
1718+
17101719
var declarationBound = ofDeclaration.bound;
17111720
var augmentationBound = ofAugmentation.bound;
17121721
switch ((declarationBound, augmentationBound)) {
@@ -1733,7 +1742,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
17331742
}
17341743
}
17351744
}
1736-
// TODO(scheglov): check names
17371745
}
17381746
}
17391747
}

pkg/analyzer/messages.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,10 @@ CompileTimeErrorCode:
11621162
problemMessage: The augmentation must have the same number of type parameters as the declaration.
11631163
correctionMessage: Try changing the augmentation to match the declaration type parameters.
11641164
hasPublishedDocs: false
1165+
AUGMENTATION_TYPE_PARAMETER_NAME:
1166+
problemMessage: The augmentation type parameter must have the same name as the corresponding type parameter of the declaration.
1167+
correctionMessage: Try changing the augmentation to match the declaration type parameters.
1168+
hasPublishedDocs: false
11651169
AUGMENTATION_WITHOUT_DECLARATION:
11661170
problemMessage: The declaration being augmented doesn't exist.
11671171
correctionMessage: Try changing the augmentation to match an existing declaration.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/context_collection_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(AugmentationTypeParameterNameTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class AugmentationTypeParameterNameTest extends PubPackageResolutionTest {
18+
test_class() async {
19+
newFile('$testPackageLibPath/a.dart', r'''
20+
import augment 'test.dart';
21+
22+
class A<T> {}
23+
''');
24+
25+
await assertErrorsInCode(r'''
26+
augment library 'a.dart';
27+
28+
augment class A<U> {}
29+
''', [
30+
error(CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_NAME, 43, 1),
31+
]);
32+
}
33+
34+
test_enum() async {
35+
newFile('$testPackageLibPath/a.dart', r'''
36+
import augment 'test.dart';
37+
38+
enum A<T> {v}
39+
''');
40+
41+
await assertErrorsInCode(r'''
42+
augment library 'a.dart';
43+
44+
augment enum A<U> {}
45+
''', [
46+
error(CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_NAME, 42, 1),
47+
]);
48+
}
49+
50+
test_extension() async {
51+
newFile('$testPackageLibPath/a.dart', r'''
52+
import augment 'test.dart';
53+
54+
extension A<T> on int {}
55+
''');
56+
57+
await assertErrorsInCode(r'''
58+
augment library 'a.dart';
59+
60+
augment extension A<U> {}
61+
''', [
62+
error(CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_NAME, 47, 1),
63+
]);
64+
}
65+
66+
test_extensionType() async {
67+
newFile('$testPackageLibPath/a.dart', r'''
68+
import augment 'test.dart';
69+
70+
extension type A<T>(int it) {}
71+
''');
72+
73+
await assertErrorsInCode(r'''
74+
augment library 'a.dart';
75+
76+
augment extension type A<U>(int it) {}
77+
''', [
78+
error(CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_NAME, 52, 1),
79+
]);
80+
}
81+
82+
test_mixin() async {
83+
newFile('$testPackageLibPath/a.dart', r'''
84+
import augment 'test.dart';
85+
86+
mixin A<T> {}
87+
''');
88+
89+
await assertErrorsInCode(r'''
90+
augment library 'a.dart';
91+
92+
augment mixin A<U> {}
93+
''', [
94+
error(CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_NAME, 43, 1),
95+
]);
96+
}
97+
}

pkg/analyzer/test/src/diagnostics/test_all.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import 'augmentation_type_parameter_bound_test.dart'
4949
as augmentation_type_parameter_bound;
5050
import 'augmentation_type_parameter_count_test.dart'
5151
as augmentation_type_parameter_count;
52+
import 'augmentation_type_parameter_name_test.dart'
53+
as augmentation_type_parameter_name;
5254
import 'augmentation_without_declaration_test.dart'
5355
as augmentation_without_declaration;
5456
import 'await_in_late_local_variable_initializer_test.dart'
@@ -955,6 +957,7 @@ main() {
955957
augmentation_of_different_declaration_kind.main();
956958
augmentation_type_parameter_bound.main();
957959
augmentation_type_parameter_count.main();
960+
augmentation_type_parameter_name.main();
958961
augmentation_without_declaration.main();
959962
await_in_late_local_variable_initializer.main();
960963
await_in_wrong_context.main();

0 commit comments

Comments
 (0)