Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 223033d

Browse files
committed
Avoid setRange with potentially incompatible types
Fixes #317
1 parent 1f5c234 commit 223033d

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Adds `shuffled` to `IterableExtension`.
44
- Shuffle `IterableExtension.sample` results.
5+
- Fix `mergeSort` when the runtime iterable generic is a subtype of the static
6+
generic.
57

68
## 1.18.0
79

lib/src/algorithms.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,9 @@ void _merge<E, K>(
394394
}
395395
// First list empties first. Reached by break above.
396396
target[targetOffset++] = secondElement;
397-
target.setRange(
398-
targetOffset, targetOffset + (secondEnd - cursor2), secondList, cursor2);
397+
for (var i = targetOffset; i < targetOffset + (secondEnd - cursor2); i++) {
398+
target[i] = secondList[(i - targetOffset) + cursor2];
399+
}
399400
}
400401

401402
/// Sort [elements] using a quick-sort algorithm.

test/algorithms_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,15 @@ void main() {
366366
reverse(l, 0, 6);
367367
expect(l, equals([2, 1, 4, 3, 6, 5]));
368368
});
369+
370+
test('mergeSort works when runtime generic is a subtype of the static type',
371+
() {
372+
// Regression test for https://github.com/dart-lang/collection/issues/317
373+
final length = 32; // _mergeSortLimit
374+
// In order list, first half empties first during merge.
375+
final list = List<int>.generate(length, (i) => i);
376+
expect(() => mergeSort<num>(list), returnsNormally);
377+
});
369378
}
370379

371380
class C {

0 commit comments

Comments
 (0)