Skip to content

Commit

Permalink
Migrate language_2/const to NNBD.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
munificent authored and commit-bot@chromium.org committed Apr 3, 2020
1 parent 1d44ef0 commit e736495
Show file tree
Hide file tree
Showing 88 changed files with 3,134 additions and 14 deletions.
16 changes: 16 additions & 0 deletions tests/language/const/cast1_test.dart
@@ -0,0 +1,16 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Implicit casts in constants are supported and treated as compile-time errors
/// if they are not valid.
class A {
final int n;
const A(dynamic input) : n = input;
}

main() {
print(const A(2)); //# 01: ok
print(const A('2')); //# 02: compile-time error
}
16 changes: 16 additions & 0 deletions tests/language/const/cast2_test.dart
@@ -0,0 +1,16 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Explicit casts in constants are supported and treated as compile-time errors
/// if they are not valid.
class A {
final int n;
const A(dynamic input) : n = input as int;
}

main() {
print(const A(2)); //# 01: ok
print(const A('2')); //# 02: compile-time error
}
22 changes: 22 additions & 0 deletions tests/language/const/cast3_test.dart
@@ -0,0 +1,22 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Casts in constants correctly substitute type variables.
class A {
const A();
}

class B implements A {
const B();
}

class M<T extends A> {
final T a;
const M(dynamic t) : a = t; // adds implicit cast `as T`
}

main() {
print(const M<B>(const B()));
}
26 changes: 26 additions & 0 deletions tests/language/const/cast4_test.dart
@@ -0,0 +1,26 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Casts in constants correctly substitute type variables.
class A {
const A();
}

class B implements A {
const B();
}

class M<T extends A> {
final T a;
const M(dynamic t) : a = t; // adds implicit cast `as T`
}

class N<S extends A> extends M<S> {
const N(dynamic t) : super(t);
}

main() {
print(const N<B>(const B()));
}
59 changes: 59 additions & 0 deletions tests/language/const/conditional_runtime_test.dart
@@ -0,0 +1,59 @@
// TODO(multitest): This was automatically migrated from a multitest and may
// contain strange or dead code.

// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Test for conditionals as compile-time constants.

import 'package:expect/expect.dart';

class Marker {
final field;
const Marker(this.field);
}

var var0 = const Marker(0);
var var1 = const Marker(1);
const const0 = const Marker(0);
const const1 = const Marker(1);

const trueConst = true;
const falseConst = false;
var nonConst = true;
const zeroConst = 0;

const cond1 = trueConst ? const0 : const1;



const cond2 = falseConst ? const0 : const1;











void main() {
Expect.identical(var0, cond1);



Expect.identical(var1, cond2);










}
110 changes: 110 additions & 0 deletions tests/language/const/conditional_test.dart
@@ -0,0 +1,110 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Test for conditionals as compile-time constants.

import 'package:expect/expect.dart';

class Marker {
final field;
const Marker(this.field);
}

var var0 = const Marker(0);
var var1 = const Marker(1);
const const0 = const Marker(0);
const const1 = const Marker(1);

const trueConst = true;
const falseConst = false;
var nonConst = true;
const zeroConst = 0;

const cond1 = trueConst ? const0 : const1;
const cond1a = trueConst ? nonConst : const1;
// ^
// [cfe] Constant evaluation error:
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
// [cfe] Not a constant expression.
const cond1b = trueConst ? const0 : nonConst;
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
// [cfe] Not a constant expression.

const cond2 = falseConst ? const0 : const1;
const cond2a = falseConst ? nonConst : const1;
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
// [cfe] Not a constant expression.
const cond2b = falseConst ? const0 : nonConst;
// ^
// [cfe] Constant evaluation error:
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
// [cfe] Not a constant expression.

const cond3 = nonConst ? const0 : const1;
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
// [cfe] Not a constant expression.
// ^
// [cfe] Constant evaluation error:
const cond3a = nonConst ? nonConst : const1;
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
// [cfe] Not a constant expression.
// ^
// [cfe] Constant evaluation error:
// ^
// [cfe] Not a constant expression.
const cond3b = nonConst ? const0 : nonConst;
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
// [cfe] Not a constant expression.
// ^
// [cfe] Constant evaluation error:
// ^
// [cfe] Not a constant expression.

const cond4 = zeroConst ? const0 : const1;
// ^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_TYPE_BOOL
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
// ^^^^^^^^^
// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION
const cond4a = zeroConst ? nonConst : const1;
// ^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_TYPE_BOOL
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
// ^^^^^^^^^
// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION
// ^
// [cfe] Not a constant expression.
const cond4b = zeroConst ? const0 : nonConst;
// ^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_TYPE_BOOL
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
// ^^^^^^^^^
// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION
// ^
// [cfe] Not a constant expression.

void main() {
Expect.identical(var0, cond1);
Expect.identical(nonConst, cond1a);
Expect.identical(var0, cond1b);

Expect.identical(var1, cond2);
Expect.identical(var1, cond2a);
Expect.identical(nonConst, cond2b);

Expect.identical(var0, cond3);
Expect.identical(nonConst, cond3a);
Expect.identical(var0, cond3b);

Expect.identical(var1, cond4);
Expect.identical(var1, cond4a);
Expect.identical(nonConst, cond4b);
}
105 changes: 105 additions & 0 deletions tests/language/const/const2_test.dart
@@ -0,0 +1,105 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// VMOptions=
// VMOptions=--compile_all

