@@ -531,15 +531,15 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
531531}
532532
533533// 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 ) {
535535 auto diag =
536536 TC.diagnose (expr->getLoc (), diag::unwrap_with_default_value);
537537
538538 // Figure out what we need to parenthesize.
539539 bool needsParensInside =
540540 exprNeedsParensBeforeAddingNilCoalescing (TC, DC, expr);
541541 bool needsParensOutside =
542- exprNeedsParensAfterAddingNilCoalescing (TC, DC, expr, expr );
542+ exprNeedsParensAfterAddingNilCoalescing (TC, DC, expr, rootExpr );
543543
544544 llvm::SmallString<2 > insertBefore;
545545 llvm::SmallString<32 > insertAfter;
@@ -603,7 +603,7 @@ class VarDeclMultipleReferencesChecker : public ASTWalker {
603603 int referencesCount () { return count; }
604604};
605605
606- static bool diagnoseUnwrap (ConstraintSystem &CS, Expr *expr, Type baseType,
606+ static bool diagnoseUnwrap (ConstraintSystem &CS, Expr *expr, Expr *rootExpr, Type baseType,
607607 Type unwrappedType) {
608608
609609 assert (!baseType->hasTypeVariable () &&
@@ -658,14 +658,14 @@ static bool diagnoseUnwrap(ConstraintSystem &CS, Expr *expr, Type baseType,
658658 }
659659 diag.flush ();
660660
661- offerDefaultValueUnwrapFixit (CS.TC , varDecl->getDeclContext (),
662- initializer);
661+ offerDefaultValueUnwrapFixit (CS.TC , varDecl->getDeclContext (),
662+ initializer, rootExpr );
663663 offerForceUnwrapFixit (CS, initializer);
664664 }
665665 }
666666 }
667667
668- offerDefaultValueUnwrapFixit (CS.TC , CS.DC , expr);
668+ offerDefaultValueUnwrapFixit (CS.TC , CS.DC , expr, rootExpr );
669669 offerForceUnwrapFixit (CS, expr);
670670 return true ;
671671}
@@ -675,6 +675,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
675675 return false ;
676676
677677 auto *anchor = getAnchor ();
678+ auto *rootExpr = getParentExpr ();
678679
679680 if (auto assignExpr = dyn_cast<AssignExpr>(anchor))
680681 anchor = assignExpr->getSrc ();
@@ -684,7 +685,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
684685
685686 auto *tryExpr = dyn_cast<OptionalTryExpr>(unwrapped);
686687 if (!tryExpr) {
687- return diagnoseUnwrap (getConstraintSystem (), unwrapped, getBaseType (),
688+ return diagnoseUnwrap (getConstraintSystem (), unwrapped, rootExpr, getBaseType (),
688689 getUnwrappedType ());
689690 }
690691
0 commit comments