Skip to content

Commit e9f4d5d

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
[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 <[email protected]> Reviewed-by: Dmitry Stefantsov <[email protected]>
1 parent c83d879 commit e9f4d5d

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ class ConstantsTransformer extends RemovingTransformer {
811811
}
812812
}
813813

814-
class ConstantEvaluator extends RecursiveResultVisitor<Constant> {
814+
class ConstantEvaluator extends ExpressionVisitor<Constant> {
815815
final ConstantsBackend backend;
816816
final NumberSemantics numberSemantics;
817817
ConstantIntFolder intFolder;
@@ -1157,7 +1157,7 @@ class ConstantEvaluator extends RecursiveResultVisitor<Constant> {
11571157
}
11581158

11591159
@override
1160-
Constant defaultTreeNode(Node node) {
1160+
Constant defaultExpression(Expression node) {
11611161
// Only a subset of the expression language is valid for constant
11621162
// evaluation.
11631163
return createInvalidExpressionConstant(
@@ -2738,19 +2738,20 @@ class ConstantEvaluator extends RecursiveResultVisitor<Constant> {
27382738
if (value is AbortConstant) return value;
27392739
env.addVariableValue(parameter, value);
27402740
}
2741-
return function.body.accept(this);
2741+
Statement body = function.body;
2742+
if (body is ReturnStatement) {
2743+
if (!enableConstFunctions) {
2744+
return createInvalidExpressionConstant(
2745+
node, "Return statements are not supported.");
2746+
}
2747+
return body.expression.accept(this);
2748+
} else {
2749+
return createInvalidExpressionConstant(
2750+
node, "Unsupported statement: ${body.runtimeType}.");
2751+
}
27422752
});
27432753
}
27442754

2745-
@override
2746-
Constant visitReturnStatement(ReturnStatement node) {
2747-
if (!enableConstFunctions) {
2748-
return createInvalidExpressionConstant(
2749-
node, "Return statements are not supported.");
2750-
}
2751-
return node.expression.accept(this);
2752-
}
2753-
27542755
@override
27552756
Constant visitAsExpression(AsExpression node) {
27562757
final Constant constant = _evaluateSubexpression(node.operand);

0 commit comments

Comments
 (0)