[ConstrainySystem] Accept trailing closure if multiple...

defaulted parameters are after last function parameter
This commit is contained in:
omochimetaru
2020-02-15 15:17:27 +09:00
parent 5a8e3ea78a
commit 252e82ff7b
2 changed files with 18 additions and 10 deletions

View File

@@ -430,24 +430,24 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
// If we have a trailing closure, it maps to the last parameter.
if (hasTrailingClosure && numParams > 0) {
unsigned lastParamIdx = numParams - 1;
bool lastAcceptsTrailingClosure =
acceptsTrailingClosure(params[lastParamIdx]);
// If the last parameter is defaulted, this might be
// an attempt to use a trailing closure with previous
// parameter that accepts a function type e.g.
//
// func foo(_: () -> Int, _ x: Int = 0) {}
// foo { 42 }
if (!lastAcceptsTrailingClosure && numParams > 1 &&
paramInfo.hasDefaultArgument(lastParamIdx)) {
auto paramType = params[lastParamIdx - 1].getPlainType();
// If the parameter before defaulted last accepts.
if (paramType->is<AnyFunctionType>()) {
bool lastAcceptsTrailingClosure = false;
unsigned lastParamIdx = numParams - 1;
for (unsigned i : indices(params)) {
unsigned idx = numParams - 1 - i;
if (acceptsTrailingClosure(params[idx])) {
lastAcceptsTrailingClosure = true;
lastParamIdx -= 1;
lastParamIdx = idx;
break;
}
if (paramInfo.hasDefaultArgument(idx))
continue;
break;
}
bool isExtraClosure = false;