Skip to content

Commit adf83fe

Browse files
committed
[ConstraintSystem] Fix single tuple vs. N arguments in function conversions
1 parent f0157b0 commit adf83fe

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ static bool fixMissingArguments(ConstraintSystem &cs, Expr *anchor,
14351435
// (which might be anonymous), it's most likely used as a
14361436
// tuple e.g. `$0.0`.
14371437
Optional<TypeBase *> argumentTuple;
1438-
if (isa<ClosureExpr>(anchor) && isSingleTupleParam(ctx, args)) {
1438+
if (isSingleTupleParam(ctx, args)) {
14391439
auto argType = args.back().getPlainType();
14401440
// Let's unpack argument tuple into N arguments, this corresponds
14411441
// to something like `foo { (bar: (Int, Int)) in }` where `foo`
@@ -1456,24 +1456,26 @@ static bool fixMissingArguments(ConstraintSystem &cs, Expr *anchor,
14561456
};
14571457

14581458
// Something like `foo { x in }` or `foo { $0 }`
1459-
anchor->forEachChildExpr([&](Expr *expr) -> Expr * {
1460-
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(expr)) {
1461-
if (!isParam(UDE->getBase()))
1462-
return expr;
1463-
1464-
auto name = UDE->getName().getBaseIdentifier();
1465-
unsigned index = 0;
1466-
if (!name.str().getAsInteger(10, index) ||
1467-
llvm::any_of(params, [&](const AnyFunctionType::Param &param) {
1468-
return param.getLabel() == name;
1469-
})) {
1470-
argumentTuple.emplace(typeVar);
1471-
args.pop_back();
1472-
return nullptr;
1459+
if (isa<ClosureExpr>(anchor)) {
1460+
anchor->forEachChildExpr([&](Expr *expr) -> Expr * {
1461+
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(expr)) {
1462+
if (!isParam(UDE->getBase()))
1463+
return expr;
1464+
1465+
auto name = UDE->getName().getBaseIdentifier();
1466+
unsigned index = 0;
1467+
if (!name.str().getAsInteger(10, index) ||
1468+
llvm::any_of(params, [&](const AnyFunctionType::Param &param) {
1469+
return param.getLabel() == name;
1470+
})) {
1471+
argumentTuple.emplace(typeVar);
1472+
args.pop_back();
1473+
return nullptr;
1474+
}
14731475
}
1474-
}
1475-
return expr;
1476-
});
1476+
return expr;
1477+
});
1478+
}
14771479
}
14781480
}
14791481

test/Constraints/tuple_arguments.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,14 +1657,14 @@ public extension Optional {
16571657
// https://bugs.swift.org/browse/SR-6837
16581658

16591659
// FIXME: Can't overlaod local functions so these must be top-level
1660-
func takePairOverload(_ pair: (Int, Int?)) {} // expected-note {{found this candidate}}
1661-
func takePairOverload(_: () -> ()) {} // expected-note {{found this candidate}}
1660+
func takePairOverload(_ pair: (Int, Int?)) {}
1661+
func takePairOverload(_: () -> ()) {}
16621662

16631663
do {
16641664
func takeFn(fn: (_ i: Int, _ j: Int?) -> ()) {}
16651665
func takePair(_ pair: (Int, Int?)) {}
16661666
takeFn(fn: takePair) // expected-error {{cannot convert value of type '((Int, Int?)) -> ()' to expected argument type '(Int, Int?) -> ()'}}
1667-
takeFn(fn: takePairOverload) // expected-error {{ambiguous reference to member 'takePairOverload'}}
1667+
takeFn(fn: takePairOverload) // expected-error {{cannot convert value of type '((Int, Int?)) -> ()' to expected argument type '(Int, Int?) -> ()'}}
16681668
takeFn(fn: { (pair: (Int, Int?)) in } ) // Disallow for -swift-version 4 and later
16691669
// expected-error@-1 {{contextual closure type '(Int, Int?) -> ()' expects 2 arguments, but 1 was used in closure body}}
16701670
takeFn { (pair: (Int, Int?)) in } // Disallow for -swift-version 4 and later

0 commit comments

Comments
 (0)