From 80a73308c4449a374364ff8131398ab3dea828cd Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Tue, 1 Apr 2025 17:51:36 -0700 Subject: [PATCH 1/4] [collection] explicitly make BoolList abstract interface It couldn't be extended in practice since it doesn't have a public constructor Also made the only implementations final --- pkgs/collection/CHANGELOG.md | 1 + pkgs/collection/lib/src/boollist.dart | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/collection/CHANGELOG.md b/pkgs/collection/CHANGELOG.md index 30f8c3f7a..0123f87fc 100644 --- a/pkgs/collection/CHANGELOG.md +++ b/pkgs/collection/CHANGELOG.md @@ -3,6 +3,7 @@ - Add `IterableMapEntryExtension` for working on `Map` as a list of pairs, using `Map.entries`. +- Explicitly mark `BoolList` as `abstract interface` - Address diagnostics from `strict_top_level_inference`. ## 1.19.1 diff --git a/pkgs/collection/lib/src/boollist.dart b/pkgs/collection/lib/src/boollist.dart index b026d858a..564ded126 100644 --- a/pkgs/collection/lib/src/boollist.dart +++ b/pkgs/collection/lib/src/boollist.dart @@ -10,7 +10,7 @@ import 'unmodifiable_wrappers.dart' show NonGrowableListMixin; /// A space-efficient list of boolean values. /// /// Uses list of integers as internal storage to reduce memory usage. -abstract /*mixin*/ class BoolList with ListMixin { +abstract interface class BoolList with ListMixin { static const int _entryShift = 5; static const int _bitsPerEntry = 32; @@ -180,7 +180,7 @@ abstract /*mixin*/ class BoolList with ListMixin { } } -class _GrowableBoolList extends BoolList { +final class _GrowableBoolList extends BoolList { static const int _growthFactor = 2; _GrowableBoolList._withCapacity(int length, int capacity) @@ -228,7 +228,8 @@ class _GrowableBoolList extends BoolList { } } -class _NonGrowableBoolList extends BoolList with NonGrowableListMixin { +final class _NonGrowableBoolList extends BoolList + with NonGrowableListMixin { _NonGrowableBoolList._withCapacity(int length, int capacity) : super._( Uint32List(BoolList._lengthInWords(capacity)), From c0d8bf81f473f33aa94787518b355400c0a3b678 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Wed, 2 Apr 2025 12:49:24 -0700 Subject: [PATCH 2/4] go with @sealed for BoolList for now --- pkgs/collection/lib/src/boollist.dart | 17 +++++++++++------ pkgs/collection/pubspec.yaml | 3 +++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pkgs/collection/lib/src/boollist.dart b/pkgs/collection/lib/src/boollist.dart index 564ded126..ccf8cdc8c 100644 --- a/pkgs/collection/lib/src/boollist.dart +++ b/pkgs/collection/lib/src/boollist.dart @@ -5,11 +5,15 @@ import 'dart:collection' show ListMixin; import 'dart:typed_data' show Uint32List; +import 'package:meta/meta.dart'; + import 'unmodifiable_wrappers.dart' show NonGrowableListMixin; /// A space-efficient list of boolean values. /// /// Uses list of integers as internal storage to reduce memory usage. +@sealed +// TODO: replace `interface` with `final` in the next major release. abstract interface class BoolList with ListMixin { static const int _entryShift = 5; @@ -119,9 +123,7 @@ abstract interface class BoolList with ListMixin { @override bool operator [](int index) { RangeError.checkValidIndex(index, this, 'index', _length); - return (_data[index >> _entryShift] & - (1 << (index & _entrySignBitIndex))) != - 0; + return _getBit(index); } @override @@ -167,6 +169,7 @@ abstract interface class BoolList with ListMixin { @override Iterator get iterator => _BoolListIterator(this); + /// Note: [index] is NOT checked for validity. void _setBit(int index, bool value) { if (value) { _data[index >> _entryShift] |= 1 << (index & _entrySignBitIndex); @@ -175,6 +178,10 @@ abstract interface class BoolList with ListMixin { } } + /// Note: [index] is NOT checked for validity. + bool _getBit(int index) => + (_data[index >> _entryShift] & (1 << (index & _entrySignBitIndex))) != 0; + static int _lengthInWords(int bitLength) { return (bitLength + (_bitsPerEntry - 1)) >> _entryShift; } @@ -263,9 +270,7 @@ class _BoolListIterator implements Iterator { if (_pos < _boolList.length) { var pos = _pos++; - _current = _boolList._data[pos >> BoolList._entryShift] & - (1 << (pos & BoolList._entrySignBitIndex)) != - 0; + _current = _boolList._getBit(pos); return true; } _current = false; diff --git a/pkgs/collection/pubspec.yaml b/pkgs/collection/pubspec.yaml index 26eb4b936..b6ba057ab 100644 --- a/pkgs/collection/pubspec.yaml +++ b/pkgs/collection/pubspec.yaml @@ -12,6 +12,9 @@ topics: environment: sdk: ^3.4.0 +dependencies: + meta: ^1.16.0 + dev_dependencies: benchmark_harness: ^2.3.1 dart_flutter_team_lints: ^3.0.0 From a0c328b3a0fb5778a10b601e3b0f105a0d47b73d Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Thu, 3 Apr 2025 15:16:54 -0700 Subject: [PATCH 3/4] fix comment --- pkgs/collection/lib/src/boollist.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/collection/lib/src/boollist.dart b/pkgs/collection/lib/src/boollist.dart index ccf8cdc8c..5ec5cdf8d 100644 --- a/pkgs/collection/lib/src/boollist.dart +++ b/pkgs/collection/lib/src/boollist.dart @@ -169,7 +169,7 @@ abstract interface class BoolList with ListMixin { @override Iterator get iterator => _BoolListIterator(this); - /// Note: [index] is NOT checked for validity. + // Note: [index] is NOT checked for validity. void _setBit(int index, bool value) { if (value) { _data[index >> _entryShift] |= 1 << (index & _entrySignBitIndex); @@ -178,7 +178,7 @@ abstract interface class BoolList with ListMixin { } } - /// Note: [index] is NOT checked for validity. + // Note: [index] is NOT checked for validity. bool _getBit(int index) => (_data[index >> _entryShift] & (1 << (index & _entrySignBitIndex))) != 0; From 57bc094194f1f2d24468ebd808e30decfe2b5be9 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Thu, 3 Apr 2025 15:19:48 -0700 Subject: [PATCH 4/4] prefer inline --- pkgs/collection/lib/src/boollist.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkgs/collection/lib/src/boollist.dart b/pkgs/collection/lib/src/boollist.dart index 5ec5cdf8d..6b973e03e 100644 --- a/pkgs/collection/lib/src/boollist.dart +++ b/pkgs/collection/lib/src/boollist.dart @@ -179,6 +179,9 @@ abstract interface class BoolList with ListMixin { } // Note: [index] is NOT checked for validity. + @pragma('dart2js:prefer-inline') + @pragma('vm:prefer-inline') + @pragma('wasm:prefer-inline') bool _getBit(int index) => (_data[index >> _entryShift] & (1 << (index & _entrySignBitIndex))) != 0;