Skip to content

ProtocolConformanceAnalysis #17177

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
Jun 19, 2018
Merged

Conversation

rajbarik
Copy link
Contributor

ProtocolConformanceAnalysis for a non-public protocol returns all the types (classes, structs, and enums) that conform to it. It is performed in the whole-module-compilation mode.
It is part one (only analysis phase) of the PR at #16739.

virtual void invalidateFunctionTables() override {}

/// Get the nominal types that implement a protocol.
const NominalTypeList &getConformances(ProtocolDecl *P) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this method itself be const?


/// Get the nominal types that implement a protocol.
const NominalTypeList &getConformances(ProtocolDecl *P) {
return ProtocolConformanceCache[P];
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use find() instead?

@slavapestov
Copy link
Contributor

This looks great, I only have two minor nitpicks.

@slavapestov slavapestov self-assigned this Jun 13, 2018
@rajbarik rajbarik force-pushed the raj-protoconfanal branch from 2e8fbab to aa368ca Compare June 15, 2018 17:57
@rajbarik
Copy link
Contributor Author

@slavapestov have a look.

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.

Sorry, just a handful more suggestions.

if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
auto Protocols = NTD->getAllProtocols();
for (auto &Protocol : Protocols) {
ProtocolConformanceCache[Protocol].push_back(NTD);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move the 'access level > internal' check up here


public:
NominalTypeWalker(ProtocolConformanceAnalysis::ProtocolConformanceMap
&ProtocolConformanceCache)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: This should line up with opening (

void ProtocolConformanceAnalysis::init() {

// We only do this in Whole-Module compilation mode.
if (!(M->isWholeModule()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unnecessary ()

/// or lower in access levels at a whole-module compilation mode.
if (!ProtocolConformanceCache.count(ProtoDecl))
continue;
if (ProtoDecl->getEffectiveAccess() > AccessLevel::Internal)
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this (see above)

const NominalTypeList *getConformances(const ProtocolDecl *P) const {
auto ConformsListIt = ProtocolConformanceCache.find(P);
return ConformsListIt != ProtocolConformanceCache.end()
? reinterpret_cast<const NominalTypeList *>(&*ConformsListIt)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the cast here is necessary

virtual void invalidateFunctionTables() override {}

/// Get the nominal types that implement a protocol.
const NominalTypeList *getConformances(const ProtocolDecl *P) const {
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be better to return an ArrayRef<NominalTypeDecl *> here instead of a pointer to a SmallVector

@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

1 similar comment
@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

@rajbarik rajbarik force-pushed the raj-protoconfanal branch from aa368ca to 9c1817c Compare June 18, 2018 17:24
@rajbarik
Copy link
Contributor Author

These are excellent suggestions @slavapestov! Please have a look now.

@rajbarik rajbarik force-pushed the raj-protoconfanal branch from 9c1817c to ab95ea2 Compare June 18, 2018 19:00

bool walkToDeclPre(Decl *D) override {
if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
if (NTD->getEffectiveAccess() <= AccessLevel::Internal) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't the right check here for an internal protocol, not an internal conforming type? Public protocols can always have conforming types from outside the module.

… types (classes, structs, and enums) that conform to it. It is performed in the whole-module-compilation mode.
@rajbarik rajbarik force-pushed the raj-protoconfanal branch from ab95ea2 to c2aef11 Compare June 18, 2018 20:38
@rajbarik
Copy link
Contributor Author

rajbarik commented Jun 18, 2018 via email

@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

1 similar comment
@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor

@rajbarik Thanks for making the changes. The PR looks good now, I'm going to merge it.

@slavapestov slavapestov merged commit e53ec62 into swiftlang:master Jun 19, 2018
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