[cfe] Apply a recovery step for 'const' for-in variables

Closes https://github.com/dart-lang/sdk/issues/48808

Change-Id: I68177a6eb7f0a5d64dee6eca9befa9957863f26c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/241604
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Chloe Stefantsova
2022-04-19 14:09:27 +00:00
committed by Commit Bot
parent e307c0b6c4
commit 6b2ee2d01f
8 changed files with 97 additions and 0 deletions
@@ -6177,6 +6177,9 @@ class BodyBuilder extends StackListenerImpl
fasta.messageForInLoopWithConstVariable,
lvalue.fileOffset,
lvalue.name!.length);
// As a recovery step, remove the const flag, to not confuse the
// constant evaluator further in the pipeline.
lvalue.isConst = false;
}
} else {
VariableDeclaration variable = elements.syntheticVariableDeclaration =
@@ -0,0 +1,9 @@
// Copyright (c) 2022, 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.
foo() {
for (const x in [1, 2, 3]) print(x);
}
main() {}
@@ -0,0 +1,2 @@
foo() {}
main() {}
@@ -0,0 +1,2 @@
foo() {}
main() {}
@@ -0,0 +1,23 @@
library /*isNonNullableByDefault*/;
//
// Problems in library:
//
// pkg/front_end/testcases/general/issue48808.dart:6:14: Error: A for-in loop-variable can't be 'const'.
// Try removing the 'const' modifier.
// for (const x in [1, 2, 3]) print(x);
// ^
//
import self as self;
import "dart:core" as core;
static method foo() → dynamic {
{
invalid-expression "pkg/front_end/testcases/general/issue48808.dart:6:14: Error: A for-in loop-variable can't be 'const'.
Try removing the 'const' modifier.
for (const x in [1, 2, 3]) print(x);
^";
for (core::int x in <core::int>[1, 2, 3])
core::print(x);
}
}
static method main() → dynamic {}
@@ -0,0 +1,23 @@
library /*isNonNullableByDefault*/;
//
// Problems in library:
//
// pkg/front_end/testcases/general/issue48808.dart:6:14: Error: A for-in loop-variable can't be 'const'.
// Try removing the 'const' modifier.
// for (const x in [1, 2, 3]) print(x);
// ^
//
import self as self;
import "dart:core" as core;
static method foo() → dynamic {
{
invalid-expression "pkg/front_end/testcases/general/issue48808.dart:6:14: Error: A for-in loop-variable can't be 'const'.
Try removing the 'const' modifier.
for (const x in [1, 2, 3]) print(x);
^";
for (core::int x in <core::int>[1, 2, 3])
core::print(x);
}
}
static method main() → dynamic {}
@@ -0,0 +1,7 @@
library /*isNonNullableByDefault*/;
import self as self;
static method foo() → dynamic
;
static method main() → dynamic
;
@@ -0,0 +1,28 @@
library /*isNonNullableByDefault*/;
//
// Problems in library:
//
// pkg/front_end/testcases/general/issue48808.dart:6:14: Error: A for-in loop-variable can't be 'const'.
// Try removing the 'const' modifier.
// for (const x in [1, 2, 3]) print(x);
// ^
//
import self as self;
import "dart:core" as core;
static method foo() → dynamic {
{
invalid-expression "pkg/front_end/testcases/general/issue48808.dart:6:14: Error: A for-in loop-variable can't be 'const'.
Try removing the 'const' modifier.
for (const x in [1, 2, 3]) print(x);
^";
{
core::Iterator<core::int> :sync-for-iterator = core::_GrowableList::_literal3<core::int>(1, 2, 3).{core::Iterable::iterator}{core::Iterator<core::int>};
for (; :sync-for-iterator.{core::Iterator::moveNext}(){() → core::bool}; ) {
core::int x = :sync-for-iterator.{core::Iterator::current}{core::int};
core::print(x);
}
}
}
}
static method main() → dynamic {}