@@ -1350,6 +1350,42 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
1350
1350
.highlight (anchor->getSourceRange ())
1351
1351
.fixItInsertAfter (anchor->getEndLoc (), " ()" );
1352
1352
1353
+ if (isa<ClosureExpr>(getAnchor ())) {
1354
+ tryComputedPropertyFixIts (TC, anchor, getDC ());
1355
+ }
1356
+
1357
+ return true ;
1358
+ }
1359
+
1360
+ bool ContextualFailure::trySequenceSubsequenceFixIts (InFlightDiagnostic &diag,
1361
+ ConstraintSystem &CS,
1362
+ Type fromType, Type toType,
1363
+ Expr *expr) {
1364
+ if (!CS.TC .Context .getStdlibModule ())
1365
+ return false ;
1366
+
1367
+ auto String = CS.TC .getStringType (CS.DC );
1368
+ auto Substring = CS.TC .getSubstringType (CS.DC );
1369
+
1370
+ if (!String || !Substring)
1371
+ return false ;
1372
+
1373
+ // Substring -> String conversion
1374
+ // Wrap in String.init
1375
+ if (fromType->isEqual (Substring)) {
1376
+ if (toType->isEqual (String)) {
1377
+ auto range = expr->getSourceRange ();
1378
+ diag.fixItInsert (range.Start , " String(" );
1379
+ diag.fixItInsertAfter (range.End , " )" );
1380
+ return true ;
1381
+ }
1382
+ }
1383
+
1384
+ return false ;
1385
+ }
1386
+
1387
+ void ContextualFailure::tryComputedPropertyFixIts (TypeChecker &TC, Expr *expr,
1388
+ DeclContext *dc) {
1353
1389
// It is possible that we're looking at a stored property being
1354
1390
// initialized with a closure. Something like:
1355
1391
//
@@ -1359,23 +1395,17 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
1359
1395
// property into a computed property. If the variable is immutable, then
1360
1396
// replace the 'let' with a 'var'.
1361
1397
1362
- if (getAnchor () && !isa<ClosureExpr>(getAnchor ())) {
1363
- return true ;
1364
- }
1365
-
1366
- // First, check if the variable is declared top level or not. If it is
1367
- // not, then it means we're inside a decl like a class or an extension.
1368
1398
PatternBindingDecl *PBD = nullptr ;
1369
1399
1370
- if (auto TLCD = dyn_cast<TopLevelCodeDecl>(getDC () )) {
1400
+ if (auto TLCD = dyn_cast<TopLevelCodeDecl>(dc )) {
1371
1401
if (TLCD->getBody ()->isImplicit ()) {
1372
1402
if (auto decl = TLCD->getBody ()->getElement (0 ).dyn_cast <Decl *>()) {
1373
1403
if (auto binding = dyn_cast<PatternBindingDecl>(decl)) {
1374
1404
PBD = binding;
1375
1405
}
1376
1406
}
1377
1407
}
1378
- } else if (auto PBI = dyn_cast<PatternBindingInitializer>(getDC () )) {
1408
+ } else if (auto PBI = dyn_cast<PatternBindingInitializer>(dc )) {
1379
1409
PBD = PBI->getBinding ();
1380
1410
}
1381
1411
@@ -1387,7 +1417,7 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
1387
1417
!VD->getAttrs ().getAttribute <DynamicReplacementAttr>() &&
1388
1418
entry.getInit () && isa<ClosureExpr>(entry.getInit ())) {
1389
1419
auto diag =
1390
- TC.diagnose (anchor ->getLoc (), diag::extension_stored_property_fixit,
1420
+ TC.diagnose (expr ->getLoc (), diag::extension_stored_property_fixit,
1391
1421
VD->getName ());
1392
1422
diag.fixItRemove (entry.getEqualLoc ());
1393
1423
@@ -1397,35 +1427,6 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
1397
1427
}
1398
1428
}
1399
1429
}
1400
-
1401
- return true ;
1402
- }
1403
-
1404
- bool ContextualFailure::trySequenceSubsequenceFixIts (InFlightDiagnostic &diag,
1405
- ConstraintSystem &CS,
1406
- Type fromType, Type toType,
1407
- Expr *expr) {
1408
- if (!CS.TC .Context .getStdlibModule ())
1409
- return false ;
1410
-
1411
- auto String = CS.TC .getStringType (CS.DC );
1412
- auto Substring = CS.TC .getSubstringType (CS.DC );
1413
-
1414
- if (!String || !Substring)
1415
- return false ;
1416
-
1417
- // Substring -> String conversion
1418
- // Wrap in String.init
1419
- if (fromType->isEqual (Substring)) {
1420
- if (toType->isEqual (String)) {
1421
- auto range = expr->getSourceRange ();
1422
- diag.fixItInsert (range.Start , " String(" );
1423
- diag.fixItInsertAfter (range.End , " )" );
1424
- return true ;
1425
- }
1426
- }
1427
-
1428
- return false ;
1429
1430
}
1430
1431
1431
1432
bool AutoClosureForwardingFailure::diagnoseAsError () {
0 commit comments