Make the NNBD List.empty default to non-growable. Since a growable list can be created using `[]`, you never need to call `List.empty` with anything but `growable: false` or `growable: dynamicValue`. That means that the default should be non-growable (which also matches `List.filled` and apparently user expectations - since there is already one bug where I assumed it would be non-grwowble while migrating List.generate). Ensure it's implemented by dart2js too and used where there were comments saying to use it when available. Change-Id: Ied20d69346ecfb93640ee2cec718e30e6f8980e8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132600 Reviewed-by: Stephen Adams <sra@google.com> Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
diff --git a/sdk_nnbd/lib/_internal/js_dev_runtime/patch/core_patch.dart b/sdk_nnbd/lib/_internal/js_dev_runtime/patch/core_patch.dart index 0cfb3f8..5180103 100644 --- a/sdk_nnbd/lib/_internal/js_dev_runtime/patch/core_patch.dart +++ b/sdk_nnbd/lib/_internal/js_dev_runtime/patch/core_patch.dart
@@ -524,7 +524,7 @@ } @patch - factory List.empty({bool growable = true}) { + factory List.empty({bool growable = false}) { var list = JSArray<E>.of(JS('', 'new Array()')); if (!growable) JSArray.markFixedList(list); return list;
diff --git a/sdk_nnbd/lib/_internal/js_runtime/lib/core_patch.dart b/sdk_nnbd/lib/_internal/js_runtime/lib/core_patch.dart index cc6e81e..bfd0896 100644 --- a/sdk_nnbd/lib/_internal/js_runtime/lib/core_patch.dart +++ b/sdk_nnbd/lib/_internal/js_runtime/lib/core_patch.dart
@@ -431,6 +431,11 @@ } @patch + factory List.empty({bool growable = false}) { + return growable ? new JSArray<E>.growable(0) : new JSArray<E>.fixed(0); + } + + @patch factory List.from(Iterable elements, {bool growable: true}) { List<E> list = <E>[]; for (E e in elements) {
diff --git a/sdk_nnbd/lib/_internal/vm/lib/array_patch.dart b/sdk_nnbd/lib/_internal/vm/lib/array_patch.dart index 6ad5297..ba97574 100644 --- a/sdk_nnbd/lib/_internal/vm/lib/array_patch.dart +++ b/sdk_nnbd/lib/_internal/vm/lib/array_patch.dart
@@ -7,7 +7,7 @@ @patch class List<E> { @patch - factory List.empty({bool growable = true}) { + factory List.empty({bool growable = false}) { return growable ? <E>[] : _List<E>(0); }
diff --git a/sdk_nnbd/lib/collection/list.dart b/sdk_nnbd/lib/collection/list.dart index 2ae10f0..2b5452a 100644 --- a/sdk_nnbd/lib/collection/list.dart +++ b/sdk_nnbd/lib/collection/list.dart
@@ -236,8 +236,7 @@ } List<E> toList({bool growable = true}) { - // TODO(rnystrom): Use List.empty() instead once that lands. - if (this.isEmpty) return List<E>.of([], growable: growable); + if (this.isEmpty) return List<E>.empty(growable: growable); var first = this[0]; var result = List<E>.filled(this.length, first, growable: growable); for (int i = 1; i < this.length; i++) {
diff --git a/sdk_nnbd/lib/collection/queue.dart b/sdk_nnbd/lib/collection/queue.dart index 7174bc3..600ea6b 100644 --- a/sdk_nnbd/lib/collection/queue.dart +++ b/sdk_nnbd/lib/collection/queue.dart
@@ -634,8 +634,7 @@ List<E> toList({bool growable = true}) { int mask = _table.length - 1; int length = (_tail - _head) & mask; - // TODO(rnystrom): Use List.empty() when that's available. - if (length == 0) return List<E>.of([], growable: growable); + if (length == 0) return List<E>.empty(growable: growable); var list = List<E>.filled(length, first, growable: growable); for (int i = 0; i < length; i++) {
diff --git a/sdk_nnbd/lib/core/list.dart b/sdk_nnbd/lib/core/list.dart index a6be658..e78f478 100644 --- a/sdk_nnbd/lib/core/list.dart +++ b/sdk_nnbd/lib/core/list.dart
@@ -124,11 +124,12 @@ /** * Creates a new empty list. * - * If [growable] is `true`, which is the default, - * the list is growable and equivalent to `<E>[]`. - * If [growable] is `false`, the list is a fixed-length list of length zero. + * If [growable] is `false`, which is the default, + * the list is a fixed-length list of length zero. + * If [growable] is `true`, the list is growable and equivalent to `<E>[]`. */ - external factory List.empty({bool growable = true}); + @Since("2.8") + external factory List.empty({bool growable = false}); /** * Creates a list containing all [elements].