Files
sdk/runtime/tests/vm/dart/deeply_immutable_const_map_test.dart
T
Alexander Aprelev cdf7bf9c78 [vm/shared] Allow const maps in deeply immutable classes.
This relies on runtime check of Map-typed variable initialization.
TEST=kernel_binary_flowgraph_test

Change-Id: Ia9be2644208883739f5896a223dcbe2b59c98114
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/482021
Reviewed-by: Slava Egorov <vegorov@google.com>
2026-02-27 07:43:14 -08:00

35 lines
939 B
Dart

// Copyright (c) 2026, 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 "dart:_compact_hash" show createConstMapFromMapOfDeeplyImmutables;
import "package:expect/expect.dart";
@pragma('vm:deeply-immutable')
final class Foo {
final Map<String, Object> bar;
Foo(this.bar);
}
main() {
final foo = Foo(const {"abc": "def"});
Expect.equals(1, foo.bar.length);
Expect.throws(() => Foo({"abc": "def"}), (e) => e is ArgumentError);
final foo1 = Foo(createConstMapFromMapOfDeeplyImmutables({"abc": "def"}));
Expect.equals(1, foo1.bar.length);
Expect.throws(
() => Foo({"ghi": foo, "jkl": foo1}),
(e) => e is ArgumentError,
);
final foo2 = Foo(
createConstMapFromMapOfDeeplyImmutables({"ghi": foo, "jkl": foo1}),
);
Expect.equals(2, foo2.bar.length);
}