Fix _inferSetOrMapLiteralType to handle a context of `?` correctly.

Also add a comment explaining why it's not necessary to compute the
greatest closure (even though the spec says to do so).

Change-Id: I74f78ca3b6d43fedfaefb13f9120248eb62b283b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96142
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
index da50a41..2c842f4 100644
--- a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
+++ b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
@@ -1774,11 +1774,17 @@
     } else if (canBeAMap && mustBeAMap) {
       return _toMapType(literal, contextType, inferredTypes);
     }
-    // TODO(paulberry): the following computations should be based on the
-    // greatest closure of the context type.
-    bool contextIsIterable = contextType != null &&
+    // Note: according to the spec, the following computations should be based
+    // on the greatest closure of the context type (unless the context type is
+    // `?`).  In practice, we can just use the context type directly, because
+    // the only way the greatest closure of the context type could possibly have
+    // a different subtype relationship to `Iterable<Object>` and
+    // `Map<Object, Object>` is if the context type is `?`.
+    bool contextProvidesAmbiguityResolutionClues =
+        contextType != null && contextType is! UnknownInferredType;
+    bool contextIsIterable = contextProvidesAmbiguityResolutionClues &&
         _typeSystem.isSubtypeOf(contextType, _typeProvider.iterableObjectType);
-    bool contextIsMap = contextType != null &&
+    bool contextIsMap = contextProvidesAmbiguityResolutionClues &&
         _typeSystem.isSubtypeOf(contextType, _typeProvider.mapObjectObjectType);
     if (contextIsIterable && !contextIsMap) {
       return _toSetType(literal, contextType, inferredTypes);