Issue 35241. Fix for NPE when the referenced constant variable does not have initializer.
R=brianwilkerson@google.com
Bug: https://github.com/dart-lang/sdk/issues/35241
Change-Id: I406a44467bf3ed2463e8a24c52c45e228c9b6dfa
Reviewed-on: https://dart-review.googlesource.com/c/86100
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index b8964c2..922caa0 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -1608,8 +1608,9 @@
// Driver, we compute values of all dependencies first (or detect cycle).
// So, the value has already been computed. Just return it.
if (evaluationEngine.forAnalysisDriver) {
- if (variableElement.isConst) {
- return variableElement.evaluationResult.value;
+ EvaluationResultImpl value = variableElement.evaluationResult;
+ if (variableElement.isConst && value != null) {
+ return value.value;
}
} else {
// TODO(scheglov) Once we remove task model, we can remove this code.
diff --git a/pkg/analyzer/test/src/dart/resolution/constant_test.dart b/pkg/analyzer/test/src/dart/resolution/constant_test.dart
index 55af1b2..4ba17ed 100644
--- a/pkg/analyzer/test/src/dart/resolution/constant_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/constant_test.dart
@@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/element/element.dart';
+import 'package:analyzer/src/error/codes.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
@@ -130,6 +131,25 @@
var value = node.elementAnnotation.constantValue;
expect(value.getField('(super)').getField('f').toIntValue(), 42);
}
+
+ test_constNotInitialized() async {
+ addTestFile(r'''
+class B {
+ const B(_);
+}
+
+class C extends B {
+ static const a;
+ const C() : super(a);
+}
+''');
+ await resolveTestFile();
+ assertTestErrors([
+ CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER,
+ CompileTimeErrorCode.CONST_NOT_INITIALIZED,
+ CompileTimeErrorCode.CONST_NOT_INITIALIZED,
+ ]);
+ }
}
@reflectiveTest