@@ -546,15 +546,15 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
546546}
547547
548548// Suggest a default value via ?? <default value>
549- static void offerDefaultValueUnwrapFixit (TypeChecker &TC, DeclContext *DC, Expr *expr) {
549+ static void offerDefaultValueUnwrapFixit (TypeChecker &TC, DeclContext *DC, Expr *expr, Expr *rootExpr ) {
550550 auto diag =
551551 TC.diagnose (expr->getLoc (), diag::unwrap_with_default_value);
552552
553553 // Figure out what we need to parenthesize.
554554 bool needsParensInside =
555555 exprNeedsParensBeforeAddingNilCoalescing (TC, DC, expr);
556556 bool needsParensOutside =
557- exprNeedsParensAfterAddingNilCoalescing (TC, DC, expr, expr );
557+ exprNeedsParensAfterAddingNilCoalescing (TC, DC, expr, rootExpr );
558558
559559 llvm::SmallString<2 > insertBefore;
560560 llvm::SmallString<32 > insertAfter;
@@ -618,7 +618,7 @@ class VarDeclMultipleReferencesChecker : public ASTWalker {
618618 int referencesCount () { return count; }
619619};
620620
621- static bool diagnoseUnwrap (ConstraintSystem &CS, Expr *expr, Type baseType,
621+ static bool diagnoseUnwrap (ConstraintSystem &CS, Expr *expr, Expr *rootExpr, Type baseType,
622622 Type unwrappedType) {
623623
624624 assert (!baseType->hasTypeVariable () &&
@@ -673,14 +673,14 @@ static bool diagnoseUnwrap(ConstraintSystem &CS, Expr *expr, Type baseType,
673673 }
674674 diag.flush ();
675675
676- offerDefaultValueUnwrapFixit (CS.TC , varDecl->getDeclContext (),
677- initializer);
676+ offerDefaultValueUnwrapFixit (CS.TC , varDecl->getDeclContext (),
677+ initializer, rootExpr );
678678 offerForceUnwrapFixit (CS, initializer);
679679 }
680680 }
681681 }
682682
683- offerDefaultValueUnwrapFixit (CS.TC , CS.DC , expr);
683+ offerDefaultValueUnwrapFixit (CS.TC , CS.DC , expr, rootExpr );
684684 offerForceUnwrapFixit (CS, expr);
685685 return true ;
686686}
@@ -690,6 +690,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
690690 return false ;
691691
692692 auto *anchor = getAnchor ();
693+ auto *rootExpr = getParentExpr ();
693694
694695 if (auto assignExpr = dyn_cast<AssignExpr>(anchor))
695696 anchor = assignExpr->getSrc ();
@@ -699,7 +700,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
699700
700701 auto *tryExpr = dyn_cast<OptionalTryExpr>(unwrapped);
701702 if (!tryExpr) {
702- return diagnoseUnwrap (getConstraintSystem (), unwrapped, getBaseType (),
703+ return diagnoseUnwrap (getConstraintSystem (), unwrapped, rootExpr, getBaseType (),
703704 getUnwrappedType ());
704705 }
705706
0 commit comments