@@ -1020,8 +1020,35 @@ namespace {
1020
1020
// `_ = nil`, let's diagnose it here because solver can't
1021
1021
// attempt any types for it.
1022
1022
auto *parentExpr = CS.getParentExpr (expr);
1023
- while (parentExpr && isa<ParenExpr>(parentExpr))
1024
- parentExpr = CS.getParentExpr (parentExpr);
1023
+ bool hasContextualType = bool (CS.getContextualType (expr));
1024
+
1025
+ while (parentExpr) {
1026
+ if (!isa<IdentityExpr>(parentExpr))
1027
+ break ;
1028
+
1029
+ // If there is a parent, use it, otherwise we need
1030
+ // to check whether the last parent node in the chain
1031
+ // had a contextual type associated with it because
1032
+ // in situations like:
1033
+ //
1034
+ // \code
1035
+ // func foo() -> Int? {
1036
+ // return (nil)
1037
+ // }
1038
+ // \endcode
1039
+ //
1040
+ // parentheses around `nil` are significant.
1041
+ if (auto *nextParent = CS.getParentExpr (parentExpr)) {
1042
+ parentExpr = nextParent;
1043
+ } else {
1044
+ hasContextualType |= bool (CS.getContextualType (parentExpr));
1045
+ // Since current expression is an identity expr
1046
+ // and there are no more parents, let's pretend
1047
+ // that `nil` don't have a parent since parens
1048
+ // are not semantically significant for further checks.
1049
+ parentExpr = nullptr ;
1050
+ }
1051
+ }
1025
1052
1026
1053
// In cases like `_ = nil?` AST would have `nil`
1027
1054
// wrapped in `BindOptionalExpr`.
@@ -1056,7 +1083,7 @@ namespace {
1056
1083
}
1057
1084
}
1058
1085
1059
- if (!parentExpr && !CS. getContextualType (expr) ) {
1086
+ if (!parentExpr && !hasContextualType ) {
1060
1087
DE.diagnose (expr->getLoc (), diag::unresolved_nil_literal);
1061
1088
return Type ();
1062
1089
}
0 commit comments