Skip to content

Commit 54f42b5

Browse files
authored
Merge pull request #61445 from hborla/pack-shape-requirements
[RequirementMachine] Implement inference and minimization of same-shape requirements.
2 parents f66d5cd + be619c6 commit 54f42b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+440
-150
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,10 @@ ERROR(requires_not_suitable_archetype,none,
25452545
"generic parameter or associated type",
25462546
(Type))
25472547

2548+
ERROR(invalid_shape_requirement,none,
2549+
"invalid same-shape requirement between %0 and %1",
2550+
(Type, Type))
2551+
25482552
ERROR(requires_generic_params_made_equal,none,
25492553
"same-type requirement makes generic parameters %0 and %1 equivalent",
25502554
(Type, Type))

include/swift/AST/GenericSignature.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,17 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
419419
/// Lookup a nested type with the given name within this type parameter.
420420
TypeDecl *lookupNestedType(Type type, Identifier name) const;
421421

422+
/// Returns the shape equivalence class of the given type parameter.
423+
///
424+
/// \param type The type parameter to compute the reduced shape for.
425+
/// Only type parameter packs have a shape, including dependent members
426+
/// whose root generic parameter is a pack.
427+
Type getReducedShape(Type type) const;
428+
429+
/// Returns \c true if the given type parameter packs are in
430+
/// the same shape equivalence class.
431+
bool haveSameShape(Type type1, Type type2) const;
432+
422433
/// Get the ordinal of a generic parameter in this generic signature.
423434
///
424435
/// For example, if you have a generic signature for a nested context like:

