Skip to content

Commit 00c2531

Browse files
Merge pull request #36883 from LucianoPAlmeida/SR-13483-closure-args
[SR-13483][Sema] Make sure to record the correct remove args fix when repairing a tuple destructure attempt
2 parents 282cbc3 + f40b9b6 commit 00c2531

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,8 +4340,33 @@ bool ConstraintSystem::repairFailures(
43404340
// as a narrow exception to SE-0110, see `matchFunctionTypes`.
43414341
//
43424342
// But if `T.Element` didn't get resolved to `Void` we'd like
4343-
// to diagnose this as a missing argument which can't be ignored.
4343+
// to diagnose this as a missing argument which can't be ignored or
4344+
// a tuple is trying to be inferred as a tuple for destructuring but
4345+
// contextual argument does not match(in this case we remove the extra
4346+
// closure arguments).
43444347
if (arg != getTypeVariables().end()) {
4348+
if (auto argToParamElt =
4349+
path.back().getAs<LocatorPathElt::ApplyArgToParam>()) {
4350+
auto loc = getConstraintLocator(anchor, path);
4351+
auto closureAnchor =
4352+
getAsExpr<ClosureExpr>(simplifyLocatorToAnchor(loc));
4353+
if (rhs->is<TupleType>() && closureAnchor &&
4354+
closureAnchor->getParameters()->size() > 1) {
4355+
auto callee = getCalleeLocator(loc);
4356+
if (auto overload = findSelectedOverloadFor(callee)) {
4357+
auto fnType =
4358+
simplifyType(overload->openedType)->castTo<FunctionType>();
4359+
auto paramIdx = argToParamElt->getParamIdx();
4360+
auto paramType = fnType->getParams()[paramIdx].getParameterType();
4361+
if (auto paramFnType = paramType->getAs<FunctionType>()) {
4362+
conversionsOrFixes.push_back(RemoveExtraneousArguments::create(
4363+
*this, paramFnType, {}, loc));
4364+
break;
4365+
}
4366+
}
4367+
}
4368+
}
4369+
43454370
conversionsOrFixes.push_back(AddMissingArguments::create(
43464371
*this, {SynthesizedArg{0, AnyFunctionType::Param(*arg)}},
43474372
getConstraintLocator(anchor, path)));
@@ -11228,7 +11253,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1122811253
case FixKind::AllowCheckedCastCoercibleOptionalType:
1122911254
case FixKind::AllowUnsupportedRuntimeCheckedCast:
1123011255
case FixKind::AllowAlwaysSucceedCheckedCast:
11231-
case FixKind::AllowInvalidStaticMemberRefOnProtocolMetatype: {
11256+
case FixKind::AllowInvalidStaticMemberRefOnProtocolMetatype:
11257+
case FixKind::RemoveExtraneousArguments: {
1123211258
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
1123311259
}
1123411260

@@ -11377,7 +11403,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1137711403
case FixKind::AllowTypeOrInstanceMember:
1137811404
case FixKind::AllowInvalidPartialApplication:
1137911405
case FixKind::AllowInvalidInitRef:
11380-
case FixKind::RemoveExtraneousArguments:
1138111406
case FixKind::AllowClosureParameterDestructuring:
1138211407
case FixKind::AllowInaccessibleMember:
1138311408
case FixKind::AllowAnyObjectKeyPathRoot:

test/Constraints/closures.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,3 +1097,11 @@ struct R_76250381<Result, Failure: Error> {
10971097
return false
10981098
}
10991099
}
1100+
1101+
// SR-13483
1102+
(0..<10).map { x, y in }
1103+
// expected-error@-1 {{contextual closure type '(Range<Int>.Element) throws -> ()' (aka '(Int) throws -> ()') expects 1 argument, but 2 were used in closure body}}
1104+
(0..<10).map { x, y, z in }
1105+
// expected-error@-1 {{contextual closure type '(Range<Int>.Element) throws -> ()' (aka '(Int) throws -> ()') expects 1 argument, but 3 were used in closure body}}
1106+
(0..<10).map { x, y, z, w in }
1107+
// expected-error@-1 {{contextual closure type '(Range<Int>.Element) throws -> ()' (aka '(Int) throws -> ()') expects 1 argument, but 4 were used in closure body}}

0 commit comments

Comments
 (0)