Skip to content

Commit b3c7894

Browse files
authored
Merge pull request #4680 from rudkx/rdar27449208-swift-3.0-branch
Special-case error for indexing into a nil literal.
2 parents fa65a39 + 1fd961e commit b3c7894

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ ERROR(cannot_subscript_base,none,
196196
"cannot subscript a value of type %0",
197197
(Type))
198198

199+
ERROR(cannot_subscript_nil_literal,none,
200+
"cannot subscript a nil literal value", ())
201+
199202
ERROR(cannot_pass_rvalue_inout_subelement,none,
200203
"cannot pass immutable value as inout argument: %0",
201204
(StringRef))

lib/Sema/CSDiag.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4762,6 +4762,12 @@ bool FailureDiagnosis::visitSubscriptExpr(SubscriptExpr *SE) {
47624762
if (!baseExpr) return true;
47634763
auto baseType = baseExpr->getType();
47644764

4765+
if (isa<NilLiteralExpr>(baseExpr)) {
4766+
diagnose(baseExpr->getLoc(), diag::cannot_subscript_nil_literal)
4767+
.highlight(baseExpr->getSourceRange());
4768+
return true;
4769+
}
4770+
47654771
auto locator =
47664772
CS->getConstraintLocator(SE, ConstraintLocator::SubscriptMember);
47674773

test/decl/subscript/subscripting.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ func testSubscript1(_ s2 : SubscriptTest2) {
254254

255255

256256
let b = s2[1, "foo"] // expected-error {{cannot convert value of type 'Int' to expected argument type 'String'}}
257+
258+
// rdar://problem/27449208
259+
let v: (Int?, [Int]?) = (nil [17]) // expected-error {{cannot subscript a nil literal value}}
257260
}
258261

259262
// sr-114 & rdar://22007370

0 commit comments

Comments
 (0)