[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: https://github.com/dart-lang/sdk/issues/49719
Change-Id: I6459f43a2deac697e201673589d73abedc8d413e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/263420
Reviewed-by: Lasse Nielsen <lrn@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
This commit is contained in:
Alexander Markov 2022-10-10 16:10:28 +00:00 committed by Commit Queue
parent 174f3d3be2
commit cf111fdcfb
2 changed files with 11 additions and 8 deletions

View file

@ -225,11 +225,12 @@ void checkValidWeakTarget(object, name) {
(object is bool) ||
(object is num) ||
(object is String) ||
(object is Record) ||
(object is Pointer) ||
(object is Struct) ||
(object is Union)) {
throw new ArgumentError.value(object, name,
"Cannot be a string, number, boolean, null, Pointer, Struct or Union");
"Cannot be a string, number, boolean, record, null, Pointer, Struct or Union");
}
}

View file

@ -13,8 +13,8 @@ part of dart.core;
/// An [Expando] allows adding new properties to objects.
///
/// Does not work on numbers, strings, booleans, `null`, `dart:ffi` pointers,
/// `dart:ffi` structs, or `dart:ffi` unions.
/// Does not work on numbers, strings, booleans, records, `null`,
/// `dart:ffi` pointers, `dart:ffi` structs, or `dart:ffi` unions.
///
/// An `Expando` does not hold on to the added property value after an object
/// becomes inaccessible.
@ -24,6 +24,8 @@ part of dart.core;
/// released. To avoid this, expando properties cannot be added to numbers.
/// The same argument applies to strings, booleans and `null`, which also have
/// literals that evaluate to identical values when they occur more than once.
/// In addition, expando properties can not be added to records because
/// records do not have a well-defined persistent identity.
///
/// There is no restriction on other classes, even for compile time constant
/// objects. Be careful if adding expando properties to compile time constants,
@ -47,8 +49,8 @@ class Expando<T extends Object> {
///
/// If the object hasn't been expanded, the result is the `null` value.
///
/// The object must not be a number, a string, a boolean, `null`, a
/// `dart:ffi` pointer, a `dart:ffi` struct, or a `dart:ffi` union.
/// The object must not be a number, a string, a boolean, a record, `null`,
/// a `dart:ffi` pointer, a `dart:ffi` struct, or a `dart:ffi` union.
external T? operator [](Object object);
/// Sets this [Expando]'s property value on the given object to [value].
@ -56,8 +58,8 @@ class Expando<T extends Object> {
/// Properties can effectively be removed again
/// by setting their value to `null`.
///
/// The object must not be a number, a string, a boolean, `null`, a
/// `dart:ffi` pointer, a `dart:ffi` struct, or a `dart:ffi` union.
/// The object must not be a number, a string, a boolean, a record, `null`,
/// a `dart:ffi` pointer, a `dart:ffi` struct, or a `dart:ffi` union.
external void operator []=(Object object, T? value);
}
@ -125,7 +127,7 @@ abstract class WeakReference<T extends Object> {
/// Creates a [WeakReference] pointing to the given [target].
///
/// The [target] must be an object supported as an [Expando] key,
/// which means [target] cannot be a number, a string, a boolean,
/// which means [target] cannot be a number, a string, a boolean, a record,
/// the `null` value, or certain other types of special objects.
external factory WeakReference(T target);