Skip to content

Commit

Permalink
[Kernel] Allow constant declarations without an initializer
Browse files Browse the repository at this point in the history
These can arise in erroneous cases which have already been reported,
e.g., in a for-in loop with a const declaration.  The constant
evaluator should not assume a non-null initializer in this case.

Change-Id: I2540cb9c659d33e23b6c00d4a8bbf56c404d8c1d
Reviewed-on: https://dart-review.googlesource.com/c/89280
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Auto-Submit: Kevin Millikin <kmillikin@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
  • Loading branch information
Kevin Millikin authored and commit-bot@chromium.org committed Jan 14, 2019
1 parent b0855ff commit 1c9eb3c
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions pkg/kernel/lib/transformations/constants.dart
Expand Up @@ -235,33 +235,33 @@ class ConstantsTransformer extends Transformer {
visitVariableDeclaration(VariableDeclaration node) {
transformAnnotations(node.annotations, node);

if (node.isConst) {
final Constant constant = tryEvaluateWithContext(node, node.initializer);

// If there was a constant evaluation error we will not continue and
// simply keep the old [node].
if (constant == null) {
return node;
}

constantEvaluator.env.addVariableValue(node, constant);

if (keepVariables) {
// So the value of the variable is still available for debugging
// purposes we convert the constant variable to be a final variable
// initialized to the evaluated constant expression.
node.initializer = new ConstantExpression(constant)..parent = node;
node.isFinal = true;
node.isConst = false;
if (node.initializer != null) {
if (node.isConst) {
final Constant constant =
tryEvaluateWithContext(node, node.initializer);

// If there was a constant evaluation error we will not continue and
// simply keep the old [node].
if (constant != null) {
constantEvaluator.env.addVariableValue(node, constant);

if (keepVariables) {
// So the value of the variable is still available for debugging
// purposes we convert the constant variable to be a final variable
// initialized to the evaluated constant expression.
node.initializer = new ConstantExpression(constant)..parent = node;
node.isFinal = true;
node.isConst = false;
} else {
// Since we convert all use-sites of constants, the constant
// [VariableDeclaration] is unused and we'll therefore remove it.
return null;
}
}
} else {
// Since we convert all use-sites of constants, the constant
// [VariableDeclaration] is unused and we'll therefore remove it.
return null;
node.initializer = node.initializer.accept(this)..parent = node;
}
}
if (node.initializer != null) {
node.initializer = node.initializer.accept(this)..parent = node;
}
return node;
}

Expand Down

0 comments on commit 1c9eb3c

Please sign in to comment.