Skip to content

Commit cf111fd

Browse files
alexmarkovCommit Queue
authored and
Commit Queue
committed
[vm,corelib] Restrict Expando/WeakReference usage for records
From records specification: ``` Like numbers, records do not have a well-defined persistent identity. That means Expandos can not be attached to them. ``` This change updates Expando and WeakReference API documentation and adds a check to disallow attaching Expando or WeakReference to a record. TEST=co19/LanguageFeatures/Records/expandos_A01_t01 Issue: #49719 Change-Id: I6459f43a2deac697e201673589d73abedc8d413e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/263420 Reviewed-by: Lasse Nielsen <[email protected]> Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent 174f3d3 commit cf111fd

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

sdk/lib/_internal/vm/lib/internal_patch.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,12 @@ void checkValidWeakTarget(object, name) {
225225
(object is bool) ||
226226
(object is num) ||
227227
(object is String) ||
228+
(object is Record) ||
228229
(object is Pointer) ||
229230
(object is Struct) ||
230231
(object is Union)) {
231232
throw new ArgumentError.value(object, name,
232-
"Cannot be a string, number, boolean, null, Pointer, Struct or Union");
233+
"Cannot be a string, number, boolean, record, null, Pointer, Struct or Union");
233234
}
234235
}
235236

sdk/lib/core/weak.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ part of dart.core;
1313

1414
/// An [Expando] allows adding new properties to objects.
1515
///
16-
/// Does not work on numbers, strings, booleans, `null`, `dart:ffi` pointers,
17-
/// `dart:ffi` structs, or `dart:ffi` unions.
16+
/// Does not work on numbers, strings, booleans, records, `null`,
17+
/// `dart:ffi` pointers, `dart:ffi` structs, or `dart:ffi` unions.
1818
///
1919
/// An `Expando` does not hold on to the added property value after an object
2020
/// becomes inaccessible.
@@ -24,6 +24,8 @@ part of dart.core;
2424
/// released. To avoid this, expando properties cannot be added to numbers.
2525
/// The same argument applies to strings, booleans and `null`, which also have
2626
/// literals that evaluate to identical values when they occur more than once.
27+
/// In addition, expando properties can not be added to records because
28+
/// records do not have a well-defined persistent identity.
2729
///
2830
/// There is no restriction on other classes, even for compile time constant
2931
/// objects. Be careful if adding expando properties to compile time constants,
@@ -47,17 +49,17 @@ class Expando<T extends Object> {
4749
///
4850
/// If the object hasn't been expanded, the result is the `null` value.
4951
///
50-
/// The object must not be a number, a string, a boolean, `null`, a
51-
/// `dart:ffi` pointer, a `dart:ffi` struct, or a `dart:ffi` union.
52+
/// The object must not be a number, a string, a boolean, a record, `null`,
53+
/// a `dart:ffi` pointer, a `dart:ffi` struct, or a `dart:ffi` union.
5254
external T? operator [](Object object);
5355

5456
/// Sets this [Expando]'s property value on the given object to [value].
5557
///
5658
/// Properties can effectively be removed again
5759
/// by setting their value to `null`.
5860
///
59-
/// The object must not be a number, a string, a boolean, `null`, a
60-
/// `dart:ffi` pointer, a `dart:ffi` struct, or a `dart:ffi` union.
61+
/// The object must not be a number, a string, a boolean, a record, `null`,
62+
/// a `dart:ffi` pointer, a `dart:ffi` struct, or a `dart:ffi` union.
6163
external void operator []=(Object object, T? value);
6264
}
6365

@@ -125,7 +127,7 @@ abstract class WeakReference<T extends Object> {
125127
/// Creates a [WeakReference] pointing to the given [target].
126128
///
127129
/// The [target] must be an object supported as an [Expando] key,
128-
/// which means [target] cannot be a number, a string, a boolean,
130+
/// which means [target] cannot be a number, a string, a boolean, a record,
129131
/// the `null` value, or certain other types of special objects.
130132
external factory WeakReference(T target);
131133

0 commit comments

Comments
 (0)