Skip to content

Commit f466e29

Browse files
authored
Merge pull request #15701 from CodaFi/humdinger
[DiagnosticsQoI] Bail on nil literal diagnostics if the source is Optional
2 parents c014b6d + 788f65b commit f466e29

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,6 +3032,10 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
30323032
// If we're diagnostic an issue with 'nil', produce a specific diagnostic,
30333033
// instead of uttering ExpressibleByNilLiteral.
30343034
if (isa<NilLiteralExpr>(expr->getValueProvidingExpr())) {
3035+
// If the source type is some kind of optional, the contextual conversion
3036+
// to 'nil' didn't fail, something else did.
3037+
if (contextualType->getOptionalObjectType())
3038+
return false;
30353039
diagnose(expr->getLoc(), nilDiag, contextualType);
30363040
if (nilFollowup)
30373041
nilFollowup();

test/decl/init/nil.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ var _: Int = nil
2626
// expected-error@-1 {{nil cannot initialize specified type 'Int'}}
2727
// expected-note@-2 {{add '?' to form the optional type 'Int?'}} {{11-11=?}}
2828

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

0 commit comments

Comments
 (0)