Skip to content

Commit 972e755

Browse files
committed
Give ConstraintSystem's outlet to the ASTContext
Make it less tempting to ask for the type checker embedded into ConstraintSystem by using the accessor to the ASTContext.
1 parent da2b063 commit 972e755

13 files changed

+53
-51
lines changed

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static bool buildObjCKeyPathString(KeyPathExpr *E,
272272
static Expr *buildDynamicMemberLookupIndexExpr(StringRef name, SourceLoc loc,
273273
DeclContext *dc,
274274
ConstraintSystem &cs) {
275-
auto &ctx = cs.TC.Context;
275+
auto &ctx = cs.getASTContext();
276276

277277
auto *stringDecl = ctx.getStringDecl();
278278
auto stringType = stringDecl->getDeclaredType();
@@ -4584,7 +4584,7 @@ namespace {
45844584
auto dc = subscript->getInnermostDeclContext();
45854585

45864586
auto indexType = AnyFunctionType::composeInput(
4587-
cs.TC.Context,
4587+
cs.getASTContext(),
45884588
subscript->getInterfaceType()->castTo<AnyFunctionType>()->getParams(),
45894589
/*canonicalVararg=*/false);
45904590

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ ConstraintSystem::determineBestBindings() {
8585
}
8686
}
8787

88-
if (TC.getLangOpts().DebugConstraintSolver) {
88+
if (getASTContext().LangOpts.DebugConstraintSolver) {
8989
auto &log = getASTContext().TypeCheckerDebug->getStream();
9090
bindings.dump(typeVar, log, solverState->depth * 2);
9191
}

lib/Sema/CSDiag.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,8 @@ bool FailureDiagnosis::diagnoseGeneralConversionFailure(Constraint *constraint){
812812
if (toType->isBool() &&
813813
fromType->mayHaveMembers()) {
814814
auto LookupResult = TypeChecker::lookupMember(
815-
CS.DC, fromType, DeclName(CS.TC.Context.getIdentifier("boolValue")));
815+
CS.DC, fromType,
816+
DeclName(CS.getASTContext().getIdentifier("boolValue")));
816817
if (!LookupResult.empty()) {
817818
if (isa<VarDecl>(LookupResult.begin()->getValueDecl())) {
818819
if (anchor->canAppendPostfixExpression())
@@ -4151,7 +4152,8 @@ bool FailureDiagnosis::diagnoseMemberFailures(
41514152

41524153
// Since the lookup was allowing inaccessible members, let's check
41534154
// if it found anything of that sort, which is easy to diagnose.
4154-
bool allUnavailable = !CS.TC.getLangOpts().DisableAvailabilityChecking;
4155+
bool allUnavailable =
4156+
!CS.getASTContext().LangOpts.DisableAvailabilityChecking;
41554157
bool allInaccessible = true;
41564158
for (auto &member : viableCandidatesToReport) {
41574159
if (!member.isDecl()) {

lib/Sema/CSGen.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,8 @@ namespace {
478478
}
479479

480480
if (lti.haveFloatLiteral) {
481-
if (auto floatProto =
482-
CS.TC.Context.getProtocol(
483-
KnownProtocolKind::ExpressibleByFloatLiteral)) {
481+
if (auto floatProto = CS.getASTContext().getProtocol(
482+
KnownProtocolKind::ExpressibleByFloatLiteral)) {
484483
if (auto defaultType = CS.TC.getDefaultType(floatProto, CS.DC)) {
485484
if (!CS.getFavoredType(expr)) {
486485
CS.setFavoredType(expr, defaultType.getPointer());
@@ -491,9 +490,8 @@ namespace {
491490
}
492491

493492
if (lti.haveIntLiteral) {
494-
if (auto intProto =
495-
CS.TC.Context.getProtocol(
496-
KnownProtocolKind::ExpressibleByIntegerLiteral)) {
493+
if (auto intProto = CS.getASTContext().getProtocol(
494+
KnownProtocolKind::ExpressibleByIntegerLiteral)) {
497495
if (auto defaultType = CS.TC.getDefaultType(intProto, CS.DC)) {
498496
if (!CS.getFavoredType(expr)) {
499497
CS.setFavoredType(expr, defaultType.getPointer());
@@ -504,9 +502,8 @@ namespace {
504502
}
505503

506504
if (lti.haveStringLiteral) {
507-
if (auto stringProto =
508-
CS.TC.Context.getProtocol(
509-
KnownProtocolKind::ExpressibleByStringLiteral)) {
505+
if (auto stringProto = CS.getASTContext().getProtocol(
506+
KnownProtocolKind::ExpressibleByStringLiteral)) {
510507
if (auto defaultType = CS.TC.getDefaultType(stringProto, CS.DC)) {
511508
if (!CS.getFavoredType(expr)) {
512509
CS.setFavoredType(expr, defaultType.getPointer());
@@ -1387,7 +1384,8 @@ namespace {
13871384
TypeResolutionOptions options(TypeResolverContext::InExpression);
13881385
options |= TypeResolutionFlags::AllowUnboundGenerics;
13891386
bool hadError = TypeChecker::validateType(
1390-
CS.TC.Context, loc, TypeResolution::forContextual(CS.DC), options);
1387+
CS.getASTContext(), loc, TypeResolution::forContextual(CS.DC),
1388+
options);
13911389
return hadError ? Type() : loc.getType();
13921390
}
13931391

@@ -1706,7 +1704,7 @@ namespace {
17061704

17071705
// Prior to Swift 5, 'try?' always adds an additional layer of optionality,
17081706
// even if the sub-expression was already optional.
1709-
if (CS.getTypeChecker().getLangOpts().isSwiftVersionAtLeast(5)) {
1707+
if (CS.getASTContext().LangOpts.isSwiftVersionAtLeast(5)) {
17101708
CS.addConstraint(ConstraintKind::Conversion,
17111709
CS.getType(expr->getSubExpr()), optTy,
17121710
CS.getConstraintLocator(expr));
@@ -2286,7 +2284,7 @@ namespace {
22862284
// of is-patterns applied to an irrefutable pattern.
22872285
pattern = pattern->getSemanticsProvidingPattern();
22882286
while (auto isp = dyn_cast<IsPattern>(pattern)) {
2289-
if (TypeChecker::validateType(CS.TC.Context,
2287+
if (TypeChecker::validateType(CS.getASTContext(),
22902288
isp->getCastTypeLoc(),
22912289
TypeResolution::forContextual(CS.DC),
22922290
TypeResolverContext::InExpression)) {
@@ -3734,7 +3732,7 @@ Type ConstraintSystem::generateConstraints(Pattern *pattern,
37343732
}
37353733

37363734
void ConstraintSystem::optimizeConstraints(Expr *e) {
3737-
if (TC.getLangOpts().DisableConstraintSolverPerformanceHacks)
3735+
if (getASTContext().LangOpts.DisableConstraintSolverPerformanceHacks)
37383736
return;
37393737

37403738
SmallVector<Expr *, 16> linkedExprs;

lib/Sema/CSRanking.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void ConstraintSystem::increaseScore(ScoreKind kind, unsigned value) {
3434
unsigned index = static_cast<unsigned>(kind);
3535
CurrentScore.Data[index] += value;
3636

37-
if (TC.getLangOpts().DebugConstraintSolver) {
37+
if (getASTContext().LangOpts.DebugConstraintSolver) {
3838
auto &log = getASTContext().TypeCheckerDebug->getStream();
3939
if (solverState)
4040
log.indent(solverState->depth * 2);
@@ -90,7 +90,7 @@ void ConstraintSystem::increaseScore(ScoreKind kind, unsigned value) {
9090
}
9191

9292
bool ConstraintSystem::worseThanBestSolution() const {
93-
if (TC.getLangOpts().DisableConstraintSolverPerformanceHacks)
93+
if (getASTContext().LangOpts.DisableConstraintSolverPerformanceHacks)
9494
return false;
9595

9696
if (retainAllSolutions())
@@ -100,7 +100,7 @@ bool ConstraintSystem::worseThanBestSolution() const {
100100
CurrentScore <= *solverState->BestScore)
101101
return false;
102102

103-
if (TC.getLangOpts().DebugConstraintSolver) {
103+
if (getASTContext().LangOpts.DebugConstraintSolver) {
104104
auto &log = getASTContext().TypeCheckerDebug->getStream();
105105
log.indent(solverState->depth * 2)
106106
<< "(solution is worse than the best solution)\n";
@@ -720,7 +720,7 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
720720
ConstraintSystem &cs, ArrayRef<Solution> solutions,
721721
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
722722
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights) {
723-
if (cs.TC.getLangOpts().DebugConstraintSolver) {
723+
if (cs.getASTContext().LangOpts.DebugConstraintSolver) {
724724
auto &log = cs.getASTContext().TypeCheckerDebug->getStream();
725725
log.indent(cs.solverState->depth * 2)
726726
<< "comparing solutions " << idx1 << " and " << idx2 <<"\n";
@@ -1126,14 +1126,14 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
11261126
if (!(score1 || score2)) {
11271127
if (auto nominalType2 = type2->getNominalOrBoundGenericNominal()) {
11281128
if ((nominalType2->getName() ==
1129-
cs.TC.Context.Id_OptionalNilComparisonType)) {
1129+
cs.getASTContext().Id_OptionalNilComparisonType)) {
11301130
++score2;
11311131
}
11321132
}
11331133

11341134
if (auto nominalType1 = type1->getNominalOrBoundGenericNominal()) {
11351135
if ((nominalType1->getName() ==
1136-
cs.TC.Context.Id_OptionalNilComparisonType)) {
1136+
cs.getASTContext().Id_OptionalNilComparisonType)) {
11371137
++score1;
11381138
}
11391139
}
@@ -1190,7 +1190,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
11901190
if (viable.size() == 1)
11911191
return 0;
11921192

1193-
if (TC.getLangOpts().DebugConstraintSolver) {
1193+
if (getASTContext().LangOpts.DebugConstraintSolver) {
11941194
auto &log = getASTContext().TypeCheckerDebug->getStream();
11951195
log.indent(solverState->depth * 2)
11961196
<< "Comparing " << viable.size() << " viable solutions\n";

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3743,7 +3743,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
37433743
// Class and protocol metatypes are interoperable with certain Objective-C
37443744
// runtime classes, but only when ObjC interop is enabled.
37453745

3746-
if (TC.getLangOpts().EnableObjCInterop) {
3746+
if (getASTContext().LangOpts.EnableObjCInterop) {
37473747
// These conversions are between concrete types that don't need further
37483748
// resolution, so we can consider them immediately solved.
37493749
auto addSolvedRestrictedConstraint
@@ -4020,7 +4020,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
40204020
if (auto optTryExpr =
40214021
dyn_cast_or_null<OptionalTryExpr>(locator.trySimplifyToExpr())) {
40224022
auto subExprType = getType(optTryExpr->getSubExpr());
4023-
bool isSwift5OrGreater = TC.getLangOpts().isSwiftVersionAtLeast(5);
4023+
const bool isSwift5OrGreater = getASTContext().LangOpts.isSwiftVersionAtLeast(5);
40244024
if (isSwift5OrGreater && (bool)subExprType->getOptionalObjectType()) {
40254025
// For 'try?' expressions, a ForceOptional fix converts 'try?'
40264026
// to 'try!'. If the sub-expression is optional, then a force-unwrap

lib/Sema/CSSolver.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Solution ConstraintSystem::finalize() {
6969

7070
// Update the best score we've seen so far.
7171
if (!retainAllSolutions()) {
72-
assert(TC.getLangOpts().DisableConstraintSolverPerformanceHacks ||
72+
assert(getASTContext().LangOpts.DisableConstraintSolverPerformanceHacks ||
7373
!solverState->BestScore || CurrentScore <= *solverState->BestScore);
7474

7575
if (!solverState->BestScore || CurrentScore <= *solverState->BestScore) {
@@ -283,7 +283,7 @@ bool ConstraintSystem::simplify(bool ContinueAfterFailures) {
283283
failedConstraint = constraint;
284284
}
285285

286-
if (TC.getLangOpts().DebugConstraintSolver) {
286+
if (getASTContext().LangOpts.DebugConstraintSolver) {
287287
auto &log = getASTContext().TypeCheckerDebug->getStream();
288288
log.indent(solverState ? solverState->depth * 2 : 0)
289289
<< "(failed constraint ";
@@ -594,7 +594,7 @@ bool ConstraintSystem::Candidate::solve(
594594
if (isTooComplexGiven(&cs, shrunkExprs))
595595
return false;
596596

597-
if (TC.getLangOpts().DebugConstraintSolver) {
597+
if (cs.getASTContext().LangOpts.DebugConstraintSolver) {
598598
auto &log = cs.getASTContext().TypeCheckerDebug->getStream();
599599
log << "--- Solving candidate for shrinking at ";
600600
auto R = E->getSourceRange();
@@ -715,7 +715,7 @@ void ConstraintSystem::Candidate::applySolutions(
715715
}
716716

717717
void ConstraintSystem::shrink(Expr *expr) {
718-
if (TC.getLangOpts().SolverDisableShrink)
718+
if (getASTContext().LangOpts.SolverDisableShrink)
719719
return;
720720

721721
using DomainMap = llvm::SmallDenseMap<Expr *, ArrayRef<ValueDecl *>>;
@@ -1099,7 +1099,7 @@ bool ConstraintSystem::solve(Expr *&expr,
10991099
SmallVectorImpl<Solution> &solutions,
11001100
FreeTypeVariableBinding allowFreeTypeVariables) {
11011101
llvm::SaveAndRestore<bool>
1102-
debugForExpr(TC.getLangOpts().DebugConstraintSolver,
1102+
debugForExpr(getASTContext().LangOpts.DebugConstraintSolver,
11031103
debugConstraintSolverForExpr(TC.Context, expr));
11041104

11051105
// Attempt to solve the constraint system.
@@ -1138,7 +1138,7 @@ bool ConstraintSystem::solve(Expr *&expr,
11381138
return true;
11391139
}
11401140

1141-
if (TC.getLangOpts().DebugConstraintSolver) {
1141+
if (getASTContext().LangOpts.DebugConstraintSolver) {
11421142
auto &log = getASTContext().TypeCheckerDebug->getStream();
11431143
if (solutions.size() == 1) {
11441144
log << "---Solution---\n";
@@ -1160,7 +1160,7 @@ ConstraintSystem::solveImpl(Expr *&expr,
11601160
ExprTypeCheckListener *listener,
11611161
SmallVectorImpl<Solution> &solutions,
11621162
FreeTypeVariableBinding allowFreeTypeVariables) {
1163-
if (TC.getLangOpts().DebugConstraintSolver) {
1163+
if (getASTContext().LangOpts.DebugConstraintSolver) {
11641164
auto &log = getASTContext().TypeCheckerDebug->getStream();
11651165
log << "---Constraint solving for the expression at ";
11661166
auto R = expr->getSourceRange();
@@ -1232,7 +1232,7 @@ ConstraintSystem::solveImpl(Expr *&expr,
12321232
return SolutionKind::Error;
12331233
}
12341234

1235-
if (TC.getLangOpts().DebugConstraintSolver) {
1235+
if (getASTContext().LangOpts.DebugConstraintSolver) {
12361236
auto &log = getASTContext().TypeCheckerDebug->getStream();
12371237
log << "---Initial constraints for the given expression---\n";
12381238
print(log, expr);
@@ -1257,7 +1257,7 @@ bool ConstraintSystem::solve(Expr *const expr,
12571257
// Solve the system.
12581258
solve(solutions);
12591259

1260-
if (TC.getLangOpts().DebugConstraintSolver) {
1260+
if (getASTContext().LangOpts.DebugConstraintSolver) {
12611261
auto &log = getASTContext().TypeCheckerDebug->getStream();
12621262
log << "---Solver statistics---\n";
12631263
log << "Total number of scopes explored: " << solverState->NumStatesExplored << "\n";
@@ -2150,7 +2150,7 @@ void ConstraintSystem::partitionDisjunction(
21502150
}
21512151

21522152
// Partition SIMD operators.
2153-
if (!TC.getLangOpts().SolverEnableOperatorDesignatedTypes &&
2153+
if (!getASTContext().LangOpts.SolverEnableOperatorDesignatedTypes &&
21542154
isOperatorBindOverload(Choices[0])) {
21552155
forEachChoice(Choices, [&](unsigned index, Constraint *constraint) -> bool {
21562156
if (!isOperatorBindOverload(constraint))
@@ -2175,7 +2175,7 @@ void ConstraintSystem::partitionDisjunction(
21752175
}
21762176
};
21772177

2178-
if (TC.getLangOpts().SolverEnableOperatorDesignatedTypes &&
2178+
if (getASTContext().LangOpts.SolverEnableOperatorDesignatedTypes &&
21792179
isOperatorBindOverload(Choices[0])) {
21802180
partitionForDesignatedTypes(Choices, forEachChoice, appendPartition);
21812181
}
@@ -2210,7 +2210,7 @@ Constraint *ConstraintSystem::selectDisjunction() {
22102210
// disjunctions that we may not be able to short-circuit, allowing
22112211
// us to eliminate behavior that is exponential in the number of
22122212
// operators in the expression.
2213-
if (TC.getLangOpts().SolverEnableOperatorDesignatedTypes) {
2213+
if (getASTContext().LangOpts.SolverEnableOperatorDesignatedTypes) {
22142214
if (auto *disjunction = selectApplyDisjunction())
22152215
return disjunction;
22162216
}

lib/Sema/CSStep.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ class SolverStep {
233233

234234
/// Check whether constraint solver is running in "debug" mode,
235235
/// which should output diagnostic information.
236-
bool isDebugMode() const { return CS.TC.getLangOpts().DebugConstraintSolver; }
236+
bool isDebugMode() const {
237+
return CS.getASTContext().LangOpts.DebugConstraintSolver;
238+
}
237239

238240
llvm::raw_ostream &getDebugLogger(bool indent = true) const {
239241
auto &log = CS.getASTContext().TypeCheckerDebug->getStream();

lib/Sema/CalleeCandidateInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void CalleeCandidateInfo::filterList(ClosenessPredicate predicate) {
137137
// treat it as unavailable, which is a very close failure.
138138
if (declCloseness.first == CC_ExactMatch &&
139139
VD->getAttrs().isUnavailable(CS.getASTContext()) &&
140-
!CS.TC.getLangOpts().DisableAvailabilityChecking)
140+
!CS.getASTContext().LangOpts.DisableAvailabilityChecking)
141141
declCloseness.first = CC_Unavailable;
142142

143143
// Likewise, if the candidate is inaccessible from the scope it is being

lib/Sema/ConstraintGraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ bool ConstraintGraph::contractEdges() {
12551255
rep2->getImpl().canBindToLValue()) ||
12561256
// Allow l-value contractions when binding parameter types.
12571257
isParamBindingConstraint)) {
1258-
if (CS.TC.getLangOpts().DebugConstraintSolver) {
1258+
if (CS.getASTContext().LangOpts.DebugConstraintSolver) {
12591259
auto &log = CS.getASTContext().TypeCheckerDebug->getStream();
12601260
if (CS.solverState)
12611261
log.indent(CS.solverState->depth * 2);

0 commit comments

Comments
 (0)