Skip to content

Commit 466c770

Browse files
committed
Sema: Sink PotentialBindings::Protocols down into BindingSet
1 parent baa1ee9 commit 466c770

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,6 @@ struct PotentialBindings {
224224
/// The set of potential bindings.
225225
llvm::SmallVector<PotentialBinding, 4> Bindings;
226226

227-
/// The set of protocol requirements placed on this type variable.
228-
llvm::SmallVector<Constraint *, 4> Protocols;
229-
230227
/// The set of unique literal protocol requirements placed on this
231228
/// type variable or inferred transitively through subtype chains.
232229
///
@@ -385,6 +382,10 @@ class BindingSet {
385382

386383
public:
387384
swift::SmallSetVector<PotentialBinding, 4> Bindings;
385+
386+
/// The set of protocol conformance requirements placed on this type variable.
387+
llvm::SmallVector<Constraint *, 4> Protocols;
388+
388389
llvm::SmallMapVector<ProtocolDecl *, LiteralRequirement, 2> Literals;
389390
llvm::SmallDenseMap<CanType, Constraint *, 2> Defaults;
390391

@@ -499,7 +500,7 @@ class BindingSet {
499500
}
500501

501502
ArrayRef<Constraint *> getConformanceRequirements() const {
502-
return Info.Protocols;
503+
return Protocols;
503504
}
504505

505506
unsigned getNumViableLiteralBindings() const;

lib/Sema/CSBindings.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ BindingSet::BindingSet(ConstraintSystem &CS, TypeVariableType *TypeVar,
5353
for (const auto &binding : info.Bindings)
5454
addBinding(binding, /*isTransitive=*/false);
5555

56+
for (auto *constraint : info.Constraints) {
57+
switch (constraint->getKind()) {
58+
case ConstraintKind::ConformsTo: {
59+
if (constraint->getSecondType()->is<ProtocolType>())
60+
Protocols.push_back(constraint);
61+
break;
62+
}
63+
default:
64+
break;
65+
}
66+
}
67+
5668
for (auto *literal : info.Literals)
5769
addLiteralRequirement(literal);
5870

@@ -1956,6 +1968,7 @@ void PotentialBindings::infer(ConstraintSystem &CS,
19561968
case ConstraintKind::PackElementOf:
19571969
case ConstraintKind::SameShape:
19581970
case ConstraintKind::MaterializePackExpansion:
1971+
case ConstraintKind::ConformsTo:
19591972
// Constraints from which we can't do anything.
19601973
break;
19611974

@@ -1996,13 +2009,6 @@ void PotentialBindings::infer(ConstraintSystem &CS,
19962009
DelayedBy.push_back(constraint);
19972010
break;
19982011

1999-
case ConstraintKind::ConformsTo: {
2000-
auto protocolTy = constraint->getSecondType();
2001-
if (protocolTy->is<ProtocolType>())
2002-
Protocols.push_back(constraint);
2003-
break;
2004-
}
2005-
20062012
case ConstraintKind::LiteralConformsTo: {
20072013
// Record constraint where protocol requirement originated
20082014
// this is useful to use for the binding later.
@@ -2084,7 +2090,7 @@ void PotentialBindings::retract(ConstraintSystem &CS,
20842090

20852091
LLVM_DEBUG(
20862092
llvm::dbgs() << Constraints.size() << " " << Bindings.size() << " "
2087-
<< Protocols.size() << " " << Literals.size() << " "
2093+
<< Literals.size() << " "
20882094
<< AdjacentVars.size() << " " << DelayedBy.size() << " "
20892095
<< SubtypeOf.size() << " " << SupertypeOf.size() << " "
20902096
<< EquivalentTo.size() << "\n");
@@ -2096,22 +2102,7 @@ void PotentialBindings::retract(ConstraintSystem &CS,
20962102
}),
20972103
Bindings.end());
20982104

2099-
auto isMatchingConstraint = [&constraint](Constraint *existing) {
2100-
return existing == constraint;
2101-
};
2102-
2103-
auto hasMatchingSource =
2104-
[&constraint](
2105-
const std::pair<TypeVariableType *, Constraint *> &adjacency) {
2106-
return adjacency.second == constraint;
2107-
};
2108-
21092105
switch (constraint->getKind()) {
2110-
case ConstraintKind::ConformsTo:
2111-
Protocols.erase(llvm::remove_if(Protocols, isMatchingConstraint),
2112-
Protocols.end());
2113-
break;
2114-
21152106
case ConstraintKind::LiteralConformsTo:
21162107
Literals.erase(constraint);
21172108
break;
@@ -2137,9 +2128,19 @@ void PotentialBindings::retract(ConstraintSystem &CS,
21372128
AdjacentVars.erase(std::make_pair(adjacentVar, constraint));
21382129
}
21392130

2131+
auto isMatchingConstraint = [&constraint](Constraint *existing) {
2132+
return existing == constraint;
2133+
};
2134+
21402135
DelayedBy.erase(llvm::remove_if(DelayedBy, isMatchingConstraint),
21412136
DelayedBy.end());
21422137

2138+
auto hasMatchingSource =
2139+
[&constraint](
2140+
const std::pair<TypeVariableType *, Constraint *> &adjacency) {
2141+
return adjacency.second == constraint;
2142+
};
2143+
21432144
SubtypeOf.remove_if(hasMatchingSource);
21442145
SupertypeOf.remove_if(hasMatchingSource);
21452146
EquivalentTo.remove_if(hasMatchingSource);
@@ -2149,7 +2150,6 @@ void PotentialBindings::reset() {
21492150
if (CONDITIONAL_ASSERT_enabled()) {
21502151
ASSERT(Constraints.empty());
21512152
ASSERT(Bindings.empty());
2152-
ASSERT(Protocols.empty());
21532153
ASSERT(Literals.empty());
21542154
ASSERT(Defaults.empty());
21552155
ASSERT(DelayedBy.empty());

0 commit comments

Comments
 (0)