Use helper methods to handle SyntheticExpressionJudgment

Change-Id: If4f0deef1e0aea630257d6c35c5e771db65ba474
Reviewed-on: https://dart-review.googlesource.com/c/85761
Commit-Queue: Peter von der Ahé <ahe@google.com>
Auto-Submit: Peter von der Ahé <ahe@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
diff --git a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart
index 302f75e..c1b7e65 100644
--- a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart
@@ -96,6 +96,8 @@
 
 import 'forest.dart' show Forest;
 
+import 'kernel_shadow_ast.dart' as shadow show SyntheticExpressionJudgment;
+
 import 'redirecting_factory_body.dart'
     show
         RedirectingFactoryBody,
@@ -104,14 +106,14 @@
         getRedirectionTarget,
         isRedirectingFactory;
 
+import 'type_algorithms.dart' show calculateBounds;
+
 import 'kernel_api.dart';
 
 import 'kernel_ast_api.dart';
 
 import 'kernel_builder.dart';
 
-import 'type_algorithms.dart' show calculateBounds;
-
 // TODO(ahe): Remove this and ensure all nodes have a location.
 const noLocation = null;
 
@@ -607,11 +609,10 @@
             Initializer initializer;
             if (member.isExternal) {
               initializer = buildInvalidInitializer(
-                  buildProblem(
-                          fasta.messageExternalConstructorWithFieldInitializers,
-                          formal.charOffset,
-                          formal.name.length)
-                      .desugared,
+                  desugarSyntheticExpression(buildProblem(
+                      fasta.messageExternalConstructorWithFieldInitializers,
+                      formal.charOffset,
+                      formal.name.length)),
                   formal.charOffset);
             } else {
               initializer = buildFieldInitializer(
@@ -868,12 +869,11 @@
         String name = constructorNameForDiagnostics(initialTarget.name.name,
             className: initialTarget.enclosingClass.name);
         // TODO(dmitryas): Report this error earlier.
-        replacementNode = buildProblem(
-                fasta.templateCyclicRedirectingFactoryConstructors
-                    .withArguments(name),
-                initialTarget.fileOffset,
-                name.length)
-            .desugared;
+        replacementNode = desugarSyntheticExpression(buildProblem(
+            fasta.templateCyclicRedirectingFactoryConstructors
+                .withArguments(name),
+            initialTarget.fileOffset,
+            name.length));
       } else if (resolvedTarget is Constructor &&
           resolvedTarget.enclosingClass.isAbstract) {
         replacementNode = evaluateArgumentsBefore(
@@ -894,14 +894,15 @@
           assert(redirectingFactoryBody.isUnresolved);
           String errorName = redirectingFactoryBody.unresolvedName;
 
-          replacementNode = new SyntheticExpressionJudgment(
+          replacementNode = wrapSyntheticExpression(
               throwNoSuchMethodError(
                   forest.literalNull(null)..fileOffset = invocation.fileOffset,
                   errorName,
                   forest.arguments(invocation.arguments.positional, null,
                       types: invocation.arguments.types,
                       named: invocation.arguments.named),
-                  initialTarget.fileOffset));
+                  initialTarget.fileOffset),
+              invocation.fileOffset);
         } else {
           Substitution substitution = Substitution.fromPairs(
               initialTarget.function.typeParameters,
@@ -926,9 +927,8 @@
           // TODO(dmitryas): Find a better way to unwrap
           // [SyntheticExpressionJudgment] or not to build it in the first place
           // when it's not needed.
-          if (replacementNode is SyntheticExpressionJudgment) {
-            replacementNode =
-                (replacementNode as SyntheticExpressionJudgment).desugared;
+          if (replacementNode is shadow.SyntheticExpressionJudgment) {
+            replacementNode = desugarSyntheticExpression(replacementNode);
           }
         }
       }
@@ -1106,12 +1106,11 @@
           length = (constructor.parent as Class).name.length;
         }
         initializer = buildInvalidInitializer(
-            buildProblem(
-                    fasta.templateSuperclassHasNoDefaultConstructor
-                        .withArguments(superclass),
-                    builder.charOffset,
-                    length)
-                .desugared,
+            desugarSyntheticExpression(buildProblem(
+                fasta.templateSuperclassHasNoDefaultConstructor
+                    .withArguments(superclass),
+                builder.charOffset,
+                length)),
             builder.charOffset);
       } else {
         initializer = buildSuperInitializer(
@@ -1441,7 +1440,7 @@
     if (legacyMode && constantContext == ConstantContext.none) {
       addProblem(message.messageObject, message.charOffset, message.length,
           wasHandled: true, context: context);
-      return new SyntheticExpressionJudgment(
+      return wrapSyntheticExpression(
           forest.throwExpression(
               null,
               library.loader.instantiateNoSuchMethodError(
@@ -1451,13 +1450,12 @@
                   isSetter: isSetter,
                   isStatic: isStatic,
                   isTopLevel: !isStatic && !isSuper))
-            ..fileOffset = charOffset)
-        ..fileOffset = charOffset;
+            ..fileOffset = charOffset,
+          charOffset);
     }
-    return buildProblem(
-            message.messageObject, message.charOffset, message.length,
-            context: context)
-        .desugared;
+    return desugarSyntheticExpression(buildProblem(
+        message.messageObject, message.charOffset, message.length,
+        context: context));
   }
 
   @override
@@ -2522,12 +2520,10 @@
     library.checkBoundsInType(type, typeEnvironment, operator.charOffset);
     Expression expression = popForValue();
     if (constantContext != ConstantContext.none) {
-      push(buildProblem(
-              fasta.templateNotConstantExpression
-                  .withArguments('As expression'),
-              operator.charOffset,
-              operator.length)
-          .desugared);
+      push(desugarSyntheticExpression(buildProblem(
+          fasta.templateNotConstantExpression.withArguments('As expression'),
+          operator.charOffset,
+          operator.length)));
     } else {
       Expression node = forest.asExpression(expression, type, operator);
       push(node);
@@ -2548,12 +2544,10 @@
           type, functionNestingLevel);
     }
     if (constantContext != ConstantContext.none) {
-      push(buildProblem(
-              fasta.templateNotConstantExpression
-                  .withArguments('Is expression'),
-              isOperator.charOffset,
-              isOperator.length)
-          .desugared);
+      push(desugarSyntheticExpression(buildProblem(
+          fasta.templateNotConstantExpression.withArguments('Is expression'),
+          isOperator.charOffset,
+          isOperator.length)));
     } else {
       push(isExpression);
     }
