Skip to content

Commit 427c647

Browse files
committed
[ConstraintSystem] ConstraintSystem cannot be const when inferring new bindings
It's possible that bindings would need to mutate constraint system e.g. by allocating new locators.
1 parent 8920da2 commit 427c647

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace swift;
2222
using namespace constraints;
2323

2424
void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
25-
const ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes,
25+
ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes,
2626
const llvm::SmallDenseMap<TypeVariableType *,
2727
ConstraintSystem::PotentialBindings>
2828
&inferredBindings) {
@@ -144,7 +144,7 @@ isUnviableDefaultType(Type defaultType,
144144
}
145145

146146
void ConstraintSystem::PotentialBindings::inferDefaultTypes(
147-
const ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes) {
147+
ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes) {
148148
auto isDirectRequirement = [&](Constraint *constraint) -> bool {
149149
if (auto *typeVar = constraint->getFirstType()->getAs<TypeVariableType>()) {
150150
auto *repr = cs.getRepresentative(typeVar);
@@ -300,7 +300,7 @@ void ConstraintSystem::PotentialBindings::inferDefaultTypes(
300300
}
301301

302302
void ConstraintSystem::PotentialBindings::finalize(
303-
const ConstraintSystem &cs,
303+
ConstraintSystem &cs,
304304
const llvm::SmallDenseMap<TypeVariableType *,
305305
ConstraintSystem::PotentialBindings>
306306
&inferredBindings) {
@@ -620,8 +620,7 @@ bool ConstraintSystem::PotentialBindings::favoredOverDisjunction(
620620
}
621621

622622
ConstraintSystem::PotentialBindings
623-
ConstraintSystem::inferBindingsFor(TypeVariableType *typeVar,
624-
bool finalize) const {
623+
ConstraintSystem::inferBindingsFor(TypeVariableType *typeVar, bool finalize) {
625624
assert(typeVar->getImpl().getRepresentative(nullptr) == typeVar &&
626625
"not a representative");
627626
assert(!typeVar->getImpl().getFixedType(nullptr) && "has a fixed type");
@@ -829,7 +828,7 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
829828
/// representative type variable, along with flags indicating whether
830829
/// those types should be opened.
831830
bool ConstraintSystem::PotentialBindings::infer(
832-
const ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &exactTypes,
831+
ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &exactTypes,
833832
Constraint *constraint) {
834833
switch (constraint->getKind()) {
835834
case ConstraintKind::Bind:

lib/Sema/ConstraintSystem.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4775,25 +4775,25 @@ class ConstraintSystem {
47754775
/// \param inferredBindings The set of all bindings inferred for type
47764776
/// variables in the workset.
47774777
void inferTransitiveBindings(
4778-
const ConstraintSystem &cs,
4778+
ConstraintSystem &cs,
47794779
llvm::SmallPtrSetImpl<CanType> &existingTypes,
47804780
const llvm::SmallDenseMap<TypeVariableType *,
47814781
ConstraintSystem::PotentialBindings>
47824782
&inferredBindings);
47834783

47844784
/// Infer bindings based on any protocol conformances that have default
47854785
/// types.
4786-
void inferDefaultTypes(const ConstraintSystem &cs,
4786+
void inferDefaultTypes(ConstraintSystem &cs,
47874787
llvm::SmallPtrSetImpl<CanType> &existingTypes);
47884788

47894789
public:
4790-
bool infer(const ConstraintSystem &cs,
4790+
bool infer(ConstraintSystem &cs,
47914791
llvm::SmallPtrSetImpl<CanType> &exactTypes,
47924792
Constraint *constraint);
47934793

47944794
/// Finalize binding computation for this type variable by
47954795
/// inferring bindings from context e.g. transitive bindings.
4796-
void finalize(const ConstraintSystem &cs,
4796+
void finalize(ConstraintSystem &cs,
47974797
const llvm::SmallDenseMap<TypeVariableType *,
47984798
ConstraintSystem::PotentialBindings>
47994799
&inferredBindings);
@@ -4862,7 +4862,7 @@ class ConstraintSystem {
48624862
/// Infer bindings for the given type variable based on current
48634863
/// state of the constraint system.
48644864
PotentialBindings inferBindingsFor(TypeVariableType *typeVar,
4865-
bool finalize = true) const;
4865+
bool finalize = true);
48664866

48674867
private:
48684868
Optional<ConstraintSystem::PotentialBinding>

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
29912991
out << " as ";
29922992
Type(fixed).print(out, PO);
29932993
} else {
2994-
inferBindingsFor(tv).dump(out, 1);
2994+
const_cast<ConstraintSystem *>(this)->inferBindingsFor(tv).dump(out, 1);
29952995
}
29962996
} else {
29972997
out << " equivalent to ";

0 commit comments

Comments
 (0)