mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 04:27:17 +00:00
9e5a6c1568
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>
57 lines
1.2 KiB
Dart
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);
|
|
}
|