[vm]/[dart2wasm] Fix list factory specialization code

The list factory specialization code can only know constant values
passed to `growable: <xxx>` if the variables are local and not
parameters.

TEST=corelib/list_factory_specialization_regression_test

Change-Id: I2c425108fd266fea6b443a7adee86c5a2b33a4d1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/365584
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
This commit is contained in:
Martin Kustermann 2024-05-08 08:59:01 +00:00 committed by Commit Queue
parent 4945c2a76b
commit bb044733cd
3 changed files with 26 additions and 2 deletions

View file

@ -203,7 +203,11 @@ bool _isNullConstant(Expression value) {
Expression _unwrapFinalVariableGet(Expression expr) {
if (expr is VariableGet) {
final variable = expr.variable;
if (variable.isFinal) {
// This optimization does not apply to parameters because they
// get their value set by the caller and their initializers
// are default values rather than true initializers.
final parent = variable.parent;
if (variable.isFinal && parent is! FunctionNode) {
final initializer = variable.initializer;
if (initializer != null) {
return initializer;

View file

@ -181,7 +181,11 @@ class ListFactorySpecializer extends BaseSpecializer {
Expression _unwrapFinalVariableGet(Expression expr) {
if (expr is VariableGet) {
final variable = expr.variable;
if (variable.isFinal) {
// This optimization does not apply to parameters because they
// get their value set by the caller and their initializers
// are default values rather than true initializers.
final parent = variable.parent;
if (variable.isFinal && parent is! FunctionNode) {
final initializer = variable.initializer;
if (initializer != null) {
return initializer;

View file

@ -0,0 +1,16 @@
// Copyright (c) 2024, 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.
import "package:expect/expect.dart";
void main() {
final l = foo(growable: false);
Expect.throws(() => l.add(1));
final l2 = foo(growable: true);
l2.clear();
}
List foo({final bool growable = true}) =>
List<dynamic>.empty(growable: growable);