@@ -3083,9 +3077,8 @@
       if ((isConst || constantContext == ConstantContext.inferred) &&
           !target.isConst) {
         return new InvalidConstructorInvocationJudgment(
-            buildProblem(
-                    fasta.messageNonConstConstructor, charOffset, charLength)
-                .desugared,
+            desugarSyntheticExpression(buildProblem(
+                fasta.messageNonConstConstructor, charOffset, charLength)),
             target,
             arguments);
       }
@@ -3103,8 +3096,8 @@
         if ((isConst || constantContext == ConstantContext.inferred) &&
             !procedure.isConst) {
           return new InvalidConstructorInvocationJudgment(
-              buildProblem(fasta.messageNonConstFactory, charOffset, charLength)
-                  .desugared,
+              desugarSyntheticExpression(buildProblem(
+                  fasta.messageNonConstFactory, charOffset, charLength)),
               target,
               arguments);
         }
@@ -3286,11 +3279,13 @@
       push(new ParserErrorGenerator(
           this, nameToken, fasta.messageSyntheticToken));
     } else {
-      push(new SyntheticExpressionJudgment(throwNoSuchMethodError(
-          forest.literalNull(null)..fileOffset = offset,
-          debugName(getNodeName(type), name),
-          arguments,
-          nameToken.charOffset)));
+      push(wrapSyntheticExpression(
+          throwNoSuchMethodError(
+              forest.literalNull(null)..fileOffset = offset,
+              debugName(getNodeName(type), name),
+              arguments,
+              nameToken.charOffset),
+          offset));
     }
     constantContext = savedConstantContext;
   }
@@ -3574,11 +3569,14 @@
           // This must have been a compile-time error.
           assert(isErroneousNode(oldInitializer));
 
-          push(new SyntheticExpressionJudgment(new Let(
-              new VariableDeclaration.forValue(oldInitializer)
-                ..fileOffset = forest.readOffset(expression),
-              expression)
-            ..fileOffset = forest.readOffset(expression)));
+          int offset = forest.readOffset(expression);
+          push(wrapSyntheticExpression(
+              new Let(
+                  new VariableDeclaration.forValue(oldInitializer)
+                    ..fileOffset = offset,
+                  expression)
+                ..fileOffset = offset,
+              offset));
         } else {
           push(expression);
         }
@@ -3692,7 +3690,7 @@
     }
     VariableDeclaration variable;
     bool declaresVariable = false;
