dart-sdk/tests/language/const_functions/const_functions_do_statements_test.dart
Kallen Tu 9e5a6c1568 [cfe] Do-while statements for const functions.
Change-Id: I881fb535ef76e564ab76c5fc661d3adbbe5aed0f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192401
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Jake Macdonald <jakemac@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
2021-03-22 22:19:45 +00:00

57 lines
1.2 KiB
Dart

// Copyright (c) 2021, 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 do-while statements for const functions.
// SharedOptions=--enable-experiment=const-functions
import "package:expect/expect.dart";
const var1 = fn();
// ^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
int fn() {
int x = 0;
do {
x++;
} while (x < 2);
return x;
}
const var2 = fn2(2);
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
const var3 = fn2(10);
// ^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
int fn2(int a) {
int x = 0, b = 0;
do {
if (x > 5) break;
x += a;
b++;
} while (b < 2);
return x;
}
const var4 = fn3();
// ^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
int fn3() {
int x = 0, b = 0;
do {
x += 1;
if (x % 2 == 1) continue;
b += x;
} while (x < 5);
return b;
}
void main() {
Expect.equals(var1, 2);
Expect.equals(var2, 4);
Expect.equals(var3, 10);
Expect.equals(var4, 6);
}