Fix recording of map/literal context type.

Previously, we were using the storage space provided by
`InferenceContext.getContext` for two purposes: 1. to store the
inference context passed down from the parent AST node, and 2. to
store the refined inference context computed by
ResolverVisitor._computeContextType.  After type inference, the data
that remained was the refined interface context (2); however,
ErrorVerifier._checkForRawTypeErrors was expecting to be able to
retrieve the downward inference context in order to report
`HintCode.STRICT_RAW_TYPE` correctly.  This caused a failure in
test_strictRawTypes_emptyMap (in checker_test.dart).

This CL fixes the issue by storing the refined inference context (2)
in a field in `SetOrMapLiteralImpl`, so that it doesn't overwrite the
downward inference context (1).

It also fixes an issue that was previously obscured by this bug, where
we weren't copying over the downward inference context when replacing
a set literal with a map literal.  (This fix will become irrelevant
after the next breaking change release of the analyzer, when we stop
creating different objects for set vs. map literals, and simply use
the base class `SetOrMapLiteralImpl`).

In addition, ResolverVisitor._computeContextType has been renamed to
_computeSetOrMapLiteralContextType to clarify that it only computes
context information for set/map literals.

Change-Id: I62a9c4bb6fe755acf60b881948538216af81b515
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96261
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/dart/ast/ast.dart b/pkg/analyzer/lib/src/dart/ast/ast.dart
index 5144eeb..e07a7e5 100644
--- a/pkg/analyzer/lib/src/dart/ast/ast.dart
+++ b/pkg/analyzer/lib/src/dart/ast/ast.dart
@@ -8990,6 +8990,19 @@
   /// whether the kind has not or cannot be determined.
   _SetOrMapKind _resolvedKind;
 
+  /// The context type computed by
+  /// [ResolverVisitor._computeSetOrMapContextType].
+  ///
+  /// Note that this is not the same as the context pushed down by type
+  /// inference (which can be obtained via [InferenceContext.getContext]).  For
+  /// example, in the following code:
+  ///
+  ///     var m = {};
+  ///
+  /// The context pushed down by type inference is null, whereas the
+  /// `contextType` is `Map<dynamic, dynamic>`.
+  InterfaceType contextType;
+
   /// Initialize a newly created set or map literal. The [constKeyword] can be
   /// `null` if the literal is not a constant. The [typeArguments] can be `null`
   /// if no type arguments were declared. The [elements] can be `null` if the
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index 6bd9085..7d6bc69 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -4775,7 +4775,7 @@
 
   @override
   void visitSetOrMapLiteral(SetOrMapLiteral node) {
-    DartType literalType = _computeContextType(node);
+    DartType literalType = _computeSetOrMapContextType(node);
     // TODO(brianwilkerson) Determine whether we need special handling for type
     // parameter types. (E-mail sent.)
     if (literalType is InterfaceType) {
@@ -4800,6 +4800,8 @@
               node.leftBracket,
               null,
               node.rightBracket);
+          InferenceContext.setType(
+              setLiteral, InferenceContext.getContext(node));
           NodeReplacer.replace(node, setLiteral);
           node = setLiteral;
         }
@@ -4809,9 +4811,9 @@
         _pushCollectionTypesDownToAll(node.elements2,
             iterableType: literalType, keyType: keyType, valueType: valueType);
       }
-      InferenceContext.setType(node, literalType);
+      (node as SetOrMapLiteralImpl).contextType = literalType;
     } else {
-      InferenceContext.clearType(node);
+      (node as SetOrMapLiteralImpl).contextType = null;
     }
     super.visitSetOrMapLiteral(node);
   }
@@ -4976,8 +4978,39 @@
     }
   }
 
