Skip to content

[DiagnosticsQoI] Bail on nil literal diagnostics if the source is Optional #15701

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
Apr 3, 2018
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
4 changes: 4 additions & 0 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3032,6 +3032,10 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
// If we're diagnostic an issue with 'nil', produce a specific diagnostic,
// instead of uttering ExpressibleByNilLiteral.
if (isa<NilLiteralExpr>(expr->getValueProvidingExpr())) {
// If the source type is some kind of optional, the contextual conversion
// to 'nil' didn't fail, something else did.
if (contextualType->getOptionalObjectType())
return false;
diagnose(expr->getLoc(), nilDiag, contextualType);
if (nilFollowup)
nilFollowup();
Expand Down
2 changes: 2 additions & 0 deletions test/decl/init/nil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ var _: Int = nil
// expected-error@-1 {{nil cannot initialize specified type 'Int'}}
// expected-note@-2 {{add '?' to form the optional type 'Int?'}} {{11-11=?}}

// 'nil' can initialize the specified type, if its generic parameters are bound
var _: Array? = nil // expected-error {{generic parameter 'Element' could not be inferred}}