Skip to content

Commit f519505

Browse files
authored
Remove some more TypeChecker uses (swiftlang#28132)
Remove some more TypeChecker uses
2 parents 8280527 + 27e3d9f commit f519505

16 files changed

+333
-317
lines changed

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 2 deletions
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

@@ -3722,7 +3722,7 @@ namespace {
37223722
// If we're performing an assignment to a weak or unowned variable from
37233723
// a constructor call, emit a warning that the instance will be
37243724
// immediately deallocated.
3725-
diagnoseUnownedImmediateDeallocation(cs.getTypeChecker(), expr);
3725+
diagnoseUnownedImmediateDeallocation(cs.getASTContext(), expr);
37263726
}
37273727
return expr;
37283728
}

lib/Sema/CSDiag.cpp

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

2417-
auto &TC = CS.TC;
2418-
auto *DC = CS.DC;
2419-
2420-
auto typeCheckExpr = [&](TypeChecker &TC, Expr *expr, DeclContext *DC,
2417+
auto typeCheckExpr = [&](Expr *expr, DeclContext *DC,
24212418
SmallPtrSetImpl<TypeBase *> &types) {
24222419
getPossibleTypesOfExpressionWithoutApplying(
24232420
expr, DC, types, FreeTypeVariableBinding::Disallow);
@@ -2427,7 +2424,7 @@ bool FailureDiagnosis::diagnoseCallContextualConversionErrors(
24272424
// see if that's going to produce a type, if so, let's type-check
24282425
// again, this time using given contextual type.
24292426
SmallPtrSet<TypeBase *, 4> withoutContextual;
2430-
typeCheckExpr(TC, callExpr, DC, withoutContextual);
2427+
typeCheckExpr(callExpr, CS.DC, withoutContextual);
24312428

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

lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() {
15331533
auto diag = emitDiagnostic(
15341534
expr->getLoc(), diag::ambiguous_because_of_trailing_closure,
15351535
choicePair.first.empty(), choicePair.second->getFullName());
1536-
swift::fixItEncloseTrailingClosure(getTypeChecker(), diag, callExpr,
1536+
swift::fixItEncloseTrailingClosure(getASTContext(), diag, callExpr,
15371537
choicePair.first);
15381538
}
15391539

@@ -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
@@ -2198,15 +2198,14 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
21982198
return getTypeMatchSuccess();
21992199
}
22002200

2201-
static bool isStringCompatiblePointerBaseType(TypeChecker &TC,
2202-
DeclContext *DC,
2201+
static bool isStringCompatiblePointerBaseType(ASTContext &ctx,
22032202
Type baseType) {
22042203
// Allow strings to be passed to pointer-to-byte or pointer-to-void types.
2205-
if (baseType->isEqual(TC.getInt8Type(DC)))
2204+
if (baseType->isEqual(TypeChecker::getInt8Type(ctx)))
22062205
return true;
2207-
if (baseType->isEqual(TC.getUInt8Type(DC)))
2206+
if (baseType->isEqual(TypeChecker::getUInt8Type(ctx)))
22082207
return true;
2209-
if (baseType->isEqual(TC.Context.TheEmptyTupleType))
2208+
if (baseType->isEqual(ctx.TheEmptyTupleType))
22102209
return true;
22112210

22122211
return false;
@@ -4152,11 +4151,12 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
41524151

41534152
// The pointer can be converted from a string, if the element
41544153
// type is compatible.
4155-
if (type1->isEqual(TC.getStringType(DC))) {
4154+
auto &ctx = getASTContext();
4155+
if (type1->isEqual(TypeChecker::getStringType(ctx))) {
41564156
auto baseTy = getFixedTypeRecursive(pointeeTy, false);
41574157

41584158
if (baseTy->isTypeVariableOrMember() ||
4159-
isStringCompatiblePointerBaseType(TC, DC, baseTy))
4159+
isStringCompatiblePointerBaseType(ctx, baseTy))
41604160
conversionsOrFixes.push_back(
41614161
ConversionRestrictionKind::StringToPointer);
41624162
}
@@ -4479,7 +4479,7 @@ ConstraintSystem::simplifyConstructionConstraint(
44794479
// The constructor will have function type T -> T2, for a fresh type
44804480
// variable T. T2 is the result type provided via the construction
44814481
// constraint itself.
4482-
addValueMemberConstraint(MetatypeType::get(valueType, TC.Context),
4482+
addValueMemberConstraint(MetatypeType::get(valueType, getASTContext()),
44834483
DeclBaseName::createConstructor(),
44844484
memberType,
44854485
useDC, functionRefKind,
@@ -5177,6 +5177,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
51775177

51785178
// If the base type is a tuple type, look for the named or indexed member
51795179
// of the tuple.
5180+
auto &ctx = getASTContext();
51805181
if (auto baseTuple = baseObjTy->getAs<TupleType>()) {
51815182
// Tuples don't have compound-name members.
51825183
if (!memberName.isSimpleName() || memberName.isSpecial())
@@ -5215,7 +5216,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
52155216
// anything else, because the cost of the general search is so
52165217
// high.
52175218
if (auto info = getArgumentInfo(memberLocator)) {
5218-
memberName = DeclName(TC.Context, memberName.getBaseName(), info->Labels);
5219+
memberName = DeclName(ctx, memberName.getBaseName(),
5220+
info->Labels);
52195221
}
52205222
}
52215223

@@ -5249,8 +5251,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
52495251
// If the instance type is String bridged to NSString, compute
52505252
// the type we'll look in for bridging.
52515253
Type bridgedType;
5252-
if (baseObjTy->getAnyNominal() == TC.Context.getStringDecl()) {
5253-
if (Type classType = TC.Context.getBridgedToObjC(DC, instanceTy)) {
5254+
if (baseObjTy->getAnyNominal() == ctx.getStringDecl()) {
5255+
if (Type classType = ctx.getBridgedToObjC(DC, instanceTy)) {
52545256
bridgedType = classType;
52555257
}
52565258
}
@@ -5517,7 +5519,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
55175519
if (!(info && info->Labels.size() == 1 &&
55185520
info->Labels[0] == getASTContext().Id_dynamicMember)) {
55195521
return OverloadChoice::getDynamicMemberLookup(
5520-
baseTy, subscript, TC.Context.getIdentifier("subscript"),
5522+
baseTy, subscript, ctx.getIdentifier("subscript"),
55215523
/*isKeyPathBased=*/true);
55225524
}
55235525
}
@@ -5535,11 +5537,11 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
55355537
// Backward compatibility hack. In Swift 4, `init` and init were
55365538
// the same name, so you could write "foo.init" to look up a
55375539
// method or property named `init`.
5538-
if (!TC.Context.isSwiftVersionAtLeast(5) &&
5540+
if (!ctx.isSwiftVersionAtLeast(5) &&
55395541
memberName.getBaseName() == DeclBaseName::createConstructor() &&
55405542
!isImplicitInit) {
55415543
auto &compatLookup = lookupMember(instanceTy,
5542-
TC.Context.getIdentifier("init"));
5544+
ctx.getIdentifier("init"));
55435545
for (auto result : compatLookup)
55445546
addChoice(getOverloadChoice(result.getValueDecl(),
55455547
/*isBridged=*/false,
@@ -6409,12 +6411,13 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
64096411
}
64106412

64116413
// Explicit bridging from a value type to an Objective-C class type.
6414+
auto &ctx = getASTContext();
64126415
if (unwrappedFromType->isPotentiallyBridgedValueType() &&
64136416
(unwrappedToType->isBridgeableObjectType() ||
64146417
(unwrappedToType->isExistentialType() &&
64156418
!unwrappedToType->isAny()))) {
64166419
countOptionalInjections();
6417-
if (Type classType = TC.Context.getBridgedToObjC(DC, unwrappedFromType)) {
6420+
if (Type classType = ctx.getBridgedToObjC(DC, unwrappedFromType)) {
64186421
return matchTypes(classType, unwrappedToType, ConstraintKind::Conversion,
64196422
subflags, locator);
64206423
}
@@ -6426,26 +6429,26 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
64266429
if (unwrappedFromType->mayHaveSuperclass() &&
64276430
unwrappedToType->isPotentiallyBridgedValueType()) {
64286431
Type bridgedValueType;
6429-
if (auto objcClass = TC.Context.getBridgedToObjC(DC, unwrappedToType,
6430-
&bridgedValueType)) {
6432+
if (auto objcClass = ctx.getBridgedToObjC(DC, unwrappedToType,
6433+
&bridgedValueType)) {
64316434
// Bridging NSNumber to NSValue is one-way, since there are multiple Swift
64326435
// value types that bridge to those object types. It requires a checked
64336436
// cast to get back.
6434-
if (TC.Context.isObjCClassWithMultipleSwiftBridgedTypes(objcClass))
6437+
if (ctx.isObjCClassWithMultipleSwiftBridgedTypes(objcClass))
64356438
return SolutionKind::Error;
64366439

64376440
// If the bridged value type is generic, the generic arguments
64386441
// must either match or be bridged.
64396442
// FIXME: This should be an associated type of the protocol.
64406443
auto &ctx = getASTContext();
64416444
if (auto fromBGT = unwrappedToType->getAs<BoundGenericType>()) {
6442-
if (fromBGT->getDecl() == TC.Context.getArrayDecl()) {
6445+
if (fromBGT->getDecl() == ctx.getArrayDecl()) {
64436446
// [AnyObject]
64446447
addConstraint(ConstraintKind::Bind, fromBGT->getGenericArgs()[0],
6445-
TC.Context.getAnyObjectType(),
6448+
ctx.getAnyObjectType(),
64466449
getConstraintLocator(locator.withPathElement(
64476450
LocatorPathElt::GenericArgument(0))));
6448-
} else if (fromBGT->getDecl() == TC.Context.getDictionaryDecl()) {
6451+
} else if (fromBGT->getDecl() == ctx.getDictionaryDecl()) {
64496452
// [NSObject : AnyObject]
64506453
auto nsObjectType = ctx.getNSObjectType();
64516454
if (!nsObjectType) {
@@ -6460,11 +6463,11 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
64606463
LocatorPathElt::GenericArgument(0))));
64616464

64626465
addConstraint(ConstraintKind::Bind, fromBGT->getGenericArgs()[1],
6463-
TC.Context.getAnyObjectType(),
6466+
ctx.getAnyObjectType(),
64646467
getConstraintLocator(
64656468
locator.withPathElement(
64666469
LocatorPathElt::GenericArgument(1))));
6467-
} else if (fromBGT->getDecl() == TC.Context.getSetDecl()) {
6470+
} else if (fromBGT->getDecl() == ctx.getSetDecl()) {
64686471
auto nsObjectType = ctx.getNSObjectType();
64696472
if (!nsObjectType) {
64706473
// Not a bridging case. Should we detect this earlier?
@@ -6597,7 +6600,7 @@ ConstraintSystem::simplifyOpenedExistentialOfConstraint(
65976600
assert(instanceTy->isExistentialType());
65986601
Type openedTy = OpenedArchetypeType::get(instanceTy);
65996602
if (isMetatype)
6600-
openedTy = MetatypeType::get(openedTy, TC.Context);
6603+
openedTy = MetatypeType::get(openedTy, getASTContext());
66016604
return matchTypes(type1, openedTy, ConstraintKind::Bind, subflags, locator);
66026605
}
66036606
if (!type2->isTypeVariableOrMember())
@@ -7897,15 +7900,18 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
78977900
if (flags.contains(TMF_GenerateConstraints)) {
78987901
increaseScore(ScoreKind::SK_ValueToPointerConversion);
78997902

7903+
auto &ctx = getASTContext();
79007904
auto int8Con = Constraint::create(*this, ConstraintKind::Bind,
7901-
baseType2, TC.getInt8Type(DC),
7902-
getConstraintLocator(locator));
7905+
baseType2,
7906+
TypeChecker::getInt8Type(ctx),
7907+
getConstraintLocator(locator));
79037908
auto uint8Con = Constraint::create(*this, ConstraintKind::Bind,
7904-
baseType2, TC.getUInt8Type(DC),
7905-
getConstraintLocator(locator));
7909+
baseType2,
7910+
TypeChecker::getUInt8Type(ctx),
7911+
getConstraintLocator(locator));
79067912
auto voidCon = Constraint::create(*this, ConstraintKind::Bind,
7907-
baseType2, TC.Context.TheEmptyTupleType,
7908-
getConstraintLocator(locator));
7913+
baseType2, ctx.TheEmptyTupleType,
7914+
getConstraintLocator(locator));
79097915

79107916
Constraint *disjunctionChoices[] = {int8Con, uint8Con, voidCon};
79117917
addDisjunctionConstraint(disjunctionChoices, locator);
@@ -7915,7 +7921,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
79157921
return SolutionKind::Unsolved;
79167922
}
79177923

7918-
if (!isStringCompatiblePointerBaseType(TC, DC, baseType2)) {
7924+
if (!isStringCompatiblePointerBaseType(getASTContext(), baseType2)) {
79197925
return SolutionKind::Error;
79207926
}
79217927

@@ -8024,7 +8030,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
80248030
}
80258031

80268032
auto hashableProtocol =
8027-
TC.Context.getProtocol(KnownProtocolKind::Hashable);
8033+
getASTContext().getProtocol(KnownProtocolKind::Hashable);
80288034
if (!hashableProtocol)
80298035
return SolutionKind::Error;
80308036

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)