Skip to content

ASTPrinter: print public inherited protocols of the skipped private protocols #38882

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
Aug 20, 2021

Conversation

egorzhdan
Copy link
Contributor

When printing the list of inherited protocols in the module interface, if private stdlib protocols are requested to be hidden (PrintOptions::SkipUnderscoredStdlibProtocols), make sure to print public inherited protocols of the hidden protocols.

This is especially noticeable for NSError types imported into Swift. For example, CocoaError is imported with a synthesized conformance to _BridgedStoredNSError, however, in the module interface generated by SourceKit, _BridgedStoredNSError is currently hidden from the inheritance list:

/// Describes errors within the Cocoa error domain.
public struct CocoaError {
    // ...
}

There is no indication in the module interface that CocoaError is an Error and can be thrown. This might cause confusion for someone reading the module interface, and for static analysis tools relying on it.

This change improves the module interface generation for synthesized conformances, so that the inheritance list contains all the public conformances that can be observed by the user. For CocoaError, we would now see this:

/// Describes errors within the Cocoa error domain.
public struct CocoaError : CustomNSError, Hashable, Error {
    // ...
}

…rotocols

When printing the list of inherited protocols in the module interface, if private stdlib protocols are requested to be hidden, make sure to print public inherited protocols of the hidden protocols.
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor Author

@nkcsgexi could you please take a look at this PR?
This is somewhat related to your change from a few years ago (#14248).

if (options.SkipPrivateStdlibDecls &&
proto->isPrivateStdlibDecl(!options.SkipUnderscoredStdlibProtocols)) {
auto inheritedProtocols = proto->getInheritedProtocols();
protocols.insert(inheritedProtocols.begin(), inheritedProtocols.end());
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible that one of these protocols would be hidden?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, in that case we'll just skip it on L5764. This actually happens with the NSError types: they are imported with a conformance to _BridgedStoredNSError (hidden), which inherits from _ObjectiveCBridgeableError (also hidden), so this logic will look into the parent protocol of _ObjectiveCBridgeableError, which is Error, and the conformance to Error will be printed.

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

Thanks Egor.

@egorzhdan egorzhdan merged commit 3464cb1 into swiftlang:main Aug 20, 2021
@egorzhdan egorzhdan deleted the astprinter-protocols branch August 20, 2021 20:47
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.

2 participants