Skip to content

Commit bf814cf

Browse files
eernstgcommit-bot@chromium.org
authored andcommitted
Add test for null spread element typing with null-safety
A spread element in a collection literal, e.g., `[...e]` or `{...?e}`, can have an `e` of type `Null` or a subtype thereof. This CL adds a test where this situation is created for different types (`Null`, `Never`, `X extends Null`) with different collection literals, and with and without elements of other types. In general, the spread does not contribute any elements, and hence the element/key/value type is `Never` such that it has no effect on the element/key/value type of the collection. Cf. dart-lang/language#1079. Change-Id: Ib760bcba18045b1afaa50f65c0e7d0a7fe9a8ab6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/154693 Commit-Queue: Erik Ernst <[email protected]> Reviewed-by: Nate Bosch <[email protected]>
1 parent e2f1460 commit bf814cf

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Test that the inferred element type, key type, and value type of a spread
6+
// element of the form `...?e` where `e` has type `Null` or a potentially
7+
// nullable subtype thereof is `Never`; and the element, key, and value types
8+
// are also `Never` for `...e` where the type of `e` is a subtype of `Never`.
9+
10+
import 'package:expect/expect.dart';
11+
12+
Function f = uncalled; // Do not optimize away `uncalled`.
13+
Never myNever = throw 1;
14+
15+
void uncalled<X extends Null>(X x) {
16+
// Test empty collection, involving only type `Never`.
17+
var l1 = [...?x];
18+
List<Never> l1b = l1;
19+
var l2 = [...myNever];
20+
List<Never> l2b = l2;
21+
var l3 = [...?myNever]; // Warning, but possible.
22+
List<Never> l3b = l3;
23+
var s1 = {...?x, if (false) throw 1};
24+
Set<Never> s1b = s1;
25+
var s2 = {...myNever, if (false) throw 1};
26+
Set<Never> s2b = s2;
27+
var s3 = {...?myNever, if (false) throw 1}; // Warning.
28+
Set<Never> s3b = s3;
29+
var m1 = {...?x, if (false) throw 1: throw 1};
30+
Map<Never, Never> m1b = m1;
31+
var m2 = {...myNever, if (false) throw 1: throw 1};
32+
Map<Never, Never> m2b = m2;
33+
var m3 = {...?myNever, if (false) throw 1: throw 1}; // Warning.
34+
Map<Never, Never> m3b = m3;
35+
36+
// Test non-empty collection of `Never` and `int`.
37+
var li1 = [...?x, 1];
38+
List<int> li1b = li1;
39+
var li2 = [...myNever, 1];
40+
List<int> li2b = li2;
41+
var li3 = [...?myNever, 1]; // Warning.
42+
List<int> li3b = li3;
43+
var si1 = {1, ...?x};
44+
Set<int> si1b = si1;
45+
var si2 = {1, ...myNever};
46+
Set<int> si2b = si2;
47+
var si3 = {1, ...?myNever}; // Warning.
48+
Set<int> si3b = si3;
49+
var mi1 = {1: 1, ...?x};
50+
Map<int, int> mi1b = mi1;
51+
var mi2 = {1: 1, ...myNever};
52+
Map<int, int> mi2b = mi2;
53+
var mi3 = {1: 1, ...?myNever}; // Warning.
54+
Map<int, int> mi3b = mi3;
55+
}
56+
57+
void main() {
58+
Null myNull = null;
59+
60+
// Test empty collection, involving only type `Never`.
61+
var l1 = [...?null];
62+
List<Never> l1b = l1;
63+
var l2 = [...?myNull];
64+
List<Never> l2b = l2;
65+
var s1 = {...?null, if (false) throw 1};
66+
Set<Never> s1b = s1;
67+
var s2 = {if (false) throw 1, ...?myNull};
68+
Set<Never> s2b = s2;
69+
var m1 = {...?null, if (false) throw 1: throw 1};
70+
Map<Never, Never> m1b = m1;
71+
var m2 = {if (false) throw 1: throw 1, ...?myNull};
72+
Map<Never, Never> m2b = m2;
73+
74+
// Test non-empty collection of `Never` and `int`.
75+
var li1 = [...?null, 1];
76+
List<int> li1b = li1;
77+
var li2 = [1, ...?myNull];
78+
List<int> li2b = li2;
79+
var si1 = {1, ...?null};
80+
Set<int> si1b = si1;
81+
var si2 = {...?myNull, 1};
82+
Set<int> si2b = si2;
83+
var mi1 = {1: 1, ...?null};
84+
Map<int, int> mi1b = mi1;
85+
var mi2 = {...?myNull, 1: 1};
86+
Map<int, int> mi2b = mi2;
87+
}

0 commit comments

Comments
 (0)