Skip to content

Commit c85deb1

Browse files
committed
[ConstraintSystem] NFC: Refactor areConservativelyCompatibleArgumentLabels to accept arguments directly
Since `areConservativelyCompatibleArgumentLabels` is only used by `simplifyAppliedOverloads` now, it's easy to pass arguments directly instead of trying to form them from list of labels.
1 parent d3205b2 commit c85deb1

File tree

2 files changed

+34
-50
lines changed

2 files changed

+34
-50
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -110,36 +110,31 @@ bool constraints::doesMemberRefApplyCurriedSelf(Type baseTy,
110110
// curried self.
111111
if (decl->isInstanceMember()) {
112112
assert(baseTy);
113-
if (isa<AbstractFunctionDecl>(decl) && baseTy->is<AnyMetatypeType>())
113+
if (isa<AbstractFunctionDecl>(decl) &&
114+
baseTy->getRValueType()->is<AnyMetatypeType>())
114115
return false;
115116
}
116117

117118
// Otherwise the reference applies self.
118119
return true;
119120
}
120121

121-
bool constraints::areConservativelyCompatibleArgumentLabels(
122-
OverloadChoice choice,
123-
ArrayRef<Identifier> labels,
124-
bool hasTrailingClosure) {
122+
static bool
123+
areConservativelyCompatibleArgumentLabels(OverloadChoice choice,
124+
ArrayRef<FunctionType::Param> args,
125+
bool hasTrailingClosure) {
125126
ValueDecl *decl = nullptr;
126-
Type baseType;
127127
switch (choice.getKind()) {
128128
case OverloadChoiceKind::Decl:
129129
case OverloadChoiceKind::DeclViaBridge:
130130
case OverloadChoiceKind::DeclViaDynamic:
131131
case OverloadChoiceKind::DeclViaUnwrappedOptional:
132132
decl = choice.getDecl();
133-
baseType = choice.getBaseType();
134-
if (baseType)
135-
baseType = baseType->getRValueType();
136133
break;
137134

138-
case OverloadChoiceKind::KeyPathApplication:
139-
// Key path applications are written as if subscript[keyPath:].
140-
return !hasTrailingClosure && labels.size() == 1 && labels[0].is("keyPath");
141-
142135
case OverloadChoiceKind::BaseType:
136+
// KeyPath application is not filtered in `performMemberLookup`.
137+
case OverloadChoiceKind::KeyPathApplication:
143138
case OverloadChoiceKind::DynamicMemberLookup:
144139
case OverloadChoiceKind::KeyPathDynamicMemberLookup:
145140
case OverloadChoiceKind::TupleIndex:
@@ -149,17 +144,13 @@ bool constraints::areConservativelyCompatibleArgumentLabels(
149144
if (!decl->hasParameterList())
150145
return true;
151146

152-
SmallVector<AnyFunctionType::Param, 8> argInfos;
153-
for (auto argLabel : labels) {
154-
argInfos.push_back(AnyFunctionType::Param(Type(), argLabel, {}));
155-
}
156-
157147
// This is a member lookup, which generally means that the call arguments
158148
// (if we have any) will apply to the second level of parameters, with
159149
// the member lookup applying the curried self at the first level. But there
160150
// are cases where we can get an unapplied declaration reference back.
161151
auto hasAppliedSelf =
162-
decl->hasCurriedSelf() && doesMemberRefApplyCurriedSelf(baseType, decl);
152+
decl->hasCurriedSelf() &&
153+
doesMemberRefApplyCurriedSelf(choice.getBaseType(), decl);
163154

164155
auto *fnType = decl->getInterfaceType()->castTo<AnyFunctionType>();
165156
if (hasAppliedSelf) {
@@ -173,10 +164,9 @@ bool constraints::areConservativelyCompatibleArgumentLabels(
173164
MatchCallArgumentListener listener;
174165
SmallVector<ParamBinding, 8> unusedParamBindings;
175166

176-
return !matchCallArguments(argInfos, params, paramInfo,
177-
hasTrailingClosure,
178-
/*allow fixes*/ false,
179-
listener, unusedParamBindings);
167+
return !matchCallArguments(args, params, paramInfo, hasTrailingClosure,
168+
/*allow fixes*/ false, listener,
169+
unusedParamBindings);
180170
}
181171

182172
Expr *constraints::getArgumentLabelTargetExpr(Expr *fn) {
@@ -5838,7 +5828,6 @@ ConstraintSystem::simplifyKeyPathApplicationConstraint(
58385828
Type ConstraintSystem::simplifyAppliedOverloads(
58395829
TypeVariableType *fnTypeVar,
58405830
const FunctionType *argFnType,
5841-
Optional<ArgumentLabelState> argumentLabels,
58425831
ConstraintLocatorBuilder locator) {
58435832
Type fnType(fnTypeVar);
58445833

@@ -5879,6 +5868,8 @@ Type ConstraintSystem::simplifyAppliedOverloads(
58795868
return markFailure();
58805869
};
58815870

5871+
auto argumentInfo = getArgumentLabels(*this, locator);
5872+
58825873
// Consider each of the constraints in the disjunction.
58835874
retry_after_fail:
58845875
bool hasUnhandledConstraints = false;
@@ -5892,12 +5883,18 @@ Type ConstraintSystem::simplifyAppliedOverloads(
58925883

58935884
// Determine whether the argument labels we have conflict with those of
58945885
// this overload choice.
5895-
if (argumentLabels &&
5896-
!areConservativelyCompatibleArgumentLabels(
5897-
choice, argumentLabels->Labels,
5898-
argumentLabels->HasTrailingClosure)) {
5899-
labelMismatch = true;
5900-
return false;
5886+
if (argumentInfo) {
5887+
auto args = argFnType->getParams();
5888+
5889+
SmallVector<FunctionType::Param, 8> argsWithLabels;
5890+
argsWithLabels.append(args.begin(), args.end());
5891+
FunctionType::relabelParams(argsWithLabels, argumentInfo->Labels);
5892+
5893+
if (!areConservativelyCompatibleArgumentLabels(
5894+
choice, argsWithLabels, argumentInfo->HasTrailingClosure)) {
5895+
labelMismatch = true;
5896+
return false;
5897+
}
59015898
}
59025899

59035900
// Determine the type that this choice will have.
@@ -5924,7 +5921,7 @@ Type ConstraintSystem::simplifyAppliedOverloads(
59245921
switch (filterResult) {
59255922
case SolutionKind::Error:
59265923
if (labelMismatch && shouldAttemptFixes()) {
5927-
argumentLabels = None;
5924+
argumentInfo.reset();
59285925
goto retry_after_fail;
59295926
}
59305927

@@ -6040,9 +6037,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
60406037
// If the right-hand side is a type variable, try to simplify the overload
60416038
// set.
60426039
if (auto typeVar = desugar2->getAs<TypeVariableType>()) {
6043-
auto argumentLabels = getArgumentLabels(*this, locator);
6044-
Type newType2 =
6045-
simplifyAppliedOverloads(typeVar, func1, argumentLabels, locator);
6040+
Type newType2 = simplifyAppliedOverloads(typeVar, func1, locator);
60466041
if (!newType2)
60476042
return SolutionKind::Error;
60486043

lib/Sema/ConstraintSystem.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,15 +2594,11 @@ class ConstraintSystem {
25942594
/// (as the function parameters) and the expected result type of the
25952595
/// call.
25962596
///
2597-
/// \param argumentLabels The argument labels provided at the call site,
2598-
/// if known.
2599-
///
26002597
/// \returns \c fnType, or some simplified form of it if this function
26012598
/// was able to find a single overload or derive some common structure
26022599
/// among the overloads.
26032600
Type simplifyAppliedOverloads(TypeVariableType *fnTypeVar,
26042601
const FunctionType *argFnType,
2605-
Optional<ArgumentLabelState> argumentLabels,
26062602
ConstraintLocatorBuilder locator);
26072603

26082604
/// Retrieve the type that will be used when matching the given overload.
@@ -3861,21 +3857,14 @@ matchCallArguments(ConstraintSystem &cs,
38613857
/// subscript, etc.), find the underlying target expression.
38623858
Expr *getArgumentLabelTargetExpr(Expr *fn);
38633859

3864-
/// Returns true if a reference to a member on a given base type will apply its
3865-
/// curried self parameter, assuming it has one.
3860+
/// Returns true if a reference to a member on a given base type will apply
3861+
/// its curried self parameter, assuming it has one.
38663862
///
3867-
/// This is true for most member references, however isn't true for things like
3868-
/// an instance member being referenced on a metatype, where the curried self
3869-
/// parameter remains unapplied.
3863+
/// This is true for most member references, however isn't true for things
3864+
/// like an instance member being referenced on a metatype, where the
3865+
/// curried self parameter remains unapplied.
38703866
bool doesMemberRefApplyCurriedSelf(Type baseTy, const ValueDecl *decl);
38713867

3872-
/// Attempt to prove that arguments with the given labels at the
3873-
/// given parameter depth cannot be used with the given value.
3874-
/// If this cannot be proven, conservatively returns true.
3875-
bool areConservativelyCompatibleArgumentLabels(OverloadChoice choice,
3876-
ArrayRef<Identifier> labels,
3877-
bool hasTrailingClosure);
3878-
38793868
/// Simplify the given locator by zeroing in on the most specific
38803869
/// subexpression described by the locator.
38813870
///

0 commit comments

Comments
 (0)