-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Diagnostics] Improve diagnostic for defaulted accessor in a protocol property. #35230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Diagnostics] Improve diagnostic for defaulted accessor in a protocol property. #35230
Conversation
6ba0975
to
aa408f1
Compare
//===--- | ||
|
||
protocol Protocol1 { | ||
var a: Int { get { 0 } } // expected-error {{protocol property cannot have a default implementation of getter}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it wouldn't make sense(or be possible) to offer a fix-it to remove from lbrace to rbrace e.g. { 0 }
in this case? This is more a question as I'm just thinking if it would be a good thing for the user also...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think it makes sense to offer a fix-it, but I'm not sure how I can get the SourceRange from the lbrace to rbrace without disrupting the ongoing parsing.. I'd really appreciate any feedbacks or pointers on how I could do that! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer not to offer a fix-it for removing. We should be very cautious about deleting user's code.
There hasn't been any progress in this PR for a while. Is there anything I can do to move this forward? Not sure whom I should ping.. |
Ah sorry, this just slipped off my head. I will review this this weekend. |
lib/Parse/ParseDecl.cpp
Outdated
@@ -5759,7 +5759,11 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags, | |||
|
|||
// parsingLimitedSyntax mode cannot have a body. | |||
if (parsingLimitedSyntax) { | |||
diagnose(Tok, diag::expected_getset_in_protocol); | |||
// No error should be generated on implicitly created getter. | |||
auto IsImplicitGetter = Kind == AccessorKind::Get && Loc.isInvalid(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is always true
since this is in NotAccessor = parseAccessorIntroducer()
branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test case for "implicit getter" in a protocol? e.g.:
protocol ProtocolXXX {
var a: Int { return 1 }
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I removed unnecessary IsImplicitGetter
and added a test case for implicit getter :)
aa408f1
to
dde7aa8
Compare
@@ -248,6 +248,8 @@ ERROR(computed_property_no_accessors, none, | |||
"%select{computed property|subscript}0 must have accessors specified", (bool)) | |||
ERROR(expected_getset_in_protocol,none, | |||
"expected get or set in a protocol property", ()) | |||
ERROR(unexpected_getset_implementation_in_protocol,none, | |||
"protocol property cannot have a default implementation of %0", (StringRef)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a slight rewording so that what we're referring to (the property getter/setter) isn't split between the beginning and end of the sentence, and to clarify that the error is not because a default implementation isn't ever allowed, but not allowed here:
"protocol property cannot have a default implementation of %0", (StringRef)) | |
"protocol property %0 cannot have a default implementation specified here; use an extension instead", (StringRef)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! changed it :)
dde7aa8
to
000c6ed
Compare
lib/Parse/ParseDecl.cpp
Outdated
if (parsingLimitedSyntax) { | ||
diagnose(Tok, diag::expected_getset_in_protocol); | ||
Invalid = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel uncomfortable with marking Invalid
without emitting diagnostics. Could you just leave this diagnostics as is for now? I know
protocol Protocol9 {
var a: Int { return 0 }
}
results kind of duplicated errors (expected get or set in a protocol property
and property in protocol must have explicit { get } or { get set } specifier
) But still.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! I reverted the change :)
000c6ed
to
518e1b0
Compare
@swift-ci Please smoke test |
@swift-ci Please smoke test Windows |
Thank you Minhyuk! |
Code example:
This PR adds a diagnostics that says
protocol property cannot have a default implementation of {ACCESSOR_TYPE}
when a body is provided to a computed property in protocol.Resolves SR-13963.