Skip to content

Commit 6f0f16e

Browse files
authored
[CS] Tighten up assertions for getCalleeDeclAndArgs (swiftlang#29077)
[CS] Tighten up assertions for getCalleeDeclAndArgs
2 parents 033ddff + 1a8477a commit 6f0f16e

File tree

2 files changed

+26
-76
lines changed

2 files changed

+26
-76
lines changed

lib/AST/Type.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -848,19 +848,14 @@ ParameterListInfo::ParameterListInfo(
848848
return;
849849
}
850850

851-
switch (params.size()) {
852-
case 0:
851+
if (params.empty())
853852
return;
854853

855-
default:
856-
// Arguments and parameters are not guaranteed to always line-up
857-
// perfectly, e.g. failure diagnostics tries to match argument type
858-
// to different "candidate" parameters.
859-
if (params.size() != paramList->size())
860-
return;
861-
862-
break;
863-
}
854+
// Arguments and parameters are not guaranteed to always line-up
855+
// perfectly, e.g. failure diagnostics tries to match argument type
856+
// to different "candidate" parameters.
857+
if (params.size() != paramList->size())
858+
return;
864859

865860
// Note which parameters have default arguments and/or function builders.
866861
for (auto i : range(0, params.size())) {

lib/Sema/CSSimplify.cpp

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -764,58 +764,6 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
764764
return listener.relabelArguments(actualArgNames);
765765
}
766766

767-
/// Find the callee declaration and uncurry level for a given call
768-
/// locator.
769-
static std::tuple<ValueDecl *, bool, ArrayRef<Identifier>, bool,
770-
ConstraintLocator *>
771-
getCalleeDeclAndArgs(ConstraintSystem &cs,
772-
ConstraintLocatorBuilder callBuilder) {
773-
auto formUnknownCallee =
774-
[]() -> std::tuple<ValueDecl *, bool, ArrayRef<Identifier>, bool,
775-
ConstraintLocator *> {
776-
return std::make_tuple(/*decl*/ nullptr, /*hasAppliedSelf*/ false,
777-
/*argLabels*/ ArrayRef<Identifier>(),
778-
/*hasTrailingClosure*/ false,
779-
/*calleeLocator*/ nullptr);
780-
};
781-
782-
auto *callLocator = cs.getConstraintLocator(callBuilder);
783-
auto *callExpr = callLocator->getAnchor();
784-
785-
// Break down the call.
786-
if (!callExpr)
787-
return formUnknownCallee();
788-
789-
// Our remaining path can only be 'ApplyArgument'.
790-
auto path = callLocator->getPath();
791-
if (!path.empty() && !path.back().is<LocatorPathElt::ApplyArgument>())
792-
return formUnknownCallee();
793-
794-
// Dig out the callee information.
795-
auto argInfo = cs.getArgumentInfo(callLocator);
796-
if (!argInfo)
797-
return formUnknownCallee();
798-
799-
auto argLabels = argInfo->Labels;
800-
auto hasTrailingClosure = argInfo->HasTrailingClosure;
801-
auto calleeLocator = cs.getCalleeLocator(callLocator);
802-
803-
// Find the overload choice corresponding to the callee locator.
804-
auto selectedOverload = cs.findSelectedOverloadFor(calleeLocator);
805-
806-
// If we didn't find any matching overloads, we're done. Just return the
807-
// argument info.
808-
if (!selectedOverload)
809-
return std::make_tuple(/*decl*/ nullptr, /*hasAppliedSelf*/ false,
810-
argLabels, hasTrailingClosure,
811-
/*calleeLocator*/ nullptr);
812-
813-
// Return the found declaration, assuming there is one.
814-
auto choice = selectedOverload->choice;
815-
return std::make_tuple(choice.getDeclOrNull(), hasAppliedSelf(cs, choice),
816-
argLabels, hasTrailingClosure, calleeLocator);
817-
}
818-
819767
class ArgumentFailureTracker : public MatchCallArgumentListener {
820768
ConstraintSystem &CS;
821769
SmallVectorImpl<AnyFunctionType::Param> &Arguments;
@@ -1002,22 +950,29 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
1002950
ArrayRef<AnyFunctionType::Param> args,
1003951
ArrayRef<AnyFunctionType::Param> params, ConstraintKind subKind,
1004952
ConstraintLocatorBuilder locator) {
1005-
// Extract the parameters.
1006-
ValueDecl *callee;
1007-
bool hasAppliedSelf;
1008-
ArrayRef<Identifier> argLabels;
1009-
bool hasTrailingClosure = false;
1010-
ConstraintLocator *calleeLocator;
1011-
std::tie(callee, hasAppliedSelf, argLabels, hasTrailingClosure,
1012-
calleeLocator) =
1013-
getCalleeDeclAndArgs(cs, locator);
1014-
1015-
ParameterListInfo paramInfo(params, callee, hasAppliedSelf);
953+
auto *loc = cs.getConstraintLocator(locator);
954+
assert(loc->isLastElement<LocatorPathElt::ApplyArgument>());
955+
956+
ValueDecl *callee = nullptr;
957+
bool appliedSelf = false;
958+
959+
// Resolve the callee for the application.
960+
auto *calleeLocator = cs.getCalleeLocator(loc);
961+
if (auto overload = cs.findSelectedOverloadFor(calleeLocator)) {
962+
callee = overload->choice.getDeclOrNull();
963+
appliedSelf = hasAppliedSelf(cs, overload->choice);
964+
}
965+
966+
ParameterListInfo paramInfo(params, callee, appliedSelf);
967+
968+
// Dig out the argument information.
969+
auto argInfo = cs.getArgumentInfo(loc);
970+
assert(argInfo);
1016971

1017972
// Apply labels to arguments.
1018973
SmallVector<AnyFunctionType::Param, 8> argsWithLabels;
1019974
argsWithLabels.append(args.begin(), args.end());
1020-
AnyFunctionType::relabelParams(argsWithLabels, argLabels);
975+
AnyFunctionType::relabelParams(argsWithLabels, argInfo->Labels);
1021976

1022977
// Special case when a single tuple argument if used
1023978
// instead of N distinct arguments e.g.:
@@ -1059,7 +1014,7 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
10591014
ArgumentFailureTracker listener(cs, argsWithLabels, params,
10601015
parameterBindings, locator);
10611016
if (constraints::matchCallArguments(
1062-
argsWithLabels, params, paramInfo, hasTrailingClosure,
1017+
argsWithLabels, params, paramInfo, argInfo->HasTrailingClosure,
10631018
cs.shouldAttemptFixes(), listener, parameterBindings))
10641019
return cs.getTypeMatchFailure(locator);
10651020

0 commit comments

Comments
 (0)