@@ -236,18 +236,42 @@ void RequirementBuilder::addRequirementRules(ArrayRef<unsigned> rules) {
236236 llvm_unreachable (" Invalid symbol kind" );
237237 }
238238
239+ MutableTerm constraintTerm = MutableTerm (rule.getLHS ());
240+ MutableTerm subjectTerm = MutableTerm (rule.getRHS ());
241+
242+ RewriteContext &ctx = this ->System .getRewriteContext ();
243+
244+ // Drop the [element] symbol from lhs to determine if we need to swap the
245+ // sides.
246+ if (constraintTerm[0 ].getKind () == Symbol::Kind::PackElement) {
247+ constraintTerm =
248+ MutableTerm (constraintTerm.begin () + 1 , constraintTerm.end ());
249+
250+ // Make sure that the shorter term is ordered first.
251+ if (constraintTerm.compare (subjectTerm, ctx) == -1 ) {
252+ MutableTerm tempTerm = subjectTerm;
253+ subjectTerm = constraintTerm;
254+ constraintTerm = tempTerm;
255+ }
256+ }
257+
239258 ASSERT (rule.getLHS ().back ().getKind () != Symbol::Kind::Protocol);
240259
241- MutableTerm constraintTerm (rule.getLHS ());
242260 if (constraintTerm.back ().getKind () == Symbol::Kind::Shape) {
243261 ASSERT (rule.getRHS ().back ().getKind () == Symbol::Kind::Shape);
244262 // Strip off the shape symbol from the constraint term.
245263 constraintTerm = MutableTerm (constraintTerm.begin (),
246264 constraintTerm.end () - 1 );
247265 }
248266
267+ if (constraintTerm.front ().getKind () == Symbol::Kind::PackElement) {
268+ // Strip off the element symbol from the constraint term.
269+ constraintTerm = MutableTerm (constraintTerm.begin () + 1 ,
270+ constraintTerm.end ());
271+ }
272+
249273 auto constraintType = Map.getTypeForTerm (constraintTerm, GenericParams);
250- Components[rule. getRHS ( )].Members .push_back (constraintType);
274+ Components[Term::get (subjectTerm, ctx )].Members .push_back (constraintType);
251275 };
252276
253277 if (Debug) {
@@ -314,6 +338,10 @@ void RequirementBuilder::processConnectedComponents() {
314338 subjectTerm = MutableTerm (subjectTerm.begin (), subjectTerm.end () - 1 );
315339 } else {
316340 kind = RequirementKind::SameType;
341+ if (subjectTerm.front ().getKind () == Symbol::Kind::PackElement) {
342+ // Strip off the element symbol from the subject term.
343+ subjectTerm = MutableTerm (subjectTerm.begin () + 1 , subjectTerm.end ());
344+ }
317345 }
318346
319347 auto subjectType = Map.getTypeForTerm (subjectTerm, GenericParams);
0 commit comments