Skip to content

Commit 12a65ff

Browse files
committed
[CSSimplify] Further Restrict tuple splat behavior
`isSingleParam` used to return `true` regardless of type of the identified single parameter, but splat only makes sense if such type is a tuple or a type variable which could later be resolved to tuple. Otherwise it makes it hard to diagnose missing arguments.
1 parent cca6fea commit 12a65ff

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,9 +1139,29 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
11391139
// that expect a function that takes a single tuple (of the same
11401140
// arity).
11411141
auto isSingleParam = [&](ArrayRef<AnyFunctionType::Param> params) {
1142-
return (params.size() == 1 &&
1143-
params[0].getLabel().empty() &&
1144-
!params[0].isVariadic());
1142+
if (params.size() != 1)
1143+
return false;
1144+
1145+
const auto &param = params.front();
1146+
if (param.isVariadic() || param.isInOut())
1147+
return false;
1148+
1149+
auto paramType = param.getPlainType();
1150+
// Support following case which was allowed until 5:
1151+
// ``swift
1152+
// func bar(_: (Int, Int) -> Void) {}
1153+
// let foo: ((Int, Int)?) -> Void = { _ in }
1154+
//
1155+
// bar(foo) // Ok
1156+
// ```
1157+
if (!getASTContext().isSwiftVersionAtLeast(5))
1158+
paramType = paramType->lookThroughAllOptionalTypes();
1159+
1160+
// Parameter should have a label and be either a tuple tuple type,
1161+
// or a type variable which might later be assigned a tuple type,
1162+
// e.g. opened generic parameter.
1163+
return !param.hasLabel() &&
1164+
(paramType->is<TupleType>() || paramType->is<TypeVariableType>());
11451165
};
11461166

11471167
auto canImplodeParams = [&](ArrayRef<AnyFunctionType::Param> params) {

0 commit comments

Comments
 (0)