[diag] fix incorrect diagnostic when unwrapping double optional

This commit is contained in:
Suyash Srijan
2019-01-22 22:03:40 +00:00
parent 07b1905d22
commit f5264b9779
2 changed files with 59 additions and 2 deletions

View File

@@ -580,8 +580,21 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
anchor = assignExpr->getSrc();
auto *unwrapped = anchor->getValueProvidingExpr();
auto type = getType(anchor)->getRValueType();
Type type = [&] {
// The anchor could be pointing to an OptionalEvaluationExpr, with the sub
// expression being a CallExpr, UnresolvedDotExpr, etc which could produce
// an >= 1 optional, so let's return the type of the sub expression.
if (auto OEE = dyn_cast<OptionalEvaluationExpr>(anchor)) {
if (auto subTy = getType(OEE->getSubExpr())) {
if (!subTy->hasLValueType() && subTy->getOptionalObjectType()) {
return subTy->getRValueType();
}
}
}
// Just return the type of the anchor
return getType(anchor)->getRValueType();
}();
auto *tryExpr = dyn_cast<OptionalTryExpr>(unwrapped);
if (!tryExpr)
return diagnoseUnwrap(getConstraintSystem(), unwrapped, type);