Skip to content

Commit 26aa91e

Browse files
authored
Merge pull request swiftlang#35230 from mininny/default-accessor-error-in-protocol
[Diagnostics] Improve diagnostic for defaulted accessor in a protocol property.
2 parents b9b5bd2 + 518e1b0 commit 26aa91e

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
@@ -6075,9 +6075,15 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags,
60756075
existingAccessor);
60766076
}
60776077

6078-
// There's no body in the limited syntax.
6079-
if (parsingLimitedSyntax)
6078+
// There's should be no body in the limited syntax.
6079+
if (parsingLimitedSyntax) {
6080+
// If there ~is~ a body in the limited syntax, alert that accessors can not
6081+
// be implemented.
6082+
if (Tok.is(tok::l_brace))
6083+
diagnose(Tok, diag::unexpected_getset_implementation_in_protocol,
6084+
getAccessorNameForDiagnostic(Kind, /*article*/ false));
60806085
continue;
6086+
}
60816087

60826088
// It's okay not to have a body if there's an external asm name.
60836089
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)