Skip to content

Commit e736495

Browse files
munificentcommit-bot@chromium.org
authored andcommitted
Migrate language_2/const to NNBD.
Change-Id: I464275799b3e7a21326d35503b776f9c1101a599 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/141764 Commit-Queue: Bob Nystrom <rnystrom@google.com> Reviewed-by: Bob Nystrom <rnystrom@google.com> Reviewed-by: Erik Ernst <eernst@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com>
1 parent 1d44ef0 commit e736495

File tree

88 files changed

+3134
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+3134
-14
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
/// Implicit casts in constants are supported and treated as compile-time errors
6+
/// if they are not valid.
7+
8+
class A {
9+
final int n;
10+
const A(dynamic input) : n = input;
11+
}
12+
13+
main() {
14+
print(const A(2)); //# 01: ok
15+
print(const A('2')); //# 02: compile-time error
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
/// Explicit casts in constants are supported and treated as compile-time errors
6+
/// if they are not valid.
7+
8+
class A {
9+
final int n;
10+
const A(dynamic input) : n = input as int;
11+
}
12+
13+
main() {
14+
print(const A(2)); //# 01: ok
15+
print(const A('2')); //# 02: compile-time error
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
/// Casts in constants correctly substitute type variables.
6+
7+
class A {
8+
const A();
9+
}
10+
11+
class B implements A {
12+
const B();
13+
}
14+
15+
class M<T extends A> {
16+
final T a;
17+
const M(dynamic t) : a = t; // adds implicit cast `as T`
18+
}
19+
20+
main() {
21+
print(const M<B>(const B()));
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
/// Casts in constants correctly substitute type variables.
6+
7+
class A {
8+
const A();
9+
}
10+
11+
class B implements A {
12+
const B();
13+
}
14+
15+
class M<T extends A> {
16+
final T a;
17+
const M(dynamic t) : a = t; // adds implicit cast `as T`
18+
}
19+
20+
class N<S extends A> extends M<S> {
21+
const N(dynamic t) : super(t);
22+
}
23+
24+
main() {
25+
print(const N<B>(const B()));
26+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// TODO(multitest): This was automatically migrated from a multitest and may
2+
// contain strange or dead code.
3+
4+
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
5+
// for details. All rights reserved. Use of this source code is governed by a
6+
// BSD-style license that can be found in the LICENSE file.
7+
8+
// Test for conditionals as compile-time constants.
9+
10+
import 'package:expect/expect.dart';
11+
12+
class Marker {
13+
final field;
14+
const Marker(this.field);
15+
}
16+
17+
var var0 = const Marker(0);
18+
var var1 = const Marker(1);
19+
const const0 = const Marker(0);
20+
const const1 = const Marker(1);
21+
22+
const trueConst = true;
23+
const falseConst = false;
24+
var nonConst = true;
25+
const zeroConst = 0;
26+
27+
const cond1 = trueConst ? const0 : const1;
28+
29+
30+
31+
const cond2 = falseConst ? const0 : const1;
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
void main() {
44+
Expect.identical(var0, cond1);
45+
46+
47+
48+
Expect.identical(var1, cond2);
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright (c) 2013, 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 for conditionals as compile-time constants.
6+
7+
import 'package:expect/expect.dart';
8+
9+
class Marker {
10+
final field;
11+
const Marker(this.field);
12+
}
13+
14+
var var0 = const Marker(0);
15+
var var1 = const Marker(1);
16+
const const0 = const Marker(0);
17+
const const1 = const Marker(1);
18+
19+
const trueConst = true;
20+
const falseConst = false;
21+
var nonConst = true;
22+
const zeroConst = 0;
23+
24+
const cond1 = trueConst ? const0 : const1;
25+
const cond1a = trueConst ? nonConst : const1;
26+
// ^
27+
// [cfe] Constant evaluation error:
28+
// ^^^^^^^^
29+
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
30+
// [cfe] Not a constant expression.
31+
const cond1b = trueConst ? const0 : nonConst;
32+
// ^^^^^^^^
33+
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
34+
// [cfe] Not a constant expression.
35+
36+
const cond2 = falseConst ? const0 : const1;
37+
const cond2a = falseConst ? nonConst : const1;
38+
// ^^^^^^^^
39+
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
40+
// [cfe] Not a constant expression.
41+
const cond2b = falseConst ? const0 : nonConst;
42+
// ^
43+
// [cfe] Constant evaluation error:
44+
// ^^^^^^^^
45+
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
46+
// [cfe] Not a constant expression.
47+
48+
const cond3 = nonConst ? const0 : const1;
49+
// ^^^^^^^^
50+
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
51+
// [cfe] Not a constant expression.
52+
// ^
53+
// [cfe] Constant evaluation error:
54+
const cond3a = nonConst ? nonConst : const1;
55+
// ^^^^^^^^
56+
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
57+
// [cfe] Not a constant expression.
58+
// ^
59+
// [cfe] Constant evaluation error:
60+
// ^
61+
// [cfe] Not a constant expression.
62+
const cond3b = nonConst ? const0 : nonConst;
63+
// ^^^^^^^^
64+
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
65+
// [cfe] Not a constant expression.
66+
// ^
67+
// [cfe] Constant evaluation error:
68+
// ^
69+
// [cfe] Not a constant expression.
70+
71+
const cond4 = zeroConst ? const0 : const1;
72+
// ^^^^^^^^^
73+
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_TYPE_BOOL
74+
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
75+
// ^^^^^^^^^
76+
// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION
77+
const cond4a = zeroConst ? nonConst : const1;
78+
// ^^^^^^^^^
79+
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_TYPE_BOOL
80+
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
81+
// ^^^^^^^^^
82+
// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION
83+
// ^
84+
// [cfe] Not a constant expression.
85+
const cond4b = zeroConst ? const0 : nonConst;
86+
// ^^^^^^^^^
87+
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_TYPE_BOOL
88+
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
89+
// ^^^^^^^^^
90+
// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION
91+
// ^
92+
// [cfe] Not a constant expression.
93+
94+
void main() {
95+
Expect.identical(var0, cond1);
96+
Expect.identical(nonConst, cond1a);
97+
Expect.identical(var0, cond1b);
98+
99+
Expect.identical(var1, cond2);
100+
Expect.identical(var1, cond2a);
101+
Expect.identical(nonConst, cond2b);
102+
103+
Expect.identical(var0, cond3);
104+
Expect.identical(nonConst, cond3a);
105+
Expect.identical(var0, cond3b);
106+
107+
Expect.identical(var1, cond4);
108+
Expect.identical(var1, cond4a);
109+
Expect.identical(nonConst, cond4b);
110+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright (c) 2011, 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+
// VMOptions=
5+
// VMOptions=--compile_all
6+
7+
// Exercises language constructs that require compile time constants
8+
9+
// Initialize with different literal types
10+
const b = true;
11+
const s = "apple";
12+
const i = 1;
13+
const d = 3.3;
14+
const h = 0xf;
15+
const n = null;
16+
const aList = const [1, 2, 3]; // array literal
17+
const aMap = const {"1": "one", "2": "banana"}; // map literal
18+
19+
const INT_LIT = 5;
20+
const INT_LIT_REF = INT_LIT;
21+
const DOUBLE_LIT = 1.5;
22+
const BOOL_LIT = true;
23+
const STRING_LIT = "Hello";
24+
25+
const BOP1_0 = INT_LIT + 1;
26+
const BOP1_1 = 1 + INT_LIT;
27+
const BOP1_2 = INT_LIT - 1;
28+
const BOP1_3 = 1 - INT_LIT;
29+
const BOP1_4 = INT_LIT * 1;
30+
const BOP1_5 = 1 * INT_LIT;
31+
const BOP1_6 = INT_LIT / 1;
32+
const BOP1_7 = 1 / INT_LIT;
33+
const BOP2_0 = DOUBLE_LIT + 1.5;
34+
const BOP2_1 = 1.5 + DOUBLE_LIT;
35+
const BOP2_2 = DOUBLE_LIT - 1.5;
36+
const BOP2_3 = 1.5 - DOUBLE_LIT;
37+
const BOP2_4 = DOUBLE_LIT * 1.5;
38+
const BOP2_5 = 1.5 * DOUBLE_LIT;
39+
const BOP2_6 = DOUBLE_LIT / 1.5;
40+
const BOP2_7 = 1.5 / DOUBLE_LIT;
41+
const BOP3_0 = 2 < INT_LIT;
42+
const BOP3_1 = INT_LIT < 2;
43+
const BOP3_2 = 2 > INT_LIT;
44+
const BOP3_3 = INT_LIT > 2;
45+
const BOP3_4 = 2 < DOUBLE_LIT;
46+
const BOP3_5 = DOUBLE_LIT < 2;
47+
const BOP3_6 = 2 > DOUBLE_LIT;
48+
const BOP3_7 = DOUBLE_LIT > 2;
49+
const BOP3_8 = 2 <= INT_LIT;
50+
const BOP3_9 = INT_LIT <= 2;
51+
const BOP3_10 = 2 >= INT_LIT;
52+
const BOP3_11 = INT_LIT >= 2;
53+
const BOP3_12 = 2.0 <= DOUBLE_LIT;
54+
const BOP3_13 = DOUBLE_LIT <= 2.0;
55+
const BOP3_14 = 2.0 >= DOUBLE_LIT;
56+
const BOP3_15 = DOUBLE_LIT >= 2;
57+
const BOP4_0 = 5 % INT_LIT;
58+
const BOP4_1 = INT_LIT % 5;
59+
const BOP4_2 = 5.0 % DOUBLE_LIT;
60+
const BOP4_3 = DOUBLE_LIT % 5.0;
61+
const BOP5_0 = 0x80 & 0x04;
62+
const BOP5_1 = 0x80 | 0x04;
63+
const BOP5_2 = 0x80 << 0x04;
64+
const BOP5_3 = 0x80 >> 0x04;
65+
const BOP5_4 = 0x80 ~/ 0x04;
66+
const BOP5_5 = 0x80 ^ 0x04;
67+
const BOP6 = BOOL_LIT && true;
68+
const BOP7 = false || BOOL_LIT;
69+
const BOP8 = STRING_LIT == "World!";
70+
const BOP9 = "Hello" != STRING_LIT;
71+
const BOP10 = INT_LIT == INT_LIT_REF;
72+
const BOP11 = BOOL_LIT != true;
73+
74+
// Multiple binary expressions
75+
const BOP20 = 1 * INT_LIT / 3 + INT_LIT + 9;
76+
77+
// Parenthised expressions
78+
const BOP30 = (1 > 2);
79+
const BOP31 = (1 * 2) + 3;
80+
const BOP32 = 3 + (1 * 2);
81+
82+
// Unary expressions
83+
const UOP1_0 = !BOOL_LIT;
84+
const UOP1_1 = BOOL_LIT || !true;
85+
const UOP1_2 = !BOOL_LIT || true;
86+
const UOP1_3 = !(BOOL_LIT && true);
87+
const UOP2_0 = ~0xf0;
88+
const UOP2_1 = ~INT_LIT;
89+
const UOP2_2 = ~INT_LIT & 123;
90+
const UOP2_3 = ~(INT_LIT | 0xff);
91+
const UOP3_0 = -0xf0;
92+
const UOP3_1 = -INT_LIT;
93+
const UOP3_2 = -INT_LIT + 123;
94+
const UOP3_3 = -(INT_LIT * 0xff);
95+
const UOP3_4 = -0xf0;
96+
const UOP3_5 = -DOUBLE_LIT;
97+
const UOP3_6 = -DOUBLE_LIT + 123;
98+
const UOP3_7 = -(DOUBLE_LIT * 0xff);
99+
100+
class A {
101+
const A();
102+
static const a = const A(); // Assignment from Constant constructor OK
103+
}
104+
105+
main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2011, 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+
// Check that initializers of const fields can be declared out of order.
5+
6+
import "package:expect/expect.dart";
7+
8+
const P = 2 * (O - N);
9+
const N = 1;
10+
const O = 1 + 3;
11+
12+
void main() {
13+
Expect.equals(1, N);
14+
Expect.equals(4, O);
15+
Expect.equals(6, P);
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2011, 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+
// See CTConst4Test.dart
5+
6+
library CTConst4Lib;
7+
8+
const B = 1;

0 commit comments

Comments
 (0)