mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 04:37:12 +00:00
3c5c1cd3f4
Change-Id: Id8fce4088042b4acf9eadebe7aa99abd5565a471 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/191680 Reviewed-by: Jake Macdonald <jakemac@google.com> Reviewed-by: Dmitry Stefantsov <dmitryas@google.com> Commit-Queue: Kallen Tu <kallentu@google.com>
112 lines
2.3 KiB
Dart
112 lines
2.3 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 while statements for const functions.
|
|
|
|
// SharedOptions=--enable-experiment=const-functions
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
const var1 = fn(2);
|
|
// ^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
const var2 = fn(3);
|
|
// ^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int fn(int a) {
|
|
int b = a;
|
|
int i = 0;
|
|
while (i < 2) {
|
|
b += a;
|
|
i++;
|
|
}
|
|
return b;
|
|
}
|
|
|
|
const var3 = fn1(2);
|
|
// ^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
const var4 = fn1(3);
|
|
// ^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int fn1(int a) {
|
|
int b = a;
|
|
while (true) {
|
|
b *= 3;
|
|
if (b > 10) return b;
|
|
}
|
|
}
|
|
|
|
const var5 = fnContinue();
|
|
// ^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int fnContinue() {
|
|
int a = 0;
|
|
int i = 0;
|
|
while (i < 5) {
|
|
if (i % 2 == 1) {
|
|
i++;
|
|
continue;
|
|
}
|
|
a += i;
|
|
i++;
|
|
}
|
|
return a;
|
|
}
|
|
|
|
const var6 = fnBreak(2);
|
|
// ^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
const var7 = fnBreak(3);
|
|
// ^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int fnBreak(int a) {
|
|
int b = a;
|
|
int i = 0;
|
|
while (i < 2) {
|
|
if (b == 2) break;
|
|
b += a;
|
|
i++;
|
|
}
|
|
return b;
|
|
}
|
|
|
|
const var8 = fnNestedWhile();
|
|
// ^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int fnNestedWhile() {
|
|
int a = 0;
|
|
while (true) {
|
|
while (true) {
|
|
break;
|
|
}
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
const var9 = fnBreakLabel();
|
|
// ^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int fnBreakLabel() {
|
|
foo:
|
|
while (true) {
|
|
while (true) {
|
|
break foo;
|
|
}
|
|
}
|
|
return 3;
|
|
}
|
|
|
|
void main() {
|
|
Expect.equals(var1, 6);
|
|
Expect.equals(var2, 9);
|
|
Expect.equals(var3, 18);
|
|
Expect.equals(var4, 27);
|
|
Expect.equals(var5, 6);
|
|
Expect.equals(var6, 2);
|
|
Expect.equals(var7, 9);
|
|
Expect.equals(var8, 1);
|
|
Expect.equals(var9, 3);
|
|
}
|