Check for InvalidExpression presence in resynthesized constant expressions.

R=brianwilkerson@google.com, paulberry@google.com

Change-Id: I9b36b8ad7dcf761150270ec0b6eb4544cf9f0c94
Reviewed-on: https://dart-review.googlesource.com/64800
Reviewed-by: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/kernel/resynthesize.dart b/pkg/analyzer/lib/src/kernel/resynthesize.dart
index b2c96be..bdbb2c8 100644
--- a/pkg/analyzer/lib/src/kernel/resynthesize.dart
+++ b/pkg/analyzer/lib/src/kernel/resynthesize.dart
@@ -446,14 +446,6 @@
 }
 
 /**
- * This exception is thrown when we detect that the Kernel has a compilation
- * error, so we cannot resynthesize the constant expression.
- */
-class _CompilationErrorFound {
-  const _CompilationErrorFound();
-}
-
-/**
  * Builder of [Expression]s from [kernel.Expression]s.
  */
 class _ExprBuilder {
@@ -463,11 +455,10 @@
   _ExprBuilder(this._context, this._contextElement);
 
   Expression build(kernel.Expression expr) {
-    try {
-      return _build(expr);
-    } on _CompilationErrorFound {
+    if (_hasInvalidExpression(expr)) {
       return AstTestFactory.identifier3('#invalidConst');
     }
+    return _build(expr);
   }
 
   ConstructorInitializer buildInitializer(kernel.Initializer k) {
@@ -582,25 +573,6 @@
       return AstTestFactory.mapLiteral(keyword, typeArguments, entries);
     }
 
-    // Invalid initializers and  annotations are represented as Let.
-    if (expr is kernel.Let) {
-      var body = expr.body;
-      if (_isStaticError(body)) {
-        throw const _CompilationErrorFound();
-      }
-      if (body is kernel.Let) {
-        var initializer = body.variable.initializer;
-        if (initializer is kernel.Let && _isStaticError(initializer.body)) {
-          throw const _CompilationErrorFound();
-        }
-      }
-    }
-
-    // Stop if there is an error.
-    if (_isStaticError(expr)) {
-      throw const _CompilationErrorFound();
-    }
-
     if (expr is kernel.StaticGet) {
       return _buildIdentifier(expr.targetReference, isGet: true);
     }
@@ -759,9 +731,6 @@
   }
 
   SimpleIdentifier _buildSimpleIdentifier(kernel.Reference reference) {
-    if (reference == null) {
-      throw const _CompilationErrorFound();
-    }
     String name = reference.canonicalName.name;
     SimpleIdentifier identifier = AstTestFactory.identifier3(name);
     Element element = _getElement(reference);
@@ -852,12 +821,19 @@
     throw new ArgumentError(name);
   }
 
-  /**
-   * Return `true` if the given [expr] throws an instance of
-   * `_ConstantExpressionError` defined in `dart:core`.
-   */
-  static bool _isStaticError(kernel.Expression expr) {
-    return expr is kernel.InvalidExpression;
+  static bool _hasInvalidExpression(kernel.Expression expr) {
+    var visitor = new _HasInvalidExpressionVisitor();
+    expr.accept(visitor);
+    return visitor.result;
+  }
+}
+
+class _HasInvalidExpressionVisitor extends kernel.RecursiveVisitor<void> {
+  bool result = false;
+
+  @override
+  void visitInvalidExpression(kernel.InvalidExpression node) {
+    result = true;
   }
 }