[CFE] Always call the constant evaluator by the evaluate method.
Change-Id: I176139c98e968cd890a05c0517781f8fad4a0d7d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96321
Reviewed-by: Jenny Messerly <jmesserly@google.com>
diff --git a/pkg/dev_compiler/lib/src/kernel/compiler.dart b/pkg/dev_compiler/lib/src/kernel/compiler.dart
index a2cba73..2322008 100644
--- a/pkg/dev_compiler/lib/src/kernel/compiler.dart
+++ b/pkg/dev_compiler/lib/src/kernel/compiler.dart
@@ -3840,7 +3840,7 @@
visitStaticGet(StaticGet node) {
var target = node.target;
if (target is Field && target.isConst) {
- var value = _constants.evaluate(target.initializer, cache: true);
+ var value = _constants.evaluate(target.initializer);
if (value is PrimitiveConstant) return value.accept(this);
}
diff --git a/pkg/dev_compiler/lib/src/kernel/constants.dart b/pkg/dev_compiler/lib/src/kernel/constants.dart
index d9817e3..a59d0bd 100644
--- a/pkg/dev_compiler/lib/src/kernel/constants.dart
+++ b/pkg/dev_compiler/lib/src/kernel/constants.dart
@@ -34,27 +34,11 @@
/// failed, or if the constant was unavailable.
///
/// Returns [NullConstant] to represent the `null` value.
- ///
- /// To avoid performance costs associated with try+catch on invalid constant
- /// evaluation, call this after [isConstant] is known to be true.
- Constant evaluate(Expression e, {bool cache = false}) {
+ Constant evaluate(Expression e) {
if (e == null) return null;
- try {
- var result = cache ? _evaluator.evaluate(e) : e.accept(_evaluator);
- return result is UnevaluatedConstant ? null : result;
- } on _AbortCurrentEvaluation {
- // TODO(jmesserly): the try+catch is necessary because the front end is
- // not issuing sufficient errors, so the constant evaluation can fail.
- //
- // It can also be caused by methods in the evaluator that don't understand
- // unavailable constants.
- return null;
- } on NoSuchMethodError {
- // TODO(jmesserly): this is probably the same issue as above, but verify
- // that it's fixed once Kernel does constant evaluation.
- return null;
- }
+ Constant result = _evaluator.evaluate(e);
+ return result is UnevaluatedConstant ? null : result;
}
/// If [node] is an annotation with a field named `name`, returns that field's
@@ -184,12 +168,7 @@
class _ErrorReporter extends SimpleErrorReporter {
const _ErrorReporter();
+ // Ignore reported errors.
@override
- report(context, message, node) => throw const _AbortCurrentEvaluation();
-}
-
-// TODO(jmesserly): this class is private in Kernel constants library, so
-// we have our own version.
-class _AbortCurrentEvaluation {
- const _AbortCurrentEvaluation();
+ report(context, message, node) => message;
}
diff --git a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
index ba99185..ac5cd3b 100644
--- a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
@@ -411,7 +411,10 @@
nodeCache = <Node, Constant>{},
env = new EvaluationEnvironment();
- /// Evaluates [node] and possibly cache the evaluation result.
+ /// Evaluate [node] and possibly cache the evaluation result.
+ /// Returns UnevaluatedConstant if the constant could not be evaluated.
+ /// If the expression in the UnevaluatedConstant is an InvalidExpression,
+ /// an error occurred during constant evaluation.
Constant evaluate(Expression node) {
evaluationRoot = node;
try {
@@ -459,7 +462,7 @@
return unevaluatedNodes != null && unevaluatedNodes.contains(node);
}
- /// Evaluates [node] and possibly cache the evaluation result.
+ /// Evaluate [node] and possibly cache the evaluation result.
/// @throws _AbortCurrentEvaluation if expression can't be evaluated.
Constant _evaluateSubexpression(Expression node) {
if (node == null) return nullConstant;