@@ -1207,8 +1207,35 @@ namespace {
1207
1207
// `_ = nil`, let's diagnose it here because solver can't
1208
1208
// attempt any types for it.
1209
1209
auto *parentExpr = CS.getParentExpr (expr);
1210
- while (parentExpr && isa<ParenExpr>(parentExpr))
1211
- parentExpr = CS.getParentExpr (parentExpr);
1210
+ bool hasContextualType = bool (CS.getContextualType (expr));
1211
+
1212
+ while (parentExpr) {
1213
+ if (!isa<IdentityExpr>(parentExpr))
1214
+ break ;
1215
+
1216
+ // If there is a parent, use it, otherwise we need
1217
+ // to check whether the last parent node in the chain
1218
+ // had a contextual type associated with it because
1219
+ // in situations like:
1220
+ //
1221
+ // \code
1222
+ // func foo() -> Int? {
1223
+ // return (nil)
1224
+ // }
1225
+ // \endcode
1226
+ //
1227
+ // parentheses around `nil` are significant.
1228
+ if (auto *nextParent = CS.getParentExpr (parentExpr)) {
1229
+ parentExpr = nextParent;
1230
+ } else {
1231
+ hasContextualType |= bool (CS.getContextualType (parentExpr));
1232
+ // Since current expression is an identity expr
1233
+ // and there are no more parents, let's pretend
1234
+ // that `nil` don't have a parent since parens
1235
+ // are not semantically significant for further checks.
1236
+ parentExpr = nullptr ;
1237
+ }
1238
+ }
1212
1239
1213
1240
// In cases like `_ = nil?` AST would have `nil`
1214
1241
// wrapped in `BindOptionalExpr`.
@@ -1243,7 +1270,7 @@ namespace {
1243
1270
}
1244
1271
}
1245
1272
1246
- if (!parentExpr && !CS. getContextualType (expr) ) {
1273
+ if (!parentExpr && !hasContextualType ) {
1247
1274
DE.diagnose (expr->getLoc (), diag::unresolved_nil_literal);
1248
1275
return Type ();
1249
1276
}
0 commit comments