Skip to content

Commit ffbe53e

Browse files
committed
[NFC] Eliminate const_casts in constraint system dumpers
1 parent 99faa03 commit ffbe53e

6 files changed

+25
-24
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ bool ConstraintSystem::PotentialBindings::isViable(
207207
}
208208

209209
static bool hasNilLiteralConstraint(TypeVariableType *typeVar,
210-
ConstraintSystem &CS) {
210+
const ConstraintSystem &CS) {
211211
// Look for a literal-conformance constraint on the type variable.
212212
auto constraints =
213213
CS.getConstraintGraph().gatherConstraints(
@@ -230,7 +230,7 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
230230
PotentialBindings &result, Constraint *constraint,
231231
bool &hasDependentMemberRelationalConstraints,
232232
bool &hasNonDependentMemberRelationalConstraints,
233-
bool &addOptionalSupertypeBindings) {
233+
bool &addOptionalSupertypeBindings) const {
234234
assert(constraint->getClassification() ==
235235
ConstraintClassification::Relational &&
236236
"only relational constraints handled here");
@@ -378,7 +378,7 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
378378
/// representative type variable, along with flags indicating whether
379379
/// those types should be opened.
380380
ConstraintSystem::PotentialBindings
381-
ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
381+
ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
382382
assert(typeVar->getImpl().getRepresentative(nullptr) == typeVar &&
383383
"not a representative");
384384
assert(!typeVar->getImpl().getFixedType(nullptr) && "has a fixed type");
@@ -796,7 +796,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
796796
///
797797
/// \returns the type to bind to, if the binding is okay.
798798
Optional<Type> ConstraintSystem::checkTypeOfBinding(TypeVariableType *typeVar,
799-
Type type) {
799+
Type type) const {
800800
// Simplify the type.
801801
type = simplifyType(type);
802802

lib/Sema/ConstraintGraph.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ void ConstraintGraph::incrementConstraintsPerContractionCounter() {
13191319

13201320
#pragma mark Debugging output
13211321

1322-
void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent) {
1322+
void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent) const {
13231323
out.indent(indent);
13241324
TypeVar->print(out);
13251325
out << ":\n";
@@ -1351,7 +1351,7 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent) {
13511351
out << ' ';
13521352
adj->print(out);
13531353

1354-
auto &info = AdjacencyInfo[adj];
1354+
const auto info = AdjacencyInfo.lookup(adj);
13551355
auto degree = info.NumConstraints;
13561356
if (degree > 1) {
13571357
out << " (" << degree << ")";
@@ -1397,7 +1397,7 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent) {
13971397
void ConstraintGraphNode::dump() const {
13981398
llvm::SaveAndRestore<bool>
13991399
debug(TypeVar->getASTContext().LangOpts.DebugConstraintSolver, true);
1400-
const_cast<ConstraintGraphNode*>(this)->print(llvm::dbgs(), 0);
1400+
print(llvm::dbgs(), 0);
14011401
}
14021402

14031403
void ConstraintGraph::print(ArrayRef<TypeVariableType *> typeVars,

lib/Sema/ConstraintGraph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class ConstraintGraphNode {
160160
mutable SmallVector<TypeVariableType *, 2> EquivalenceClass;
161161

162162
/// Print this graph node.
163-
void print(llvm::raw_ostream &out, unsigned indent);
163+
void print(llvm::raw_ostream &out, unsigned indent) const;
164164

165165
SWIFT_DEBUG_DUMP;
166166

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ bool ConstraintSystem::isAnyHashableType(Type type) {
746746

747747
Type ConstraintSystem::getFixedTypeRecursive(Type type,
748748
TypeMatchOptions &flags,
749-
bool wantRValue) {
749+
bool wantRValue) const {
750750

751751
if (wantRValue)
752752
type = type->getRValueType();
@@ -2259,7 +2259,8 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
22592259
}
22602260

22612261
template <typename Fn>
2262-
Type simplifyTypeImpl(ConstraintSystem &cs, Type type, Fn getFixedTypeFn) {
2262+
Type simplifyTypeImpl(const ConstraintSystem &cs, Type type,
2263+
Fn getFixedTypeFn) {
22632264
return type.transform([&](Type type) -> Type {
22642265
if (auto tvt = dyn_cast<TypeVariableType>(type.getPointer()))
22652266
return getFixedTypeFn(tvt);
@@ -2305,7 +2306,7 @@ Type simplifyTypeImpl(ConstraintSystem &cs, Type type, Fn getFixedTypeFn) {
23052306
});
23062307
}
23072308

2308-
Type ConstraintSystem::simplifyType(Type type) {
2309+
Type ConstraintSystem::simplifyType(Type type) const {
23092310
if (!type->hasTypeVariable())
23102311
return type;
23112312

lib/Sema/ConstraintSystem.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,7 +2360,7 @@ class ConstraintSystem {
23602360

23612361
/// Retrieve the representative of the equivalence class containing
23622362
/// this type variable.
2363-
TypeVariableType *getRepresentative(TypeVariableType *typeVar) {
2363+
TypeVariableType *getRepresentative(TypeVariableType *typeVar) const {
23642364
return typeVar->getImpl().getRepresentative(getSavedBindings());
23652365
}
23662366

@@ -2467,7 +2467,7 @@ class ConstraintSystem {
24672467
///
24682468
/// \param wantRValue Whether this routine should look through
24692469
/// lvalues at each step.
2470-
Type getFixedTypeRecursive(Type type, bool wantRValue) {
2470+
Type getFixedTypeRecursive(Type type, bool wantRValue) const {
24712471
TypeMatchOptions flags = None;
24722472
return getFixedTypeRecursive(type, flags, wantRValue);
24732473
}
@@ -2485,7 +2485,7 @@ class ConstraintSystem {
24852485
/// \param wantRValue Whether this routine should look through
24862486
/// lvalues at each step.
24872487
Type getFixedTypeRecursive(Type type, TypeMatchOptions &flags,
2488-
bool wantRValue);
2488+
bool wantRValue) const;
24892489

24902490
/// Determine whether the given type variable occurs within the given type.
24912491
///
@@ -3027,7 +3027,7 @@ class ConstraintSystem {
30273027
///
30283028
/// The resulting types can be compared canonically, so long as additional
30293029
/// type equivalence requirements aren't introduced between comparisons.
3030-
Type simplifyType(Type type);
3030+
Type simplifyType(Type type) const;
30313031

30323032
/// Simplify a type, by replacing type variables with either their
30333033
/// fixed types (if available) or their representatives.
@@ -3478,15 +3478,15 @@ class ConstraintSystem {
34783478
}
34793479
};
34803480

3481-
Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type);
3481+
Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type) const;
34823482
Optional<PotentialBindings> determineBestBindings();
34833483
Optional<ConstraintSystem::PotentialBinding>
34843484
getPotentialBindingForRelationalConstraint(
34853485
PotentialBindings &result, Constraint *constraint,
34863486
bool &hasDependentMemberRelationalConstraints,
34873487
bool &hasNonDependentMemberRelationalConstraints,
3488-
bool &addOptionalSupertypeBindings);
3489-
PotentialBindings getPotentialBindings(TypeVariableType *typeVar);
3488+
bool &addOptionalSupertypeBindings) const;
3489+
PotentialBindings getPotentialBindings(TypeVariableType *typeVar) const;
34903490

34913491
private:
34923492
/// Add a constraint to the constraint system.
@@ -3836,8 +3836,8 @@ class ConstraintSystem {
38363836
SWIFT_DEBUG_DUMP;
38373837
SWIFT_DEBUG_DUMPER(dump(Expr *));
38383838

3839-
void print(raw_ostream &out);
3840-
void print(raw_ostream &out, Expr *);
3839+
void print(raw_ostream &out) const;
3840+
void print(raw_ostream &out, Expr *) const;
38413841
};
38423842

38433843
/// Compute the shuffle required to map from a given tuple type to

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,14 +3618,14 @@ void Solution::dump(raw_ostream &out) const {
36183618
}
36193619

36203620
void ConstraintSystem::dump() const {
3621-
const_cast<ConstraintSystem*>(this)->print(llvm::errs());
3621+
print(llvm::errs());
36223622
}
36233623

36243624
void ConstraintSystem::dump(Expr *E) const {
3625-
const_cast<ConstraintSystem*>(this)->print(llvm::errs(), E);
3625+
print(llvm::errs(), E);
36263626
}
36273627

3628-
void ConstraintSystem::print(raw_ostream &out, Expr *E) {
3628+
void ConstraintSystem::print(raw_ostream &out, Expr *E) const {
36293629
auto getTypeOfExpr = [&](const Expr *E) -> Type {
36303630
if (hasType(E))
36313631
return getType(E);
@@ -3646,7 +3646,7 @@ void ConstraintSystem::print(raw_ostream &out, Expr *E) {
36463646
E->dump(out, getTypeOfExpr, getTypeOfTypeLoc, getTypeOfKeyPathComponent);
36473647
}
36483648

3649-
void ConstraintSystem::print(raw_ostream &out) {
3649+
void ConstraintSystem::print(raw_ostream &out) const {
36503650
// Print all type variables as $T0 instead of _ here.
36513651
llvm::SaveAndRestore<bool> X(getASTContext().LangOpts.DebugConstraintSolver,
36523652
true);

0 commit comments

Comments
 (0)