-    SyntheticExpressionJudgment syntheticAssignment;
+    Expression syntheticAssignment;
     if (lvalue is VariableDeclaration) {
       declaresVariable = true;
       variable = lvalue;
@@ -3725,8 +3723,8 @@
           ? fasta.messageForInLoopExactlyOneVariable
           : fasta.messageForInLoopNotAssignable;
       Token token = forToken.next.next;
-      variable = new VariableDeclaration.forValue(
-          buildProblem(message, offsetForToken(token), lengthForToken(token)));
+      variable = new VariableDeclaration.forValue(desugarSyntheticExpression(
+          buildProblem(message, offsetForToken(token), lengthForToken(token))));
     }
     Statement result = new ForInJudgment(
         variable, expression, kernelBody, declaresVariable, syntheticAssignment,
@@ -4225,13 +4223,12 @@
   @override
   void handleInvalidStatement(Token token, Message message) {
     Statement statement = pop();
-    push(new ExpressionStatement(
-        buildProblem(message, statement.fileOffset, noLength).desugared));
+    push(new ExpressionStatement(desugarSyntheticExpression(
+        buildProblem(message, statement.fileOffset, noLength))));
   }
 
   @override
-  SyntheticExpressionJudgment buildProblem(
-      Message message, int charOffset, int length,
+  Expression buildProblem(Message message, int charOffset, int length,
       {List<LocatedMessage> context, bool suppressMessage: false}) {
     if (!suppressMessage) {
       addProblem(message, charOffset, length,
@@ -4239,8 +4236,8 @@
     }
     String text = library.loader.target.context
         .format(message.withLocation(uri, charOffset, length), Severity.error);
-    return new SyntheticExpressionJudgment(
-        new InvalidExpression(text)..fileOffset = charOffset);
+    return wrapSyntheticExpression(
+        new InvalidExpression(text)..fileOffset = charOffset, charOffset);
   }
 
   @override
@@ -4270,10 +4267,9 @@
     }
     return new Let(
         new VariableDeclaration.forValue(
-            buildProblem(
-                    message.messageObject, message.charOffset, message.length,
-                    context: context)
-                .desugared,
+            desugarSyntheticExpression(buildProblem(
+                message.messageObject, message.charOffset, message.length,
+                context: context)),
             type: const BottomType())
           ..fileOffset = offset,
         expression);
@@ -4358,12 +4354,12 @@
     return new ShadowInvalidFieldInitializer(
         field,
         value,
-        new VariableDeclaration.forValue(buildProblem(
+        new VariableDeclaration.forValue(desugarSyntheticExpression(
+            buildProblem(
                 fasta.templateFinalInstanceVariableAlreadyInitialized
                     .withArguments(name),
                 offset,
-                noLength)
-            .desugared))
+                noLength))))
       ..fileOffset = offset;
   }
 
@@ -4386,12 +4382,10 @@
       // Duplicated name, already reported.
       return new LocalInitializer(
           new VariableDeclaration.forValue(
-              buildProblem(
-                      fasta.templateDuplicatedDeclarationUse
-                          .withArguments(name),
-                      fieldNameOffset,
-                      name.length)
-                  .desugared
+              desugarSyntheticExpression(buildProblem(
+                  fasta.templateDuplicatedDeclarationUse.withArguments(name),
+                  fieldNameOffset,
+                  name.length))
                 ..fileOffset = fieldNameOffset)
             ..fileOffset = fieldNameOffset)
         ..fileOffset = fieldNameOffset;
@@ -4451,11 +4445,10 @@
       }
     } else {
       return buildInvalidInitializer(
-          buildProblem(
-                  fasta.templateInitializerForStaticField.withArguments(name),
-                  fieldNameOffset,
-                  name.length)
-              .desugared,
+          desugarSyntheticExpression(buildProblem(
+              fasta.templateInitializerForStaticField.withArguments(name),
+              fieldNameOffset,
+              name.length)),
           fieldNameOffset);
     }
   }
@@ -4468,9 +4461,10 @@
       return buildInvalidSuperInitializer(
           constructor,
           forest.castArguments(arguments),
-          buildProblem(fasta.messageConstConstructorWithNonConstSuper,
-                  charOffset, constructor.name.name.length)
-              .desugared,
+          desugarSyntheticExpression(buildProblem(
+              fasta.messageConstConstructorWithNonConstSuper,
+              charOffset,
+              constructor.name.name.length)),
           charOffset);
     }
     needsImplicitSuperInitializer = false;
@@ -4758,6 +4752,18 @@
     }
     return name.isEmpty ? className : "$className.$name";
   }
