@@ -531,15 +531,15 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
531
531
}
532
532
533
533
// Suggest a default value via ?? <default value>
534
- static void offerDefaultValueUnwrapFixit (TypeChecker &TC, DeclContext *DC, Expr *expr) {
534
+ static void offerDefaultValueUnwrapFixit (TypeChecker &TC, DeclContext *DC, Expr *expr, Expr *rootExpr ) {
535
535
auto diag =
536
536
TC.diagnose (expr->getLoc (), diag::unwrap_with_default_value);
537
537
538
538
// Figure out what we need to parenthesize.
539
539
bool needsParensInside =
540
540
exprNeedsParensBeforeAddingNilCoalescing (TC, DC, expr);
541
541
bool needsParensOutside =
542
- exprNeedsParensAfterAddingNilCoalescing (TC, DC, expr, expr );
542
+ exprNeedsParensAfterAddingNilCoalescing (TC, DC, expr, rootExpr );
543
543
544
544
llvm::SmallString<2 > insertBefore;
545
545
llvm::SmallString<32 > insertAfter;
@@ -603,7 +603,7 @@ class VarDeclMultipleReferencesChecker : public ASTWalker {
603
603
int referencesCount () { return count; }
604
604
};
605
605
606
- static bool diagnoseUnwrap (ConstraintSystem &CS, Expr *expr, Type baseType,
606
+ static bool diagnoseUnwrap (ConstraintSystem &CS, Expr *expr, Expr *rootExpr, Type baseType,
607
607
Type unwrappedType) {
608
608
609
609
assert (!baseType->hasTypeVariable () &&
@@ -658,14 +658,14 @@ static bool diagnoseUnwrap(ConstraintSystem &CS, Expr *expr, Type baseType,
658
658
}
659
659
diag.flush ();
660
660
661
- offerDefaultValueUnwrapFixit (CS.TC , varDecl->getDeclContext (),
662
- initializer);
661
+ offerDefaultValueUnwrapFixit (CS.TC , varDecl->getDeclContext (),
662
+ initializer, rootExpr );
663
663
offerForceUnwrapFixit (CS, initializer);
664
664
}
665
665
}
666
666
}
667
667
668
- offerDefaultValueUnwrapFixit (CS.TC , CS.DC , expr);
668
+ offerDefaultValueUnwrapFixit (CS.TC , CS.DC , expr, rootExpr );
669
669
offerForceUnwrapFixit (CS, expr);
670
670
return true ;
671
671
}
@@ -675,6 +675,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
675
675
return false ;
676
676
677
677
auto *anchor = getAnchor ();
678
+ auto *rootExpr = getParentExpr ();
678
679
679
680
if (auto assignExpr = dyn_cast<AssignExpr>(anchor))
680
681
anchor = assignExpr->getSrc ();
@@ -684,7 +685,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
684
685
685
686
auto *tryExpr = dyn_cast<OptionalTryExpr>(unwrapped);
686
687
if (!tryExpr) {
687
- return diagnoseUnwrap (getConstraintSystem (), unwrapped, getBaseType (),
688
+ return diagnoseUnwrap (getConstraintSystem (), unwrapped, rootExpr, getBaseType (),
688
689
getUnwrappedType ());
689
690
}
690
691
0 commit comments