File tree Expand file tree Collapse file tree 2 files changed +15
-8
lines changed Expand file tree Collapse file tree 2 files changed +15
-8
lines changed Original file line number Diff line number Diff line change @@ -1152,6 +1152,19 @@ namespace {
1152
1152
// `_ = nil`, let's diagnose it here because solver can't
1153
1153
// attempt any types for it.
1154
1154
auto *parentExpr = CS.getParentExpr (expr);
1155
+ if (auto *PE = dyn_cast<ParenExpr>(parentExpr))
1156
+ parentExpr = CS.getParentExpr (parentExpr);
1157
+
1158
+ if (isa<ForceValueExpr>(parentExpr)) {
1159
+ DE.diagnose (expr->getLoc (), diag::cannot_force_unwrap_nil_literal);
1160
+ return Type ();
1161
+ }
1162
+
1163
+ if (isa<NilLiteralExpr>(parentExpr)
1164
+ || isa<OptionalEvaluationExpr>(parentExpr)) {
1165
+ DE.diagnose (expr->getLoc (), diag::unresolved_nil_literal);
1166
+ return Type ();
1167
+ }
1155
1168
1156
1169
// `_ = nil`
1157
1170
if (auto *assignment = dyn_cast_or_null<AssignExpr>(parentExpr)) {
@@ -3207,14 +3220,6 @@ namespace {
3207
3220
}
3208
3221
3209
3222
Type visitForceValueExpr (ForceValueExpr *expr) {
3210
- // Diagnose force-unwrapping a 'nil' literal
3211
- auto subExpr = expr->getSubExpr ();
3212
- if (isa<NilLiteralExpr>(subExpr)) {
3213
- auto &DE = CS.getASTContext ().Diags ;
3214
- DE.diagnose (subExpr->getLoc (), diag::cannot_force_unwrap_nil_literal);
3215
- return Type ();
3216
- }
3217
-
3218
3223
// Force-unwrap an optional of type T? to produce a T.
3219
3224
auto locator = CS.getConstraintLocator (expr);
3220
3225
Original file line number Diff line number Diff line change @@ -439,4 +439,6 @@ func sr_12309() {
439
439
_ = nil ? // expected-error {{value of optional type 'Optional<_>' must be unwrapped to a value of type '_'}}
440
440
// expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
441
441
// expected-note@-2 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
442
+ _ = ( nil ) // expected-error {{'nil' requires a contextual type}}
443
+ _ = nil // expected-error {{'nil' requires a contextual type}}
442
444
}
You can’t perform that action at this time.
0 commit comments