-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[AST] Introduce the notion of a protocol requirement signature. #7029
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
Conversation
b07e0c1 to
28d2234
Compare
|
@swift-ci Please test. |
DougGregor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functionality looks great, but I'd prefer a simpler API on ProtocolDecl.
lib/AST/ArchetypeBuilder.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is just a refactoring of what was there before, but it seems odd to use RequirementSource::Redundant here rather than RequirementSource::Protocol. Maybe we should (separately!) see if we can make that change to align it with what we do below.
include/swift/AST/ArchetypeBuilder.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the right API. addGenericSignature() is meant to introduce an arbitrary signature, but when protocolRequirementSig == true, the constraints on that signature are so specific that they'd be better served by calling them out via a new API. Moreover, a client of ArchetypeBuilder can do all of this work itself. For a given protocol proto, get the protocol's Self interface type as a GenericTypeParamType * and call addGenericParameter(), then addRequirement with a newly-synthesized conformance Requirement of Self: proto and your newly-created ProtocolRequirementSignatureSelf source.
To be really specific, I think this API should be on ProtocolDecl:
GenericSignature *getRequirementSignature();
You can make it perform on-demand computation for now (creating a new ArchetypeBuilder and following the above process) while we're just dumping() the results for debugging, then start caching the result on the AST, serializing it, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like it will be much better.
lib/AST/ArchetypeBuilder.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the bit that convinced me that we don't want the protocolRequirementSig parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "Self: P1" conformance shouldn't be there in the resulting signature, should it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not? Do you mean we should treat ProtocolRequirementSignatureSelf like they're redundant? I recall from earlier discussions that we'd just drop this requirement when doing later processing, but it would also make sense to just not include it in the first place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can ask clients to ignore it, I guess. It just feels wrong to have this requirement in the signature because it makes all of the other requirements redundant.
|
@huonw What is the intended use for this feature? |
|
@swiftix it provides the set of new requirements introduced by a protocol in a semantic form, minimized and canonicalized. There are a bunch of places we want to use it:
|
|
@huonw Thanks for the explanation! |
e05210e to
aa6b6a1
Compare
include/swift/AST/Decl.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put a doc comment here so it's clear to the reader what the requirement signature actually is. You can basically use the text of your commit message here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, whoops. Forgot to circle back and do that.
aa6b6a1 to
a180df0
Compare
This is a generic signature that stores exactly the requirements that a
protocol decl introduces, not letting them be implied by the Self :
Protocol requirement, nor storing any requirements introduced by the
protocols requirements.
Specifically, suppose we have
protocol Foo {}
protocol Bar {}
protocol Baz {
associatedtype X : Foo
}
protocol Qux: Baz {
associatedtype X : Bar
}
The normal generic signature and (canonical) protocol requirement
signature of `Baz` will be, respectively
<Self where Self : Baz>
<Self where Self : Baz, Self.X : Foo>
And for `Qux`, they will be:
<Self where Self : Qux>
<Self where Self : Qux, Self : Baz, Self.X : Bar>
Note that the `Self.X : Foo` requirement is not listed.
For the moment, this is unused except for `-debug-generic-signatures`.
a180df0 to
a964b6a
Compare
|
Build failed |
|
Build failed |
DougGregor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version looks fantastic, thanks!
|
@swift-ci Please test and merge. |
|
Looks like CI didn't get through the macOS build; the other smoke tests suffice, so I'm going to merge this now. |
This is a generic signature that stores exactly the requirements that a
protocol decl introduces, not letting them be implied by the Self :
Protocol requirement, nor storing any requirements introduced by the
protocols requirements.
Specifically, suppose we have
The normal generic signature and (canonical) protocol requirement
signature of
Bazwill be, respectivelyAnd for
Qux, they will be:Note that the
Self.X : Foorequirement is not listed.For the moment, this is unused except for
-debug-generic-signatures.