Skip to content

Commit 5c32f21

Browse files
committed
AST: Introduce RequirementKind::SameCount
1 parent 7a335d6 commit 5c32f21

37 files changed

+209
-32
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,10 +2340,10 @@ NOTE(protocol_witness_type_conflict,none,
23402340
"candidate has non-matching type %0%1", (Type, StringRef))
23412341
NOTE(protocol_witness_missing_requirement,none,
23422342
"candidate would match if %0 %select{conformed to|subclassed|"
2343-
"was the same type as}2 %1", (Type, Type, unsigned))
2343+
"was the same type as|%error|%error}2 %1", (Type, Type, unsigned))
23442344
NOTE(protocol_type_witness_missing_requirement,none,
23452345
"candidate would match if the conformance required that %0 "
2346-
"%select{conformed to|subclassed|was the same type as}2 %1",
2346+
"%select{conformed to|subclassed|was the same type as|%error|%error}2 %1",
23472347
(Type, Type, unsigned))
23482348

23492349
NOTE(protocol_witness_optionality_conflict,none,

include/swift/AST/Requirement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +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:
6162
case RequirementKind::Conformance:
6263
case RequirementKind::Superclass:
6364
case RequirementKind::SameType: {

include/swift/AST/RequirementBase.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ 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
40+
/// parameters.
41+
SameCount
3942

40-
// Note: there is code that packs this enum in a 2-bit bitfield. Audit users
43+
// Note: there is code that packs this enum in a 3-bit bitfield. Audit users
4144
// when adding enumerators.
4245
};
4346

@@ -102,6 +105,7 @@ class RequirementBase {
102105
hash_value(requirement.FirstTypeAndKind.getOpaqueValue());
103106
llvm::hash_code second;
104107
switch (requirement.getKind()) {
108+
case RequirementKind::SameCount:
105109
case RequirementKind::Conformance:
106110
case RequirementKind::Superclass:
107111
case RequirementKind::SameType:
@@ -123,6 +127,7 @@ class RequirementBase {
123127
return false;
124128

125129
switch (lhs.getKind()) {
130+
case RequirementKind::SameCount:
126131
case RequirementKind::Conformance:
127132
case RequirementKind::Superclass:
128133
case RequirementKind::SameType:

include/swift/SIL/SILWitnessVisitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ 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");
62+
6063
// These requirements don't show up in the witness table.
6164
case RequirementKind::Superclass:
6265
case RequirementKind::SameType:

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +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");
638640
case RequirementKind::Conformance:
639641
case RequirementKind::Superclass:
640642
case RequirementKind::Layout:

lib/AST/ASTMangler.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2862,8 +2862,10 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
28622862
Type FirstTy = reqt.getFirstType()->getCanonicalType();
28632863

28642864
switch (reqt.getKind()) {
2865-
case RequirementKind::Layout: {
2866-
} break;
2865+
case RequirementKind::SameCount:
2866+
llvm_unreachable("Same-count requirement not supported here");
2867+
case RequirementKind::Layout:
2868+
break;
28672869
case RequirementKind::Conformance: {
28682870
// If we don't allow marker protocols but we have one here, skip it.
28692871
if (!AllowMarkerProtocols &&
@@ -2876,12 +2878,15 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
28762878
case RequirementKind::SameType: {
28772879
Type SecondTy = reqt.getSecondType();
28782880
appendType(SecondTy->getCanonicalType(), sig);
2879-
} break;
2881+
break;
2882+
}
28802883
}
28812884

28822885
if (auto *DT = FirstTy->getAs<DependentMemberType>()) {
28832886
if (tryMangleTypeSubstitution(DT, sig)) {
28842887
switch (reqt.getKind()) {
2888+
case RequirementKind::SameCount:
2889+
llvm_unreachable("Same-count requirement not supported here");
28852890
case RequirementKind::Conformance:
28862891
return appendOperator("RQ");
28872892
case RequirementKind::Layout:
@@ -2901,6 +2906,8 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
29012906
addTypeSubstitution(DT, sig);
29022907
assert(gpBase);
29032908
switch (reqt.getKind()) {
2909+
case RequirementKind::SameCount:
2910+
llvm_unreachable("Same-count requirement not supported here");
29042911
case RequirementKind::Conformance:
29052912
return appendOpWithGenericParamIndex(isAssocTypeAtDepth ? "RP" : "Rp",
29062913
gpBase, lhsBaseIsProtocolSelf);
@@ -2920,6 +2927,8 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
29202927
}
29212928
GenericTypeParamType *gpBase = FirstTy->castTo<GenericTypeParamType>();
29222929
switch (reqt.getKind()) {
2930+
case RequirementKind::SameCount:
2931+
llvm_unreachable("Same-count requirement not supported here");
29232932
case RequirementKind::Conformance:
29242933
return appendOpWithGenericParamIndex("R", gpBase);
29252934
case RequirementKind::Layout:
@@ -3479,6 +3488,8 @@ void ASTMangler::appendConcreteProtocolConformance(
34793488
bool firstRequirement = true;
34803489
for (const auto &conditionalReq : conformance->getConditionalRequirements()) {
34813490
switch (conditionalReq.getKind()) {
3491+
case RequirementKind::SameCount:
3492+
llvm_unreachable("Same-count requirement not supported here");
34823493
case RequirementKind::Layout:
34833494
case RequirementKind::SameType:
34843495
case RequirementKind::Superclass:
@@ -3628,6 +3639,8 @@ void ASTMangler::appendConstrainedExistential(Type base, GenericSignature sig,
36283639
bool firstRequirement = true;
36293640
for (const auto &reqt : requirements) {
36303641
switch (reqt.getKind()) {
3642+
case RequirementKind::SameCount:
3643+
llvm_unreachable("Same-count requirement not supported here");
36313644
case RequirementKind::Layout:
36323645
case RequirementKind::Conformance:
36333646
case RequirementKind::Superclass:

lib/AST/ASTPrinter.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
240240
if (!isPublicOrUsableFromInline(req.getSecondType()))
241241
return false;
242242
break;
243+
case RequirementKind::SameCount:
243244
case RequirementKind::Layout:
244245
break;
245246
}
@@ -1414,6 +1415,8 @@ bestRequirementPrintLocation(ProtocolDecl *proto, const Requirement &req) {
14141415
bool inWhereClause;
14151416

14161417
switch (req.getKind()) {
1418+
case RequirementKind::SameCount:
1419+
llvm_unreachable("Same-count requirements not supported here");
14171420
case RequirementKind::Layout:
14181421
case RequirementKind::Conformance:
14191422
case RequirementKind::Superclass: {
@@ -1505,7 +1508,8 @@ static unsigned getDepthOfRequirement(const Requirement &req) {
15051508
return getDepthOfType(req.getFirstType());
15061509

15071510
case RequirementKind::Superclass:
1508-
case RequirementKind::SameType: {
1511+
case RequirementKind::SameType:
1512+
case RequirementKind::SameCount: {
15091513
// Return the max valid depth of firstType and secondType.
15101514
unsigned firstDepth = getDepthOfType(req.getFirstType());
15111515
unsigned secondDepth = getDepthOfType(req.getSecondType());
@@ -1752,6 +1756,9 @@ void PrintAST::printSingleDepthOfGenericSignature(
17521756
// We only print the second part of a requirement in the "inherited"
17531757
// clause.
17541758
switch (req.getKind()) {
1759+
case RequirementKind::SameCount:
1760+
llvm_unreachable("Same-count requirement not supported here");
1761+
17551762
case RequirementKind::Layout:
17561763
req.getLayoutConstraint()->print(Printer, Options);
17571764
break;
@@ -1780,6 +1787,11 @@ void PrintAST::printSingleDepthOfGenericSignature(
17801787
void PrintAST::printRequirement(const Requirement &req) {
17811788
printTransformedType(req.getFirstType());
17821789
switch (req.getKind()) {
1790+
case RequirementKind::SameCount:
1791+
Printer << ".count == ";
1792+
printTransformedType(req.getSecondType());
1793+
Printer << ".count";
1794+
return;
17831795
case RequirementKind::Layout:
17841796
Printer << " : ";
17851797
req.getLayoutConstraint()->print(Printer, Options);
@@ -6659,6 +6671,9 @@ void Requirement::dump() const {
66596671
}
66606672
void Requirement::dump(raw_ostream &out) const {
66616673
switch (getKind()) {
6674+
case RequirementKind::SameCount:
6675+
out << "same_count: ";
6676+
break;
66626677
case RequirementKind::Conformance:
66636678
out << "conforms_to: ";
66646679
break;

lib/AST/Decl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4044,6 +4044,9 @@ findGenericParameterReferences(CanGenericSignature genericSig,
40444044
auto opaqueSig = opaque->getDecl()->getOpaqueInterfaceGenericSignature();
40454045
for (const auto &req : opaqueSig.getRequirements()) {
40464046
switch (req.getKind()) {
4047+
case RequirementKind::SameCount:
4048+
llvm_unreachable("Same-count requirement not supported here");
4049+
40474050
case RequirementKind::Conformance:
40484051
case RequirementKind::Layout:
40494052
continue;

lib/AST/GenericSignature.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ bool Requirement::isCanonical() const {
599599
return false;
600600

601601
switch (getKind()) {
602+
case RequirementKind::SameCount:
602603
case RequirementKind::Conformance:
603604
case RequirementKind::SameType:
604605
case RequirementKind::Superclass:
@@ -618,6 +619,7 @@ Requirement Requirement::getCanonical() const {
618619
Type firstType = getFirstType()->getCanonicalType();
619620

620621
switch (getKind()) {
622+
case RequirementKind::SameCount:
621623
case RequirementKind::Conformance:
622624
case RequirementKind::SameType:
623625
case RequirementKind::Superclass: {
@@ -640,6 +642,9 @@ bool
640642
Requirement::isSatisfied(ArrayRef<Requirement> &conditionalRequirements,
641643
bool allowMissing) const {
642644
switch (getKind()) {
645+
case RequirementKind::SameCount:
646+
llvm_unreachable("Same-count requirements not supported here");
647+
643648
case RequirementKind::Conformance: {
644649
auto *proto = getProtocolDecl();
645650
auto *module = proto->getParentModule();
@@ -678,6 +683,9 @@ Requirement::isSatisfied(ArrayRef<Requirement> &conditionalRequirements,
678683

679684
bool Requirement::canBeSatisfied() const {
680685
switch (getKind()) {
686+
case RequirementKind::SameCount:
687+
llvm_unreachable("Same-count requirements not supported here");
688+
681689
case RequirementKind::Conformance:
682690
return getFirstType()->is<ArchetypeType>();
683691

@@ -705,6 +713,7 @@ bool Requirement::canBeSatisfied() const {
705713
/// Determine the canonical ordering of requirements.
706714
static unsigned getRequirementKindOrder(RequirementKind kind) {
707715
switch (kind) {
716+
case RequirementKind::SameCount: return 4;
708717
case RequirementKind::Conformance: return 2;
709718
case RequirementKind::Superclass: return 0;
710719
case RequirementKind::SameType: return 3;
@@ -852,6 +861,36 @@ void GenericSignature::verify(ArrayRef<Requirement> reqts) const {
852861

853862
// Check canonicalization of requirement itself.
854863
switch (reqt.getKind()) {
864+
case RequirementKind::SameCount:
865+
if (!reqt.getFirstType()->is<GenericTypeParamType>()) {
866+
llvm::errs() << "Left hand side is not a generic parameter: ";
867+
reqt.dump(llvm::errs());
868+
llvm::errs() << "\n";
869+
abort();
870+
}
871+
872+
if (!reqt.getFirstType()->castTo<GenericTypeParamType>()->isTypeSequence()) {
873+
llvm::errs() << "Left hand side is not a type sequence: ";
874+
reqt.dump(llvm::errs());
875+
llvm::errs() << "\n";
876+
abort();
877+
}
878+
879+
if (!reqt.getSecondType()->is<GenericTypeParamType>()) {
880+
llvm::errs() << "Right hand side is not a generic parameter: ";
881+
reqt.dump(llvm::errs());
882+
llvm::errs() << "\n";
883+
abort();
884+
}
885+
886+
if (!reqt.getSecondType()->castTo<GenericTypeParamType>()->isTypeSequence()) {
887+
llvm::errs() << "Right hand side is not a type sequence: ";
888+
reqt.dump(llvm::errs());
889+
llvm::errs() << "\n";
890+
abort();
891+
}
892+
893+
break;
855894
case RequirementKind::Superclass:
856895
if (!canSig->isReducedType(reqt.getSecondType())) {
857896
llvm::errs() << "Right-hand side is not reduced: ";
@@ -1020,6 +1059,10 @@ static Requirement stripBoundDependentMemberTypes(Requirement req) {
10201059
auto subjectType = stripBoundDependentMemberTypes(req.getFirstType());
10211060

10221061
switch (req.getKind()) {
1062+
case RequirementKind::SameCount:
1063+
// Same-count requirements do not involve dependent member types.
1064+
return req;
1065+
10231066
case RequirementKind::Conformance:
10241067
return Requirement(RequirementKind::Conformance, subjectType,
10251068
req.getSecondType());

lib/AST/RequirementMachine/ConcreteContraction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ ConcreteContraction::substRequirement(const Requirement &req) const {
371371
auto firstType = req.getFirstType();
372372

373373
switch (req.getKind()) {
374+
case RequirementKind::SameCount:
375+
llvm_unreachable("Same-count requirement not supported here");
376+
374377
case RequirementKind::Superclass:
375378
case RequirementKind::SameType: {
376379
auto position = (req.getKind() == RequirementKind::Superclass
@@ -550,6 +553,9 @@ bool ConcreteContraction::performConcreteContraction(
550553

551554
auto kind = req.req.getKind();
552555
switch (kind) {
556+
case RequirementKind::SameCount:
557+
llvm_unreachable("Same-count requirement not supported here");
558+
553559
case RequirementKind::SameType: {
554560
auto constraintType = req.req.getSecondType();
555561

0 commit comments

Comments
 (0)