From 8ae9842e12390e646b747046ae78e833c4b7a0f9 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Fri, 28 Aug 2020 21:52:19 +0000 Subject: [PATCH] Deprecate ElementAnnotation.constantValue R=brianwilkerson@google.com, pquitslund@google.com Change-Id: Ifae6a614c20b703d9614d7ccc2f8b254bafcb070 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/160942 Reviewed-by: Brian Wilkerson Reviewed-by: Phil Quitslund Commit-Queue: Konstantin Shcheglov --- pkg/analyzer/CHANGELOG.md | 2 ++ pkg/analyzer/lib/dart/element/element.dart | 1 + pkg/analyzer/lib/src/dart/element/element.dart | 4 +++- pkg/analyzer/lib/src/error/best_practices_verifier.dart | 2 +- pkg/analyzer/test/src/dart/analysis/driver_test.dart | 2 +- pkg/analyzer/test/src/dart/element/element_test.dart | 2 -- pkg/analyzer/test/src/dart/resolution/constant_test.dart | 4 ++-- 7 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/analyzer/CHANGELOG.md b/pkg/analyzer/CHANGELOG.md index 1eb1e16e051..5919677751a 100644 --- a/pkg/analyzer/CHANGELOG.md +++ b/pkg/analyzer/CHANGELOG.md @@ -2,6 +2,8 @@ * Added `LocalVariableElement.hasInitializer`, `PropertyInducingElement.hasInitializer`, `ParameterElement.hasDefaultValue`. * `ElementImpl.toString()` uses `getDisplayString(withNullability: true)`. +* Deprecated `ElementAnnotation.constantValue`, it does not guarantee that + the value has been computed. Use `computeConstantValue()` instead. ## 0.40.0 * Added `LibraryElement.featureSet`. diff --git a/pkg/analyzer/lib/dart/element/element.dart b/pkg/analyzer/lib/dart/element/element.dart index 0b17e13719a..71b825122f3 100644 --- a/pkg/analyzer/lib/dart/element/element.dart +++ b/pkg/analyzer/lib/dart/element/element.dart @@ -691,6 +691,7 @@ abstract class ElementAnnotation implements ConstantEvaluationTarget { /// Return a representation of the value of this annotation, or `null` if the /// value of this annotation has not been computed or if the value could not /// be computed because of errors. + @Deprecated('Use computeConstantValue() instead') DartObject get constantValue; /// Return the element representing the field, variable, or const constructor diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 911500902a4..0dfd4dbec0e 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -2409,6 +2409,7 @@ class ElementAnnotationImpl implements ElementAnnotation { List get constantEvaluationErrors => evaluationResult?.errors ?? const []; + @Deprecated('Use computeConstantValue() instead') @override DartObject get constantValue => evaluationResult?.value; @@ -2565,7 +2566,7 @@ class ElementAnnotationImpl implements ElementAnnotation { computeConstants(library.typeProvider, library.typeSystem, context.declaredVariables, [this], analysisOptions.experimentStatus); } - return constantValue; + return evaluationResult?.value; } @override @@ -7596,6 +7597,7 @@ abstract class VariableElementImpl extends ElementImpl /// initializers. Expression get constantInitializer => null; + @Deprecated('Use computeConstantValue() instead') @override DartObject get constantValue => evaluationResult?.value; diff --git a/pkg/analyzer/lib/src/error/best_practices_verifier.dart b/pkg/analyzer/lib/src/error/best_practices_verifier.dart index 43ab1dda841..aef2cc9ce08 100644 --- a/pkg/analyzer/lib/src/error/best_practices_verifier.dart +++ b/pkg/analyzer/lib/src/error/best_practices_verifier.dart @@ -1480,7 +1480,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor { } for (var annotation in classElement.metadata) { if (annotation.isTarget) { - var value = annotation.constantValue; + var value = annotation.computeConstantValue(); var kinds = {}; for (var kindObject in value.getField('kinds').toSetValue()) { var index = kindObject.getField('index').toIntValue(); diff --git a/pkg/analyzer/test/src/dart/analysis/driver_test.dart b/pkg/analyzer/test/src/dart/analysis/driver_test.dart index 2522b772cc2..493d29a2412 100644 --- a/pkg/analyzer/test/src/dart/analysis/driver_test.dart +++ b/pkg/analyzer/test/src/dart/analysis/driver_test.dart @@ -860,7 +860,7 @@ const x = 1; '''); var result = await driver.getResult(testFile); Annotation at_x = AstFinder.getClass(result.unit, 'C').metadata[0]; - expect(at_x.elementAnnotation.constantValue.toIntValue(), 1); + expect(at_x.elementAnnotation.computeConstantValue().toIntValue(), 1); } test_const_circular_reference() async { diff --git a/pkg/analyzer/test/src/dart/element/element_test.dart b/pkg/analyzer/test/src/dart/element/element_test.dart index 1b926d37339..a5956bb715b 100644 --- a/pkg/analyzer/test/src/dart/element/element_test.dart +++ b/pkg/analyzer/test/src/dart/element/element_test.dart @@ -858,12 +858,10 @@ main() { ParameterElement parameter = argument.staticParameterElement; ElementAnnotation annotation = parameter.metadata[0]; - expect(annotation.constantValue, isNull); DartObject value = annotation.computeConstantValue(); expect(value, isNotNull); expect(value.getField('f').toStringValue(), 'x'); - expect(annotation.constantValue, value); } } diff --git a/pkg/analyzer/test/src/dart/resolution/constant_test.dart b/pkg/analyzer/test/src/dart/resolution/constant_test.dart index 11c9ec8add5..0d0a0421a33 100644 --- a/pkg/analyzer/test/src/dart/resolution/constant_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/constant_test.dart @@ -40,7 +40,7 @@ const a = const A(); // To evaluate `const A()` we have to evaluate `{int p}`. // Even if its value is `null`. expect(p.isConstantEvaluated, isTrue); - expect(p.constantValue.isNull, isTrue); + expect(p.computeConstantValue().isNull, isTrue); } test_constFactoryRedirection_super() async { @@ -64,7 +64,7 @@ main() {} '''); var node = findNode.annotation('@I'); - var value = node.elementAnnotation.constantValue; + var value = node.elementAnnotation.computeConstantValue(); expect(value.getField('(super)').getField('f').toIntValue(), 42); }