-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] Warn for redundant access-level modifiers on setters or used in an extension. #18623
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
Conversation
…sion. This patch adds warning for redundant access-level modifiers used in an extension. It also refines the diagnostics of access_control_ext_member_more issues, in case the fixit could suggest redundant modifiers. Resolves: SR-8453.
@jrose-apple Hi Jordan, would you help review this, please? |
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 for picking this up, Ding! I have a few comments but the general approach seems reasonable.
public func extMemberPublic() {} | ||
// CHECK-DAG: sil private @$S22accessibility_warnings12PublicStructV07extImplC033_5D2F2E026754A901C0FF90C404896D02LLyyF | ||
private func extImplPublic() {} | ||
} |
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.
Please don't remove this; just add the expected diagnostics. It's still checking the SIL-level linkage.
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.
Fixed.
public func apply<Result>(_ transform: ((Wrapped) -> Result)?) -> Result? { | ||
return self.flatMap { value in | ||
transform.map { $0(value) } | ||
} | ||
} | ||
|
||
// expected-warning@+1 {{'public' modifier is redundant for instance method declared in a public extension}} |
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.
Since this test isn't really about the access modifiers, it's probably better to just remove the public
from the extension (both here and in test/Compatibility).
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.
Done.
@@ -1189,7 +1189,12 @@ ERROR(access_control_setter_more,none, | |||
(AccessLevel, unsigned, AccessLevel)) | |||
WARNING(access_control_ext_member_more,none, | |||
"declaring %select{%error|a fileprivate|an internal|a public|open}0 %1 in " | |||
"%select{a private|a fileprivate|an internal|public|%error}2 extension", | |||
"%select{a private|a fileprivate|an internal|a public|%error}2 extension", |
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 for fixing this. :-)
lib/Sema/MiscDiagnostics.cpp
Outdated
diag.fixItReplace(attr->getLocation(), fixItString.drop_back()); | ||
if (shouldNotReplace) { | ||
// Remove the attribute if replacement is not preferred. | ||
diag.fixItRemove(attr->getRange()); |
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.
Can you add a test with public(set)
?
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.
Good catch! I extended the solution to also produce warnings for redundant access modifiers on setters.
lib/Sema/MiscDiagnostics.h
Outdated
ValueDecl *VD, | ||
AccessLevel desiredAccess, | ||
bool isForSetter = false, | ||
bool shouldNotReplace = false); |
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.
Nitpick: "shouldNotReplace" doesn't tell us what to do instead. Maybe "shouldUseDefaultAccess" or something?
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.
Yeah, shouldUseDefaultAccess
sounds better!
Thank you for your feedback, Jordan! I updated the code as you suggested. In addition, this update produces a warning for the following case: 'fileprivate' modifier is redundant for instance method declared in a private (equivalent to fileprivate) extension private extension SomeClass {
fileprivate func foo() {}
+ ^~~~~~~~~~~~
} My earlier solution didn't report any warning for such cases. I think it more appropriate to warn, since it makes the entire redundancy checking comprehensive and consistent. |
modifier is used in an extension. In addition, add warnings for access modifier redundancy on property setters; and address comments from Jordan Rose.
I think that's consistent, yes. We warn if the access is greater and if the access is the same, so it makes sense to also handle "written greater but effectively the same". |
@swift-ci Please test |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thank you for the reply, Jordan! I had
Should all those warnings be fixed before landing this PR? |
Ooh, good thing to check. I should have looked at that first. Yes, we should definitely fix all those, but I can see Dispatch, Foundation, and XCTest all having different preferences from the rest (as the three teams who manage corelibs equivalents for their libraries). @mwwa, @phausler / @millenomi, and @briancroom, would you prefer to standardize on
or
for your respective projects? |
Personally I prefer the latter because it enforces knowingly making things public |
Thank you, Philippe! I updated the code to fix the |
I'm also happy with @phausler's preference, seems reasonable. We'll need to perform the equivalent fixes in corelibs-dispatch too. |
Thanks for your inputs, Matt! I updated the code for the |
@jrose-apple Hi Jordan, thanks for your all time support, and now all relevant projects have been prepared to embrace this patch. Would you help finalise this, please? |
Ah, I thought we were still waiting on XCTest. I missed that Stuart already took care of that. @swift-ci Please test |
Yes, Stuart helped XCTest earlier. The last one we were waiting for was libdispatch, which was merged just hours ago. Thanks to Kim! |
Build failed |
@swift-ci Please test Linux |
Thanks for all your work here! |
…d to swiftlang/swift#18623. Signed-off-by: Kim Topley <[email protected]>
This patch adds warning for redundant access-level modifiers used in an extension.This patch adds warnings for redundant access-level modifiers for two aspects: (1) used on members declared in an extension, and (2) used on property setters.
(1) For the following example, it produces a warning - 'public' modifier is redundant for instance method declared in a public extension.
public extension SomeClass { public func foo() {} + ^~~~~~~ }
(2) For instance below, it produces a warning - 'public(set)' modifier is redundant for a public property.
public SomeClass { public public(set) var x: Int = 0 + ^~~~~~~~~~~~ }
In addition, it refines the diagnostics of
access_control_ext_member_more
issues, in case the fixitcould suggest redundant modifiers. Consider the example below, the existing fixit suggests replacing
public
byinternal
, which is indeed a redundant modifier. This patch changes the fixit from replacement to removal.internal extension SomeClass { public func foo() {} ^~~~~~ - internal }
Moreover, the corresponding redundancy warnings in the existing code of Swift projects are also fixed. These include:
Resolves SR-8453.