mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 01:30:32 +00:00
Language tests for erroneous conditions in control flow statements.
Conditions which are not bools. The error checking should be handled by the compiler so asserts added just in case. Change-Id: Ia53c7aa017300f5afbfe636604855e4183e1d0a6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192580 Reviewed-by: Dmitry Stefantsov <dmitryas@google.com> Reviewed-by: Jake Macdonald <jakemac@google.com> Commit-Queue: Kallen Tu <kallentu@google.com>
This commit is contained in:
parent
c8d115bd06
commit
51b0832516
|
@ -3383,15 +3383,8 @@ class StatementConstantEvaluator extends StatementVisitor<ExecutionStatus> {
|
|||
|
||||
if (condition is AbortConstant) {
|
||||
return new AbortStatus(condition);
|
||||
} else if (condition is! BoolConstant) {
|
||||
return new AbortStatus(exprEvaluator.createErrorConstant(
|
||||
node.condition,
|
||||
templateConstEvalInvalidType.withArguments(
|
||||
condition,
|
||||
exprEvaluator.typeEnvironment.coreTypes.boolLegacyRawType,
|
||||
condition.getType(exprEvaluator._staticTypeContext),
|
||||
exprEvaluator.isNonNullableByDefault)));
|
||||
}
|
||||
assert(condition is BoolConstant);
|
||||
return const ProceedStatus();
|
||||
}
|
||||
|
||||
|
@ -3399,23 +3392,13 @@ class StatementConstantEvaluator extends StatementVisitor<ExecutionStatus> {
|
|||
ExecutionStatus visitIfStatement(IfStatement node) {
|
||||
Constant condition = evaluate(node.condition);
|
||||
if (condition is AbortConstant) return new AbortStatus(condition);
|
||||
if (condition is BoolConstant) {
|
||||
if (condition.value) {
|
||||
return node.then.accept(this);
|
||||
} else if (node.otherwise != null) {
|
||||
return node.otherwise.accept(this);
|
||||
} else {
|
||||
return const ProceedStatus();
|
||||
}
|
||||
} else {
|
||||
return new AbortStatus(exprEvaluator.createErrorConstant(
|
||||
node.condition,
|
||||
templateConstEvalInvalidType.withArguments(
|
||||
condition,
|
||||
exprEvaluator.typeEnvironment.coreTypes.boolLegacyRawType,
|
||||
condition.getType(exprEvaluator._staticTypeContext),
|
||||
exprEvaluator.isNonNullableByDefault)));
|
||||
assert(condition is BoolConstant);
|
||||
if ((condition as BoolConstant).value) {
|
||||
return node.then.accept(this);
|
||||
} else if (node.otherwise != null) {
|
||||
return node.otherwise.accept(this);
|
||||
}
|
||||
return const ProceedStatus();
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -3446,6 +3429,7 @@ class StatementConstantEvaluator extends StatementVisitor<ExecutionStatus> {
|
|||
}
|
||||
|
||||
if (condition is AbortConstant) return new AbortStatus(condition);
|
||||
assert(condition is BoolConstant);
|
||||
return const ProceedStatus();
|
||||
}
|
||||
|
||||
|
@ -3486,6 +3470,7 @@ class StatementConstantEvaluator extends StatementVisitor<ExecutionStatus> {
|
|||
condition = evaluate(node.condition);
|
||||
}
|
||||
if (condition is AbortConstant) return new AbortStatus(condition);
|
||||
assert(condition is BoolConstant);
|
||||
return const ProceedStatus();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,3 +33,15 @@ int fn2() {
|
|||
} while (x as dynamic);
|
||||
return 2;
|
||||
}
|
||||
|
||||
const var3 = fn3();
|
||||
// ^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
||||
// [cfe] Constant evaluation error:
|
||||
int fn3() {
|
||||
dynamic x = 0;
|
||||
do {
|
||||
x++;
|
||||
} while (x);
|
||||
return 2;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
// 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 erroneous for 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 val = 0;
|
||||
for (; val;) {
|
||||
// ^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.NON_BOOL_CONDITION
|
||||
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
|
||||
val += 1;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
const var2 = fn2();
|
||||
// ^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
||||
// [cfe] Constant evaluation error:
|
||||
int fn2() {
|
||||
int val = 0;
|
||||
for (; val as dynamic;) {
|
||||
val += 1;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
const var3 = fn3();
|
||||
// ^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
||||
// [cfe] Constant evaluation error:
|
||||
int fn3() {
|
||||
dynamic val = 0;
|
||||
for (; val;) {
|
||||
val += 1;
|
||||
}
|
||||
return val;
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
// 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 erroneous if 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 val = 0;
|
||||
if (val) {
|
||||
//^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.NON_BOOL_CONDITION
|
||||
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
|
||||
val += 1;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
const var2 = fn2();
|
||||
// ^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
||||
// [cfe] Constant evaluation error:
|
||||
int fn2() {
|
||||
int val = 0;
|
||||
if (val as dynamic) {
|
||||
val += 1;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
const var3 = fn3();
|
||||
// ^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
||||
// [cfe] Constant evaluation error:
|
||||
int fn3() {
|
||||
dynamic val = 0;
|
||||
if (val) {
|
||||
val += 1;
|
||||
}
|
||||
return val;
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
// 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 erroneous 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 val = 0;
|
||||
while (val) {
|
||||
// ^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.NON_BOOL_CONDITION
|
||||
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
|
||||
val += 1;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
const var2 = fn2();
|
||||
// ^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
||||
// [cfe] Constant evaluation error:
|
||||
int fn2() {
|
||||
int val = 0;
|
||||
while (val as dynamic) {
|
||||
val += 1;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
const var3 = fn3();
|
||||
// ^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
||||
// [cfe] Constant evaluation error:
|
||||
int fn3() {
|
||||
dynamic val = 0;
|
||||
while (val) {
|
||||
val += 1;
|
||||
}
|
||||
return val;
|
||||
}
|
Loading…
Reference in a new issue