Skip to content

Commit ccb3801

Browse files
munificentcommit-bot@chromium.org
authored andcommitted
Migrate language_2/set_literals to NNBD.
Change-Id: Iac6e6f6e6fbd6b426398f0fca954d8a73cfcb2eb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150844 Auto-Submit: Bob Nystrom <[email protected]> Commit-Queue: Srujan Gaddam <[email protected]> Reviewed-by: Srujan Gaddam <[email protected]>
1 parent 348499d commit ccb3801

File tree

5 files changed

+401
-0
lines changed

5 files changed

+401
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright (c) 2018, 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+
import 'dart:async';
6+
7+
import "package:expect/expect.dart";
8+
9+
void main() {
10+
test();
11+
}
12+
13+
void test() {
14+
checkSet<T>(Object o, List elements) {
15+
Expect.type<Set<T>>(o);
16+
Set<T> set = o as Set<T>;
17+
Expect.listEquals(elements, set.toList());
18+
Expect.throws<Error>(set.clear);
19+
}
20+
21+
// Various context types for literals.
22+
Object setContext<T>(Set<T> object) => object;
23+
Object iterableContext<T>(Iterable<T> object) => object;
24+
Object foSetContext<T>(FutureOr<Set<T>> object) => object;
25+
Object foIterableContext<T>(FutureOr<Set<T>> object) => object;
26+
27+
// Empty literal, no type arguments.
28+
// No context.
29+
Expect.type<Map<dynamic, dynamic>>(const {});
30+
// Set context with no inferred type argument.
31+
checkSet<dynamic>(setContext(const {}), []);
32+
checkSet<dynamic>(iterableContext(const {}), []);
33+
checkSet<dynamic>(foSetContext(const {}), []);
34+
checkSet<dynamic>(foIterableContext(const {}), []);
35+
// Specific set context.
36+
checkSet<int>(setContext<int>(const {}), []);
37+
checkSet<int>(iterableContext<int>(const {}), []);
38+
checkSet<int>(foSetContext<int>(const {}), []);
39+
checkSet<int>(foIterableContext<int>(const {}), []);
40+
41+
// Non-empty set literal, no type argument.
42+
// No context.
43+
checkSet<int>(const {1}, [1]);
44+
checkSet<int>(const {3, 1, 2, 4}, [3, 1, 2, 4]);
45+
// Set context with no inferred type argument.
46+
checkSet<int>(setContext(const {1}), [1]);
47+
checkSet<int>(iterableContext(const {1}), [1]);
48+
checkSet<int>(foSetContext(const {1}), [1]);
49+
checkSet<int>(foIterableContext(const {1}), [1]);
50+
checkSet<int>(setContext(const {3, 1, 2, 4}), [3, 1, 2, 4]);
51+
checkSet<int>(iterableContext(const {3, 1, 2, 4}), [3, 1, 2, 4]);
52+
checkSet<int>(foSetContext(const {1}), [1]);
53+
checkSet<int>(foIterableContext(const {1}), [1]);
54+
// Specific set context.
55+
checkSet<num>(setContext<num>(const {1}), [1]);
56+
checkSet<num>(iterableContext<num>(const {1}), [1]);
57+
checkSet<num>(foSetContext<num>(const {1}), [1]);
58+
checkSet<num>(foIterableContext<num>(const {1}), [1]);
59+
checkSet<num>(setContext<num>(const {3, 1, 2, 4}), [3, 1, 2, 4]);
60+
checkSet<num>(iterableContext<num>(const {3, 1, 2, 4}), [3, 1, 2, 4]);
61+
checkSet<num>(foSetContext<num>(const {3, 1, 2, 4}), [3, 1, 2, 4]);
62+
checkSet<num>(foIterableContext<num>(const {3, 1, 2, 4}), [3, 1, 2, 4]);
63+
64+
// Non-empty set literal with type argument.
65+
checkSet<num>(const <num>{1}, [1]);
66+
checkSet<num>(const <num>{3, 1, 2, 4}, [3, 1, 2, 4]);
67+
68+
// Integers, String and symbols work, even if they override ==.
69+
checkSet<String>(const {"foo", "bar"}, ["foo", "bar"]);
70+
checkSet<Symbol>(const {#foo, #bar}, [#foo, #bar]);
71+
checkSet<Symbol>(const {#_foo, #_bar}, [#_foo, #_bar]);
72+
const object = Object();
73+
checkSet<Object>(const {#foo, 1, "string", object, true},
74+
[#foo, 1, "string", object, true]);
75+
76+
// Nested constant literals.
77+
const Object o = {{2}};
78+
Expect.type<Set<Set<int>>>(o);
79+
Set<Set<int>> set = o as Set<Set<int>>;
80+
Expect.equals(1, set.length);
81+
Expect.equals(1, set.first.length);
82+
Expect.equals(2, set.first.first);
83+
84+
const Object o2 = {{2}, <int>{}};
85+
Expect.type<Set<Set<int>>>(o);
86+
set = o2 as Set<Set<int>>;
87+
Expect.equals(2, set.length);
88+
Expect.equals(1, set.first.length);
89+
Expect.equals(2, set.first.first);
90+
91+
const Set<Set<int>> o3 = {{}};
92+
Expect.equals(1, o3.length);
93+
Expect.equals(0, o3.first.length);
94+
95+
const o4 = {{}};
96+
Expect.type<Set<Map<dynamic, dynamic>>>(o4);
97+
Expect.equals(1, o4.length);
98+
Expect.equals(0, o4.first.length);
99+
100+
const o5 = {{1}, {}}; // Set<Object>
101+
Expect.type<Set<Object>>(o5);
102+
Expect.notType<Set<Set<Object>>>(o5);
103+
104+
// User defined constant class.
105+
const o6 = {
106+
Something(1, "a"),
107+
Something(2, "a"),
108+
Something(1, "b"),
109+
Something(2, "b"),
110+
};
111+
Expect.equals("1:a,2:a,1:b,2:b", o6.toList().join(","));
112+
113+
// Canonicalization of constant sets takes ordering into account,
114+
// that is, o7 and o8 cannot be the same object.
115+
const o7 = {1, 2, 3};
116+
const o8 = {3, 2, 1};
117+
Expect.notIdentical(o7, o8);
118+
// But o7 and o9 must be identical.
119+
const o9 = {1, 2, 3};
120+
Expect.identical(o7, o9);
121+
}
122+
123+
class Something {
124+
final int id;
125+
final String name;
126+
const Something(this.id, this.name);
127+
String toString() => "$id:$name";
128+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2019, 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+
@Meta({})
6+
import 'dart:core';
7+
8+
@Meta({})
9+
void f(@Meta({}) int foo) {}
10+
11+
@Meta({})
12+
class A<@Meta({}) T> {
13+
@Meta({})
14+
String? x, y;
15+
16+
@Meta({})
17+
A();
18+
19+
@Meta({})
20+
void m() {
21+
@Meta({})
22+
int z;
23+
}
24+
}
25+
26+
@Meta({})
27+
enum E {
28+
@Meta({})
29+
v
30+
}
31+
32+
class Meta {
33+
final Set<int> value;
34+
35+
const Meta(this.value);
36+
}
37+
38+
main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2019, 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+
class Bag {
6+
final Set<Object> things;
7+
Bag({Set<Object>? things}) : this.things = things ?? <Object>{};
8+
Bag.full({Set<Object> this.things = const {"cat"}});
9+
}
10+
11+
main() {
12+
new Bag();
13+
new Bag(things: {});
14+
new Bag.full();
15+
new Bag.full(things: {});
16+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2018, 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+
import "dart:collection" show HashSet, LinkedHashSet;
6+
7+
import "package:expect/expect.dart";
8+
9+
const Object d = 3.5;
10+
11+
void main() {
12+
var o //
13+
= <int>{1: 1} //# 01: compile-time error
14+
= <int, int, int>{} //# 02: compile-time error
15+
= <int, int, int>{1} //# 03: compile-time error
16+
= <int, int>{1} //# 04: compile-time error
17+
= const <int, int, int>{} //# 05: compile-time error
18+
= const <int, int, int>{1} //# 06: compile-time error
19+
= const <int, int>{1} //# 07: compile-time error
20+
= const {Duration(seconds: 0)} // Overrides ==. //# 08: compile-time error
21+
= const {4.2} // Overrides ==. //# 09: compile-time error
22+
= const {d} // Overrides ==. //# 10: compile-time error
23+
= {,} //# 11: syntax error
24+
= {1,,} //# 12: syntax error
25+
= {1,,1} //# 13: syntax error
26+
;
27+
Expect.isNull(o); // Should be unreachable with a value.
28+
29+
Set<int>? s //
30+
= {"not int"} //# 14: compile-time error
31+
= {4.2} //# 15: compile-time error
32+
= {1: 1} //# 16: compile-time error
33+
= {{}} //# 17: compile-time error
34+
;
35+
Expect.isNull(s);
36+
37+
Set<Set<Object>>? ss //
38+
= {{1: 1}} //# 19: compile-time error
39+
= {<int, int>{}} //# 20: compile-time error
40+
= {<int>{1: 1}} //# 21: compile-time error
41+
= const {ss} //# 22: compile-time error
42+
;
43+
Expect.isNull(ss);
44+
45+
HashSet<int>? hs //
46+
= {} // Exact type is LinkedHashSet //# 23: compile-time error
47+
;
48+
Expect.isNull(hs);
49+
50+
<T>(x) {
51+
// Type constants are allowed, type variables are not.
52+
var o //
53+
= const {T} //# 26: compile-time error
54+
= const {x} //# 27: compile-time error
55+
;
56+
Expect.isNull(o);
57+
}<int>(42);
58+
59+
<T extends Set<num>>() {
60+
// Regression test for http://dartbug.com/35300.
61+
// The `Set<Null>` type is not assignable to `T extends Set<num>`,
62+
// so we don't make this a set. You can't assign a map to `T`.
63+
T o //
64+
= {}; //# 28: compile-time error
65+
;
66+
}();
67+
68+
// Constant sets must not contain equal elements.
69+
const cs = {
70+
1,
71+
"s",
72+
#foo,
73+
int,
74+
C(1),
75+
{1},
76+
1, //# 29: compile-time error
77+
"s", //# 30: compile-time error
78+
#foo, //# 31: compile-time error
79+
int, //# 32: compile-time error
80+
C(1), //# 33: compile-time error
81+
{1}, //# 34: compile-time error
82+
};
83+
}
84+
85+
class C {
86+
final Object id;
87+
const C(this.id);
88+
}

0 commit comments

Comments
 (0)