Skip to content

Commit abcf0a6

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 35241. Fix for NPE when the referenced constant variable does not have initializer.
[email protected] Bug: #35241 Change-Id: I406a44467bf3ed2463e8a24c52c45e228c9b6dfa Reviewed-on: https://dart-review.googlesource.com/c/86100 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 670f9ab commit abcf0a6

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pkg/analyzer/lib/src/dart/constant/evaluation.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -1608,8 +1608,9 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
16081608
// Driver, we compute values of all dependencies first (or detect cycle).
16091609
// So, the value has already been computed. Just return it.
16101610
if (evaluationEngine.forAnalysisDriver) {
1611-
if (variableElement.isConst) {
1612-
return variableElement.evaluationResult.value;
1611+
EvaluationResultImpl value = variableElement.evaluationResult;
1612+
if (variableElement.isConst && value != null) {
1613+
return value.value;
16131614
}
16141615
} else {
16151616
// TODO(scheglov) Once we remove task model, we can remove this code.

pkg/analyzer/test/src/dart/resolution/constant_test.dart

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/src/dart/element/element.dart';
6+
import 'package:analyzer/src/error/codes.dart';
67
import 'package:test/test.dart';
78
import 'package:test_reflective_loader/test_reflective_loader.dart';
89

@@ -130,6 +131,25 @@ main() {}
130131
var value = node.elementAnnotation.constantValue;
131132
expect(value.getField('(super)').getField('f').toIntValue(), 42);
132133
}
134+
135+
test_constNotInitialized() async {
136+
addTestFile(r'''
137+
class B {
138+
const B(_);
139+
}
140+
141+
class C extends B {
142+
static const a;
143+
const C() : super(a);
144+
}
145+
''');
146+
await resolveTestFile();
147+
assertTestErrors([
148+
CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER,
149+
CompileTimeErrorCode.CONST_NOT_INITIALIZED,
150+
CompileTimeErrorCode.CONST_NOT_INITIALIZED,
151+
]);
152+
}
133153
}
134154

135155
@reflectiveTest

0 commit comments

Comments
 (0)