Skip to content

Commit b9e2680

Browse files
committed
[Constraint solver] Migrate ConstraintGraph::gatherConstraints() off adjacencies list.
Use the adjacencies implied by the constraints of a node rather than looking at the "adjacency" list, and try to simplify this code path a bit. The diagnostic change is because we are now uniformly recording the members of the equivalence class. (cherry picked from commit 54bdd7b)
1 parent dbb5343 commit b9e2680

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

lib/Sema/ConstraintGraph.cpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -486,51 +486,43 @@ void ConstraintGraph::gatherConstraints(
486486
auto &reprNode = (*this)[CS.getRepresentative(typeVar)];
487487
auto equivClass = reprNode.getEquivalenceClass();
488488
llvm::SmallPtrSet<TypeVariableType *, 4> typeVars;
489-
for (auto typeVar : equivClass) {
490-
if (!typeVars.insert(typeVar).second)
491-
continue;
492-
493-
for (auto constraint : (*this)[typeVar].getConstraints()) {
494-
if (acceptConstraint(constraint))
495-
constraints.insert(constraint);
496-
}
497-
498-
auto &node = (*this)[typeVar];
499489

500-
// Retrieve the constraints from adjacent bindings.
501-
for (auto adjTypeVar : node.getAdjacencies()) {
502-
switch (kind) {
503-
case GatheringKind::EquivalenceClass:
504-
if (!node.getAdjacency(adjTypeVar).FixedBinding)
505-
continue;
506-
break;
507-
508-
case GatheringKind::AllMentions:
509-
break;
510-
}
511490

512-
ArrayRef<TypeVariableType *> adjTypeVarsToVisit;
513-
switch (kind) {
514-
case GatheringKind::EquivalenceClass:
515-
adjTypeVarsToVisit = adjTypeVar;
516-
break;
491+
/// Add constraints for the given adjacent type variable.
492+
auto addAdjacentConstraints = [&](TypeVariableType *adjTypeVar) {
493+
auto adjTypeVarsToVisit =
494+
(*this)[CS.getRepresentative(adjTypeVar)].getEquivalenceClass();
495+
for (auto adjTypeVarEquiv : adjTypeVarsToVisit) {
496+
if (!typeVars.insert(adjTypeVarEquiv).second)
497+
continue;
517498

518-
case GatheringKind::AllMentions:
519-
adjTypeVarsToVisit
520-
= (*this)[CS.getRepresentative(adjTypeVar)].getEquivalenceClass();
521-
break;
499+
for (auto constraint : (*this)[adjTypeVarEquiv].getConstraints()) {
500+
if (acceptConstraint(constraint))
501+
constraints.insert(constraint);
522502
}
503+
}
504+
};
523505

524-
for (auto adjTypeVarEquiv : adjTypeVarsToVisit) {
525-
if (!typeVars.insert(adjTypeVarEquiv).second)
526-
continue;
506+
for (auto typeVar : equivClass) {
507+
auto &node = (*this)[typeVar];
508+
for (auto constraint : node.getConstraints()) {
509+
if (acceptConstraint(constraint))
510+
constraints.insert(constraint);
527511

528-
for (auto constraint : (*this)[adjTypeVarEquiv].getConstraints()) {
529-
if (acceptConstraint(constraint))
530-
constraints.insert(constraint);
512+
// If we want all mentions, visit type variables within each of our
513+
// constraints.
514+
if (kind == GatheringKind::AllMentions) {
515+
for (auto adjTypeVar : constraint->getTypeVariables()) {
516+
addAdjacentConstraints(adjTypeVar);
531517
}
532518
}
533519
}
520+
521+
// For any type variable mentioned in a fixed binding, add adjacent
522+
// constraints.
523+
for (auto adjTypeVar : node.getFixedAdjacencies()) {
524+
addAdjacentConstraints(adjTypeVar);
525+
}
534526
}
535527
}
536528

test/Constraints/generics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ let arr = [BottleLayout]()
646646
let layout = BottleLayout(count:1)
647647
let ix = arr.firstIndex(of:layout) // expected-error {{argument type 'BottleLayout' does not conform to expected type 'Equatable'}}
648648

649-
let _: () -> UInt8 = { .init("a" as Unicode.Scalar) } // expected-error {{initializer 'init(_:)' requires that 'Unicode.Scalar' conform to 'BinaryInteger'}}
649+
let _: () -> UInt8 = { .init("a" as Unicode.Scalar) } // expected-error {{missing argument label 'ascii:' in call}}
650650

651651
// https://bugs.swift.org/browse/SR-9068
652652
func compare<C: Collection, Key: Hashable, Value: Equatable>(c: C)

test/Constraints/patterns.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,7 @@ switch staticMembers {
291291
case .init(0): break
292292
case .init(_): break // expected-error{{'_' can only appear in a pattern}}
293293
case .init(let x): break // expected-error{{cannot appear in an expression}}
294-
case .init(opt: 0): break // expected-error{{value of optional type 'StaticMembers?' must be unwrapped to a value of type 'StaticMembers'}}
295-
// expected-note@-1{{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
296-
// expected-note@-2{{coalesce using '??' to provide a default when the optional value contains 'nil'}}
294+
case .init(opt: 0): break // expected-error{{pattern cannot match values of type 'StaticMembers'}}
297295

298296
case .prop: break
299297
// TODO: repeated error message

0 commit comments

Comments
 (0)