Skip to content

Use 'fileprivate' for synthesized members of 'private' decls #15980

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
merged 1 commit into from
Apr 17, 2018

Conversation

jrose-apple
Copy link
Contributor

Since private means "limit to the enclosing scope (and extensions thereof)", putting it on a member means that the member can't be accessed everywhere the type might show up. That's normally a good thing, but it's not the desired effect for synthesized members used for derived conformances, and when it comes to class initializers this actually violates AST invariants.

rdar://problem/39478298

@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

@jrose-apple
Copy link
Contributor Author

@swift-ci Please test source compatibility

@itaiferber
Copy link
Contributor

itaiferber commented Apr 17, 2018

How does this affect the synthesis of CodingKeys, if at all? We have always explicitly synthesized private enum CodingKeys on purpose; exposing them to the rest of the file isn't necessarily something we want. (Or at least, this is something we need to consider when changing.)

A quick glance indicates that this doesn't change anything there, but I want to make sure I haven't missed anything. The intent here is just to have private {struct,class} Foo have a fileprivate init(from:)/fileprivate func encode(to:) rather than private, yeah?

@jrose-apple
Copy link
Contributor Author

Yep, that's correct. I didn't touch CodingKeys for exactly the reason you said: it's not part of the protocol and does not need to be exposed to others by default, no matter what the access level of the enclosing type.

@jrose-apple
Copy link
Contributor Author

Oops, there's a predicate I can use instead of isa<ConstructorDecl>.

@itaiferber
Copy link
Contributor

@jrose-apple Okay, excellent, thanks for confirming! Do you think we can add that assertion to the unit test as well, then? Just to be safe around future changes here.

@@ -2259,9 +2259,21 @@ ValueDecl::getFormalAccessScope(const DeclContext *useDC,
llvm_unreachable("unknown access level");
}

void ValueDecl::copyFormalAccessFrom(ValueDecl *source) {
void ValueDecl::copyFormalAccessFrom(const ValueDecl *source,
bool sourceIsParentContext) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need sourceIsParentContext, or can you just check if the source's parent DeclContext is a SourceFile, and if so, turn Private to FilePrivate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's relevant for any type, unfortunately, not just those at the top level. If we wanted to do it without the flag we could check if source and this are related in some way, but that seems more complicated for something every call site already knows.

Since 'private' means "limit to the enclosing scope (and extensions
thereof)", putting it on a member means that the member can't be
accessed everywhere the type might show up. That's normally a good
thing, but it's not the desired effect for synthesized members used
for derived conformances, and when it comes to class initializers this
actually violates AST invariants.

rdar://problem/39478298
Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

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

The fix makes sense, you can merge it as-is but I have an optional suggestion that may not make sense

@jrose-apple
Copy link
Contributor Author

Made the tweaks mentioned above.

@swift-ci Please smoke test

@jrose-apple jrose-apple merged commit 28a1dc7 into swiftlang:master Apr 17, 2018
@jrose-apple jrose-apple deleted the access-to-access branch April 17, 2018 22:19
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