Skip to content

Commit c5acfa1

Browse files
committed
fix <rdar://problem/23952125> QoI: Subscript in protocol with missing {}, better diagnostic please
we used to say: error: expected '{' for subscripting we now say: error: subscript in protocol must have explicit { get } or { get set } specifier and produce a fixit to insert { get set }
1 parent f7252d2 commit c5acfa1

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,11 @@ ERROR(expected_arrow_subscript,decl_parsing,PointsToFirstBadToken,
329329
ERROR(expected_type_subscript,type_parsing,PointsToFirstBadToken,
330330
"expected subscripting element type", ())
331331
ERROR(expected_lbrace_subscript,decl_parsing,PointsToFirstBadToken,
332-
"expected '{' for subscripting", ())
332+
"expected '{' in subscript to specify getter and setter implementation",
333+
())
334+
ERROR(expected_lbrace_subscript_protocol,decl_parsing,PointsToFirstBadToken,
335+
"subscript in protocol must have explicit { get } or "
336+
"{ get set } specifier", ())
333337
ERROR(subscript_without_get,decl_parsing,none,
334338
"subscript declarations must have a getter", ())
335339
ERROR(subscript_static,decl_parsing,none,

lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4840,7 +4840,11 @@ ParserStatus Parser::parseDeclSubscript(ParseDeclOptions Flags,
48404840
if (Tok.isNot(tok::l_brace)) {
48414841
// Subscript declarations must always have at least a getter, so they need
48424842
// to be followed by a {.
4843-
diagnose(Tok, diag::expected_lbrace_subscript);
4843+
if (Flags.contains(PD_InProtocol))
4844+
diagnose(Tok, diag::expected_lbrace_subscript_protocol)
4845+
.fixItInsertAfter(ElementTy.get()->getEndLoc(), " { get set }");
4846+
else
4847+
diagnose(Tok, diag::expected_lbrace_subscript);
48444848
Status.setIsParseError();
48454849
} else {
48464850
if (parseGetSet(Flags, Indices.get(), ElementTy.get(),

test/Parse/subscripting.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ struct A4 {
127127
}
128128

129129
struct A5 {
130-
subscript(i : Int) -> Int // expected-error {{expected '{' for subscripting}}
130+
subscript(i : Int) -> Int // expected-error {{expected '{' in subscript to specify getter and setter implementation}}
131131
}
132132

133133
struct A6 {
@@ -155,7 +155,7 @@ struct A7b {
155155
}
156156

157157
struct A8 {
158-
subscript(i : Int) -> Int // expected-error{{expected '{' for subscripting}}
158+
subscript(i : Int) -> Int // expected-error{{expected '{' in subscript to specify getter and setter implementation}}
159159
get { // expected-error{{expected declaration}}
160160
return stored
161161
}

test/decl/subscript/subscripting.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,14 @@ class Foo {
272272
}
273273
}
274274

275+
// <rdar://problem/23952125> QoI: Subscript in protocol with missing {}, better diagnostic please
276+
protocol r23952125 {
277+
typealias ItemType
278+
var count: Int { get }
279+
subscript(index: Int) -> ItemType // expected-error {{subscript in protocol must have explicit { get } or { get set } specifier}} {{36-36= { get set \}}}
280+
281+
var c : Int // expected-error {{property in protocol must have explicit { get } or { get set } specifier}}
282+
}
283+
284+
285+

0 commit comments

Comments
 (0)