Skip to content

Commit 7f35fdb

Browse files
scheglovCommit Queue
authored andcommitted
---
yaml --- r: 361503 b: refs/heads/main c: 5ef329c h: refs/heads/main i: 361501: 839d9d3 361499: d23cacc 361495: 959e2e1 361487: 657680d 361471: 8e858a6
1 parent eb39355 commit 7f35fdb

File tree

8 files changed

+102
-3
lines changed

8 files changed

+102
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,7 @@ refs/tags/2.14.0-96.0.dev: 1eee24e50fc3028754d9a8e98852b949c04da4e4
19881988
refs/tags/2.14.0-97.0.dev: ba9c1636e87fbdcc02b8bc4a584455c32f8378b4
19891989
refs/tags/2.14.0-98.0.dev: f2d370c93582bbf4da42b5dd4c906d6145b01ea9
19901990
refs/tags/2.14.0-99.0.dev: e722f62b48fb382534efc1f20735def68848006f
1991-
refs/heads/main: 79b66e79bad9dc88c893e001ffed26dc6ef8cdf6
1991+
refs/heads/main: 5ef329cf747f38d6196fed180c9689af1369e69b
19921992
refs/heads/TedSander-patch-1: 739563c962fea6f5e2883a8d84f8638c48f2aa40
19931993
refs/tags/2.13.1: 53a67a7b0650edb435535a50b5c430abbc68bc60
19941994
refs/tags/2.13.3: a77223333944631f8f48a76e7f4697a4877e29d7

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ CompileTimeErrorCode.DUPLICATE_NAMED_ARGUMENT:
354354
status: needsEvaluation
355355
CompileTimeErrorCode.DUPLICATE_PART:
356356
status: needsEvaluation
357+
CompileTimeErrorCode.DUPLICATE_VARIABLE_PATTERN:
358+
status: needsEvaluation
357359
CompileTimeErrorCode.ENUM_CONSTANT_SAME_NAME_AS_ENCLOSING:
358360
status: noFix
359361
notes: |-

trunk/pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ const List<ErrorCode> errorCodeValues = [
162162
CompileTimeErrorCode.DUPLICATE_FIELD_FORMAL_PARAMETER,
163163
CompileTimeErrorCode.DUPLICATE_NAMED_ARGUMENT,
164164
CompileTimeErrorCode.DUPLICATE_PART,
165+
CompileTimeErrorCode.DUPLICATE_VARIABLE_PATTERN,
165166
CompileTimeErrorCode.ENUM_CONSTANT_SAME_NAME_AS_ENCLOSING,
166167
CompileTimeErrorCode.ENUM_CONSTANT_WITH_NON_CONST_CONSTRUCTOR,
167168
CompileTimeErrorCode.ENUM_INSTANTIATED_TO_BOUNDS_IS_NOT_WELL_BOUNDED,

trunk/pkg/analyzer/lib/src/dart/resolver/resolution_visitor.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'package:analyzer/src/dart/resolver/ast_rewrite.dart';
2323
import 'package:analyzer/src/dart/resolver/named_type_resolver.dart';
2424
import 'package:analyzer/src/dart/resolver/record_type_annotation_resolver.dart';
2525
import 'package:analyzer/src/dart/resolver/scope.dart';
26+
import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
2627
import 'package:analyzer/src/error/codes.dart';
2728
import 'package:analyzer/src/generated/element_walker.dart';
2829
import 'package:analyzer/src/generated/utilities_dart.dart';
@@ -1621,8 +1622,16 @@ class _PatternContext
16211622
required DartPatternImpl pattern,
16221623
required DartPatternImpl previousPattern,
16231624
}) {
1624-
// TODO: implement matchVarOverlap
1625-
throw UnimplementedError();
1625+
pattern as VariablePatternImpl;
1626+
visitor._errorReporter.reportError(
1627+
DiagnosticFactory().duplicateDefinitionForNodes(
1628+
visitor._errorReporter.source,
1629+
CompileTimeErrorCode.DUPLICATE_VARIABLE_PATTERN,
1630+
pattern,
1631+
previousPattern,
1632+
[pattern.name.lexeme],
1633+
),
1634+
);
16261635
}
16271636

16281637
@override

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,15 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
11701170
hasPublishedDocs: true,
11711171
);
11721172

