diff --git a/pkg/dart2wasm/lib/intrinsics.dart b/pkg/dart2wasm/lib/intrinsics.dart index d4b2b8fa8d2..98296bfac71 100644 --- a/pkg/dart2wasm/lib/intrinsics.dart +++ b/pkg/dart2wasm/lib/intrinsics.dart @@ -313,9 +313,11 @@ class Intrinsifier { } } if (constIndex != null) { - ListConstant list = receiver.constant as ListConstant; - Expression element = ConstantExpression(list.entries[constIndex]); - return codeGen.wrap(element, typeOfExp(element)); + final entries = (receiver.constant as ListConstant).entries; + if (0 <= constIndex && constIndex < entries.length) { + Expression element = ConstantExpression(entries[constIndex]); + return codeGen.wrap(element, typeOfExp(element)); + } } return null; diff --git a/tests/corelib/regress_55817_test.dart b/tests/corelib/regress_55817_test.dart new file mode 100644 index 00000000000..8a477092868 --- /dev/null +++ b/tests/corelib/regress_55817_test.dart @@ -0,0 +1,14 @@ +// 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"; + +int foo(bool returnA0, List a, [List b = const []]) { + return returnA0 ? a[0] : b[0]; +} + +main() { + Expect.equals(42, foo(true, const [42])); + Expect.throws(() => foo(false, const [42]), (e) => e is RangeError); +}