Skip to content

Commit 8d5a33e

Browse files
Move to visitNilLiteralExpr
1 parent ceb3f38 commit 8d5a33e

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/Sema/CSGen.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,19 @@ 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 (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+
}
11551168

11561169
// `_ = nil`
11571170
if (auto *assignment = dyn_cast_or_null<AssignExpr>(parentExpr)) {
@@ -3207,14 +3220,6 @@ namespace {
32073220
}
32083221

32093222
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-
32183223
// Force-unwrap an optional of type T? to produce a T.
32193224
auto locator = CS.getConstraintLocator(expr);
32203225

test/Constraints/optional.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,6 @@ func sr_12309() {
439439
_ = nil? // expected-error {{value of optional type 'Optional<_>' must be unwrapped to a value of type '_'}}
440440
// expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
441441
// 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}}
442444
}

0 commit comments

Comments
 (0)