@@ -546,15 +546,15 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
546
546
}
547
547
548
548
// 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 ) {
550
550
auto diag =
551
551
TC.diagnose (expr->getLoc (), diag::unwrap_with_default_value);
552
552
553
553
// Figure out what we need to parenthesize.
554
554
bool needsParensInside =
555
555
exprNeedsParensBeforeAddingNilCoalescing (TC, DC, expr);
556
556
bool needsParensOutside =
557
- exprNeedsParensAfterAddingNilCoalescing (TC, DC, expr, expr );
557
+ exprNeedsParensAfterAddingNilCoalescing (TC, DC, expr, rootExpr );
558
558
559
559
llvm::SmallString<2 > insertBefore;
560
560
llvm::SmallString<32 > insertAfter;
@@ -618,7 +618,7 @@ class VarDeclMultipleReferencesChecker : public ASTWalker {
618
618
int referencesCount () { return count; }
619
619
};
620
620
621
- static bool diagnoseUnwrap (ConstraintSystem &CS, Expr *expr, Type baseType,
621
+ static bool diagnoseUnwrap (ConstraintSystem &CS, Expr *expr, Expr *rootExpr, Type baseType,
622
622
Type unwrappedType) {
623
623
624
624
assert (!baseType->hasTypeVariable () &&
@@ -673,14 +673,14 @@ static bool diagnoseUnwrap(ConstraintSystem &CS, Expr *expr, Type baseType,
673
673
}
674
674
diag.flush ();
675
675
676
- offerDefaultValueUnwrapFixit (CS.TC , varDecl->getDeclContext (),
677
- initializer);
676
+ offerDefaultValueUnwrapFixit (CS.TC , varDecl->getDeclContext (),
677
+ initializer, rootExpr );
678
678
offerForceUnwrapFixit (CS, initializer);
679
679
}
680
680
}
681
681
}
682
682
683
- offerDefaultValueUnwrapFixit (CS.TC , CS.DC , expr);
683
+ offerDefaultValueUnwrapFixit (CS.TC , CS.DC , expr, rootExpr );
684
684
offerForceUnwrapFixit (CS, expr);
685
685
return true ;
686
686
}
@@ -690,6 +690,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
690
690
return false ;
691
691
692
692
auto *anchor = getAnchor ();
693
+ auto *rootExpr = getParentExpr ();
693
694
694
695
if (auto assignExpr = dyn_cast<AssignExpr>(anchor))
695
696
anchor = assignExpr->getSrc ();
@@ -699,7 +700,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
699
700
700
701
auto *tryExpr = dyn_cast<OptionalTryExpr>(unwrapped);
701
702
if (!tryExpr) {
702
- return diagnoseUnwrap (getConstraintSystem (), unwrapped, getBaseType (),
703
+ return diagnoseUnwrap (getConstraintSystem (), unwrapped, rootExpr, getBaseType (),
703
704
getUnwrappedType ());
704
705
}
705
706
0 commit comments