When const evaluating ConditionalExpression, if the condition is unknown, checks both then/else for errors.

Bug: https://github.com/flutter/flutter/issues/91906
Change-Id: I29057d9b5704fe9850a6ba2ed1416472c7e1abc6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/217025
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2021-10-15 19:32:30 +00:00 committed by commit-bot@chromium.org
parent 1aaed6385f
commit 71da05e920
3 changed files with 24 additions and 3 deletions

View file

@ -80,7 +80,7 @@ import 'package:meta/meta.dart';
/// TODO(scheglov) Clean up the list of implicitly analyzed files.
class AnalysisDriver implements AnalysisDriverGeneric {
/// The version of data format, should be incremented on every format change.
static const int DATA_VERSION = 188;
static const int DATA_VERSION = 189;
/// The number of exception contexts allowed to write. Once this field is
/// zero, we stop writing any new exception contexts in this process.

View file

@ -610,10 +610,15 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
if (conditionResult == null) {
return conditionResult;
}
if (conditionResult.toBoolValue() == true) {
var conditionResultBool = conditionResult.toBoolValue();
if (conditionResultBool == null) {
node.thenExpression.accept(this);
node.elseExpression.accept(this);
} else if (conditionResultBool == true) {
_reportNotPotentialConstants(node.elseExpression);
return node.thenExpression.accept(this);
} else if (conditionResult.toBoolValue() == false) {
} else if (conditionResultBool == false) {
_reportNotPotentialConstants(node.thenExpression);
return node.elseExpression.accept(this);
}

View file

@ -1393,6 +1393,22 @@ class C {}
errorCodes: [CompileTimeErrorCode.INVALID_CONSTANT]);
}
test_visitConditionalExpression_lazy_unknown_int_invalid() async {
await resolveTestCode('''
const c = identical(0, 0.0) ? 1 : new Object();
''');
_evaluateConstantOrNull('c',
errorCodes: [CompileTimeErrorCode.INVALID_CONSTANT]);
}
test_visitConditionalExpression_lazy_unknown_invalid_int() async {
await resolveTestCode('''
const c = identical(0, 0.0) ? 1 : new Object();
''');
_evaluateConstantOrNull('c',
errorCodes: [CompileTimeErrorCode.INVALID_CONSTANT]);
}
test_visitIntegerLiteral() async {
await resolveTestCode('''
const double d = 3;