Skip to content

Sema: Explicit stricter minimization of protocol composition types for redeclaration checking #39694

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 2 commits into from
Jan 25, 2022

Conversation

AnthonyLatsis
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis commented Oct 11, 2021

Enable explicit stronger minimization of protocol compositions via generic signature computation for purposes like redeclaration checking.

Note: This is a source-breaking change, because the following is now a redeclaration error:

protocol P: class {}
func foo(_: P) {}
func foo(_: P & AnyObject) {}

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please test source compatibility debug

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@slavapestov
Copy link
Contributor

Is this also ABI-breaking potentially, if you previously had a redundant AnyObject constraint in a composition that is now minimized away?

@AnthonyLatsis
Copy link
Collaborator Author

Is this also ABI-breaking potentially, if you previously had a redundant AnyObject constraint in a composition that is now minimized away?

Yes. Does a smoke test not exclude any mangling differences?

@slavapestov
Copy link
Contributor

It's pretty unlikely that someone has a binary framework that relies on this, but it might be safer if minimization was an explicit step and not part of getCanonicalType()?

@AnthonyLatsis
Copy link
Collaborator Author

You mean, to minimize selectively instead of having canonical types be truly minimal?

@slavapestov
Copy link
Contributor

Yeah. I'm assuming this is for the type erasure when you use an existential member that returns an associated type -- you could minimize inside the member access path when computing the erased type of the member.

@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented Oct 28, 2021

I'm assuming this is for the type erasure when you use an existential member that returns an associated type

Not anymore. That was the original plan, but it looks like using canonical types is enough to make the erasure work (we never retain sugar in generic signatures anyway, so there's no reason to separate minimization from canonicalization in this particular case).

If the ABI break is a concern, we can drop that part of the code and still benefit from the absence of "premature canonicalization".

@AnthonyLatsis
Copy link
Collaborator Author

@slavapestov What if we special-case protocol composition types in ASTMangler::getDeclTypeForMangling to preserve the mangling and make this ABI-compatible?

@slavapestov
Copy link
Contributor

I'd prefer not to add any special cases and avoid changing getCanonicalType() to minimize protocol compositions. If you need to minimize protocol compositions for something, you can add a new method to ProtocolCompositionType (or ExistentialLayout or whatever) which performs it explicitly.

@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented Oct 31, 2021

To be clear, are we ready to take on the source break if I make stricter minimization explicit and use it in redeclaration checking, as well as the other part of this PR (4602bf1)?

@AnthonyLatsis AnthonyLatsis changed the title AST: More principled ProtocolCompositionType minimization ProtocolCompositionType: Misc improvements Nov 1, 2021
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@AnthonyLatsis AnthonyLatsis changed the title ProtocolCompositionType: Misc improvements Sema: Explicit stricter minimization of protocol composition types for redeclaration checking Jan 23, 2022
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test

@AnthonyLatsis
Copy link
Collaborator Author

@slavapestov Could I ask for another opinion on this? I kept it narrow as per your suggestion.

lib/AST/Type.cpp Outdated
{Requirement(RequirementKind::Conformance, GParam, CanTy)});
if (!Sig) {
return CanTy;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool! Can this use ASTContext::getOpenedArchetypeSignature()?

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please test source compatibility

@AnthonyLatsis
Copy link
Collaborator Author

@slavapestov Much obliged

@AnthonyLatsis AnthonyLatsis merged commit 00abccb into swiftlang:main Jan 25, 2022
@AnthonyLatsis AnthonyLatsis deleted the mincomp branch January 25, 2022 13:38
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