Stop reporting some errors when constant evaluation produces a valid object with an unknown state (issue 36873)

Change-Id: I1bf9c0aebaee24ab0c9e2b40c9881d478e33f87c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/102001
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson 2019-05-09 18:45:01 +00:00 committed by commit-bot@chromium.org
parent d1a5eb5900
commit 130ad1bcf9
2 changed files with 36 additions and 9 deletions

View file

@ -717,7 +717,7 @@ class ConstantEvaluationEngine {
DartObjectImpl evaluationResult = condition.accept(initializerVisitor);
if (evaluationResult == null ||
!evaluationResult.isBool ||
evaluationResult.toBoolValue() != true) {
evaluationResult.toBoolValue() == false) {
errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, node);
return null;
@ -1599,14 +1599,16 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
DartObjectImpl conditionResult = condition.accept(this);
bool conditionValue = conditionResult?.toBoolValue();
if (conditionValue == null) {
// TODO(brianwilkerson) Figure out why the static type is sometimes null.
DartType staticType = condition.staticType;
if (staticType == null ||
typeSystem.isAssignableTo(staticType, _typeProvider.boolType)) {
// If the static type is not assignable, then we will have already
// reported this error.
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, condition);
if (conditionResult?.type != _typeProvider.boolType) {
// TODO(brianwilkerson) Figure out why the static type is sometimes null.
DartType staticType = condition.staticType;
if (staticType == null ||
typeSystem.isAssignableTo(staticType, _typeProvider.boolType)) {
// If the static type is not assignable, then we will have already
// reported this error.
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, condition);
}
}
}
return conditionValue;

View file

@ -121,6 +121,19 @@ main() {
expect(otherFileResult.errors, isEmpty);
}
test_fromEnvironment_assertInitializer() async {
await assertNoErrorsInCode('''
class A {
const A(int x) : assert(x > 5);
}
main() {
var c = const A(int.fromEnvironment('x'));
print(c);
}
''');
}
test_ifElement_false_thenNotEvaluated() async {
await assertErrorsInCode(
'''
@ -212,6 +225,18 @@ class T {
final Object value;
const T.eq(Object o1, Object o2) : value = o1 == o2;
}
''');
}
test_fromEnvironment_ifElement() async {
await assertNoErrorsInCode('''
const b = bool.fromEnvironment('foo');
main() {
const l1 = [1, 2, 3];
const l2 = [if (b) ...l1];
print(l2);
}
''');
}
}