Skip to content

[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

Closed
wants to merge 7 commits into from

Conversation

hborla
Copy link
Member

@hborla hborla commented Jul 22, 2023

SE-0393 supports same-element requirements. From the proposal:

A same-type requirement where one side is a type parameter pack and the other type is a scalar type that does not capture any type parameter packs is interpreted as constraining each element of the replacement type pack to the same scalar type:

func variadic<each S: Sequence, T>(_: repeat each S) where repeat (each S).Element == T {}

This is called a same-element requirement.

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 type A, the generic signature <each T, U> where repeat each T: P, repeat (each T).A == U maps to the following re-write system:

Rewrite system: {
- [P].[P] => [P]
- [P].A => [P:A]
- [P].[P:A] => [P:A]
- [P:A].[shape] => [shape]
- τ_0_1.[shape] => [shape]
- each τ_0_0.[P] => each τ_0_0
- [element].each τ_0_0.A => τ_0_1
- each τ_0_0.A => each τ_0_0.[P:A]
- [element].each τ_0_0.[P:A] => τ_0_1
- [element].each τ_0_0.[shape] => [shape]
}

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:

Rewrite system: {
- [P].[P] => [P]
- [P].A => [P:A]
- [P].[P:A] => [P:A]
- [P:A].[shape] => [shape]
- τ_0_1.[shape] => [shape]
- τ_0_1.[P] => τ_0_1
- [element].each τ_0_0 => τ_0_1
- τ_0_1.A => τ_0_1.[P:A]
}

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 rule each τ_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 rule each τ_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!

@hborla hborla force-pushed the same-element-requirements branch from e3a2b4c to 882e1e7 Compare August 29, 2023 21:02
@hborla hborla force-pushed the same-element-requirements branch from e06d45e to 68d6a39 Compare August 31, 2023 03:28
// requirement that becomes the following rewrite rule:
//
// [element].T.[concrete: C<X, Y>] => [element].T
if (subjectType->isParameterPack()) {
Copy link
Contributor

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?

Copy link
Member Author

@hborla hborla Aug 31, 2023

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 {};
Copy link
Contributor

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.

@hborla hborla force-pushed the same-element-requirements branch from 3253208 to 34971b9 Compare September 2, 2023 19:07
@hborla hborla marked this pull request as ready for review September 2, 2023 21:18
@hborla
Copy link
Member Author

hborla commented Sep 2, 2023

@swift-ci please smoke test

@hborla hborla force-pushed the same-element-requirements branch from 34971b9 to f381b3f Compare September 3, 2023 00:29
@hborla
Copy link
Member Author

hborla commented Sep 3, 2023

@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) {
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 change RequirementBuilder.cpp to build them in the other order instead?

Copy link
Contributor

@xedin xedin left a 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);
Copy link
Contributor

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));
Copy link
Contributor

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?

@hborla
Copy link
Member Author

hborla commented Jan 26, 2024

This is subsumed by #70227

@hborla hborla closed this Jan 26, 2024
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.

4 participants