Skip to content

Commit 9544fe9

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] local and parameter scopes
Update wildcard variable scopes. See: #55862 and #55680. See discussion on related/ updated co19 test (`co19/LanguageFeatures/Wildcards/wildcards_do_not_shadow_A01_t04`) here: dart-lang/co19#2698. Change-Id: I388895ef7bac69617700504a964ce9f3021b2d24 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/368526 Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 7ead4b5 commit 9544fe9

File tree

7 files changed

+212
-154
lines changed

7 files changed

+212
-154
lines changed

pkg/analyzer/lib/src/dart/element/scope.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ class FormalParameterScope extends EnclosedScope {
9393
for (var parameter in elements) {
9494
if (parameter is! FieldFormalParameterElement &&
9595
parameter is! SuperFormalParameterElement) {
96-
_addGetter(parameter);
96+
if (!parameter.isWildcardVariable) {
97+
_addGetter(parameter);
98+
}
9799
}
98100
}
99101
}
@@ -174,7 +176,9 @@ class LocalScope extends EnclosedScope {
174176
LocalScope(super.parent);
175177

176178
void add(Element element) {
177-
_addGetter(element);
179+
if (!element.isWildcardVariable) {
180+
_addGetter(element);
181+
}
178182
}
179183
}
180184

@@ -436,3 +440,10 @@ class _LibraryOrAugmentationImportScope implements Scope {
436440
return _nullPrefixScope.lookup(id);
437441
}
438442
}
443+
444+
extension on Element {
445+
bool get isWildcardVariable =>
446+
name == '_' &&
447+
(this is LocalVariableElement || this is ParameterElement) &&
448+
(library?.featureSet.isEnabled(Feature.wildcard_variables) ?? false);
449+
}

pkg/analyzer/test/src/dart/resolution/local_variable_test.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,43 @@ void f() {
133133
expect(x.isLate, isTrue);
134134
expect(x.isStatic, isFalse);
135135
}
136+
137+
test_localVariable_wildcardVariable_field() async {
138+
await assertNoErrorsInCode('''
139+
class C {
140+
var _ = 1;
141+
void m() {
142+
var _ = 0;
143+
_;
144+
}
145+
}
146+
''');
147+
148+
var node = findNode.simple('_;');
149+
assertResolvedNodeText(node, r'''
150+
SimpleIdentifier
151+
token: _
152+
staticElement: self::@class::C::@getter::_
153+
staticType: int
154+
''');
155+
}
156+
157+
test_localVariable_wildcardVariable_topLevel() async {
158+
await assertNoErrorsInCode('''
159+
var _ = 1;
160+
161+
void f() {
162+
var _ = 0;
163+
_;
164+
}
165+
''');
166+
167+
var node = findNode.simple('_;');
168+
assertResolvedNodeText(node, r'''
169+
SimpleIdentifier
170+
token: _
171+
staticElement: self::@getter::_
172+
staticType: int
173+
''');
174+
}
136175
}

pkg/analyzer/test/src/dart/resolution/method_declaration_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,23 @@ class B {
5151
findElement.parameter('a'),
5252
);
5353
}
54+
55+
test_formalParameterScope_wildcardVariable() async {
56+
await assertNoErrorsInCode('''
57+
class A {
58+
var _ = 1;
59+
void m(int? _) {
60+
_;
61+
}
62+
}
63+
''');
64+
65+
var node = findNode.simple('_;');
66+
assertResolvedNodeText(node, r'''
67+
SimpleIdentifier
68+
token: _
69+
staticElement: self::@class::A::@getter::_
70+
staticType: int
71+
''');
72+
}
5473
}

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

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,46 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/dart/analysis/features.dart';
6-
import 'package:analyzer/src/error/codes.g.dart';
5+
import 'package:analyzer/src/error/codes.dart';
76
import 'package:test_reflective_loader/test_reflective_loader.dart';
87

98
import '../dart/resolution/context_collection_resolution.dart';
109

