Skip to content

Commit 518e1b0

Browse files
committed
[Parse] Improve error for defaulted accessor in a protocol property. SR-13963
1 parent dbcf7fe commit 518e1b0

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ ERROR(computed_property_no_accessors, none,
248248
"%select{computed property|subscript}0 must have accessors specified", (bool))
249249
ERROR(expected_getset_in_protocol,none,
250250
"expected get or set in a protocol property", ())
251+
ERROR(unexpected_getset_implementation_in_protocol,none,
252+
"protocol property %0 cannot have a default implementation specified here; use extension instead", (StringRef))
251253
ERROR(computed_property_missing_type,none,
252254
"computed property must have an explicit type", ())
253255
ERROR(getset_nontrivial_pattern,none,

lib/Parse/ParseDecl.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5817,9 +5817,15 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags,
58175817
existingAccessor);
58185818
}
58195819

5820-
// There's no body in the limited syntax.
5821-
if (parsingLimitedSyntax)
5820+
// There's should be no body in the limited syntax.
5821+
if (parsingLimitedSyntax) {
5822+
// If there ~is~ a body in the limited syntax, alert that accessors can not
5823+
// be implemented.
5824+
if (Tok.is(tok::l_brace))
5825+
diagnose(Tok, diag::unexpected_getset_implementation_in_protocol,
5826+
getAccessorNameForDiagnostic(Kind, /*article*/ false));
58225827
continue;
5828+
}
58235829

58245830
// It's okay not to have a body if there's an external asm name.
58255831
if (!Tok.is(tok::l_brace)) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
4+
//===---
5+
// SR-13963
6+
//===---
7+
8+
protocol Protocol1 {
9+
var a: Int { get { 0 } } // expected-error {{protocol property getter cannot have a default implementation specified here; use extension instead}} expected-error {{expected get or set in a protocol property}}
10+
}
11+
12+
protocol Protocol2 {
13+
var a: Int { set { a = 0 } } // expected-error {{protocol property setter cannot have a default implementation specified here; use extension instead}} expected-error {{expected get or set in a protocol property}}
14+
}
15+
16+
protocol Protocol3 {
17+
var a: Int { get { 0 } set } // expected-error {{protocol property getter cannot have a default implementation specified here; use extension instead}} expected-error {{expected get or set in a protocol property}}
18+
}
19+
20+
protocol Protocol4 {
21+
var a: Int { get { 0 } set { a = 0 } } // expected-error {{protocol property getter cannot have a default implementation specified here; use extension instead}} expected-error {{expected get or set in a protocol property}}
22+
}
23+
24+
protocol Protocol5 {
25+
var a: Int { get set willSet { print("HI")} } // expected-error {{expected get or set in a protocol property}} expected-error {{expected get or set in a protocol property}}
26+
}
27+
28+
protocol Protocol6 {
29+
var a: Int { get { 0 } set willSet { print("HI")} } // expected-error {{protocol property getter cannot have a default implementation specified here; use extension instead}} expected-error {{expected get or set in a protocol property}}
30+
}
31+
32+
protocol Protocol7 {
33+
var a: Int { set { print("HI") } willSet { print("HI") } } // expected-error {{protocol property setter cannot have a default implementation specified here; use extension instead}} expected-error {{expected get or set in a protocol property}}
34+
}
35+
36+
protocol Protocol8 {
37+
var a: Int { set willSet { print("HI") } } // expected-error {{expected get or set in a protocol property}} expected-error {{expected get or set in a protocol property}}
38+
}
39+
40+
protocol Protocol9 {
41+
var a: Int { return 0 } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} expected-error {{expected get or set in a protocol property}}
42+
}
43+
44+
protocol Protocol10 {
45+
}
46+
// No error in extension
47+
extension Protocol10 {
48+
var a: Int { return 0 }
49+
}

0 commit comments

Comments
 (0)