Skip to content

[ConstraintSystem] Diagnose more cases of invalid nil use during co… #30621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,18 @@ namespace {
while (parentExpr && isa<ParenExpr>(parentExpr))
parentExpr = CS.getParentExpr(parentExpr);

// In cases like `_ = nil?` AST would have `nil`
// wrapped in `BindOptionalExpr`.
if (parentExpr && isa<BindOptionalExpr>(parentExpr))
parentExpr = CS.getParentExpr(parentExpr);

if (parentExpr) {
// `_ = nil as? ...`
if (isa<ConditionalCheckedCastExpr>(parentExpr)) {
DE.diagnose(expr->getLoc(), diag::conditional_cast_from_nil);
return Type();
}

// `_ = nil!`
if (isa<ForceValueExpr>(parentExpr)) {
DE.diagnose(expr->getLoc(), diag::cannot_force_unwrap_nil_literal);
Expand All @@ -1167,13 +1178,13 @@ namespace {
DE.diagnose(expr->getLoc(), diag::unresolved_nil_literal);
return Type();
}
}

// `_ = nil`
if (auto *assignment = dyn_cast_or_null<AssignExpr>(parentExpr)) {
if (isa<DiscardAssignmentExpr>(assignment->getDest())) {
DE.diagnose(expr->getLoc(), diag::unresolved_nil_literal);
return Type();
// `_ = nil`
if (auto *assignment = dyn_cast<AssignExpr>(parentExpr)) {
if (isa<DiscardAssignmentExpr>(assignment->getDest())) {
DE.diagnose(expr->getLoc(), diag::unresolved_nil_literal);
return Type();
}
}
}

Expand Down Expand Up @@ -2982,26 +2993,6 @@ namespace {
if (!fromExpr) // Either wasn't constructed correctly or wasn't folded.
return nullptr;

std::function<Expr *(Expr *)> nilLiteralExpr = [&](Expr *expr) -> Expr * {
expr = expr->getSemanticsProvidingExpr();
if (expr->getKind() == ExprKind::NilLiteral)
return expr;

if (auto *optionalEvalExpr = dyn_cast<OptionalEvaluationExpr>(expr))
return nilLiteralExpr(optionalEvalExpr->getSubExpr());

if (auto *bindOptionalExpr = dyn_cast<BindOptionalExpr>(expr))
return nilLiteralExpr(bindOptionalExpr->getSubExpr());

return nullptr;
};

if (auto nilLiteral = nilLiteralExpr(fromExpr)) {
ctx.Diags.diagnose(nilLiteral->getLoc(),
diag::conditional_cast_from_nil);
return nullptr;
}

// Validate the resulting type.
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
options |= TypeResolutionFlags::AllowUnboundGenerics;
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func process(p: Any?) {
func compare<T>(_: T, _: T) {} // expected-note {{'compare' declared here}}
func compare<T>(_: T?, _: T?) {}

_ = nil? as? Int?? // expected-error {{nil literal cannot be the source of a conditional cast}}
_ = nil? as? Int?? // expected-error {{'nil' requires a contextual type}}

func test_tuple_casts_no_warn() {
struct Foo {}
Expand Down
7 changes: 4 additions & 3 deletions test/Constraints/optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,10 @@ func sr_12309() {
_ = (nil!) // expected-error {{'nil' literal cannot be force unwrapped}}
_ = (nil)! // expected-error {{'nil' literal cannot be force unwrapped}}
_ = ((nil))! // expected-error {{'nil' literal cannot be force unwrapped}}
_ = nil? // expected-error {{value of optional type 'Optional<_>' must be unwrapped to a value of type '_'}}
// expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
// expected-note@-2 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
_ = nil? // expected-error {{'nil' requires a contextual type}}
_ = ((nil?)) // expected-error {{'nil' requires a contextual type}}
_ = ((nil))? // expected-error {{'nil' requires a contextual type}}
_ = ((nil)?) // expected-error {{'nil' requires a contextual type}}
_ = nil // expected-error {{'nil' requires a contextual type}}
_ = (nil) // expected-error {{'nil' requires a contextual type}}
_ = ((nil)) // expected-error {{'nil' requires a contextual type}}
Expand Down