1110
main() {
1211
defineReflectiveSuite(() {
1312
defineReflectiveTests(UnusedCatchClauseTest);
14-
defineReflectiveTests(UnusedCatchClauseTestWildCardVariablesTest);
1513
});
1614
}
1715

1816
@reflectiveTest
1917
class UnusedCatchClauseTest extends PubPackageResolutionTest {
2018
test_on_unusedException() async {
2119
await assertErrorsInCode(r'''
22-
main() {
20+
f() {
2321
try {
2422
} on String catch (exception) {
2523
}
2624
}
2725
''', [
28-
error(WarningCode.UNUSED_CATCH_CLAUSE, 38, 9),
26+
error(WarningCode.UNUSED_CATCH_CLAUSE, 35, 9),
27+
]);
28+
}
29+
30+
test_on_unusedStack_underscores() async {
31+
await assertErrorsInCode(r'''
32+
f() {
33+
try {
34+
} on String catch (exception, __) {
35+
}
36+
}
37+
''', [
38+
error(WarningCode.UNUSED_CATCH_STACK, 46, 2),
2939
]);
3040
}
3141

3242
test_on_unusedStack_wildcard() async {
3343
await assertNoErrorsInCode(r'''
34-
main() {
44+
f() {
3545
try {
3646
} on String catch (exception, _) {
3747
}
@@ -41,7 +51,7 @@ main() {
4151

4252
test_on_usedException() async {
4353
await assertNoErrorsInCode(r'''
44-
main() {
54+
f() {
4555
try {
4656
} on String catch (exception) {
4757
print(exception);
@@ -52,7 +62,7 @@ main() {
5262

5363
test_unusedException() async {
5464
await assertNoErrorsInCode(r'''
55-
main() {
65+
f() {
5666
try {
5767
} catch (exception) {
5868
}
@@ -62,7 +72,7 @@ main() {
6272

6373
test_unusedException_underscores() async {
6474
await assertNoErrorsInCode(r'''
65-
main() {
75+
f() {
6676
try {
6777
} catch (__) {
6878
}
@@ -72,7 +82,7 @@ main() {
7282

7383
test_unusedException_wildcard() async {
7484
await assertNoErrorsInCode(r'''
75-
main() {
85+
f() {
7686
try {
7787
} catch (_) {
7888
}
@@ -82,7 +92,7 @@ main() {
8292

8393
test_usedException() async {
8494
await assertNoErrorsInCode(r'''
85-
main() {
95+
f() {
8696
try {
8797
} catch (exception) {
8898
print(exception);
@@ -91,24 +101,3 @@ main() {
91101
''');
92102
}
93103
}
94-
95-
@reflectiveTest
96-
class UnusedCatchClauseTestWildCardVariablesTest extends UnusedCatchClauseTest {
97-
@override
98-
List<String> get experiments => [
99-
...super.experiments,
100-
Feature.wildcard_variables.enableString,
101-
];
102-
103-
test_on_unusedStack_underscores() async {
104-
await assertErrorsInCode(r'''
105-
main() {
106-
try {
107-
} on String catch (exception, __) {
108-
}
109-
}
110-
''', [
111-
error(WarningCode.UNUSED_CATCH_STACK, 49, 2),
112-
]);
113-
}
114-
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@ print(x) {}
186186
''');
187187
}
188188

189+
test_isUsed_underscoreField_shadowsLocal() async {
190+
await assertNoErrorsInCode(r'''
191+
class A {
192+
var _ = 1;
193+
void m() {
194+
var _ = 0;
195+
print(_);
196+
}
197+
}
198+
''');
199+
}
200+
201+
// See: https://github.com/dart-lang/sdk/issues/55862
202+
test_isUsed_underscoreField_shadowsParameter() async {
203+
await assertNoErrorsInCode(r'''
204+
class A {
205+
var _ = 1;
206+
void m(int? _) {
207+
print(_);
208+
}
209+
}
210+
''');
211+
}
212+
189213
test_notUsed_compoundAssign() async {
190214
await assertErrorsInCode(r'''
191215
class A {

0 commit comments

Comments
 (0)