Files
sdk/tests/language_2/constants_2018/potential_const_test.dart
T
Lasse Reichstein Holst Nielsen 8abf6c67ad Add test of potentially constant scopes.
Tweaks.

Change-Id: I0fb8b4088c86e5b83f1c76db8a2dfa7c9023c1b9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/99000
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
2020-10-29 10:11:53 +00:00

101 lines
2.9 KiB
Dart

// Copyright (c) 2019, 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.
// Tests that the correct places allows, and requires, potentially constant
// expressions.
bool get nonConst => true;
class C {
final v;
const C(this.v);
// Redirecting generative constructor invocation parameters,
// must be potenentially constant.
const C.r1() : this(const C(null));
// Only evaluates the true branch when passed `true` as argument.
const C.r2(bool b) : this(b ? null : 1 ~/ 0);
const C.rn1() : this(nonConst);
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_CONSTANT
// [cfe] Constant evaluation error:
const C.rn2(bool b) : this(b ? null : nonConst);
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_CONSTANT
// [cfe] Constant evaluation error:
// Initializer list expressions must be potentially constant.
const C.g1() : v = const C(null);
const C.g2(bool b) : v = b ? null : 1 ~/ 0;
const C.gn3() : v = nonConst;
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_CONSTANT
// [cfe] Constant evaluation error:
const C.gn4(bool b) : v = b ? null : nonConst;
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_CONSTANT
// [cfe] Constant evaluation error:
// Constant constructor initializer list assert expressions
// must be potentially constant (and boolean).
const C.a1()
: assert(const C(null) != null),
v = null;
const C.a2(bool b)
: assert(b ? const C(null) != null : ((1 ~/ 0) as bool)),
v = null;
const C.an1()
: assert(nonConst),
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_CONSTANT
// [cfe] Constant evaluation error:
v = null;
const C.an2(bool b)
: assert(b ? true : nonConst),
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_CONSTANT
// [cfe] Constant evaluation error:
v = null;
}
main() {
var c = const C(null);
var cc = const C(C(null));
var r1 = const C.r1();
var r2 = const C.r2(true);
/// Const constructor invocation which would throw.
/**/ const C.r2(false);
// ^^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_THROWS_EXCEPTION
// ^^^^^^^^^^^
// [cfe] Constant evaluation error:
var g1 = const C.g1();
var g2 = const C.g2(true);
/**/ const C.g2(false);
// ^^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_THROWS_EXCEPTION
// ^^^^^^^^^^^
// [cfe] Constant evaluation error:
var a1 = const C.a1();
var a2 = const C.a2(true);
/**/ const C.a2(false);
// ^^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_THROWS_EXCEPTION
// ^^^^^^^^^^^
// [cfe] Constant evaluation error:
}