+  /// Given the declared return type of a function, compute the type of the
+  /// values which should be returned or yielded as appropriate.  If a type
+  /// cannot be computed from the declared return type, return null.
+  DartType _computeReturnOrYieldType(DartType declaredType) {
+    bool isGenerator = _enclosingFunction.isGenerator;
+    bool isAsynchronous = _enclosingFunction.isAsynchronous;
+
+    // Ordinary functions just return their declared types.
+    if (!isGenerator && !isAsynchronous) {
+      return declaredType;
+    }
+    if (declaredType is InterfaceType) {
+      if (isGenerator) {
+        // If it's sync* we expect Iterable<T>
+        // If it's async* we expect Stream<T>
+        InterfaceType rawType = isAsynchronous
+            ? typeProvider.streamType
+            : typeProvider.iterableType;
+        // Match the types to instantiate the type arguments if possible
+        List<DartType> targs = declaredType.typeArguments;
+        if (targs.length == 1 && rawType.instantiate(targs) == declaredType) {
+          return targs[0];
+        }
+      }
+      // async functions expect `Future<T> | T`
+      var futureTypeParam = typeSystem.flatten(declaredType);
+      return _createFutureOr(futureTypeParam);
+    }
+    return declaredType;
+  }
+
   /// Compute the context type for the given set or map [literal].
-  DartType _computeContextType(SetOrMapLiteral literal) {
+  DartType _computeSetOrMapContextType(SetOrMapLiteral literal) {
     _LiteralResolution typeArgumentsResolution =
         _fromTypeArguments(literal.typeArguments);
     DartType contextType = InferenceContext.getContext(literal);
@@ -5029,37 +5062,6 @@
     return null;
   }
 
-  /// Given the declared return type of a function, compute the type of the
-  /// values which should be returned or yielded as appropriate.  If a type
-  /// cannot be computed from the declared return type, return null.
-  DartType _computeReturnOrYieldType(DartType declaredType) {
-    bool isGenerator = _enclosingFunction.isGenerator;
-    bool isAsynchronous = _enclosingFunction.isAsynchronous;
-
-    // Ordinary functions just return their declared types.
-    if (!isGenerator && !isAsynchronous) {
-      return declaredType;
-    }
-    if (declaredType is InterfaceType) {
-      if (isGenerator) {
-        // If it's sync* we expect Iterable<T>
-        // If it's async* we expect Stream<T>
-        InterfaceType rawType = isAsynchronous
-            ? typeProvider.streamType
-            : typeProvider.iterableType;
-        // Match the types to instantiate the type arguments if possible
-        List<DartType> targs = declaredType.typeArguments;
-        if (targs.length == 1 && rawType.instantiate(targs) == declaredType) {
-          return targs[0];
-        }
-      }
-      // async functions expect `Future<T> | T`
-      var futureTypeParam = typeSystem.flatten(declaredType);
-      return _createFutureOr(futureTypeParam);
-    }
-    return declaredType;
-  }
-
   /// Return a newly created cloner that can be used to clone constant
   /// expressions.
   ConstantAstCloner _createCloner() {
diff --git a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
index 2c842f4..d9ba671 100644
--- a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
+++ b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
@@ -1753,7 +1753,9 @@
   }
 
   DartType _inferSetOrMapLiteralType(SetOrMapLiteral literal) {
-    DartType contextType = InferenceContext.getContext(literal);
+    var literalImpl = literal as SetOrMapLiteralImpl;
+    DartType contextType = literalImpl.contextType;
+    literalImpl.contextType = null; // Not needed anymore.
     NodeList<CollectionElement> elements = literal.elements2;
     List<_InferredCollectionElementTypeInformation> inferredTypes = [];
     bool canBeAMap = true;
diff --git a/pkg/analyzer/test/src/task/strong/checker_test.dart b/pkg/analyzer/test/src/task/strong/checker_test.dart
index 408b75a..43de76f 100644
--- a/pkg/analyzer/test/src/task/strong/checker_test.dart
+++ b/pkg/analyzer/test/src/task/strong/checker_test.dart
@@ -3647,7 +3647,6 @@
 ''');
   }
 
-  @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/35569')
   test_strictRawTypes_emptyMap() async {
     addFile('''
 main() {