include/swift/AST/Requirement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Requirement
5858
Requirement subst(Args &&...args) const {
5959
auto newFirst = getFirstType().subst(std::forward<Args>(args)...);
6060
switch (getKind()) {
61-
case RequirementKind::SameCount:
61+
case RequirementKind::SameShape:
6262
case RequirementKind::Conformance:
6363
case RequirementKind::Superclass:
6464
case RequirementKind::SameType: {

include/swift/AST/RequirementBase.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ enum class RequirementKind : unsigned {
3636
/// A layout bound T : L, where T is a type that depends on a generic
3737
/// parameter and L is some layout specification that should bound T.
3838
Layout,
39-
/// A same-count requirement T.count == U.count, where T and U are pack
39+
/// A same-shape requirement shape(T) == shape(U), where T and U are pack
4040
/// parameters.
41-
SameCount
41+
SameShape
4242

4343
// Note: there is code that packs this enum in a 3-bit bitfield. Audit users
4444
// when adding enumerators.
@@ -105,7 +105,7 @@ class RequirementBase {
105105
hash_value(requirement.FirstTypeAndKind.getOpaqueValue());
106106
llvm::hash_code second;
107107
switch (requirement.getKind()) {
108-
case RequirementKind::SameCount:
108+
case RequirementKind::SameShape:
109109
case RequirementKind::Conformance:
110110
case RequirementKind::Superclass:
111111
case RequirementKind::SameType:
@@ -127,7 +127,7 @@ class RequirementBase {
127127
return false;
128128

129129
switch (lhs.getKind()) {
130-
case RequirementKind::SameCount:
130+
case RequirementKind::SameShape:
131131
case RequirementKind::Conformance:
132132
case RequirementKind::Superclass:
133133
case RequirementKind::SameType:

include/swift/SIL/SILWitnessVisitor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
5757
auto requirements = protocol->getRequirementSignature().getRequirements();
5858
for (const auto &reqt : requirements) {
5959
switch (reqt.getKind()) {
60-
case RequirementKind::SameCount:
61-
llvm_unreachable("Same-count requirement not supported here");
60+
case RequirementKind::SameShape:
61+
llvm_unreachable("Same-shape requirement not supported here");
6262

6363
// These requirements don't show up in the witness table.
6464
case RequirementKind::Superclass:

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5584,8 +5584,8 @@ ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig) {
55845584

55855585
for (auto requirement : baseGenericSig.getRequirements()) {
55865586
switch (requirement.getKind()) {
5587-
case RequirementKind::SameCount:
5588-
// Drop same-length requirements from the element signature.
5587+
case RequirementKind::SameShape:
5588+
// Drop same-shape requirements from the element signature.
55895589
break;
55905590
case RequirementKind::Conformance:
55915591
case RequirementKind::Superclass:

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,8 @@ Type ASTBuilder::createConstrainedExistentialType(
635635
llvm::SmallDenseMap<Identifier, Type> cmap;
636636
for (const auto &req : constraints) {
637637
switch (req.getKind()) {
638-
case RequirementKind::SameCount:
639-
llvm_unreachable("Same-count requirement not supported here");
638+
case RequirementKind::SameShape:
639+
llvm_unreachable("Same-shape requirement not supported here");
640640
case RequirementKind::Conformance:
641641
case RequirementKind::Superclass:
642642
case RequirementKind::Layout:

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4277,8 +4277,8 @@ void Requirement::dump() const {
42774277
}
42784278
void Requirement::dump(raw_ostream &out) const {
42794279
switch (getKind()) {
4280-
case RequirementKind::SameCount:
4281-
out << "same_count: ";
4280+
case RequirementKind::SameShape:
4281+
out << "same_shape: ";
42824282
break;
42834283
case RequirementKind::Conformance:
42844284
out << "conforms_to: ";

lib/AST/ASTMangler.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,8 +2868,8 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
28682868
Type FirstTy = reqt.getFirstType()->getCanonicalType();
28692869

28702870
switch (reqt.getKind()) {
2871-
case RequirementKind::SameCount:
2872-
llvm_unreachable("Same-count requirement not supported here");
2871+
case RequirementKind::SameShape:
2872+
llvm_unreachable("Same-shape requirement not supported here");
28732873
case RequirementKind::Layout:
28742874
break;
28752875
case RequirementKind::Conformance: {
@@ -2891,8 +2891,8 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
28912891
if (auto *DT = FirstTy->getAs<DependentMemberType>()) {
28922892
if (tryMangleTypeSubstitution(DT, sig)) {
28932893
switch (reqt.getKind()) {
2894-
case RequirementKind::SameCount:
2895-
llvm_unreachable("Same-count requirement not supported here");
2894+
case RequirementKind::SameShape:
2895+
llvm_unreachable("Same-shape requirement not supported here");
28962896
case RequirementKind::Conformance:
28972897
return appendOperator("RQ");
28982898
case RequirementKind::Layout:
@@ -2912,8 +2912,8 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
29122912
addTypeSubstitution(DT, sig);
29132913
assert(gpBase);
29142914
switch (reqt.getKind()) {
2915-
case RequirementKind::SameCount:
2916-
llvm_unreachable("Same-count requirement not supported here");
2915+
case RequirementKind::SameShape:
2916+
llvm_unreachable("Same-shape requirement not supported here");
29172917
case RequirementKind::Conformance:
29182918
return appendOpWithGenericParamIndex(isAssocTypeAtDepth ? "RP" : "Rp",
29192919
gpBase, lhsBaseIsProtocolSelf);
@@ -2933,8 +2933,8 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
29332933
}
29342934
GenericTypeParamType *gpBase = FirstTy->castTo<GenericTypeParamType>();
29352935
switch (reqt.getKind()) {
2936-
case RequirementKind::SameCount:
2937-
llvm_unreachable("Same-count requirement not supported here");
2936+
case RequirementKind::SameShape:
2937+
llvm_unreachable("Same-shape requirement not supported here");
29382938
case RequirementKind::Conformance:
29392939
return appendOpWithGenericParamIndex("R", gpBase);
29402940
case RequirementKind::Layout:
@@ -3494,8 +3494,8 @@ void ASTMangler::appendConcreteProtocolConformance(
34943494
bool firstRequirement = true;
34953495
for (const auto &conditionalReq : conformance->getConditionalRequirements()) {
34963496
switch (conditionalReq.getKind()) {
3497-
case RequirementKind::SameCount:
3498-
llvm_unreachable("Same-count requirement not supported here");
3497+
case RequirementKind::SameShape:
3498+
llvm_unreachable("Same-shape requirement not supported here");
34993499
case RequirementKind::Layout:
35003500
case RequirementKind::SameType:
35013501
case RequirementKind::Superclass:
@@ -3645,8 +3645,8 @@ void ASTMangler::appendConstrainedExistential(Type base, GenericSignature sig,
36453645
bool firstRequirement = true;
36463646
for (const auto &reqt : requirements) {
36473647
switch (reqt.getKind()) {
3648-
case RequirementKind::SameCount:
3649-
llvm_unreachable("Same-count requirement not supported here");
3648+
case RequirementKind::SameShape:
3649+
llvm_unreachable("Same-shape requirement not supported here");
36503650
case RequirementKind::Layout:
36513651
case RequirementKind::Conformance:
36523652
case RequirementKind::Superclass:

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
240240
if (!isPublicOrUsableFromInline(req.getSecondType()))
241241
return false;
242242
break;
243-
case RequirementKind::SameCount:
243+
case RequirementKind::SameShape:
244244
case RequirementKind::Layout:
245245
break;
246246
}
@@ -1415,8 +1415,8 @@ bestRequirementPrintLocation(ProtocolDecl *proto, const Requirement &req) {
14151415
bool inWhereClause;
14161416

14171417
switch (req.getKind()) {
1418-
case RequirementKind::SameCount:
1419-
llvm_unreachable("Same-count requirements not supported here");
1418+
case RequirementKind::SameShape:
1419+
llvm_unreachable("Same-shape requirements not supported here");
14201420
case RequirementKind::Layout:
14211421
case RequirementKind::Conformance:
14221422
case RequirementKind::Superclass: {
@@ -1510,7 +1510,7 @@ static unsigned getDepthOfRequirement(const Requirement &req) {
15101510

15111511
case RequirementKind::Superclass:
15121512
case RequirementKind::SameType:
1513-
case RequirementKind::SameCount: {
1513+
case RequirementKind::SameShape: {
15141514
// Return the max valid depth of firstType and secondType.
15151515
unsigned firstDepth = getDepthOfType(req.getFirstType());
15161516
unsigned secondDepth = getDepthOfType(req.getSecondType());
@@ -1757,8 +1757,8 @@ void PrintAST::printSingleDepthOfGenericSignature(
17571757
// We only print the second part of a requirement in the "inherited"
17581758
// clause.
17591759
switch (req.getKind()) {
1760-
case RequirementKind::SameCount:
1761-
llvm_unreachable("Same-count requirement not supported here");
1760+
case RequirementKind::SameShape:
1761+
llvm_unreachable("Same-shape requirement not supported here");
17621762

17631763
case RequirementKind::Layout:
17641764
req.getLayoutConstraint()->print(Printer, Options);
@@ -1788,10 +1788,10 @@ void PrintAST::printSingleDepthOfGenericSignature(
17881788
void PrintAST::printRequirement(const Requirement &req) {
17891789
printTransformedType(req.getFirstType());
17901790
switch (req.getKind()) {
1791-
case RequirementKind::SameCount:
1792-
Printer << ".count == ";
1791+
case RequirementKind::SameShape:
1792+
Printer << ".shape == ";
17931793
printTransformedType(req.getSecondType());
1794-
Printer << ".count";
1794+
Printer << ".shape";
17951795
return;
17961796
case RequirementKind::Layout:
17971797
Printer << " : ";

0 commit comments

Comments
 (0)