-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Requirement Machine] Implement same-element requirements. #67465
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
e3a2b4c
to
882e1e7
Compare
e06d45e
to
68d6a39
Compare
// requirement that becomes the following rewrite rule: | ||
// | ||
// [element].T.[concrete: C<X, Y>] => [element].T | ||
if (subjectType->isParameterPack()) { |
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.
But only if the constraint type does not contain any packs right?
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.
Yeah, that's right. I'm not sure what the rewrite rule looks like if the constraint type contains packs but the subject type does not? That's still conceptually a same-element requirement. Slava reminded me that something like repeat T == Array<each U>
is invalid because it's not representable!
@@ -80,6 +80,14 @@ struct Symbol::Storage final | |||
Kind = Kind::Shape; | |||
} | |||
|
|||
/// A dummy type for overload resolution of the | |||
/// 'pack element' constructor for Storage. | |||
struct ForPackElement {}; |
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 think you can get rid of both ForPack and ForShape and just add a Storage(Kind) overload, but it's a matter of taste.
3253208
to
34971b9
Compare
@swift-ci please smoke test |
opened element generic environments containing same-element requirements.
34971b9
to
f381b3f
Compare
@swift-ci please smoke test |
// pack element must always be on the left side of the rule, so type | ||
// parameters can appear out of order in the final same-type requirement. | ||
if (firstType->isParameterPack() == secondType->isParameterPack() && | ||
compareDependentTypes(firstType, secondType) >= 0) { |
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.
Can you change RequirementBuilder.cpp to build them in the other order instead?
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 constraint solver changes look good to me, I left a small comment inline about locator changes.
@@ -13584,6 +13584,16 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint( | |||
return SolutionKind::Solved; | |||
} | |||
|
|||
if (isSingleUnlabeledPackExpansionTuple(packTy)) { | |||
auto *packVar = | |||
createTypeVariable(getConstraintLocator(locator), TVO_CanBindToPack); |
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 think the locator for everything here except to the new ShapeOf
constraint needs to be locator
without its last element (which is PackShape
).
@@ -3125,18 +3125,9 @@ namespace { | |||
auto expansionType = | |||
CS.getType(packEnvironment)->castTo<PackExpansionType>(); | |||
CS.addConstraint(ConstraintKind::ShapeOf, expansionType->getCountType(), | |||
elementType, | |||
packType, | |||
CS.getConstraintLocator(packEnvironment, | |||
ConstraintLocator::PackShape)); |
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.
Interesting, this was completely backwards but worked... I wonder whether there are some checks we could add to archetype handling or shape-of constraint handling to fail if code like this is added in the future?
This is subsumed by #70227 |
SE-0393 supports same-element requirements. From the proposal:
We represent pack elements in the requirement machine through a new singleton
PackElement
symbol, written[element]
. Same-element requirements are mapped to re-write rules that prefix type parameter pack terms with the[element]
symbol.For example, given a protocol
P
with an associated typeA
, the generic signature<each T, U> where repeat each T: P, repeat (each T).A == U
maps to the following re-write system:The implementation is gated behind
-enable-experimental-feature SameElementRequirements
because the minimal conformance computation doesn't yet support same-element requirements. The current rewrite rules also do not support deriving pack element conformance from a scalar type parameter conformance through same-element requirements. For example, the generic signature<each T, U> where U: P, repeat each T == U
maps to the following rewrite system:The conformance rule
τ_0_1.[P] => τ_0_1
and the same-element rule[element].each τ_0_0 => τ_0_1
do not provide a rewrite path to the pack conformance ruleeach τ_0_0.[P] => each τ_0_0
. One possible solution is to use element symbols for all property rules, which would also allow the minimal conformance support to fallout naturally. However, this approach poses a problem for same-type-pack requirements. If we use[element]
symbols for same-type-pack requirements, e.g.[element].each τ_0_0 => [element].each τ_0_1
, then this rule no longer implies a same-shape ruleeach τ_0_0.[shape] => each τ_0_1.[shape]
. Stripping the[element]
symbols from same-type-pack rewrite rules creates a different set of problems for conformances, because the pack conformance rules contain[element]
symbols, which would prohibit applying associated type rewrite rules to non-element pack terms. I'm still exploring strategies for resolving these issues, hence the experimental flag!