[Kernel] Allow constant declarations without an initializer

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>
diff --git a/pkg/kernel/lib/transformations/constants.dart b/pkg/kernel/lib/transformations/constants.dart
index 4b1208c..ed4095a 100644
--- a/pkg/kernel/lib/transformations/constants.dart
+++ b/pkg/kernel/lib/transformations/constants.dart
@@ -235,32 +235,32 @@
   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;
-      } else {
-        // Since we convert all use-sites of constants, the constant
-        // [VariableDeclaration] is unused and we'll therefore remove it.
-        return null;
-      }
-    }
     if (node.initializer != null) {
-      node.initializer = node.initializer.accept(this)..parent = 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) {
+          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 {
+        node.initializer = node.initializer.accept(this)..parent = node;
+      }
     }
     return node;
   }