Set or map literals can be ambiguous in invalid code

Change-Id: Ic17f5b80836bdf1891065af97adf5e9a5e021995
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/105021
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/task/strong/checker.dart b/pkg/analyzer/lib/src/task/strong/checker.dart
index 2ca6313..7725c55 100644
--- a/pkg/analyzer/lib/src/task/strong/checker.dart
+++ b/pkg/analyzer/lib/src/task/strong/checker.dart
@@ -1184,11 +1184,12 @@
           if (expr.isMap) {
             _recordMessage(
                 expr, StrongModeCode.INVALID_CAST_LITERAL_MAP, [from, to]);
-          } else {
-            // Ambiguity should be resolved by now
-            assert(expr.isSet);
+          } else if (expr.isSet) {
             _recordMessage(
                 expr, StrongModeCode.INVALID_CAST_LITERAL_SET, [from, to]);
+          } else {
+            // This should only happen when the code is invalid, in which case
+            // the error should have been reported elsewhere.
           }
         } else {
           _recordMessage(
diff --git a/pkg/analyzer/test/src/diagnostics/ambiguous_set_or_map_literal_test.dart b/pkg/analyzer/test/src/diagnostics/ambiguous_set_or_map_literal_test.dart
index 78754f2..26f5983 100644
--- a/pkg/analyzer/test/src/diagnostics/ambiguous_set_or_map_literal_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/ambiguous_set_or_map_literal_test.dart
@@ -29,6 +29,15 @@
 
 @reflectiveTest
 class AmbiguousSetOrMapLiteralEitherTest extends DriverResolutionTest {
+  test_invalidPrefixOperator() async {
+    // Guard against an exception being thrown.
+    await assertErrorsInCode('''
+union(a, b) => !{...a, ...b};
+''', [
+      error(CompileTimeErrorCode.AMBIGUOUS_SET_OR_MAP_LITERAL_EITHER, 16, 12),
+    ]);
+  }
+
   test_setAndMap() async {
     assertErrorsInCode('''
 var map;