Skip to content

[SourceKit] Add accessibility in extension structure (SR-4823) #11260

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

Merged

Conversation

marcelofabri
Copy link
Contributor

Adds the key.accessibility key in source.lang.swift.decl.extension structure.

Resolves SR-4823.

@xwu
Copy link
Collaborator

xwu commented Jul 29, 2017

...I may be misunderstanding what you're going for here, but as discussed previously on Swift-Evolution, extensions don't have their own accessibility. Sometimes, it is allowed to write public extension, for example, but that is intended to be a shorthand for all members being public. The extension itself continues to have no accessibility.

@marcelofabri
Copy link
Contributor Author

I know that formally extensions themselves doesn't have accessibility, but since public extension is allowed, I think that information should be available in the structure for consumers.

For example, SwiftLint has a rule to enforce that if all the members from an extension have the same accessibility, you should use the shorthand form. Currently, the implementation needs to look for all tokens with attributeBuiltin kind, check if it's an ACL keyword and try to match with an extension declaration.

If this information was available on the extension structure, that implementation would be much simpler and reliable.

@xwu
Copy link
Collaborator

xwu commented Jul 29, 2017

Cool, I can see how that's useful. However, it is misleading to label extensions as internal when they lack a modifier; that's simply not what they are.

@marcelofabri
Copy link
Contributor Author

it is misleading to label extensions as internal when they lack a modifier; that's simply not what they are.

What do you think it'd be better? Omit the key when there's no modifier?

@xwu
Copy link
Collaborator

xwu commented Jul 30, 2017

IMO, that would be superior, yes. Would that be a problematic approach?

@marcelofabri
Copy link
Contributor Author

Would that be a problematic approach?

From my point of view, no. I'd just like to hear from more people involved in SourceKit before implementing it. Maybe @nkcsgexi?

@nkcsgexi
Copy link
Contributor

@marcelofabri could you elaborated why we need to handle the accessibility of extension explicitly here? do we have issue with syntax highlighting?

@marcelofabri
Copy link
Contributor Author

No issues with syntax highlighting, it's more about providing more information for clients.

In particular, SwiftLint has some rules that would benefit from that information:

@nkcsgexi
Copy link
Contributor

Interesting, thanks for clarification! Judging from the requirements, we really don't need to infer accessibility when an extension has no explicit one, am I right? i.e. we need to only report the accessibility user specified.

@marcelofabri
Copy link
Contributor Author

Right, I just wanted to check if that'd be the preferred approach before making changes.

Just to be clear:
key.accessibility will only be present in source.lang.swift.decl.extension iff the accessibility was added explicitly.

@nkcsgexi
Copy link
Contributor

Right, this seems to me a better approach. To do this, could you refactor the accessibility collection part of inferAccessibility to a new static function getAccessibilityStrictly , and using getAccessibilityStrictly here for your improvement?

@marcelofabri
Copy link
Contributor Author

@nkcsgexi I've made the suggested changes in 86d9e91. 🎉

static Accessibility inferDefaultAccessibility(const ExtensionDecl *ED) {
auto StrictAccess = getAccessibilityStrictly(ED);
if (StrictAccess.hasValue())
return StrictAccess.getValue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Minor nitpick: this above three lines can be combined to:

 if (auto StrictAccess = getAccessibilityStrictly(ED))
   return StrictAccess.getValue();

auto StrictAccess = getAccessibilityStrictly(ED);
if (StrictAccess.hasValue()) {
AccessLevel = getAccessibilityUID(StrictAccess.getValue());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here : )

@nkcsgexi
Copy link
Contributor

@swift-ci please smoke test

@nkcsgexi nkcsgexi merged commit 8a8f7c8 into swiftlang:master Jul 31, 2017
@marcelofabri
Copy link
Contributor Author

🎉

@marcelofabri marcelofabri deleted the extension-accessibility-sourcekit branch July 31, 2017 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants