Skip to content

Commit a060fc7

Browse files
authored
Many Things Don't Need a TypeChecker (swiftlang#27837)
Many Things Don't Need a TypeChecker
2 parents 279ee76 + 8adf340 commit a060fc7

14 files changed

+136
-137
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,15 +1813,13 @@ AssignmentFailure::getMemberRef(ConstraintLocator *locator) const {
18131813
return member->choice;
18141814

18151815
auto *DC = getDC();
1816-
auto &TC = getTypeChecker();
1817-
18181816
auto *decl = member->choice.getDecl();
18191817
if (isa<SubscriptDecl>(decl) &&
1820-
isValidDynamicMemberLookupSubscript(cast<SubscriptDecl>(decl), DC, TC)) {
1818+
isValidDynamicMemberLookupSubscript(cast<SubscriptDecl>(decl), DC)) {
18211819
auto *subscript = cast<SubscriptDecl>(decl);
18221820
// If this is a keypath dynamic member lookup, we have to
18231821
// adjust the locator to find member referred by it.
1824-
if (isValidKeyPathDynamicMemberLookup(subscript, TC)) {
1822+
if (isValidKeyPathDynamicMemberLookup(subscript)) {
18251823
auto &cs = getConstraintSystem();
18261824
// Type has a following format:
18271825
// `(Self) -> (dynamicMember: {Writable}KeyPath<T, U>) -> U`

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5218,7 +5218,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
52185218
if (memberLocator &&
52195219
::hasDynamicMemberLookupAttribute(instanceTy,
52205220
DynamicMemberLookupCache) &&
5221-
isValidKeyPathDynamicMemberLookup(subscript, TC)) {
5221+
isValidKeyPathDynamicMemberLookup(subscript)) {
52225222
auto info = getArgumentInfo(memberLocator);
52235223

52245224
if (!(info && info->Labels.size() == 1 &&
@@ -5324,9 +5324,9 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
53245324
auto name = memberName.getBaseIdentifier();
53255325
for (const auto &candidate : subscripts.ViableCandidates) {
53265326
auto *SD = cast<SubscriptDecl>(candidate.getDecl());
5327-
bool isKeyPathBased = isValidKeyPathDynamicMemberLookup(SD, TC);
5327+
bool isKeyPathBased = isValidKeyPathDynamicMemberLookup(SD);
53285328

5329-
if (isValidStringDynamicMemberLookup(SD, DC, TC) || isKeyPathBased)
5329+
if (isValidStringDynamicMemberLookup(SD, DC) || isKeyPathBased)
53305330
result.addViable(OverloadChoice::getDynamicMemberLookup(
53315331
baseTy, SD, name, isKeyPathBased));
53325332
}
@@ -5335,7 +5335,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
53355335
auto *SD =
53365336
cast<SubscriptDecl>(subscripts.UnviableCandidates[index].getDecl());
53375337
auto choice = OverloadChoice::getDynamicMemberLookup(
5338-
baseTy, SD, name, isValidKeyPathDynamicMemberLookup(SD, TC));
5338+
baseTy, SD, name, isValidKeyPathDynamicMemberLookup(SD));
53395339
result.addUnviable(choice, subscripts.UnviableReasons[index]);
53405340
}
53415341
}
@@ -7094,7 +7094,7 @@ lookupDynamicCallableMethods(Type type, ConstraintSystem &CS,
70947094
auto candidates = matches.ViableCandidates;
70957095
auto filter = [&](OverloadChoice choice) {
70967096
auto cand = cast<FuncDecl>(choice.getDecl());
7097-
return !isValidDynamicCallableMethod(cand, decl, CS.TC, hasKeywordArgs);
7097+
return !isValidDynamicCallableMethod(cand, decl, hasKeywordArgs);
70987098
};
70997099
candidates.erase(
71007100
std::remove_if(candidates.begin(), candidates.end(), filter),

lib/Sema/CodeSynthesis.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ static bool areAllStoredPropertiesDefaultInitializable(NominalTypeDecl *decl) {
833833
return true;
834834
}
835835

836-
static void addImplicitConstructorsToStruct(StructDecl *decl, ASTContext &ctx) {
836+
static void addImplicitConstructorsToStruct(StructDecl *decl) {
837837
assert(!decl->hasClangNode() &&
838838
"ClangImporter is responsible for adding implicit constructors");
839839
assert(!decl->hasUnreferenceableStorage() &&
@@ -884,6 +884,7 @@ static void addImplicitConstructorsToStruct(StructDecl *decl, ASTContext &ctx) {
884884

885885
if (FoundMemberwiseInitializedProperty) {
886886
// Create the implicit memberwise constructor.
887+
auto &ctx = decl->getASTContext();
887888
auto ctor = createImplicitConstructor(
888889
decl, ImplicitConstructorKind::Memberwise, ctx);
889890
decl->addMember(ctor);
@@ -893,7 +894,7 @@ static void addImplicitConstructorsToStruct(StructDecl *decl, ASTContext &ctx) {
893894
TypeChecker::defineDefaultConstructor(decl);
894895
}
895896

896-
static void addImplicitConstructorsToClass(ClassDecl *decl, ASTContext &ctx) {
897+
static void addImplicitConstructorsToClass(ClassDecl *decl) {
897898
// Bail out if we're validating one of our constructors already;
898899
// we'll revisit the issue later.
899900
if (!decl->hasClangNode()) {
@@ -911,6 +912,7 @@ static void addImplicitConstructorsToClass(ClassDecl *decl, ASTContext &ctx) {
911912
// variable.
912913
bool FoundDesignatedInit = false;
913914

915+
auto &ctx = decl->getASTContext();
914916
SmallVector<std::pair<ValueDecl *, Type>, 4> declaredInitializers;
915917
llvm::SmallPtrSet<ConstructorDecl *, 4> overriddenInits;
916918
if (decl->hasClangNode()) {
@@ -1104,9 +1106,9 @@ void TypeChecker::addImplicitConstructors(NominalTypeDecl *decl) {
11041106
}
11051107

11061108
if (auto *structDecl = dyn_cast<StructDecl>(decl))
1107-
addImplicitConstructorsToStruct(structDecl, Context);
1109+
addImplicitConstructorsToStruct(structDecl);
11081110
if (auto *classDecl = dyn_cast<ClassDecl>(decl))
1109-
addImplicitConstructorsToClass(classDecl, Context);
1111+
addImplicitConstructorsToClass(classDecl);
11101112
}
11111113

11121114
void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,

lib/Sema/IDETypeCheckingRequests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ TypeRelationCheckRequest::evaluate(Evaluator &evaluator,
114114
llvm::Expected<TypePair>
115115
RootAndResultTypeOfKeypathDynamicMemberRequest::evaluate(Evaluator &evaluator,
116116
SubscriptDecl *subscript) const {
117-
auto &TC = TypeChecker::createForContext(subscript->getASTContext());
118-
119-
if (!isValidKeyPathDynamicMemberLookup(subscript, TC))
117+
if (!isValidKeyPathDynamicMemberLookup(subscript))
120118
return TypePair();
121119

122120
const auto *param = subscript->getIndices()->get(0);

lib/Sema/PCMacro.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ class Instrumenter : InstrumenterBase {
321321

322322
if (NB != B) {
323323
FD->setBody(NB);
324-
TypeChecker::createForContext(Context).checkFunctionErrorHandling(FD);
324+
TypeChecker::checkFunctionErrorHandling(FD);
325325
}
326326
}
327327
} else if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
@@ -691,9 +691,8 @@ void swift::performPCMacro(SourceFile &SF, TopLevelContext &TLC) {
691691
BraceStmt *NewBody = I.transformBraceStmt(Body, true);
692692
if (NewBody != Body) {
693693
TLCD->setBody(NewBody);
694-
TypeChecker &TC = TypeChecker::createForContext(ctx);
695-
TC.checkTopLevelErrorHandling(TLCD);
696-
TC.contextualizeTopLevelCode(TLC, TLCD);
694+
TypeChecker::checkTopLevelErrorHandling(TLCD);
695+
TypeChecker::contextualizeTopLevelCode(TLC, TLCD);
697696
}
698697
return false;
699698
}

lib/Sema/PlaygroundTransform.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class Instrumenter : InstrumenterBase {
279279
BraceStmt *NB = transformBraceStmt(B);
280280
if (NB != B) {
281281
FD->setBody(NB);
282-
TypeChecker::createForContext(Context).checkFunctionErrorHandling(FD);
282+
TypeChecker::checkFunctionErrorHandling(FD);
283283
}
284284
}
285285
} else if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
@@ -903,7 +903,7 @@ void swift::performPlaygroundTransform(SourceFile &SF, bool HighPerformance) {
903903
BraceStmt *NewBody = I.transformBraceStmt(Body);
904904
if (NewBody != Body) {
905905
FD->setBody(NewBody);
906-
TypeChecker::createForContext(ctx).checkFunctionErrorHandling(FD);
906+
TypeChecker::checkFunctionErrorHandling(FD);
907907
}
908908
return false;
909909
}
@@ -915,8 +915,7 @@ void swift::performPlaygroundTransform(SourceFile &SF, bool HighPerformance) {
915915
BraceStmt *NewBody = I.transformBraceStmt(Body, true);
916916
if (NewBody != Body) {
917917
TLCD->setBody(NewBody);
918-
TypeChecker::createForContext(ctx)
919-
.checkTopLevelErrorHandling(TLCD);
918+
TypeChecker::checkTopLevelErrorHandling(TLCD);
920919
}
921920
return false;
922921
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,8 @@ void TypeChecker::checkDeclAttributes(Decl *D) {
10581058
/// as one of the following: `dynamicallyCall(withArguments:)` or
10591059
/// `dynamicallyCall(withKeywordArguments:)`.
10601060
bool swift::isValidDynamicCallableMethod(FuncDecl *decl, DeclContext *DC,
1061-
TypeChecker &TC,
10621061
bool hasKeywordArguments) {
1062+
auto &ctx = decl->getASTContext();
10631063
// There are two cases to check.
10641064
// 1. `dynamicallyCall(withArguments:)`.
10651065
// In this case, the method is valid if the argument has type `A` where
@@ -1081,43 +1081,41 @@ bool swift::isValidDynamicCallableMethod(FuncDecl *decl, DeclContext *DC,
10811081
// `ExpressibleByArrayLiteral`.
10821082
if (!hasKeywordArguments) {
10831083
auto arrayLitProto =
1084-
TC.Context.getProtocol(KnownProtocolKind::ExpressibleByArrayLiteral);
1084+
ctx.getProtocol(KnownProtocolKind::ExpressibleByArrayLiteral);
10851085
return TypeChecker::conformsToProtocol(argType, arrayLitProto, DC,
10861086
ConformanceCheckOptions()).hasValue();
10871087
}
10881088
// If keyword arguments, check that argument type conforms to
10891089
// `ExpressibleByDictionaryLiteral` and that the `Key` associated type
10901090
// conforms to `ExpressibleByStringLiteral`.
10911091
auto stringLitProtocol =
1092-
TC.Context.getProtocol(KnownProtocolKind::ExpressibleByStringLiteral);
1092+
ctx.getProtocol(KnownProtocolKind::ExpressibleByStringLiteral);
10931093
auto dictLitProto =
1094-
TC.Context.getProtocol(KnownProtocolKind::ExpressibleByDictionaryLiteral);
1094+
ctx.getProtocol(KnownProtocolKind::ExpressibleByDictionaryLiteral);
10951095
auto dictConf = TypeChecker::conformsToProtocol(argType, dictLitProto, DC,
10961096
ConformanceCheckOptions());
10971097
if (!dictConf) return false;
1098-
auto keyType = dictConf.getValue().getTypeWitnessByName(
1099-
argType, TC.Context.Id_Key);
1098+
auto keyType = dictConf.getValue().getTypeWitnessByName(argType, ctx.Id_Key);
11001099
return TypeChecker::conformsToProtocol(keyType, stringLitProtocol, DC,
11011100
ConformanceCheckOptions()).hasValue();
11021101
}
11031102

11041103
/// Returns true if the given nominal type has a valid implementation of a
11051104
/// @dynamicCallable attribute requirement with the given argument name.
1106-
static bool hasValidDynamicCallableMethod(TypeChecker &TC,
1107-
NominalTypeDecl *decl,
1105+
static bool hasValidDynamicCallableMethod(NominalTypeDecl *decl,
11081106
Identifier argumentName,
11091107
bool hasKeywordArgs) {
1108+
auto &ctx = decl->getASTContext();
11101109
auto declType = decl->getDeclaredType();
1111-
auto methodName = DeclName(TC.Context,
1112-
DeclBaseName(TC.Context.Id_dynamicallyCall),
1110+
auto methodName = DeclName(ctx, DeclBaseName(ctx.Id_dynamicallyCall),
11131111
{ argumentName });
1114-
auto candidates = TC.lookupMember(decl, declType, methodName);
1112+
auto candidates = TypeChecker::lookupMember(decl, declType, methodName);
11151113
if (candidates.empty()) return false;
11161114

11171115
// Filter valid candidates.
11181116
candidates.filter([&](LookupResultEntry entry, bool isOuter) {
11191117
auto candidate = cast<FuncDecl>(entry.getValueDecl());
1120-
return isValidDynamicCallableMethod(candidate, decl, TC, hasKeywordArgs);
1118+
return isValidDynamicCallableMethod(candidate, decl, hasKeywordArgs);
11211119
});
11221120

11231121
// If there are no valid candidates, return false.
@@ -1133,10 +1131,10 @@ visitDynamicCallableAttr(DynamicCallableAttr *attr) {
11331131

11341132
bool hasValidMethod = false;
11351133
hasValidMethod |=
1136-
hasValidDynamicCallableMethod(TC, decl, TC.Context.Id_withArguments,
1134+
hasValidDynamicCallableMethod(decl, TC.Context.Id_withArguments,
11371135
/*hasKeywordArgs*/ false);
11381136
hasValidMethod |=
1139-
hasValidDynamicCallableMethod(TC, decl, TC.Context.Id_withKeywordArguments,
1137+
hasValidDynamicCallableMethod(decl, TC.Context.Id_withKeywordArguments,
11401138
/*hasKeywordArgs*/ true);
11411139
if (!hasValidMethod) {
11421140
TC.diagnose(attr->getLocation(), diag::invalid_dynamic_callable_type, type);
@@ -1167,48 +1165,48 @@ static bool hasSingleNonVariadicParam(SubscriptDecl *decl,
11671165
/// The method is given to be defined as `subscript(dynamicMember:)`.
11681166
bool swift::isValidDynamicMemberLookupSubscript(SubscriptDecl *decl,
11691167
DeclContext *DC,
1170-
TypeChecker &TC,
11711168
bool ignoreLabel) {
11721169
// It could be
11731170
// - `subscript(dynamicMember: {Writable}KeyPath<...>)`; or
11741171
// - `subscript(dynamicMember: String*)`
1175-
return isValidKeyPathDynamicMemberLookup(decl, TC, ignoreLabel) ||
1176-
isValidStringDynamicMemberLookup(decl, DC, TC, ignoreLabel);
1172+
return isValidKeyPathDynamicMemberLookup(decl, ignoreLabel) ||
1173+
isValidStringDynamicMemberLookup(decl, DC, ignoreLabel);
11771174
}
11781175

11791176
bool swift::isValidStringDynamicMemberLookup(SubscriptDecl *decl,
1180-
DeclContext *DC, TypeChecker &TC,
1177+
DeclContext *DC,
11811178
bool ignoreLabel) {
1179+
auto &ctx = decl->getASTContext();
11821180
// There are two requirements:
11831181
// - The subscript method has exactly one, non-variadic parameter.
11841182
// - The parameter type conforms to `ExpressibleByStringLiteral`.
1185-
if (!hasSingleNonVariadicParam(decl, TC.Context.Id_dynamicMember,
1183+
if (!hasSingleNonVariadicParam(decl, ctx.Id_dynamicMember,
11861184
ignoreLabel))
11871185
return false;
11881186

11891187
const auto *param = decl->getIndices()->get(0);
11901188
auto paramType = param->getType();
11911189

11921190
auto stringLitProto =
1193-
TC.Context.getProtocol(KnownProtocolKind::ExpressibleByStringLiteral);
1191+
ctx.getProtocol(KnownProtocolKind::ExpressibleByStringLiteral);
11941192

11951193
// If this is `subscript(dynamicMember: String*)`
11961194
return bool(TypeChecker::conformsToProtocol(paramType, stringLitProto, DC,
11971195
ConformanceCheckOptions()));
11981196
}
11991197

12001198
bool swift::isValidKeyPathDynamicMemberLookup(SubscriptDecl *decl,
1201-
TypeChecker &TC,
12021199
bool ignoreLabel) {
1203-
if (!hasSingleNonVariadicParam(decl, TC.Context.Id_dynamicMember,
1200+
auto &ctx = decl->getASTContext();
1201+
if (!hasSingleNonVariadicParam(decl, ctx.Id_dynamicMember,
12041202
ignoreLabel))
12051203
return false;
12061204

12071205
const auto *param = decl->getIndices()->get(0);
12081206
if (auto NTD = param->getType()->getAnyNominal()) {
1209-
return NTD == TC.Context.getKeyPathDecl() ||
1210-
NTD == TC.Context.getWritableKeyPathDecl() ||
1211-
NTD == TC.Context.getReferenceWritableKeyPathDecl();
1207+
return NTD == ctx.getKeyPathDecl() ||
1208+
NTD == ctx.getWritableKeyPathDecl() ||
1209+
NTD == ctx.getReferenceWritableKeyPathDecl();
12121210
}
12131211
return false;
12141212
}
@@ -1245,7 +1243,7 @@ visitDynamicMemberLookupAttr(DynamicMemberLookupAttr *attr) {
12451243
auto cand = cast<SubscriptDecl>(entry.getValueDecl());
12461244
// FIXME(InterfaceTypeRequest): Remove this.
12471245
(void)cand->getInterfaceType();
1248-
return isValidDynamicMemberLookupSubscript(cand, decl, TC);
1246+
return isValidDynamicMemberLookupSubscript(cand, decl);
12491247
});
12501248

12511249
if (candidates.empty()) {
@@ -1269,7 +1267,7 @@ visitDynamicMemberLookupAttr(DynamicMemberLookupAttr *attr) {
12691267
auto cand = cast<SubscriptDecl>(entry.getValueDecl());
12701268
// FIXME(InterfaceTypeRequest): Remove this.
12711269
(void)cand->getInterfaceType();
1272-
return isValidDynamicMemberLookupSubscript(cand, decl, TC,
1270+
return isValidDynamicMemberLookupSubscript(cand, decl,
12731271
/*ignoreLabel*/ true);
12741272
});
12751273

lib/Sema/TypeCheckDecl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
16431643
Expr *exprToCheck = prevValue;
16441644
if (TC->typeCheckExpression(exprToCheck, ED, TypeLoc::withoutLoc(rawTy),
16451645
CTP_EnumCaseRawValue)) {
1646-
TC->checkEnumElementErrorHandling(elt, exprToCheck);
1646+
TypeChecker::checkEnumElementErrorHandling(elt, exprToCheck);
16471647
}
16481648
}
16491649

@@ -1999,7 +1999,7 @@ static void checkDefaultArguments(TypeChecker &tc, ParameterList *params) {
19991999
param->setDefaultValue(e);
20002000
}
20012001

2002-
tc.checkInitializerErrorHandling(initContext, e);
2002+
TypeChecker::checkInitializerErrorHandling(initContext, e);
20032003

20042004
// Walk the checked initializer and contextualize any closures
20052005
// we saw there.
@@ -2564,7 +2564,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
25642564
PBD->getInitContext(i));
25652565
if (initContext) {
25662566
// Check safety of error-handling in the declaration, too.
2567-
TC.checkInitializerErrorHandling(initContext, init);
2567+
TypeChecker::checkInitializerErrorHandling(initContext, init);
25682568
(void) TC.contextualizeInitializer(initContext, init);
25692569
}
25702570
}
@@ -2783,7 +2783,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
27832783
// Force lowering of stored properties.
27842784
(void) SD->getStoredProperties();
27852785

2786-
TC.addImplicitConstructors(SD);
2786+
TypeChecker::addImplicitConstructors(SD);
27872787

27882788
for (Decl *Member : SD->getMembers())
27892789
visit(Member);
@@ -4408,7 +4408,7 @@ EmittedMembersRequest::evaluate(Evaluator &evaluator,
44084408

44094409
// We need to add implicit initializers because they
44104410
// affect vtable layout.
4411-
TC.addImplicitConstructors(CD);
4411+
TypeChecker::addImplicitConstructors(CD);
44124412

44134413
auto forceConformance = [&](ProtocolDecl *protocol) {
44144414
if (auto ref = TypeChecker::conformsToProtocol(

0 commit comments

Comments
 (0)