+
+  @override
+  Expression wrapSyntheticExpression(Expression desugared, int charOffset) {
+    return new shadow.SyntheticExpressionJudgment(desugared)
+      ..fileOffset = charOffset;
+  }
+
+  @override
+  Expression desugarSyntheticExpression(Expression node) {
+    shadow.SyntheticExpressionJudgment shadowNode = node;
+    return shadowNode.desugared;
+  }
 }
 
 class Operator {
diff --git a/pkg/front_end/lib/src/fasta/kernel/expression_generator.dart b/pkg/front_end/lib/src/fasta/kernel/expression_generator.dart
index b3c0434..24b47b4 100644
--- a/pkg/front_end/lib/src/fasta/kernel/expression_generator.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/expression_generator.dart
@@ -71,7 +71,6 @@
         Member,
         Name,
         Procedure,
-        SyntheticExpressionJudgment,
         UnresolvedTargetInvocationJudgment,
         VariableDeclaration;
 
@@ -185,10 +184,8 @@
   Initializer buildFieldInitializer(Map<String, int> initializedFields) {
     int offset = offsetForToken(token);
     return helper.buildInvalidInitializer(
-        helper
-            .buildProblem(
-                messageInvalidInitializer, offset, lengthForToken(token))
-            .desugared,
+        helper.desugarSyntheticExpression(helper.buildProblem(
+            messageInvalidInitializer, offset, lengthForToken(token))),
         offset);
   }
 
@@ -668,7 +665,7 @@
   /// Pass [arguments] that must be evaluated before throwing an error.  At
   /// most one of [isGetter] and [isSetter] should be true and they're passed
   /// to [ExpressionGeneratorHelper.throwNoSuchMethodError] if it is used.
-  SyntheticExpressionJudgment buildError(Arguments arguments,
+  Expression buildError(Arguments arguments,
       {bool isGetter: false, bool isSetter: false, int offset});
 
   Name get name => unsupported("name", offsetForToken(token), uri);
@@ -680,8 +677,8 @@
 
   @override
   Initializer buildFieldInitializer(Map<String, int> initializedFields) {
-    return helper.buildInvalidInitializer(
-        buildError(forest.argumentsEmpty(token), isSetter: true).desugared);
+    return helper.buildInvalidInitializer(helper.desugarSyntheticExpression(
+        buildError(forest.argumentsEmpty(token), isSetter: true)));
   }
 
   @override
@@ -773,7 +770,9 @@
           arguments, helper.buildDartTypeArguments(typeArguments));
     }
     return new InvalidConstructorInvocationJudgment(
-        buildError(arguments).desugared, null, arguments);
+        helper.desugarSyntheticExpression(buildError(arguments)),
+        null,
+        arguments);
   }
 }
 
@@ -792,22 +791,25 @@
   @override
   Expression doInvocation(int charOffset, Arguments arguments) {
     return new UnresolvedTargetInvocationJudgment(
-        buildError(arguments, offset: charOffset).desugared, arguments)
+        helper.desugarSyntheticExpression(
+            buildError(arguments, offset: charOffset)),
+        arguments)
       ..fileOffset = arguments.fileOffset;
   }
 
   @override
