Skip to content

Commit 9612283

Browse files
committed
RequirementMachine: getRuleForRequirement() supports relative terms
1 parent c46c777 commit 9612283

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -844,10 +844,17 @@ void RuleBuilder::addAssociatedType(const AssociatedTypeDecl *type,
844844
std::pair<MutableTerm, MutableTerm>
845845
swift::rewriting::getRuleForRequirement(const Requirement &req,
846846
const ProtocolDecl *proto,
847+
Optional<ArrayRef<Term>> substitutions,
847848
RewriteContext &ctx) {
849+
assert(!substitutions.hasValue() || proto == nullptr && "Can't have both");
850+
848851
// Compute the left hand side.
849852
auto subjectType = CanType(req.getFirstType());
850-
auto subjectTerm = ctx.getMutableTermForType(subjectType, proto);
853+
auto subjectTerm = (substitutions
854+
? ctx.getRelativeTermForType(
855+
subjectType, *substitutions)
856+
: ctx.getMutableTermForType(
857+
subjectType, proto));
851858

852859
// Compute the right hand side.
853860
MutableTerm constraintTerm;
@@ -873,11 +880,13 @@ swift::rewriting::getRuleForRequirement(const Requirement &req,
873880
auto otherType = CanType(req.getSecondType());
874881

875882
// Build the symbol [superclass: C<X, Y>].
876-
SmallVector<Term, 1> substitutions;
877-
otherType = ctx.getSubstitutionSchemaFromType(otherType, proto,
878-
substitutions);
879-
auto superclassSymbol = Symbol::forSuperclass(otherType, substitutions,
880-
ctx);
883+
SmallVector<Term, 1> result;
884+
otherType = (substitutions
885+
? ctx.getRelativeSubstitutionSchemaFromType(
886+
otherType, *substitutions, result)
887+
: ctx.getSubstitutionSchemaFromType(
888+
otherType, proto, result));
889+
auto superclassSymbol = Symbol::forSuperclass(otherType, result, ctx);
881890

882891
// Build the term T.[superclass: C<X, Y>].
883892
constraintTerm = subjectTerm;
@@ -890,8 +899,7 @@ swift::rewriting::getRuleForRequirement(const Requirement &req,
890899
//
891900
// T.[layout: L] == T
892901
constraintTerm = subjectTerm;
893-
constraintTerm.add(Symbol::forLayout(req.getLayoutConstraint(),
894-
ctx));
902+
constraintTerm.add(Symbol::forLayout(req.getLayoutConstraint(), ctx));
895903
break;
896904
}
897905

@@ -903,17 +911,23 @@ swift::rewriting::getRuleForRequirement(const Requirement &req,
903911
// rewrite rule
904912
//
905913
// T.[concrete: C<X, Y>] => T
906-
SmallVector<Term, 1> substitutions;
907-
otherType = ctx.getSubstitutionSchemaFromType(otherType, proto,
908-
substitutions);
914+
SmallVector<Term, 1> result;
915+
otherType = (substitutions
916+
? ctx.getRelativeSubstitutionSchemaFromType(
917+
otherType, *substitutions, result)
918+
: ctx.getSubstitutionSchemaFromType(
919+
otherType, proto, result));
909920

910921
constraintTerm = subjectTerm;
911-
constraintTerm.add(Symbol::forConcreteType(otherType, substitutions,
912-
ctx));
922+
constraintTerm.add(Symbol::forConcreteType(otherType, result, ctx));
913923
break;
914924
}
915925

916-
constraintTerm = ctx.getMutableTermForType(otherType, proto);
926+
constraintTerm = (substitutions
927+
? ctx.getRelativeTermForType(
928+
otherType, *substitutions)
929+
: ctx.getMutableTermForType(
930+
otherType, proto));
917931
break;
918932
}
919933
}
@@ -929,7 +943,9 @@ void RuleBuilder::addRequirement(const Requirement &req,
929943
llvm::dbgs() << "\n";
930944
}
931945

932-
RequirementRules.push_back(getRuleForRequirement(req, proto, Context));
946+
RequirementRules.push_back(
947+
getRuleForRequirement(req, proto, /*substitutions=*/None,
948+
Context));
933949
}
934950

935951
void RuleBuilder::addRequirement(const StructuralRequirement &req,

lib/AST/RequirementMachine/RequirementLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void realizeInheritedRequirements(TypeDecl *decl, Type type,
5555
std::pair<MutableTerm, MutableTerm>
5656
getRuleForRequirement(const Requirement &req,
5757
const ProtocolDecl *proto,
58+
Optional<ArrayRef<Term>> substitutions,
5859
RewriteContext &ctx);
5960

6061
/// A utility class for bulding rewrite rules from the top-level requirements

0 commit comments

Comments
 (0)