Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 71ffe97

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Correctly handle constant fields when computing constness (take 2)
Change-Id: I03e78ff4a2ea18fa7362f159a07d6d035c59a4ae Reviewed-on: https://dart-review.googlesource.com/50280 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent d58b0e2 commit 71ffe97

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,20 +2766,23 @@ class ConstantAnalysisErrorListener extends AnalysisErrorListener {
27662766

27672767
@override
27682768
void onError(AnalysisError error) {
2769-
switch (error.errorCode) {
2770-
case CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL:
2771-
case CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_NUM_STRING:
2772-
case CompileTimeErrorCode.CONST_EVAL_TYPE_INT:
2773-
case CompileTimeErrorCode.CONST_EVAL_TYPE_NUM:
2774-
case CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION:
2775-
case CompileTimeErrorCode.CONST_EVAL_THROWS_IDBZE:
2776-
case CompileTimeErrorCode.CONST_WITH_NON_CONSTANT_ARGUMENT:
2777-
case CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER:
2778-
case CompileTimeErrorCode
2779-
.CONST_CONSTRUCTOR_WITH_FIELD_INITIALIZED_BY_NON_CONST:
2780-
case CompileTimeErrorCode.INVALID_CONSTANT:
2781-
case CompileTimeErrorCode.MISSING_CONST_IN_LIST_LITERAL:
2782-
hasConstError = true;
2769+
ErrorCode errorCode = error.errorCode;
2770+
if (errorCode is CompileTimeErrorCode) {
2771+
switch (errorCode) {
2772+
case CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL:
2773+
case CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_NUM_STRING:
2774+
case CompileTimeErrorCode.CONST_EVAL_TYPE_INT:
2775+
case CompileTimeErrorCode.CONST_EVAL_TYPE_NUM:
2776+
case CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION:
2777+
case CompileTimeErrorCode.CONST_EVAL_THROWS_IDBZE:
2778+
case CompileTimeErrorCode.CONST_WITH_NON_CONSTANT_ARGUMENT:
2779+
case CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER:
2780+
case CompileTimeErrorCode
2781+
.CONST_CONSTRUCTOR_WITH_FIELD_INITIALIZED_BY_NON_CONST:
2782+
case CompileTimeErrorCode.INVALID_CONSTANT:
2783+
case CompileTimeErrorCode.MISSING_CONST_IN_LIST_LITERAL:
2784+
hasConstError = true;
2785+
}
27832786
}
27842787
}
27852788
}
@@ -6622,6 +6625,13 @@ class InstanceCreationExpressionImpl extends ExpressionImpl
66226625
}
66236626
}
66246627
}
6628+
} else if (argument is Identifier) {
6629+
Element element = argument.bestElement;
6630+
if (element is PropertyAccessorElement && !element.variable.isConst) {
6631+
return false;
6632+
} else if (element is VariableElement && !element.isConst) {
6633+
return false;
6634+
}
66256635
} else {
66266636
return false;
66276637
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
16331633
element is PropertyAccessorElement ? element.variable : element;
16341634
if (variableElement is VariableElementImpl) {
16351635
evaluationEngine.validator.beforeGetEvaluationResult(variableElement);
1636+
variableElement.computeConstantValue();
16361637
EvaluationResultImpl value = variableElement.evaluationResult;
16371638
if (variableElement.isConst && value != null) {
16381639
return value.value;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ class ConstantAstCloner extends AstCloner {
6969
InstanceCreationExpression node) {
7070
InstanceCreationExpression expression =
7171
super.visitInstanceCreationExpression(node);
72-
if (previewDart2 && node.keyword == null && node.isConst) {
73-
expression.keyword = new KeywordToken(Keyword.CONST, node.offset);
72+
if (previewDart2 && node.keyword == null) {
73+
if (node.isConst) {
74+
expression.keyword = new KeywordToken(Keyword.CONST, node.offset);
75+
} else {
76+
expression.keyword = new KeywordToken(Keyword.NEW, node.offset);
77+
}
7478
}
7579
expression.staticElement = node.staticElement;
7680
return expression;

pkg/analyzer/test/src/dart/ast/ast_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ class InstanceCreationExpressionImplTest extends ResolverTestCase {
442442
String testSource;
443443
CompilationUnitImpl testUnit;
444444

445+
bool get enableNewAnalysisDriver => true;
446+
445447
void assertIsConst(String snippet, bool isConst) {
446448
int index = testSource.indexOf(snippet);
447449
expect(index >= 0, isTrue);
@@ -462,6 +464,20 @@ class InstanceCreationExpressionImplTest extends ResolverTestCase {
462464
testUnit = await resolveSource2('/test.dart', source);
463465
}
464466

467+
void
468+
test_isConst_notInContext_constructor_const_constParam_identifier() async {
469+
enablePreviewDart2();
470+
await resolve('''
471+
var v = C(C.a);
472+
class C {
473+
static const C a = C.c();
474+
const C(c);
475+
const C.c();
476+
}
477+
''');
478+
assertIsConst("C(C", true);
479+
}
480+
465481
void test_isConst_notInContext_constructor_const_constParam_named() async {
466482
enablePreviewDart2();
467483
await resolve('''

0 commit comments

Comments
 (0)