Skip to content

Commit fca8106

Browse files
committed
[Diagnostics] Guard against missing parent initializer in missing unwrap diagnostic
While trying to diagnose missing optional unwrap for single use vars guard against it not having a parent initializer. Unfortunately there is no test-case for this but we have received multiple reports about `offerDefaultValueUnwrapFixIt` crashing trying to access locator on passed in `expr`. Resolves: rdar://problem/51784793
1 parent 04f74b8 commit fca8106

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,9 +819,10 @@ MissingOptionalUnwrapFailure::getOperatorParameterFor(Expr *expr) const {
819819

820820
void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt(
821821
DeclContext *DC, Expr *expr) const {
822-
auto *anchor = getAnchor();
822+
assert(expr);
823823

824-
// If anchor is an explicit address-of, or expression which produces
824+
auto *anchor = getAnchor();
825+
// If anchor is n explicit address-of, or expression which produces
825826
// an l-value (e.g. first argument of `+=` operator), let's not
826827
// suggest default value here because that would produce r-value type.
827828
if (isa<InOutExpr>(anchor))
@@ -966,7 +967,10 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
966967
if (singleUse && binding && binding->getNumPatternEntries() == 1 &&
967968
varDecl->getTypeSourceRangeForDiagnostics().isInvalid()) {
968969

969-
Expr *initializer = varDecl->getParentInitializer();
970+
auto *initializer = varDecl->getParentInitializer();
971+
if (!initializer)
972+
return true;
973+
970974
if (auto declRefExpr = dyn_cast<DeclRefExpr>(initializer)) {
971975
if (declRefExpr->getDecl()
972976
->getAttrs()

0 commit comments

Comments
 (0)