From 6b2ee2d01fc3142f25973e2f41509592ffde1867 Mon Sep 17 00:00:00 2001 From: Chloe Stefantsova Date: Tue, 19 Apr 2022 14:09:27 +0000 Subject: [PATCH] [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 Commit-Queue: Chloe Stefantsova --- .../lib/src/fasta/kernel/body_builder.dart | 3 ++ .../testcases/general/issue48808.dart | 9 ++++++ .../issue48808.dart.textual_outline.expect | 2 ++ ...48808.dart.textual_outline_modelled.expect | 2 ++ .../general/issue48808.dart.weak.expect | 23 +++++++++++++++ .../issue48808.dart.weak.modular.expect | 23 +++++++++++++++ .../issue48808.dart.weak.outline.expect | 7 +++++ .../issue48808.dart.weak.transformed.expect | 28 +++++++++++++++++++ 8 files changed, 97 insertions(+) create mode 100644 pkg/front_end/testcases/general/issue48808.dart create mode 100644 pkg/front_end/testcases/general/issue48808.dart.textual_outline.expect create mode 100644 pkg/front_end/testcases/general/issue48808.dart.textual_outline_modelled.expect create mode 100644 pkg/front_end/testcases/general/issue48808.dart.weak.expect create mode 100644 pkg/front_end/testcases/general/issue48808.dart.weak.modular.expect create mode 100644 pkg/front_end/testcases/general/issue48808.dart.weak.outline.expect create mode 100644 pkg/front_end/testcases/general/issue48808.dart.weak.transformed.expect diff --git a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart index af89434c91d..05ba30747a7 100644 --- a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart +++ b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart @@ -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 = diff --git a/pkg/front_end/testcases/general/issue48808.dart b/pkg/front_end/testcases/general/issue48808.dart new file mode 100644 index 00000000000..b955652985f --- /dev/null +++ b/pkg/front_end/testcases/general/issue48808.dart @@ -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() {} diff --git a/pkg/front_end/testcases/general/issue48808.dart.textual_outline.expect b/pkg/front_end/testcases/general/issue48808.dart.textual_outline.expect new file mode 100644 index 00000000000..d530fa8d4d7 --- /dev/null +++ b/pkg/front_end/testcases/general/issue48808.dart.textual_outline.expect @@ -0,0 +1,2 @@ +foo() {} +main() {} diff --git a/pkg/front_end/testcases/general/issue48808.dart.textual_outline_modelled.expect b/pkg/front_end/testcases/general/issue48808.dart.textual_outline_modelled.expect new file mode 100644 index 00000000000..d530fa8d4d7 --- /dev/null +++ b/pkg/front_end/testcases/general/issue48808.dart.textual_outline_modelled.expect @@ -0,0 +1,2 @@ +foo() {} +main() {} diff --git a/pkg/front_end/testcases/general/issue48808.dart.weak.expect b/pkg/front_end/testcases/general/issue48808.dart.weak.expect new file mode 100644 index 00000000000..10ba349118d --- /dev/null +++ b/pkg/front_end/testcases/general/issue48808.dart.weak.expect @@ -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 [1, 2, 3]) + core::print(x); + } +} +static method main() → dynamic {} diff --git a/pkg/front_end/testcases/general/issue48808.dart.weak.modular.expect b/pkg/front_end/testcases/general/issue48808.dart.weak.modular.expect new file mode 100644 index 00000000000..10ba349118d --- /dev/null +++ b/pkg/front_end/testcases/general/issue48808.dart.weak.modular.expect @@ -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 [1, 2, 3]) + core::print(x); + } +} +static method main() → dynamic {} diff --git a/pkg/front_end/testcases/general/issue48808.dart.weak.outline.expect b/pkg/front_end/testcases/general/issue48808.dart.weak.outline.expect new file mode 100644 index 00000000000..8570841fbe3 --- /dev/null +++ b/pkg/front_end/testcases/general/issue48808.dart.weak.outline.expect @@ -0,0 +1,7 @@ +library /*isNonNullableByDefault*/; +import self as self; + +static method foo() → dynamic + ; +static method main() → dynamic + ; diff --git a/pkg/front_end/testcases/general/issue48808.dart.weak.transformed.expect b/pkg/front_end/testcases/general/issue48808.dart.weak.transformed.expect new file mode 100644 index 00000000000..4a8424fb871 --- /dev/null +++ b/pkg/front_end/testcases/general/issue48808.dart.weak.transformed.expect @@ -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 :sync-for-iterator = core::_GrowableList::_literal3(1, 2, 3).{core::Iterable::iterator}{core::Iterator}; + 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 {}