1173+
/// Parameters:
1174+
/// 0: the name of the variable
1175+
static const CompileTimeErrorCode DUPLICATE_VARIABLE_PATTERN =
1176+
CompileTimeErrorCode(
1177+
'DUPLICATE_VARIABLE_PATTERN',
1178+
"The variable '{0}' is already defined in this pattern.",
1179+
correctionMessage: "Try renaming the variable.",
1180+
);
1181+
11731182
static const CompileTimeErrorCode ENUM_CONSTANT_SAME_NAME_AS_ENCLOSING =
11741183
CompileTimeErrorCode(
11751184
'ENUM_CONSTANT_SAME_NAME_AS_ENCLOSING',

trunk/pkg/analyzer/messages.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,6 +3518,12 @@ CompileTimeErrorCode:
35183518

35193519
part 'part.dart';
35203520
```
3521+
DUPLICATE_VARIABLE_PATTERN:
3522+
problemMessage: The variable '{0}' is already defined in this pattern.
3523+
correctionMessage: Try renaming the variable.
3524+
comment: |-
3525+
Parameters:
3526+
0: the name of the variable
35213527
ENUM_CONSTANT_SAME_NAME_AS_ENCLOSING:
35223528
problemMessage: "The name of the enum constant can't be the same as the enum's name."
35233529
correctionMessage: Try renaming the constant.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2022, 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(DuplicateVariablePatternTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class DuplicateVariablePatternTest extends PatternsResolutionTest {
18+
test_ifCase() async {
19+
await assertErrorsInCode(r'''
20+
void f(int x) {
21+
if (x case var a & var a) {}
22+
}
23+
''', [
24+
error(CompileTimeErrorCode.DUPLICATE_VARIABLE_PATTERN, 41, 1,
25+
contextMessages: [message('/home/test/lib/test.dart', 33, 1)]),
26+
]);
27+
final node = findNode.caseClause('case').pattern;
28+
assertResolvedNodeText(node, r'''
29+
BinaryPattern
30+
leftOperand: VariablePattern
31+
keyword: var
32+
name: a
33+
declaredElement: hasImplicitType a@33
34+
type: int
35+
operator: &
36+
rightOperand: VariablePattern
37+
keyword: var
38+
name: a
39+
declaredElement: a@33
40+
''');
41+
}
42+
43+
test_switchStatement() async {
44+
await assertErrorsInCode(r'''
45+
void f(int x) {
46+
switch (x) {
47+
case var a & var a:
48+
break;
49+
}
50+
}
51+
''', [
52+
error(CompileTimeErrorCode.DUPLICATE_VARIABLE_PATTERN, 52, 1,
53+
contextMessages: [message('/home/test/lib/test.dart', 44, 1)]),
54+
]);
55+
final node = findNode.switchPatternCase('case').pattern;
56+
assertResolvedNodeText(node, r'''
57+
BinaryPattern
58+
leftOperand: VariablePattern
59+
keyword: var
60+
name: a
61+
declaredElement: hasImplicitType a@44
62+
type: int
63+
operator: &
64+
rightOperand: VariablePattern
65+
keyword: var
66+
name: a
67+
declaredElement: a@44
68+
''');
69+
}
70+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ import 'duplicate_import_test.dart' as duplicate_import;
160160
import 'duplicate_named_argument_test.dart' as duplicate_named_argument;
161161
import 'duplicate_part_test.dart' as duplicate_part;
162162
import 'duplicate_shown_name_test.dart' as duplicate_shown_name;
163+
import 'duplicate_variable_pattern_test.dart' as duplicate_variable_pattern;
163164
import 'enum_constant_same_name_as_enclosing_test.dart'
164165
as enum_constant_same_name_as_enclosing;
165166
import 'enum_constant_with_non_const_constructor_test.dart'
@@ -913,6 +914,7 @@ main() {
913914
duplicate_named_argument.main();
914915
duplicate_part.main();
915916
duplicate_shown_name.main();
917+
duplicate_variable_pattern.main();
916918
enum_constant_same_name_as_enclosing.main();
917919
enum_constant_with_non_const_constructor.main();
918920
enum_instantiated_to_bounds_is_not_well_bounded.main();

0 commit comments

Comments
 (0)