blob: 1ca0c2ba3ec65c0a8cab05b0851c649c946d7faf [file] [log] [blame]
// 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);
}