-  SyntheticExpressionJudgment buildError(Arguments arguments,
+  Expression buildError(Arguments arguments,
       {bool isGetter: false, bool isSetter: false, int offset}) {
     offset ??= offsetForToken(this.token);
-    return new SyntheticExpressionJudgment(helper.throwNoSuchMethodError(
-        forest.literalNull(null)..fileOffset = offset,
-        plainNameForRead,
-        arguments,
-        offset,
-        isGetter: isGetter,
-        isSetter: isSetter))
-      ..fileOffset = offset;
+    return helper.wrapSyntheticExpression(
+        helper.throwNoSuchMethodError(
+            forest.literalNull(null)..fileOffset = offset,
+            plainNameForRead,
+            arguments,
+            offset,
+            isGetter: isGetter,
+            isSetter: isSetter),
+        offset);
   }
 
   @override
@@ -1135,11 +1137,13 @@
 
   @override
   Expression doInvocation(int offset, Arguments arguments) {
-    return new SyntheticExpressionJudgment(helper.throwNoSuchMethodError(
-        forest.literalNull(null)..fileOffset = offset,
-        plainNameForRead,
-        arguments,
-        offsetForToken(token)));
+    return helper.wrapSyntheticExpression(
+        helper.throwNoSuchMethodError(
+            forest.literalNull(null)..fileOffset = offset,
+            plainNameForRead,
+            arguments,
+            offsetForToken(token)),
+        offsetForToken(token));
   }
 
   @override
@@ -1181,7 +1185,7 @@
   @override
   void printOn(StringSink sink) {}
 
-  SyntheticExpressionJudgment buildProblem() {
+  Expression buildProblem() {
     return helper.buildProblem(message, offsetForToken(token), noLength,
         suppressMessage: true);
   }
@@ -1222,7 +1226,8 @@
   Expression makeInvalidWrite(Expression value) => buildProblem();
 
   Initializer buildFieldInitializer(Map<String, int> initializedFields) {
-    return helper.buildInvalidInitializer(buildProblem().desugared);
+    return helper.buildInvalidInitializer(
+        helper.desugarSyntheticExpression(buildProblem()));
   }
 
   Expression doInvocation(int offset, Arguments arguments) {
diff --git a/pkg/front_end/lib/src/fasta/kernel/expression_generator_helper.dart b/pkg/front_end/lib/src/fasta/kernel/expression_generator_helper.dart
index 61e8b57..282afc2 100644
--- a/pkg/front_end/lib/src/fasta/kernel/expression_generator_helper.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/expression_generator_helper.dart
@@ -150,4 +150,6 @@
 
   void reportDuplicatedDeclaration(
       Declaration existing, String name, int charOffset);
+
+  Expression wrapSyntheticExpression(Expression node, int charOffset);
 }
diff --git a/pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart b/pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart
index ea35492..755d2a0 100644
--- a/pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart
@@ -573,12 +573,11 @@
 
     int intValue = node.asInt64();
     if (intValue == null) {
-      Expression replacement = inferrer.helper
-          .buildProblem(
+      Expression replacement = inferrer.helper.desugarSyntheticExpression(
+          inferrer.helper.buildProblem(
               templateIntegerLiteralIsOutOfRange.withArguments(node.literal),
               node.fileOffset,
-              node.literal.length)
-          .desugared;
+              node.literal.length));
       node.parent.replaceChild(node, replacement);
       node.inferredType = const BottomType();
       return null;
@@ -850,13 +849,12 @@
           }
           int intValue = receiver.asInt64(negated: true);
           if (intValue == null) {
-            Expression error = inferrer.helper
-                .buildProblem(
+            Expression error = inferrer.helper.desugarSyntheticExpression(
+                inferrer.helper.buildProblem(
                     templateIntegerLiteralIsOutOfRange
                         .withArguments(receiver.literal),
                     receiver.fileOffset,
-                    receiver.literal.length)
-                .desugared;
+                    receiver.literal.length));
             node.parent.replaceChild(node, error);
             node.inferredType = const BottomType();
             return null;
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_ast_api.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_ast_api.dart
index 7e7ddca..3c4e3ae 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_ast_api.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_ast_api.dart
@@ -120,7 +120,6 @@
         SuperPropertyGetJudgment,
         SwitchCaseJudgment,
         SwitchStatementJudgment,
-        SyntheticExpressionJudgment,
         UnresolvedTargetInvocationJudgment,
         UnresolvedVariableAssignmentJudgment,
         VariableAssignmentJudgment,
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_expression_generator.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_expression_generator.dart
index cb68c1b..fa1dd06 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_expression_generator.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_expression_generator.dart
@@ -106,7 +106,6 @@
         SuperMethodInvocationJudgment,
         SuperPropertyGetJudgment,
         SuperPropertySet,
-        SyntheticExpressionJudgment,
         TreeNode,
         TypeParameter,
         UnresolvedVariableAssignmentJudgment,
@@ -239,22 +238,26 @@
 
   @override
   Expression makeInvalidRead() {
-    return new SyntheticExpressionJudgment(helper.throwNoSuchMethodError(
-        forest.literalNull(token),
-        plainNameForRead,
-        forest.argumentsEmpty(noLocation),
-        offsetForToken(token),
-        isGetter: true));
+    return helper.wrapSyntheticExpression(
+        helper.throwNoSuchMethodError(
+            forest.literalNull(token),
+            plainNameForRead,
+            forest.argumentsEmpty(noLocation),
+            offsetForToken(token),
+            isGetter: true),
+        offsetForToken(token));
   }
 
   @override
   Expression makeInvalidWrite(Expression value) {
-    return new SyntheticExpressionJudgment(helper.throwNoSuchMethodError(
-        forest.literalNull(token),
-        plainNameForRead,
-        forest.arguments(<Expression>[value], noLocation),
-        offsetForToken(token),
-        isSetter: true));
+    return helper.wrapSyntheticExpression(
+        helper.throwNoSuchMethodError(
+            forest.literalNull(token),
+            plainNameForRead,
+            forest.arguments(<Expression>[value], noLocation),
+            offsetForToken(token),
+            isSetter: true),
+        offsetForToken(token));
   }
 
   Expression _makeSimpleRead() => _makeRead(null);
@@ -1254,10 +1257,11 @@
         KernelInvalidTypeBuilder declaration = this.declaration;
         helper.addProblemErrorIfConst(
             declaration.message.messageObject, offset, token.length);
-        super.expression = new SyntheticExpressionJudgment(
+        super.expression = helper.wrapSyntheticExpression(
             forest.throwExpression(
                 null, forest.literalString(declaration.message.message, token))
-              ..fileOffset = offset);
+              ..fileOffset = offset,
+            offset);
       } else {
         super.expression = forest.literalType(
             helper.buildDartType(
@@ -1272,13 +1276,15 @@
 
   @override
   Expression makeInvalidWrite(Expression value) {
-    return new SyntheticExpressionJudgment(helper.throwNoSuchMethodError(
-        forest.literalNull(token),
-        plainNameForRead,
-        forest.arguments(<Expression>[value], null)
-          ..fileOffset = value.fileOffset,
-        offsetForToken(token),
-        isSetter: true));
+    return helper.wrapSyntheticExpression(
+        helper.throwNoSuchMethodError(
+            forest.literalNull(token),
+            plainNameForRead,
+            forest.arguments(<Expression>[value], null)
+              ..fileOffset = value.fileOffset,
+            offsetForToken(token),
+            isSetter: true),
+        offsetForToken(token));
   }
 
   @override
@@ -1430,11 +1436,12 @@
   UnresolvedVariableAssignmentJudgment _buildUnresolvedVariableAssignment(
       bool isCompound, Expression value) {
     return new UnresolvedVariableAssignmentJudgment(
-      buildError(forest.arguments(<Expression>[value], token), isSetter: true)
-          .desugared,
-      isCompound,
-      value,
-    )..fileOffset = token.charOffset;
+        helper.desugarSyntheticExpression(buildError(
+            forest.arguments(<Expression>[value], token),
+            isSetter: true)),
+        isCompound,
+        value)
+      ..fileOffset = token.charOffset;
   }
 }
 
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_expression_generator_impl.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_expression_generator_impl.dart
index 009288ffe..9b2bf83 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_expression_generator_impl.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_expression_generator_impl.dart
@@ -46,8 +46,7 @@
     }
   }
 
-  SyntheticExpressionJudgment buildFieldInitializerError(
-      Map<String, int> initializedFields) {
+  Expression buildFieldInitializerError(Map<String, int> initializedFields) {
     String keyword = isSuper ? "super" : "this";
     return helper.buildProblem(
         templateThisOrSuperAccessInFieldInitializer.withArguments(keyword),
@@ -57,7 +56,8 @@
 
   @override
   Initializer buildFieldInitializer(Map<String, int> initializedFields) {
-    Expression error = buildFieldInitializerError(initializedFields).desugared;
+    Expression error = helper.desugarSyntheticExpression(
+        buildFieldInitializerError(initializedFields));
     return helper.buildInvalidInitializer(error, error.fileOffset);
   }
 
@@ -139,14 +139,15 @@
           .withLocation(uri, offsetForToken(token), lengthForToken(token));
     }
     if (message != null) {
-      return helper.buildInvalidInitializer(new SyntheticExpressionJudgment(
+      return helper.buildInvalidInitializer(helper.wrapSyntheticExpression(
           helper.throwNoSuchMethodError(
               forest.literalNull(null)..fileOffset = offset,
               helper.constructorNameForDiagnostics(name.name, isSuper: isSuper),
               arguments,
               offset,
               isSuper: isSuper,
-              message: message)));
+              message: message),
+          offset));
     } else if (isSuper) {
       return helper.buildSuperInitializer(
           false, constructor, arguments, offset);
@@ -189,10 +190,10 @@
   }
 
   Expression buildAssignmentError() {
-    return helper
-        .buildProblem(isSuper ? messageCannotAssignToSuper : messageNotAnLvalue,
-            offsetForToken(token), token.length)
-        .desugared;
+    return helper.desugarSyntheticExpression(helper.buildProblem(
+        isSuper ? messageCannotAssignToSuper : messageNotAnLvalue,
+        offsetForToken(token),
+        token.length));
   }
 
   @override
@@ -236,7 +237,7 @@
   String get debugName => "IncompleteErrorGenerator";
 
   @override
-  SyntheticExpressionJudgment buildError(Arguments arguments,
+  Expression buildError(Arguments arguments,
       {bool isGetter: false, bool isSetter: false, int offset}) {
     int length = noLength;
     if (offset == null) {
@@ -412,10 +413,10 @@
 
   Expression makeInvalidWrite(Expression value) {
     return new InvalidWriteJudgment(
-        helper
-            .buildProblem(messageCannotAssignToParenthesizedExpression,
-                offsetForToken(token), lengthForToken(token))
-            .desugared,
+        helper.desugarSyntheticExpression(helper.buildProblem(
+            messageCannotAssignToParenthesizedExpression,
+            offsetForToken(token),
+            lengthForToken(token))),
         expression);
   }
 }
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_procedure_builder.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_procedure_builder.dart
index 5bf9349..03accd9 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_procedure_builder.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_procedure_builder.dart
@@ -505,7 +505,8 @@
     assert(lastInitializer == superInitializer ||
         lastInitializer == redirectingInitializer);
     Initializer error = helper.buildInvalidInitializer(
-        helper.buildProblem(message, charOffset, noLength).desugared,
+        helper.desugarSyntheticExpression(
+            helper.buildProblem(message, charOffset, noLength)),
         charOffset);
     initializers.add(error..parent = constructor);
     initializers.add(lastInitializer);
@@ -529,10 +530,8 @@
       } else if (constructor.initializers.isNotEmpty) {
         Initializer first = constructor.initializers.first;
         Initializer error = helper.buildInvalidInitializer(
-            helper
-                .buildProblem(
-                    messageThisInitializerNotAlone, first.fileOffset, noLength)
-                .desugared,
+            helper.desugarSyntheticExpression(helper.buildProblem(
+                messageThisInitializerNotAlone, first.fileOffset, noLength)),
             first.fileOffset);
         initializers.add(error..parent = constructor);
       } else {
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart
index 51f6c11..88ad954 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart
@@ -918,13 +918,12 @@
       ? '0x${asDouble.toRadixString(16)}'
       : asDouble.toString();
   int length = literal?.length ?? noLength;
-  return inferrer.helper
+  return inferrer.helper.desugarSyntheticExpression(inferrer.helper
       .buildProblem(
           templateWebLiteralCannotBeRepresentedExactly.withArguments(
               text, nearest),
           charOffset,
-          length)
-      .desugared;
+          length));
 }
 
 /// Concrete shadow object representing an integer literal in kernel form.
diff --git a/pkg/front_end/lib/src/fasta/type_inference/inference_helper.dart b/pkg/front_end/lib/src/fasta/type_inference/inference_helper.dart
index 3c704fe..ff3b0d4 100644
--- a/pkg/front_end/lib/src/fasta/type_inference/inference_helper.dart
+++ b/pkg/front_end/lib/src/fasta/type_inference/inference_helper.dart
@@ -8,15 +8,12 @@
 
 import '../fasta_codes.dart' show LocatedMessage, Message;
 
-import '../kernel/kernel_shadow_ast.dart' show SyntheticExpressionJudgment;
-
 abstract class InferenceHelper {
   CoreTypes get coreTypes;
 
   Uri get uri;
 
-  SyntheticExpressionJudgment buildProblem(
-      Message message, int charOffset, int length,
+  Expression buildProblem(Message message, int charOffset, int length,
       {List<LocatedMessage> context, bool suppressMessage});
 
   LocatedMessage checkArgumentsForType(
@@ -30,4 +27,6 @@
 
   String constructorNameForDiagnostics(String name,
       {String className, bool isSuper});
+
+  Expression desugarSyntheticExpression(Expression node);
 }
diff --git a/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart b/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart
index 7390859..ec3c1f1 100644
--- a/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart
+++ b/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart
@@ -783,12 +783,10 @@
           new Let(
               new VariableDeclaration.forValue(receiver)
                 ..fileOffset = receiver.fileOffset,
-              helper
-                  .buildProblem(
-                      errorTemplate.withArguments(name.name, receiverType),
-                      fileOffset,
-                      length)
-                  .desugared)
+              helper.desugarSyntheticExpression(helper.buildProblem(
+                  errorTemplate.withArguments(name.name, receiverType),
+                  fileOffset,
+                  length)))
             ..fileOffset = fileOffset);
     }
     return interfaceMember;
@@ -1285,10 +1283,10 @@
     if (named.length == 2) {
       if (named[0].name == named[1].name) {
         var name = named[1].name;
-        var error = helper
-            .buildProblem(templateDuplicatedNamedArgument.withArguments(name),
-                named[1].fileOffset, name.length)
-            .desugared;
+        var error = helper.desugarSyntheticExpression(helper.buildProblem(
+            templateDuplicatedNamedArgument.withArguments(name),
+            named[1].fileOffset,
+            name.length));
         arguments.named = [new kernel.NamedExpression(named[1].name, error)];
         formalTypes.removeLast();
         actualTypes.removeLast();
@@ -1303,10 +1301,11 @@
         if (seenNames.containsKey(name)) {
           hasProblem = true;
           var prevNamedExpression = seenNames[name];
-          prevNamedExpression.value = helper
-              .buildProblem(templateDuplicatedNamedArgument.withArguments(name),
-                  expression.fileOffset, name.length)
-              .desugared
+          prevNamedExpression.value = helper.desugarSyntheticExpression(
+              helper.buildProblem(
+                  templateDuplicatedNamedArgument.withArguments(name),
+                  expression.fileOffset,
+                  name.length))
             ..parent = prevNamedExpression;
           formalTypes.removeAt(namedTypeIndex);
           actualTypes.removeAt(namedTypeIndex);
diff --git a/pkg/front_end/testcases/rasta/issue_000042.dart.strong.expect b/pkg/front_end/testcases/rasta/issue_000042.dart.strong.expect
index a54413f..1c85c97 100644
--- a/pkg/front_end/testcases/rasta/issue_000042.dart.strong.expect
+++ b/pkg/front_end/testcases/rasta/issue_000042.dart.strong.expect
@@ -39,7 +39,7 @@
 import "dart:core" as core;
 
 static method main() → dynamic {
-  for (final dynamic #t1 = let dynamic _ = null in invalid-expression "pkg/front_end/testcases/rasta/issue_000042.dart:6:8: Error: A for-in loop can't have more than one loop variable.
+  for (final dynamic #t1 = invalid-expression "pkg/front_end/testcases/rasta/issue_000042.dart:6:8: Error: A for-in loop can't have more than one loop variable.
   for (var x, y in []) {}
        ^^^" in <dynamic>[]) {
   }
diff --git a/pkg/front_end/testcases/rasta/issue_000042.dart.strong.transformed.expect b/pkg/front_end/testcases/rasta/issue_000042.dart.strong.transformed.expect
index 709363f..0cf518b 100644
--- a/pkg/front_end/testcases/rasta/issue_000042.dart.strong.transformed.expect
+++ b/pkg/front_end/testcases/rasta/issue_000042.dart.strong.transformed.expect
@@ -19,7 +19,7 @@
 import "dart:core" as core;
 
 static method main() → dynamic {
-  for (final dynamic #t1 = let dynamic _ = null in invalid-expression "pkg/front_end/testcases/rasta/issue_000042.dart:6:8: Error: A for-in loop can't have more than one loop variable.
+  for (final dynamic #t1 = invalid-expression "pkg/front_end/testcases/rasta/issue_000042.dart:6:8: Error: A for-in loop can't have more than one loop variable.
   for (var x, y in []) {}
        ^^^" in <dynamic>[]) {
   }
diff --git a/pkg/front_end/testcases/rasta/unresolved_for_in.dart.legacy.expect b/pkg/front_end/testcases/rasta/unresolved_for_in.dart.legacy.expect
index 6f451c8..70ea902 100644
--- a/pkg/front_end/testcases/rasta/unresolved_for_in.dart.legacy.expect
+++ b/pkg/front_end/testcases/rasta/unresolved_for_in.dart.legacy.expect
@@ -112,7 +112,7 @@
       let dynamic _ = null in let dynamic _ = null in throw new core::NoSuchMethodError::withInvocation(null, new core::_InvocationMirror::_withType(#VoidFunction, 34, const <core::Type>[], core::List::unmodifiable<dynamic>(<dynamic>[#t4]), core::Map::unmodifiable<core::Symbol, dynamic>(const <core::Symbol, dynamic>{})));
       core::print(() → void);
     }
-    for (final dynamic #t5 = let dynamic _ = null in invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:23:10: Error: Can't assign to this, so it can't be used in a for-in loop.
+    for (final dynamic #t5 = invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:23:10: Error: Can't assign to this, so it can't be used in a for-in loop.
     for (1 in x) {
          ^" in x) {
       core::print(this.key);
@@ -141,7 +141,7 @@
     let dynamic _ = null in let dynamic _ = null in throw new core::NoSuchMethodError::withInvocation(null, new core::_InvocationMirror::_withType(#VoidFunction, 34, const <core::Type>[], core::List::unmodifiable<dynamic>(<dynamic>[#t9]), core::Map::unmodifiable<core::Symbol, dynamic>(const <core::Symbol, dynamic>{})));
     core::print(() → void);
   }
-  for (final dynamic #t10 = let dynamic _ = null in invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:43:8: Error: Can't assign to this, so it can't be used in a for-in loop.
+  for (final dynamic #t10 = invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:43:8: Error: Can't assign to this, so it can't be used in a for-in loop.
   for (1 in arguments) {
        ^" in arguments) {
     core::print(let dynamic _ = null in throw new core::NoSuchMethodError::withInvocation(null, new core::_InvocationMirror::_withType(#key, 33, const <core::Type>[], const <dynamic>[], core::Map::unmodifiable<core::Symbol, dynamic>(const <core::Symbol, dynamic>{}))));
diff --git a/pkg/front_end/testcases/rasta/unresolved_for_in.dart.legacy.transformed.expect b/pkg/front_end/testcases/rasta/unresolved_for_in.dart.legacy.transformed.expect
index 3957542..a833422 100644
--- a/pkg/front_end/testcases/rasta/unresolved_for_in.dart.legacy.transformed.expect
+++ b/pkg/front_end/testcases/rasta/unresolved_for_in.dart.legacy.transformed.expect
@@ -38,7 +38,7 @@
       let dynamic _ = null in let dynamic _ = null in throw new core::NoSuchMethodError::withInvocation(null, new core::_InvocationMirror::_withType(#VoidFunction, 34, const <core::Type>[], core::List::unmodifiable<dynamic>(<dynamic>[#t4]), core::Map::unmodifiable<core::Symbol, dynamic>(const <core::Symbol, dynamic>{})));
       core::print(() → void);
     }
-    for (final dynamic #t5 = let dynamic _ = null in invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:23:10: Error: Can't assign to this, so it can't be used in a for-in loop.
+    for (final dynamic #t5 = invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:23:10: Error: Can't assign to this, so it can't be used in a for-in loop.
     for (1 in x) {
          ^" in x) {
       core::print(this.key);
@@ -67,7 +67,7 @@
     let dynamic _ = null in let dynamic _ = null in throw new core::NoSuchMethodError::withInvocation(null, new core::_InvocationMirror::_withType(#VoidFunction, 34, const <core::Type>[], core::List::unmodifiable<dynamic>(<dynamic>[#t9]), core::Map::unmodifiable<core::Symbol, dynamic>(const <core::Symbol, dynamic>{})));
     core::print(() → void);
   }
-  for (final dynamic #t10 = let dynamic _ = null in invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:43:8: Error: Can't assign to this, so it can't be used in a for-in loop.
+  for (final dynamic #t10 = invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:43:8: Error: Can't assign to this, so it can't be used in a for-in loop.
   for (1 in arguments) {
        ^" in arguments) {
     core::print(let dynamic _ = null in throw new core::NoSuchMethodError::withInvocation(null, new core::_InvocationMirror::_withType(#key, 33, const <core::Type>[], const <dynamic>[], core::Map::unmodifiable<core::Symbol, dynamic>(const <core::Symbol, dynamic>{}))));