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>
diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index a574704..3c98239 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -717,7 +717,7 @@
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 @@
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;
diff --git a/pkg/analyzer/test/src/diagnostics/const_eval_throws_exception_test.dart b/pkg/analyzer/test/src/diagnostics/const_eval_throws_exception_test.dart
index 8c5c365..aa13cab 100644
--- a/pkg/analyzer/test/src/diagnostics/const_eval_throws_exception_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/const_eval_throws_exception_test.dart
@@ -121,6 +121,19 @@
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(
'''
@@ -214,4 +227,16 @@
}
''');
}
+
+ test_fromEnvironment_ifElement() async {
+ await assertNoErrorsInCode('''
+const b = bool.fromEnvironment('foo');
+
+main() {
+ const l1 = [1, 2, 3];
+ const l2 = [if (b) ...l1];
+ print(l2);
+}
+''');
+ }
}