@@ -423,20 +423,33 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
423
423
// / We have a collection literal with a defaulted type, e.g. of [Any]. Emit
424
424
// / an error if it was inferred to this type in an invalid context, which is
425
425
// / one in which the parent expression is not itself a collection literal.
426
- void checkTypeDefaultedCollectionExpr (CollectionExpr *c) {
426
+ void checkTypeDefaultedCollectionExpr (CollectionExpr *c,
427
+ bool diagnoseAsWarning) {
427
428
// If the parent is a non-expression, or is not itself a literal, then
428
429
// produce an error with a fixit to add the type as an explicit
429
430
// annotation.
430
- if (c->getNumElements () == 0 )
431
- Ctx.Diags .diagnose (c->getLoc (), diag::collection_literal_empty)
432
- .highlight (c->getSourceRange ());
433
- else {
431
+ auto &diags = Ctx.Diags ;
432
+
433
+ if (c->getNumElements () == 0 ) {
434
+ InFlightDiagnostic inFlight =
435
+ diags.diagnose (c->getLoc (), diag::collection_literal_empty);
436
+ inFlight.highlight (c->getSourceRange ());
437
+
438
+ if (diagnoseAsWarning) {
439
+ inFlight.limitBehavior (DiagnosticBehavior::Warning);
440
+ }
441
+ } else {
434
442
assert (c->getType ()->hasTypeRepr () &&
435
443
" a defaulted type should always be printable" );
436
- Ctx.Diags .diagnose (c->getLoc (), diag::collection_literal_heterogeneous,
437
- c->getType ())
438
- .highlight (c->getSourceRange ())
439
- .fixItInsertAfter (c->getEndLoc (), " as " + c->getType ()->getString ());
444
+ InFlightDiagnostic inFlight = diags.diagnose (
445
+ c->getLoc (), diag::collection_literal_heterogeneous, c->getType ());
446
+ inFlight.highlight (c->getSourceRange ());
447
+ inFlight.fixItInsertAfter (c->getEndLoc (),
448
+ " as " + c->getType ()->getString ());
449
+
450
+ if (diagnoseAsWarning) {
451
+ inFlight.limitBehavior (DiagnosticBehavior::Warning);
452
+ }
440
453
}
441
454
}
442
455
@@ -1313,11 +1326,26 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
1313
1326
1314
1327
// Diagnose uses of collection literals with defaulted types at the top
1315
1328
// level.
1316
- if (auto collection
1317
- = dyn_cast<CollectionExpr>(E->getSemanticsProvidingExpr ())) {
1329
+ E = E->getSemanticsProvidingExpr ();
1330
+ bool diagnoseAsWarning = false ;
1331
+
1332
+ // Look through implicit conversions.
1333
+ if (auto conversion = dyn_cast<ImplicitConversionExpr>(E)) {
1334
+ // This case was not diagnosed previously; emit a warning instead of an
1335
+ // error for source compatibility.
1336
+ diagnoseAsWarning = true ;
1337
+ while (auto *expr =
1338
+ dyn_cast<ImplicitConversionExpr>(conversion->getSubExpr ())) {
1339
+ conversion = expr;
1340
+ }
1341
+
1342
+ E = conversion->getSubExpr ();
1343
+ }
1344
+
1345
+ if (auto collection = dyn_cast<CollectionExpr>(E)) {
1318
1346
if (collection->isTypeDefaulted ()) {
1319
1347
Walker.checkTypeDefaultedCollectionExpr (
1320
- const_cast <CollectionExpr *>(collection));
1348
+ const_cast <CollectionExpr *>(collection), diagnoseAsWarning );
1321
1349
}
1322
1350
}
1323
1351
}
0 commit comments