Skip to content

Commit be23ecc

Browse files
committed
These functions don't need a TypeChecker
1 parent 8b9cced commit be23ecc

File tree

5 files changed

+35
-43
lines changed

5 files changed

+35
-43
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/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/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/TypeChecker.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,13 +1976,12 @@ class EncodedDiagnosticMessage {
19761976
/// as one of the following: `dynamicallyCall(withArguments:)` or
19771977
/// `dynamicallyCall(withKeywordArguments:)`.
19781978
bool isValidDynamicCallableMethod(FuncDecl *decl, DeclContext *DC,
1979-
TypeChecker &TC, bool hasKeywordArguments);
1979+
bool hasKeywordArguments);
19801980

19811981
/// Returns true if the given subscript method is an valid implementation of
19821982
/// the `subscript(dynamicMember:)` requirement for @dynamicMemberLookup.
19831983
/// The method is given to be defined as `subscript(dynamicMember:)`.
19841984
bool isValidDynamicMemberLookupSubscript(SubscriptDecl *decl, DeclContext *DC,
1985-
TypeChecker &TC,
19861985
bool ignoreLabel = false);
19871986

19881987
/// Returns true if the given subscript method is an valid implementation of
@@ -1991,15 +1990,14 @@ bool isValidDynamicMemberLookupSubscript(SubscriptDecl *decl, DeclContext *DC,
19911990
/// takes a single non-variadic parameter that conforms to
19921991
/// `ExpressibleByStringLiteral` protocol.
19931992
bool isValidStringDynamicMemberLookup(SubscriptDecl *decl, DeclContext *DC,
1994-
TypeChecker &TC,
19951993
bool ignoreLabel = false);
19961994

19971995
/// Returns true if the given subscript method is an valid implementation of
19981996
/// the `subscript(dynamicMember: {Writable}KeyPath<...>)` requirement for
19991997
/// @dynamicMemberLookup.
20001998
/// The method is given to be defined as `subscript(dynamicMember:)` which
20011999
/// takes a single non-variadic parameter of `{Writable}KeyPath<T, U>` type.
2002-
bool isValidKeyPathDynamicMemberLookup(SubscriptDecl *decl, TypeChecker &TC,
2000+
bool isValidKeyPathDynamicMemberLookup(SubscriptDecl *decl,
20032001
bool ignoreLabel = false);
20042002

20052003
/// Compute the wrapped value type for the given property that has attached

0 commit comments

Comments
 (0)