Skip to content

Commit eeca7c5

Browse files
mkustermanncommit-bot@chromium.org
authored andcommitted
[vm/nnbd] Second set of changes for migrating VM patch files for NNBD
Issue #39754 Change-Id: Icfbeed29a2c81c3b118686a14071d5156452e352 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/130370 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Lasse R.H. Nielsen <[email protected]>
1 parent a312a84 commit eeca7c5

11 files changed

+69
-60
lines changed

sdk_nnbd/lib/_internal/vm/lib/array.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class _List<E> extends FixedLengthListBase<E> {
2424
int get length native "List_getLength";
2525

2626
@pragma("vm:prefer-inline")
27-
List _slice(int start, int count, bool needsTypeArgument) {
27+
_List _slice(int start, int count, bool needsTypeArgument) {
2828
if (count <= 64) {
2929
final result = needsTypeArgument ? new _List<E>(count) : new _List(count);
3030
for (int i = 0; i < result.length; i++) {
@@ -77,7 +77,7 @@ class _List<E> extends FixedLengthListBase<E> {
7777
if (identical(this, iterable)) {
7878
iterableAsList = this;
7979
} else if (ClassID.getID(iterable) == ClassID.cidArray) {
80-
iterableAsList = iterable;
80+
iterableAsList = unsafeCast<_List<E>>(iterable);
8181
} else if (iterable is List<E>) {
8282
iterableAsList = iterable;
8383
} else {
@@ -95,8 +95,8 @@ class _List<E> extends FixedLengthListBase<E> {
9595

9696
List<E> sublist(int start, [int? end]) {
9797
final int listLength = this.length;
98-
end = RangeError.checkValidRange(start, end, listLength);
99-
int length = end - start;
98+
final int actualEnd = RangeError.checkValidRange(start, end, listLength);
99+
int length = actualEnd - start;
100100
if (length == 0) return <E>[];
101101
var result = new _GrowableList<E>._withData(_slice(start, length, false));
102102
result._setLength(length);
@@ -137,11 +137,11 @@ class _List<E> extends FixedLengthListBase<E> {
137137
List<E> toList({bool growable: true}) {
138138
var length = this.length;
139139
if (length > 0) {
140-
var result = _slice(0, length, !growable);
140+
_List result = _slice(0, length, !growable);
141141
if (growable) {
142-
result = new _GrowableList<E>._withData(result).._setLength(length);
142+
return new _GrowableList<E>._withData(result).._setLength(length);
143143
}
144-
return result;
144+
return unsafeCast<_List<E>>(result);
145145
}
146146
// _GrowableList._withData must not be called with empty list.
147147
return growable ? <E>[] : new List<E>(0);
@@ -171,9 +171,9 @@ class _ImmutableList<E> extends UnmodifiableListBase<E> {
171171
@pragma("vm:prefer-inline")
172172
int get length native "List_getLength";
173173

174-
List<E> sublist(int start, [int end]) {
175-
end = RangeError.checkValidRange(start, end, this.length);
176-
int length = end - start;
174+
List<E> sublist(int start, [int? end]) {
175+
final int actualEnd = RangeError.checkValidRange(start, end, this.length);
176+
int length = actualEnd - start;
177177
if (length == 0) return <E>[];
178178
List list = new _List(length);
179179
for (int i = 0; i < length; i++) {

sdk_nnbd/lib/_internal/vm/lib/array_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class List<E> {
1212
}
1313

1414
@patch
15-
factory List([int length]) native "List_new";
15+
factory List([int? length]) native "List_new";
1616

1717
@patch
1818
factory List.filled(int length, E fill, {bool growable: false}) {

sdk_nnbd/lib/_internal/vm/lib/bigint_patch.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class _BigIntImpl implements BigInt {
184184
// Read in the source 9 digits at a time.
185185
// The first part may have a few leading virtual '0's to make the remaining
186186
// parts all have exactly 9 digits.
187-
int digitInPartCount = 9 - source.length.remainder(9);
187+
int digitInPartCount = 9 - unsafeCast<int>(source.length.remainder(9));
188188
if (digitInPartCount == 9) digitInPartCount = 0;
189189
for (int i = 0; i < source.length; i++) {
190190
part = part * 10 + source.codeUnitAt(i) - _0;
@@ -322,8 +322,8 @@ class _BigIntImpl implements BigInt {
322322
}
323323

324324
// The RegExp guarantees that one of the 3 matches is non-null.
325-
final match = (decimalMatch ?? nonDecimalMatch ?? hexMatch)!;
326-
return _parseRadix(match, radix, isNegative);
325+
final nonNullMatch = (decimalMatch ?? nonDecimalMatch ?? hexMatch)!;
326+
return _parseRadix(nonNullMatch, radix, isNegative);
327327
}
328328

329329
static RegExp _parseRE = RegExp(
@@ -370,10 +370,13 @@ class _BigIntImpl implements BigInt {
370370
if (value == 1) return one;
371371
if (value == 2) return two;
372372

373-
if (value.abs() < 0x100000000)
373+
if (value.abs() < 0x100000000) {
374374
return new _BigIntImpl._fromInt(value.toInt());
375-
if (value is double) return new _BigIntImpl._fromDouble(value);
376-
return new _BigIntImpl._fromInt(value);
375+
}
376+
if (value is double) {
377+
return new _BigIntImpl._fromDouble(value);
378+
}
379+
return new _BigIntImpl._fromInt(value as int);
377380
}
378381

379382
factory _BigIntImpl._fromInt(int value) {

sdk_nnbd/lib/_internal/vm/lib/date_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class DateTime {
277277

278278
/// Converts the given broken down date to microseconds.
279279
@patch
280-
static int _brokenDownDateToValue(int year, int month, int day, int hour,
280+
static int? _brokenDownDateToValue(int year, int month, int day, int hour,
281281
int minute, int second, int millisecond, int microsecond, bool isUtc) {
282282
// Simplify calculations by working with zero-based month.
283283
--month;

sdk_nnbd/lib/_internal/vm/lib/double_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class double {
7171
i++;
7272
if (i == end) return null;
7373
// int._tryParseSmi treats its end argument as inclusive.
74-
int expPart = int._tryParseSmi(str, i, end - 1);
74+
final int? expPart = int._tryParseSmi(str, i, end - 1);
7575
if (expPart == null) return null;
7676
exponent += expPart;
7777
break;

sdk_nnbd/lib/_internal/vm/lib/growable_array.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ class _GrowableList<T> extends ListBase<T> {
7777
this.length = this.length - (end - start);
7878
}
7979

80-
List<T> sublist(int start, [int end]) {
81-
end = RangeError.checkValidRange(start, end, this.length);
82-
int length = end - start;
80+
List<T> sublist(int start, [int? end]) {
81+
final int actualEnd = RangeError.checkValidRange(start, end, this.length);
82+
int length = actualEnd - start;
8383
if (length == 0) return <T>[];
8484
List list = new _List(length);
8585
for (int i = 0; i < length; i++) {

sdk_nnbd/lib/_internal/vm/lib/integers.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ abstract class _IntegerImplementation implements int {
3939
@pragma("vm:non-nullable-result-type")
4040
@pragma("vm:never-inline")
4141
int operator -() {
42-
return 0 - this;
42+
// Issue(https://dartbug.com/39639): The analyzer incorrectly reports the
43+
// result type as `num`.
44+
return unsafeCast<int>(0 - this);
4345
}
4446

4547
@pragma("vm:non-nullable-result-type")
@@ -79,7 +81,9 @@ abstract class _IntegerImplementation implements int {
7981
@pragma("vm:non-nullable-result-type")
8082
int _moduloFromInteger(int other) native "Integer_moduloFromInteger";
8183
int _remainderFromInteger(int other) {
82-
return other - (other ~/ this) * this;
84+
// Issue(https://dartbug.com/39639): The analyzer incorrectly reports the
85+
// result type as `num`.
86+
return unsafeCast<int>(other - (other ~/ this) * this);
8387
}
8488

8589
@pragma("vm:non-nullable-result-type")
@@ -351,7 +355,7 @@ abstract class _IntegerImplementation implements int {
351355
int value = this;
352356
assert(value < 0);
353357
do {
354-
int digit = -value.remainder(radix);
358+
int digit = -unsafeCast<int>(value.remainder(radix));
355359
value ~/= radix;
356360
temp.add(_digits.codeUnitAt(digit));
357361
} while (value != 0);
@@ -605,7 +609,11 @@ class _Smi extends _IntegerImplementation {
605609
}
606610

607611
String toString() {
608-
if (this < 100 && this > -100) return _smallLookupTable[this + 99];
612+
if (this < 100 && this > -100) {
613+
// Issue(https://dartbug.com/39639): The analyzer incorrectly reports the
614+
// result type as `num`.
615+
return _smallLookupTable[unsafeCast<int>(this + 99)];
616+
}
609617
if (this < 0) return _negativeToString(this);
610618
// Inspired by Andrei Alexandrescu: "Three Optimization Tips for C++"
611619
// Avoid expensive remainder operation by doing it on more than
@@ -614,11 +622,11 @@ class _Smi extends _IntegerImplementation {
614622
int length = _positiveBase10Length(this);
615623
_OneByteString result = _OneByteString._allocate(length);
616624
int index = length - 1;
617-
var smi = this;
625+
_Smi smi = this;
618626
do {
619627
// Two digits at a time.
620-
var twoDigits = smi.remainder(100);
621-
smi = smi ~/ 100;
628+
final int twoDigits = unsafeCast<int>(smi.remainder(100));
629+
smi = unsafeCast<_Smi>(smi ~/ 100);
622630
int digitIndex = twoDigits * 2;
623631
result._setAt(index, _digitTable[digitIndex + 1]);
624632
result._setAt(index - 1, _digitTable[digitIndex]);

sdk_nnbd/lib/_internal/vm/lib/integers_patch.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class int {
1818
int _shrFromInteger(int other);
1919
int _shlFromInteger(int other);
2020

21-
static int _tryParseSmi(String str, int first, int last) {
21+
static int? _tryParseSmi(String str, int first, int last) {
2222
assert(first <= last);
2323
var ix = first;
2424
var sign = 1;
@@ -54,7 +54,7 @@ class int {
5454
}
5555
if (radix == null || radix == 10) {
5656
// Try parsing immediately, without trimming whitespace.
57-
int result = _tryParseSmi(source, 0, source.length - 1);
57+
int? result = _tryParseSmi(source, 0, source.length - 1);
5858
if (result != null) return result;
5959
} else if (radix < 2 || radix > 36) {
6060
throw new RangeError("Radix $radix not in range 2..36");
@@ -106,7 +106,7 @@ class int {
106106
if (source.isEmpty) return null;
107107
if (radix == null || radix == 10) {
108108
// Try parsing immediately, without trimming whitespace.
109-
int result = _tryParseSmi(source, 0, source.length - 1);
109+
int? result = _tryParseSmi(source, 0, source.length - 1);
110110
if (result != null) return result;
111111
} else if (radix < 2 || radix > 36) {
112112
throw new RangeError("Radix $radix not in range 2..36");
@@ -137,7 +137,7 @@ class int {
137137
int blockSize = _PARSE_LIMITS[tableIndex];
138138
int length = end - start;
139139
if (length <= blockSize) {
140-
_Smi? smi = _parseBlock(source, radix, start, end);
140+
int? smi = _parseBlock(source, radix, start, end);
141141
if (smi == null) {
142142
return _throwFormatException(onError, source, start, radix, null);
143143
}
@@ -152,7 +152,7 @@ class int {
152152
int result = 0;
153153
if (smallBlockSize > 0) {
154154
int blockEnd = start + smallBlockSize;
155-
_Smi? smi = _parseBlock(source, radix, start, blockEnd);
155+
int? smi = _parseBlock(source, radix, start, blockEnd);
156156
if (smi == null) {
157157
return _throwFormatException(onError, source, start, radix, null);
158158
}
@@ -170,7 +170,7 @@ class int {
170170
negativeOverflowLimit = _int64OverflowLimits[tableIndex + 1];
171171
int blockEnd = start + blockSize;
172172
do {
173-
_Smi? smi = _parseBlock(source, radix, start, blockEnd);
173+
int? smi = _parseBlock(source, radix, start, blockEnd);
174174
if (smi == null) {
175175
return _throwFormatException(onError, source, start, radix, null);
176176
}
@@ -299,8 +299,10 @@ class int {
299299
static int _initInt64OverflowLimits(int tableIndex, int multiplier) {
300300
_int64OverflowLimits[tableIndex] = _maxInt64 ~/ multiplier;
301301
_int64OverflowLimits[tableIndex + 1] = _minInt64 ~/ multiplier;
302-
_int64OverflowLimits[tableIndex + 2] = _maxInt64.remainder(multiplier);
303-
_int64OverflowLimits[tableIndex + 3] = -(_minInt64.remainder(multiplier));
302+
_int64OverflowLimits[tableIndex + 2] =
303+
unsafeCast<int>(_maxInt64.remainder(multiplier));
304+
_int64OverflowLimits[tableIndex + 3] =
305+
-unsafeCast<int>(_minInt64.remainder(multiplier));
304306
return _int64OverflowLimits[tableIndex];
305307
}
306308
}

sdk_nnbd/lib/_internal/vm/lib/regexp_patch.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class RegExp {
1414
bool dotAll: false}) {
1515
_RegExpHashKey key =
1616
new _RegExpHashKey(source, multiLine, caseSensitive, unicode, dotAll);
17-
_RegExpHashValue value = _cache[key];
17+
_RegExpHashValue? value = _cache[key];
1818

1919
if (value == null) {
2020
if (_cache.length > _MAX_CACHE_SIZE) {
@@ -216,7 +216,7 @@ class _RegExp implements RegExp {
216216
bool unicode: false,
217217
bool dotAll: false}) native "RegExp_factory";
218218

219-
RegExpMatch firstMatch(String str) {
219+
RegExpMatch? firstMatch(String str) {
220220
if (str is! String) throw new ArgumentError(str);
221221
List match = _ExecuteMatch(str, 0);
222222
if (match == null) {

sdk_nnbd/lib/_internal/vm/lib/stopwatch_patch.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Stopwatch {
3131
}
3232
// Multiplication would have overflowed.
3333
int ticksPerSecond = ticks ~/ _frequency;
34-
int remainingTicks = ticks.remainder(_frequency);
34+
int remainingTicks = unsafeCast<int>(ticks.remainder(_frequency));
3535
return ticksPerSecond * 1000000 + (remainingTicks * 1000000) ~/ _frequency;
3636
}
3737

@@ -46,7 +46,7 @@ class Stopwatch {
4646
}
4747
// Multiplication would have overflowed.
4848
int ticksPerSecond = ticks ~/ _frequency;
49-
int remainingTicks = ticks.remainder(_frequency);
49+
int remainingTicks = unsafeCast<int>(ticks.remainder(_frequency));
5050
return ticksPerSecond * 1000 + (remainingTicks * 1000) ~/ _frequency;
5151
}
5252
}

sdk_nnbd/lib/_internal/vm/lib/string_patch.dart

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ const int _maxUnicode = 0x10ffff;
1313
class String {
1414
@patch
1515
factory String.fromCharCodes(Iterable<int> charCodes,
16-
[int start = 0, int end]) {
17-
if (charCodes is! Iterable)
18-
throw new ArgumentError.value(charCodes, "charCodes");
19-
if (start is! int) throw new ArgumentError.value(start, "start");
20-
if (end != null && end is! int) throw new ArgumentError.value(end, "end");
16+
[int start = 0, int? end]) {
17+
if (end != null) throw new ArgumentError.value(end, "end");
2118
return _StringBase.createFromCharCodes(charCodes, start, end, null);
2219
}
2320

@@ -119,42 +116,41 @@ abstract class _StringBase implements String {
119116
* It's `null` if unknown.
120117
*/
121118
static String createFromCharCodes(
122-
Iterable<int> charCodes, int start, int end, int limit) {
123-
if (start == null) throw new ArgumentError.notNull("start");
124-
if (charCodes == null) throw new ArgumentError(charCodes);
119+
Iterable<int> charCodes, int start, int? end, int? limit) {
125120
// TODO(srdjan): Also skip copying of wide typed arrays.
126121
final ccid = ClassID.getID(charCodes);
127122
if ((ccid != ClassID.cidArray) &&
128123
(ccid != ClassID.cidGrowableObjectArray) &&
129124
(ccid != ClassID.cidImmutableArray)) {
130125
if (charCodes is Uint8List) {
131-
end = RangeError.checkValidRange(start, end, charCodes.length);
132-
return _createOneByteString(charCodes, start, end - start);
126+
final actualEnd = RangeError.checkValidRange(start, end, charCodes.length);
127+
return _createOneByteString(charCodes, start, actualEnd - start);
133128
} else if (charCodes is! Uint16List) {
134129
return _createStringFromIterable(charCodes, start, end);
135130
}
136131
}
137-
int codeCount = charCodes.length;
138-
end = RangeError.checkValidRange(start, end, codeCount);
139-
final len = end - start;
132+
final int codeCount = charCodes.length;
133+
final int actualEnd = RangeError.checkValidRange(start, end, codeCount);
134+
final len = actualEnd - start;
140135
if (len == 0) return "";
141136

142137
final typedCharCodes = unsafeCast<List<int>>(charCodes);
143138

144-
limit ??= _scanCodeUnits(typedCharCodes, start, end);
145-
if (limit < 0) {
139+
final int actualLimit = limit ?? _scanCodeUnits(typedCharCodes, start, actualEnd);
140+
if (actualLimit < 0) {
146141
throw new ArgumentError(typedCharCodes);
147142
}
148-
if (limit <= _maxLatin1) {
143+
if (actualLimit <= _maxLatin1) {
149144
return _createOneByteString(typedCharCodes, start, len);
150145
}
151-
if (limit <= _maxUtf16) {
152-
return _TwoByteString._allocateFromTwoByteList(typedCharCodes, start, end);
146+
if (actualLimit <= _maxUtf16) {
147+
return _TwoByteString._allocateFromTwoByteList(typedCharCodes, start,
148+
actualEnd);
153149
}
154150
// TODO(lrn): Consider passing limit to _createFromCodePoints, because
155151
// the function is currently fully generic and doesn't know that its
156152
// charCodes are not all Latin-1 or Utf-16.
157-
return _createFromCodePoints(typedCharCodes, start, end);
153+
return _createFromCodePoints(typedCharCodes, start, actualEnd);
158154
}
159155

160156
static int _scanCodeUnits(List<int> charCodes, int start, int end) {

0 commit comments

Comments
 (0)