Skip to content

Commit 5bb2a62

Browse files
authored
Merge pull request swiftlang#30409 from HassanElDesouky/SR-12309
[CSGen] Force unwrapping 'nil' compiles without warning
2 parents 72bca3b + e747dc2 commit 5bb2a62

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,6 +3204,8 @@ ERROR(cannot_infer_base_of_unresolved_member,none,
32043204
"cannot infer contextual base in reference to member %0", (DeclNameRef))
32053205
ERROR(unresolved_nil_literal,none,
32063206
"'nil' requires a contextual type", ())
3207+
ERROR(cannot_force_unwrap_nil_literal,none,
3208+
"'nil' literal cannot be force unwrapped", ())
32073209

32083210
ERROR(type_of_expression_is_ambiguous,none,
32093211
"type of expression is ambiguous without more context", ())

lib/Sema/CSGen.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,22 @@ namespace {
11521152
// `_ = nil`, let's diagnose it here because solver can't
11531153
// attempt any types for it.
11541154
auto *parentExpr = CS.getParentExpr(expr);
1155+
if (parentExpr && isa<ParenExpr>(parentExpr))
1156+
parentExpr = CS.getParentExpr(parentExpr);
1157+
1158+
if (parentExpr) {
1159+
// `_ = nil!`
1160+
if (isa<ForceValueExpr>(parentExpr)) {
1161+
DE.diagnose(expr->getLoc(), diag::cannot_force_unwrap_nil_literal);
1162+
return Type();
1163+
}
1164+
1165+
// `_ = nil?`
1166+
if (isa<OptionalEvaluationExpr>(parentExpr)) {
1167+
DE.diagnose(expr->getLoc(), diag::unresolved_nil_literal);
1168+
return Type();
1169+
}
1170+
}
11551171

11561172
// `_ = nil`
11571173
if (auto *assignment = dyn_cast_or_null<AssignExpr>(parentExpr)) {

test/Constraints/optional.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,18 @@ func invalidOptionalChaining(a: Any) {
427427
// expected-error@-1 {{value of protocol type 'Any' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols}}
428428
// expected-note@-2 {{requirement from conditional conformance of 'Any?' to 'Equatable'}}
429429
}
430+
431+
// SR-12309 - Force unwrapping 'nil' compiles without warning
432+
func sr_12309() {
433+
struct S {
434+
var foo: Int
435+
}
436+
437+
_ = S(foo: nil!) // expected-error {{'nil' literal cannot be force unwrapped}}
438+
_ = nil! // expected-error {{'nil' literal cannot be force unwrapped}}
439+
_ = nil? // expected-error {{value of optional type 'Optional<_>' must be unwrapped to a value of type '_'}}
440+
// expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
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}}
444+
}

0 commit comments

Comments
 (0)