@@ -1350,6 +1350,42 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
13501350 .highlight (anchor->getSourceRange ())
13511351 .fixItInsertAfter (anchor->getEndLoc (), " ()" );
13521352
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) {
13531389 // It is possible that we're looking at a stored property being
13541390 // initialized with a closure. Something like:
13551391 //
@@ -1359,23 +1395,17 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
13591395 // property into a computed property. If the variable is immutable, then
13601396 // replace the 'let' with a 'var'.
13611397
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.
13681398 PatternBindingDecl *PBD = nullptr ;
13691399
1370- if (auto TLCD = dyn_cast<TopLevelCodeDecl>(getDC () )) {
1400+ if (auto TLCD = dyn_cast<TopLevelCodeDecl>(dc )) {
13711401 if (TLCD->getBody ()->isImplicit ()) {
13721402 if (auto decl = TLCD->getBody ()->getElement (0 ).dyn_cast <Decl *>()) {
13731403 if (auto binding = dyn_cast<PatternBindingDecl>(decl)) {
13741404 PBD = binding;
13751405 }
13761406 }
13771407 }
1378- } else if (auto PBI = dyn_cast<PatternBindingInitializer>(getDC () )) {
1408+ } else if (auto PBI = dyn_cast<PatternBindingInitializer>(dc )) {
13791409 PBD = PBI->getBinding ();
13801410 }
13811411
@@ -1387,7 +1417,7 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
13871417 !VD->getAttrs ().getAttribute <DynamicReplacementAttr>() &&
13881418 entry.getInit () && isa<ClosureExpr>(entry.getInit ())) {
13891419 auto diag =
1390- TC.diagnose (anchor ->getLoc (), diag::extension_stored_property_fixit,
1420+ TC.diagnose (expr ->getLoc (), diag::extension_stored_property_fixit,
13911421 VD->getName ());
13921422 diag.fixItRemove (entry.getEqualLoc ());
13931423
@@ -1397,35 +1427,6 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
13971427 }
13981428 }
13991429 }
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 ;
14291430}
14301431
14311432bool AutoClosureForwardingFailure::diagnoseAsError () {
0 commit comments