[cfe] Change ConstantEvaluator to extend ExpressionVisitor
The constant evaluator is only intended to visit expressions but
by extending RecursiveResultVisitor falsely gave the impression that it
supported visitor all nodes.
Change-Id: Id6afc2b2c51dd4dee1327f8df2b20a6fae6d2c91
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/189060
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
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 4c4e74d..0d2f50f 100644
--- a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
@@ -811,7 +811,7 @@
}
}
-class ConstantEvaluator extends RecursiveResultVisitor<Constant> {
+class ConstantEvaluator extends ExpressionVisitor<Constant> {
final ConstantsBackend backend;
final NumberSemantics numberSemantics;
ConstantIntFolder intFolder;
@@ -1157,7 +1157,7 @@
}
@override
- Constant defaultTreeNode(Node node) {
+ Constant defaultExpression(Expression node) {
// Only a subset of the expression language is valid for constant
// evaluation.
return createInvalidExpressionConstant(
@@ -2738,20 +2738,21 @@
if (value is AbortConstant) return value;
env.addVariableValue(parameter, value);
}
- return function.body.accept(this);
+ Statement body = function.body;
+ if (body is ReturnStatement) {
+ if (!enableConstFunctions) {
+ return createInvalidExpressionConstant(
+ node, "Return statements are not supported.");
+ }
+ return body.expression.accept(this);
+ } else {
+ return createInvalidExpressionConstant(
+ node, "Unsupported statement: ${body.runtimeType}.");
+ }
});
}
@override
- Constant visitReturnStatement(ReturnStatement node) {
- if (!enableConstFunctions) {
- return createInvalidExpressionConstant(
- node, "Return statements are not supported.");
- }
- return node.expression.accept(this);
- }
-
- @override
Constant visitAsExpression(AsExpression node) {
final Constant constant = _evaluateSubexpression(node.operand);
if (constant is AbortConstant) return constant;