Skip to content

Commit fe3fadb

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 39025. ForEachPartsWithIdentifier does not define anything, don't pretend that it is a LocalVariableElement.
[email protected] Bug: #39025 Change-Id: Ieda56c73a1928434a29aa947c44fc29b66a79464 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125525 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 3a17beb commit fe3fadb

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

pkg/analysis_server/test/services/completion/dart/local_reference_contributor_test.dart

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,51 @@ class B extends ^
25372537
assertNoSuggestions();
25382538
}
25392539

2540+
test_forEachPartsWithIdentifier_class() async {
2541+
addTestSource('''
2542+
class C {}
2543+
2544+
main() {
2545+
for(C in [0, 1, 2]) {
2546+
^
2547+
}
2548+
}
2549+
''');
2550+
await computeSuggestions();
2551+
// Using `C` in for-each is invalid, but we should not crash.
2552+
}
2553+
2554+
test_forEachPartsWithIdentifier_localLevelVariable() async {
2555+
addTestSource('''
2556+
main() {
2557+
int v;
2558+
for(v in [0, 1, 2]) {
2559+
^
2560+
}
2561+
}
2562+
''');
2563+
await computeSuggestions();
2564+
// We don't actually use anything from the `for`, and `v` is suggested
2565+
// just because it is a visible top-level declaration.
2566+
assertSuggestLocalVariable('v', 'int');
2567+
}
2568+
2569+
test_forEachPartsWithIdentifier_topLevelVariable() async {
2570+
addTestSource('''
2571+
int v;
2572+
main() {
2573+
for(v in [0, 1, 2]) {
2574+
^
2575+
}
2576+
}
2577+
''');
2578+
await computeSuggestions();
2579+
// We don't actually use anything from the `for`, and `v` is suggested
2580+
// just because it is a visible top-level declaration.
2581+
assertSuggestTopLevelVar('v', 'int',
2582+
relevance: DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE);
2583+
}
2584+
25402585
test_ForEachStatement() async {
25412586
// SimpleIdentifier ForEachStatement
25422587
addTestSource('main() {List<int> values; for (int index in ^)}');
@@ -2584,7 +2629,7 @@ class B extends ^
25842629

25852630
test_ForEachStatement_body_untyped() async {
25862631
// Block ForEachStatement
2587-
addTestSource('main(args) {for (foo in bar) {^}}');
2632+
addTestSource('main(args) {for (var foo in bar) {^}}');
25882633
await computeSuggestions();
25892634

25902635
expect(replacementOffset, completionOffset);

pkg/analyzer_plugin/lib/src/utilities/visitors/local_declaration_visitor.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,6 @@ abstract class LocalDeclarationVisitor extends GeneralizingAstVisitor {
163163
declaredLocalVar(id, loopVar.type);
164164
}
165165
}
166-
} else if (forLoopParts is ForEachPartsWithIdentifier) {
167-
SimpleIdentifier id = forLoopParts.identifier;
168-
if (id != null) {
169-
// If there is no loop variable, don't declare it.
170-
declaredLocalVar(id, null);
171-
}
172166
} else if (forLoopParts is ForPartsWithDeclarations) {
173167
VariableDeclarationList varList = forLoopParts.variables;
174168
if (varList != null) {

0 commit comments

Comments
 (0)