@@ -1155,7 +1155,18 @@ namespace {
1155
1155
while (parentExpr && isa<ParenExpr>(parentExpr))
1156
1156
parentExpr = CS.getParentExpr (parentExpr);
1157
1157
1158
+ // In cases like `_ = nil?` AST would have `nil`
1159
+ // wrapped in `BindOptionalExpr`.
1160
+ if (parentExpr && isa<BindOptionalExpr>(parentExpr))
1161
+ parentExpr = CS.getParentExpr (parentExpr);
1162
+
1158
1163
if (parentExpr) {
1164
+ // `_ = nil as? ...`
1165
+ if (isa<ConditionalCheckedCastExpr>(parentExpr)) {
1166
+ DE.diagnose (expr->getLoc (), diag::conditional_cast_from_nil);
1167
+ return Type ();
1168
+ }
1169
+
1159
1170
// `_ = nil!`
1160
1171
if (isa<ForceValueExpr>(parentExpr)) {
1161
1172
DE.diagnose (expr->getLoc (), diag::cannot_force_unwrap_nil_literal);
@@ -1167,13 +1178,13 @@ namespace {
1167
1178
DE.diagnose (expr->getLoc (), diag::unresolved_nil_literal);
1168
1179
return Type ();
1169
1180
}
1170
- }
1171
1181
1172
- // `_ = nil`
1173
- if (auto *assignment = dyn_cast_or_null<AssignExpr>(parentExpr)) {
1174
- if (isa<DiscardAssignmentExpr>(assignment->getDest ())) {
1175
- DE.diagnose (expr->getLoc (), diag::unresolved_nil_literal);
1176
- return Type ();
1182
+ // `_ = nil`
1183
+ if (auto *assignment = dyn_cast<AssignExpr>(parentExpr)) {
1184
+ if (isa<DiscardAssignmentExpr>(assignment->getDest ())) {
1185
+ DE.diagnose (expr->getLoc (), diag::unresolved_nil_literal);
1186
+ return Type ();
1187
+ }
1177
1188
}
1178
1189
}
1179
1190
@@ -2982,26 +2993,6 @@ namespace {
2982
2993
if (!fromExpr) // Either wasn't constructed correctly or wasn't folded.
2983
2994
return nullptr ;
2984
2995
2985
- std::function<Expr *(Expr *)> nilLiteralExpr = [&](Expr *expr) -> Expr * {
2986
- expr = expr->getSemanticsProvidingExpr ();
2987
- if (expr->getKind () == ExprKind::NilLiteral)
2988
- return expr;
2989
-
2990
- if (auto *optionalEvalExpr = dyn_cast<OptionalEvaluationExpr>(expr))
2991
- return nilLiteralExpr (optionalEvalExpr->getSubExpr ());
2992
-
2993
- if (auto *bindOptionalExpr = dyn_cast<BindOptionalExpr>(expr))
2994
- return nilLiteralExpr (bindOptionalExpr->getSubExpr ());
2995
-
2996
- return nullptr ;
2997
- };
2998
-
2999
- if (auto nilLiteral = nilLiteralExpr (fromExpr)) {
3000
- ctx.Diags .diagnose (nilLiteral->getLoc (),
3001
- diag::conditional_cast_from_nil);
3002
- return nullptr ;
3003
- }
3004
-
3005
2996
// Validate the resulting type.
3006
2997
TypeResolutionOptions options (TypeResolverContext::ExplicitCastExpr);
3007
2998
options |= TypeResolutionFlags::AllowUnboundGenerics;
0 commit comments