Skip to content

Commit 41ab235

Browse files
CodaFihamishknight
authored andcommitted
[CS] Remove some TypeChecker uses
1 parent be84153 commit 41ab235

11 files changed

+74
-73
lines changed

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2151,7 +2151,7 @@ namespace {
21512151
// Make the integer literals for the parameters.
21522152
auto buildExprFromUnsigned = [&](unsigned value) {
21532153
LiteralExpr *expr = IntegerLiteralExpr::createFromUnsigned(ctx, value);
2154-
cs.setType(expr, cs.getTypeChecker().getIntType(cs.DC));
2154+
cs.setType(expr, TypeChecker::getIntType(ctx));
21552155
return handleIntegerLiteralExpr(expr);
21562156
};
21572157

lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,10 +2547,7 @@ bool FailureDiagnosis::diagnoseCallContextualConversionErrors(
25472547
if (!contextualType || contextualType->hasUnresolvedType())
25482548
return false;
25492549

2550-
auto &TC = CS.TC;
2551-
auto *DC = CS.DC;
2552-
2553-
auto typeCheckExpr = [&](TypeChecker &TC, Expr *expr, DeclContext *DC,
2550+
auto typeCheckExpr = [&](Expr *expr, DeclContext *DC,
25542551
SmallPtrSetImpl<TypeBase *> &types) {
25552552
getPossibleTypesOfExpressionWithoutApplying(
25562553
expr, DC, types, FreeTypeVariableBinding::Disallow);
@@ -2560,7 +2557,7 @@ bool FailureDiagnosis::diagnoseCallContextualConversionErrors(
25602557
// see if that's going to produce a type, if so, let's type-check
25612558
// again, this time using given contextual type.
25622559
SmallPtrSet<TypeBase *, 4> withoutContextual;
2563-
typeCheckExpr(TC, callExpr, DC, withoutContextual);
2560+
typeCheckExpr(callExpr, CS.DC, withoutContextual);
25642561

25652562
// If there are no types returned, it means that problem was
25662563
// nothing to do with contextual information, probably parameter/argument

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,9 +2416,9 @@ bool ContextualFailure::diagnoseThrowsTypeMismatch() const {
24162416

24172417
// If we tried to throw the error code of an error type, suggest object
24182418
// construction.
2419-
auto &TC = getTypeChecker();
2419+
auto &Ctx = getASTContext();
24202420
if (auto errorCodeProtocol =
2421-
TC.Context.getProtocol(KnownProtocolKind::ErrorCodeProtocol)) {
2421+
Ctx.getProtocol(KnownProtocolKind::ErrorCodeProtocol)) {
24222422
Type errorCodeType = getFromType();
24232423
auto conformance = TypeChecker::conformsToProtocol(
24242424
errorCodeType, errorCodeProtocol, getDC(),
@@ -2641,11 +2641,8 @@ bool ContextualFailure::trySequenceSubsequenceFixIts(
26412641
if (!getASTContext().getStdlibModule())
26422642
return false;
26432643

2644-
auto &TC = getTypeChecker();
2645-
auto *DC = getDC();
2646-
2647-
auto String = TC.getStringType(DC);
2648-
auto Substring = TC.getSubstringType(DC);
2644+
auto String = TypeChecker::getStringType(getASTContext());
2645+
auto Substring = TypeChecker::getSubstringType(getASTContext());
26492646

26502647
if (!String || !Substring)
26512648
return false;

lib/Sema/CSRanking.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
998998
// compatibility under Swift 4 mode by ensuring we don't introduce any new
999999
// ambiguities. This will become a more general "is more specialised" rule
10001000
// in Swift 5 mode.
1001-
if (!tc.Context.isSwiftVersionAtLeast(5) &&
1001+
if (!cs.getASTContext().isSwiftVersionAtLeast(5) &&
10021002
choice1.getKind() != OverloadChoiceKind::DeclViaDynamic &&
10031003
choice2.getKind() != OverloadChoiceKind::DeclViaDynamic &&
10041004
isa<VarDecl>(decl1) && isa<VarDecl>(decl2)) {
@@ -1158,7 +1158,7 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
11581158
// All other things being equal, apply the Swift 4.1 compatibility hack for
11591159
// preferring var members in concrete types over a protocol requirement
11601160
// (see the comment above for the rationale of this hack).
1161-
if (!tc.Context.isSwiftVersionAtLeast(5) && score1 == score2) {
1161+
if (!cs.getASTContext().isSwiftVersionAtLeast(5) && score1 == score2) {
11621162
score1 += isVarAndNotProtocol1;
11631163
score2 += isVarAndNotProtocol2;
11641164
}

lib/Sema/CSSimplify.cpp

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,15 +2181,14 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
21812181
return getTypeMatchSuccess();
21822182
}
21832183

2184-
static bool isStringCompatiblePointerBaseType(TypeChecker &TC,
2185-
DeclContext *DC,
2184+
static bool isStringCompatiblePointerBaseType(ASTContext &ctx,
21862185
Type baseType) {
21872186
// Allow strings to be passed to pointer-to-byte or pointer-to-void types.
2188-
if (baseType->isEqual(TC.getInt8Type(DC)))
2187+
if (baseType->isEqual(TypeChecker::getInt8Type(ctx)))
21892188
return true;
2190-
if (baseType->isEqual(TC.getUInt8Type(DC)))
2189+
if (baseType->isEqual(TypeChecker::getUInt8Type(ctx)))
21912190
return true;
2192-
if (baseType->isEqual(TC.Context.TheEmptyTupleType))
2191+
if (baseType->isEqual(ctx.TheEmptyTupleType))
21932192
return true;
21942193

21952194
return false;
@@ -4135,11 +4134,12 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
41354134

41364135
// The pointer can be converted from a string, if the element
41374136
// type is compatible.
4138-
if (type1->isEqual(TC.getStringType(DC))) {
4137+
auto &ctx = getASTContext();
4138+
if (type1->isEqual(TypeChecker::getStringType(ctx))) {
41394139
auto baseTy = getFixedTypeRecursive(pointeeTy, false);
41404140

41414141
if (baseTy->isTypeVariableOrMember() ||
4142-
isStringCompatiblePointerBaseType(TC, DC, baseTy))
4142+
isStringCompatiblePointerBaseType(ctx, baseTy))
41434143
conversionsOrFixes.push_back(
41444144
ConversionRestrictionKind::StringToPointer);
41454145
}
@@ -4462,7 +4462,7 @@ ConstraintSystem::simplifyConstructionConstraint(
44624462
// The constructor will have function type T -> T2, for a fresh type
44634463
// variable T. T2 is the result type provided via the construction
44644464
// constraint itself.
4465-
addValueMemberConstraint(MetatypeType::get(valueType, TC.Context),
4465+
addValueMemberConstraint(MetatypeType::get(valueType, getASTContext()),
44664466
DeclBaseName::createConstructor(),
44674467
memberType,
44684468
useDC, functionRefKind,
@@ -5160,6 +5160,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
51605160

51615161
// If the base type is a tuple type, look for the named or indexed member
51625162
// of the tuple.
5163+
auto &ctx = getASTContext();
51635164
if (auto baseTuple = baseObjTy->getAs<TupleType>()) {
51645165
// Tuples don't have compound-name members.
51655166
if (!memberName.isSimpleName() || memberName.isSpecial())
@@ -5198,7 +5199,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
51985199
// anything else, because the cost of the general search is so
51995200
// high.
52005201
if (auto info = getArgumentInfo(memberLocator)) {
5201-
memberName = DeclName(TC.Context, memberName.getBaseName(), info->Labels);
5202+
memberName = DeclName(ctx, memberName.getBaseName(),
5203+
info->Labels);
52025204
}
52035205
}
52045206

@@ -5232,8 +5234,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
52325234
// If the instance type is String bridged to NSString, compute
52335235
// the type we'll look in for bridging.
52345236
Type bridgedType;
5235-
if (baseObjTy->getAnyNominal() == TC.Context.getStringDecl()) {
5236-
if (Type classType = TC.Context.getBridgedToObjC(DC, instanceTy)) {
5237+
if (baseObjTy->getAnyNominal() == ctx.getStringDecl()) {
5238+
if (Type classType = ctx.getBridgedToObjC(DC, instanceTy)) {
52375239
bridgedType = classType;
52385240
}
52395241
}
@@ -5500,7 +5502,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
55005502
if (!(info && info->Labels.size() == 1 &&
55015503
info->Labels[0] == getASTContext().Id_dynamicMember)) {
55025504
return OverloadChoice::getDynamicMemberLookup(
5503-
baseTy, subscript, TC.Context.getIdentifier("subscript"),
5505+
baseTy, subscript, ctx.getIdentifier("subscript"),
55045506
/*isKeyPathBased=*/true);
55055507
}
55065508
}
@@ -5518,11 +5520,11 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
55185520
// Backward compatibility hack. In Swift 4, `init` and init were
55195521
// the same name, so you could write "foo.init" to look up a
55205522
// method or property named `init`.
5521-
if (!TC.Context.isSwiftVersionAtLeast(5) &&
5523+
if (!ctx.isSwiftVersionAtLeast(5) &&
55225524
memberName.getBaseName() == DeclBaseName::createConstructor() &&
55235525
!isImplicitInit) {
55245526
auto &compatLookup = lookupMember(instanceTy,
5525-
TC.Context.getIdentifier("init"));
5527+
ctx.getIdentifier("init"));
55265528
for (auto result : compatLookup)
55275529
addChoice(getOverloadChoice(result.getValueDecl(),
55285530
/*isBridged=*/false,
@@ -6392,12 +6394,13 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
63926394
}
63936395

63946396
// Explicit bridging from a value type to an Objective-C class type.
6397+
auto &ctx = getASTContext();
63956398
if (unwrappedFromType->isPotentiallyBridgedValueType() &&
63966399
(unwrappedToType->isBridgeableObjectType() ||
63976400
(unwrappedToType->isExistentialType() &&
63986401
!unwrappedToType->isAny()))) {
63996402
countOptionalInjections();
6400-
if (Type classType = TC.Context.getBridgedToObjC(DC, unwrappedFromType)) {
6403+
if (Type classType = ctx.getBridgedToObjC(DC, unwrappedFromType)) {
64016404
return matchTypes(classType, unwrappedToType, ConstraintKind::Conversion,
64026405
subflags, locator);
64036406
}
@@ -6409,26 +6412,26 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
64096412
if (unwrappedFromType->mayHaveSuperclass() &&
64106413
unwrappedToType->isPotentiallyBridgedValueType()) {
64116414
Type bridgedValueType;
6412-
if (auto objcClass = TC.Context.getBridgedToObjC(DC, unwrappedToType,
6413-
&bridgedValueType)) {
6415+
if (auto objcClass = ctx.getBridgedToObjC(DC, unwrappedToType,
6416+
&bridgedValueType)) {
64146417
// Bridging NSNumber to NSValue is one-way, since there are multiple Swift
64156418
// value types that bridge to those object types. It requires a checked
64166419
// cast to get back.
6417-
if (TC.Context.isObjCClassWithMultipleSwiftBridgedTypes(objcClass))
6420+
if (ctx.isObjCClassWithMultipleSwiftBridgedTypes(objcClass))
64186421
return SolutionKind::Error;
64196422

64206423
// If the bridged value type is generic, the generic arguments
64216424
// must either match or be bridged.
64226425
// FIXME: This should be an associated type of the protocol.
64236426
auto &ctx = getASTContext();
64246427
if (auto fromBGT = unwrappedToType->getAs<BoundGenericType>()) {
6425-
if (fromBGT->getDecl() == TC.Context.getArrayDecl()) {
6428+
if (fromBGT->getDecl() == ctx.getArrayDecl()) {
64266429
// [AnyObject]
64276430
addConstraint(ConstraintKind::Bind, fromBGT->getGenericArgs()[0],
6428-
TC.Context.getAnyObjectType(),
6431+
ctx.getAnyObjectType(),
64296432
getConstraintLocator(locator.withPathElement(
64306433
LocatorPathElt::GenericArgument(0))));
6431-
} else if (fromBGT->getDecl() == TC.Context.getDictionaryDecl()) {
6434+
} else if (fromBGT->getDecl() == ctx.getDictionaryDecl()) {
64326435
// [NSObject : AnyObject]
64336436
auto nsObjectType = ctx.getNSObjectType();
64346437
if (!nsObjectType) {
@@ -6443,11 +6446,11 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
64436446
LocatorPathElt::GenericArgument(0))));
64446447

64456448
addConstraint(ConstraintKind::Bind, fromBGT->getGenericArgs()[1],
6446-
TC.Context.getAnyObjectType(),
6449+
ctx.getAnyObjectType(),
64476450
getConstraintLocator(
64486451
locator.withPathElement(
64496452
LocatorPathElt::GenericArgument(1))));
6450-
} else if (fromBGT->getDecl() == TC.Context.getSetDecl()) {
6453+
} else if (fromBGT->getDecl() == ctx.getSetDecl()) {
64516454
auto nsObjectType = ctx.getNSObjectType();
64526455
if (!nsObjectType) {
64536456
// Not a bridging case. Should we detect this earlier?
@@ -6580,7 +6583,7 @@ ConstraintSystem::simplifyOpenedExistentialOfConstraint(
65806583
assert(instanceTy->isExistentialType());
65816584
Type openedTy = OpenedArchetypeType::get(instanceTy);
65826585
if (isMetatype)
6583-
openedTy = MetatypeType::get(openedTy, TC.Context);
6586+
openedTy = MetatypeType::get(openedTy, getASTContext());
65846587
return matchTypes(type1, openedTy, ConstraintKind::Bind, subflags, locator);
65856588
}
65866589
if (!type2->isTypeVariableOrMember())
@@ -7880,15 +7883,18 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
78807883
if (flags.contains(TMF_GenerateConstraints)) {
78817884
increaseScore(ScoreKind::SK_ValueToPointerConversion);
78827885

7886+
auto &ctx = getASTContext();
78837887
auto int8Con = Constraint::create(*this, ConstraintKind::Bind,
7884-
baseType2, TC.getInt8Type(DC),
7885-
getConstraintLocator(locator));
7888+
baseType2,
7889+
TypeChecker::getInt8Type(ctx),
7890+
getConstraintLocator(locator));
78867891
auto uint8Con = Constraint::create(*this, ConstraintKind::Bind,
7887-
baseType2, TC.getUInt8Type(DC),
7888-
getConstraintLocator(locator));
7892+
baseType2,
7893+
TypeChecker::getUInt8Type(ctx),
7894+
getConstraintLocator(locator));
78897895
auto voidCon = Constraint::create(*this, ConstraintKind::Bind,
7890-
baseType2, TC.Context.TheEmptyTupleType,
7891-
getConstraintLocator(locator));
7896+
baseType2, ctx.TheEmptyTupleType,
7897+
getConstraintLocator(locator));
78927898

78937899
Constraint *disjunctionChoices[] = {int8Con, uint8Con, voidCon};
78947900
addDisjunctionConstraint(disjunctionChoices, locator);
@@ -7898,7 +7904,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
78987904
return SolutionKind::Unsolved;
78997905
}
79007906

7901-
if (!isStringCompatiblePointerBaseType(TC, DC, baseType2)) {
7907+
if (!isStringCompatiblePointerBaseType(getASTContext(), baseType2)) {
79027908
return SolutionKind::Error;
79037909
}
79047910

@@ -8007,7 +8013,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
80078013
}
80088014

80098015
auto hashableProtocol =
8010-
TC.Context.getProtocol(KnownProtocolKind::Hashable);
8016+
getASTContext().getProtocol(KnownProtocolKind::Hashable);
80118017
if (!hashableProtocol)
80128018
return SolutionKind::Error;
80138019

lib/Sema/CSSolver.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ TypeVariableType *ConstraintSystem::createTypeVariable(
5555
ConstraintLocator *locator,
5656
unsigned options) {
5757
++TotalNumTypeVariables;
58-
auto tv = TypeVariableType::getNew(TC.Context, assignTypeVariableID(),
58+
auto tv = TypeVariableType::getNew(getASTContext(), assignTypeVariableID(),
5959
locator, options);
6060
addTypeVariable(tv);
6161
return tv;
@@ -89,7 +89,7 @@ Solution ConstraintSystem::finalize() {
8989
break;
9090

9191
case FreeTypeVariableBinding::UnresolvedType:
92-
assignFixedType(tv, TC.Context.TheUnresolvedType);
92+
assignFixedType(tv, getASTContext().TheUnresolvedType);
9393
break;
9494
}
9595
}
@@ -573,7 +573,7 @@ bool ConstraintSystem::Candidate::solve(
573573
};
574574

575575
// Allocate new constraint system for sub-expression.
576-
ConstraintSystem cs(TC, DC, None, E);
576+
ConstraintSystem cs(BaseCS.getTypeChecker(), DC, None, E);
577577
cs.baseCS = &BaseCS;
578578

579579
// Set up expression type checker timer for the candidate.
@@ -594,12 +594,13 @@ bool ConstraintSystem::Candidate::solve(
594594
if (isTooComplexGiven(&cs, shrunkExprs))
595595
return false;
596596

597-
if (cs.getASTContext().LangOpts.DebugConstraintSolver) {
597+
auto &ctx = cs.getASTContext();
598+
if (ctx.LangOpts.DebugConstraintSolver) {
598599
auto &log = cs.getASTContext().TypeCheckerDebug->getStream();
599600
log << "--- Solving candidate for shrinking at ";
600601
auto R = E->getSourceRange();
601602
if (R.isValid()) {
602-
R.print(log, TC.Context.SourceMgr, /*PrintText=*/ false);
603+
R.print(log, ctx.SourceMgr, /*PrintText=*/ false);
603604
} else {
604605
log << "<invalid range>";
605606
}
@@ -631,7 +632,7 @@ bool ConstraintSystem::Candidate::solve(
631632
cs.solve(solutions);
632633
}
633634

634-
if (TC.getLangOpts().DebugConstraintSolver) {
635+
if (ctx.LangOpts.DebugConstraintSolver) {
635636
auto &log = cs.getASTContext().TypeCheckerDebug->getStream();
636637
if (solutions.empty()) {
637638
log << "--- No Solutions ---\n";
@@ -1053,7 +1054,8 @@ void ConstraintSystem::shrink(Expr *expr) {
10531054
// survive even after primary constraint system is destroyed.
10541055
for (auto &OSR : shrunkExprs) {
10551056
auto choices = OSR->getDecls();
1056-
auto decls = TC.Context.AllocateUninitialized<ValueDecl *>(choices.size());
1057+
auto decls =
1058+
getASTContext().AllocateUninitialized<ValueDecl *>(choices.size());
10571059

10581060
std::uninitialized_copy(choices.begin(), choices.end(), decls.begin());
10591061
OSR->setDecls(decls);
@@ -1100,7 +1102,7 @@ bool ConstraintSystem::solve(Expr *&expr,
11001102
FreeTypeVariableBinding allowFreeTypeVariables) {
11011103
llvm::SaveAndRestore<bool> debugForExpr(
11021104
getASTContext().LangOpts.DebugConstraintSolver,
1103-
debugConstraintSolverForExpr(TC.Context, expr));
1105+
debugConstraintSolverForExpr(getASTContext(), expr));
11041106

11051107
// Attempt to solve the constraint system.
11061108
auto solution = solveImpl(expr,
@@ -1165,7 +1167,7 @@ ConstraintSystem::solveImpl(Expr *&expr,
11651167
log << "---Constraint solving for the expression at ";
11661168
auto R = expr->getSourceRange();
11671169
if (R.isValid()) {
1168-
R.print(log, TC.Context.SourceMgr, /*PrintText=*/ false);
1170+
R.print(log, getASTContext().SourceMgr, /*PrintText=*/ false);
11691171
} else {
11701172
log << "<invalid range>";
11711173
}

lib/Sema/ConstraintLocator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ void ConstraintLocator::dump(SourceManager *sm) const {
255255
}
256256

257257
void ConstraintLocator::dump(ConstraintSystem *CS) const {
258-
dump(&CS->TC.Context.SourceMgr, llvm::errs());
258+
dump(&CS->getASTContext().SourceMgr, llvm::errs());
259259
llvm::errs() << "\n";
260260
}
261261

0 commit comments

Comments
 (0)