// Exercises language constructs that require compile time constants

// Initialize with different literal types
const b = true;
const s = "apple";
const i = 1;
const d = 3.3;
const h = 0xf;
const n = null;
const aList = const [1, 2, 3]; // array literal
const aMap = const {"1": "one", "2": "banana"}; // map literal

const INT_LIT = 5;
const INT_LIT_REF = INT_LIT;
const DOUBLE_LIT = 1.5;
const BOOL_LIT = true;
const STRING_LIT = "Hello";

const BOP1_0 = INT_LIT + 1;
const BOP1_1 = 1 + INT_LIT;
const BOP1_2 = INT_LIT - 1;
const BOP1_3 = 1 - INT_LIT;
const BOP1_4 = INT_LIT * 1;
const BOP1_5 = 1 * INT_LIT;
const BOP1_6 = INT_LIT / 1;
const BOP1_7 = 1 / INT_LIT;
const BOP2_0 = DOUBLE_LIT + 1.5;
const BOP2_1 = 1.5 + DOUBLE_LIT;
const BOP2_2 = DOUBLE_LIT - 1.5;
const BOP2_3 = 1.5 - DOUBLE_LIT;
const BOP2_4 = DOUBLE_LIT * 1.5;
const BOP2_5 = 1.5 * DOUBLE_LIT;
const BOP2_6 = DOUBLE_LIT / 1.5;
const BOP2_7 = 1.5 / DOUBLE_LIT;
const BOP3_0 = 2 < INT_LIT;
const BOP3_1 = INT_LIT < 2;
const BOP3_2 = 2 > INT_LIT;
const BOP3_3 = INT_LIT > 2;
const BOP3_4 = 2 < DOUBLE_LIT;
const BOP3_5 = DOUBLE_LIT < 2;
const BOP3_6 = 2 > DOUBLE_LIT;
const BOP3_7 = DOUBLE_LIT > 2;
const BOP3_8 = 2 <= INT_LIT;
const BOP3_9 = INT_LIT <= 2;
const BOP3_10 = 2 >= INT_LIT;
const BOP3_11 = INT_LIT >= 2;
const BOP3_12 = 2.0 <= DOUBLE_LIT;
const BOP3_13 = DOUBLE_LIT <= 2.0;
const BOP3_14 = 2.0 >= DOUBLE_LIT;
const BOP3_15 = DOUBLE_LIT >= 2;
const BOP4_0 = 5 % INT_LIT;
const BOP4_1 = INT_LIT % 5;
const BOP4_2 = 5.0 % DOUBLE_LIT;
const BOP4_3 = DOUBLE_LIT % 5.0;
const BOP5_0 = 0x80 & 0x04;
const BOP5_1 = 0x80 | 0x04;
const BOP5_2 = 0x80 << 0x04;
const BOP5_3 = 0x80 >> 0x04;
const BOP5_4 = 0x80 ~/ 0x04;
const BOP5_5 = 0x80 ^ 0x04;
const BOP6 = BOOL_LIT && true;
const BOP7 = false || BOOL_LIT;
const BOP8 = STRING_LIT == "World!";
const BOP9 = "Hello" != STRING_LIT;
const BOP10 = INT_LIT == INT_LIT_REF;
const BOP11 = BOOL_LIT != true;

// Multiple binary expressions
const BOP20 = 1 * INT_LIT / 3 + INT_LIT + 9;

// Parenthised expressions
const BOP30 = (1 > 2);
const BOP31 = (1 * 2) + 3;
const BOP32 = 3 + (1 * 2);

// Unary expressions
const UOP1_0 = !BOOL_LIT;
const UOP1_1 = BOOL_LIT || !true;
const UOP1_2 = !BOOL_LIT || true;
const UOP1_3 = !(BOOL_LIT && true);
const UOP2_0 = ~0xf0;
const UOP2_1 = ~INT_LIT;
const UOP2_2 = ~INT_LIT & 123;
const UOP2_3 = ~(INT_LIT | 0xff);
const UOP3_0 = -0xf0;
const UOP3_1 = -INT_LIT;
const UOP3_2 = -INT_LIT + 123;
const UOP3_3 = -(INT_LIT * 0xff);
const UOP3_4 = -0xf0;
const UOP3_5 = -DOUBLE_LIT;
const UOP3_6 = -DOUBLE_LIT + 123;
const UOP3_7 = -(DOUBLE_LIT * 0xff);

class A {
const A();
static const a = const A(); // Assignment from Constant constructor OK
}

main() {}
16 changes: 16 additions & 0 deletions tests/language/const/const3_test.dart
@@ -0,0 +1,16 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Check that initializers of const fields can be declared out of order.

import "package:expect/expect.dart";

const P = 2 * (O - N);
const N = 1;
const O = 1 + 3;

void main() {
Expect.equals(1, N);
Expect.equals(4, O);
Expect.equals(6, P);
}
8 changes: 8 additions & 0 deletions tests/language/const/const4_lib.dart
@@ -0,0 +1,8 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// See CTConst4Test.dart

library CTConst4Lib;

const B = 1;

0 comments on commit e736495

Please sign in to comment.