Skip to content

Commit f42158b

Browse files
authored
Revert "[Sema] ban multi-arguments to tuple coercion" (#3922)
It breaks cases where there really is a single unlabeled argument of tuple type, like this: let pairs = [(1, "A"), (2, "B")] print(pairs.map { $0.0 })
1 parent d709ebe commit f42158b

36 files changed

+77
-70
lines changed

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6417,7 +6417,7 @@ namespace {
64176417
// Coerce the pattern, in case we resolved something.
64186418
auto fnType = closure->getType()->castTo<FunctionType>();
64196419
auto *params = closure->getParameters();
6420-
if (tc.coerceParameterListToType(params, closure, fnType))
6420+
if (tc.coerceParameterListToType(params, closure, fnType->getInput()))
64216421
return { false, nullptr };
64226422

64236423
// If this is a single-expression closure, convert the expression

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5411,7 +5411,7 @@ bool FailureDiagnosis::visitClosureExpr(ClosureExpr *CE) {
54115411
return true;
54125412
}
54135413

5414-
if (CS->TC.coerceParameterListToType(params, CE, fnType))
5414+
if (CS->TC.coerceParameterListToType(params, CE, inferredArgType))
54155415
return true;
54165416

54175417
expectedResultType = fnType->getResult();

lib/Sema/TypeCheckPattern.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,9 +1537,8 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
15371537
/// TODO: These diagnostics should be a lot better now that we know this is
15381538
/// all specific to closures.
15391539
///
1540-
bool TypeChecker::coerceParameterListToType(ParameterList *P, ClosureExpr *CE,
1541-
AnyFunctionType *FN) {
1542-
Type paramListType = FN->getInput();
1540+
bool TypeChecker::coerceParameterListToType(ParameterList *P, DeclContext *DC,
1541+
Type paramListType) {
15431542
bool hadError = paramListType->is<ErrorType>();
15441543

15451544
// Sometimes a scalar type gets applied to a single-argument parameter list.
@@ -1548,7 +1547,7 @@ bool TypeChecker::coerceParameterListToType(ParameterList *P, ClosureExpr *CE,
15481547

15491548
// Check that the type, if explicitly spelled, is ok.
15501549
if (param->getTypeLoc().getTypeRepr()) {
1551-
hadError |= validateParameterType(param, CE, TypeResolutionOptions(),
1550+
hadError |= validateParameterType(param, DC, TypeResolutionOptions(),
15521551
nullptr, *this);
15531552

15541553
// Now that we've type checked the explicit argument type, see if it
@@ -1590,10 +1589,11 @@ bool TypeChecker::coerceParameterListToType(ParameterList *P, ClosureExpr *CE,
15901589
// The number of elements must match exactly.
15911590
// TODO: incomplete tuple patterns, with some syntax.
15921591
if (!hadError && tupleTy->getNumElements() != P->size()) {
1593-
auto fnType = FunctionType::get(paramListType->getDesugaredType(),
1594-
FN->getResult());
1595-
diagnose(P->getStartLoc(), diag::closure_argument_list_tuple,
1596-
fnType, tupleTy->getNumElements(), P->size());
1592+
if (P->size() == 1)
1593+
return handleParameter(P->get(0), paramListType);
1594+
1595+
diagnose(P->getStartLoc(), diag::tuple_pattern_length_mismatch,
1596+
paramListType);
15971597
hadError = true;
15981598
}
15991599

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,8 +1386,7 @@ class TypeChecker final : public LazyResolver {
13861386
/// contextual type.
13871387
///
13881388
/// \returns true if an error occurred, false otherwise.
1389-
bool coerceParameterListToType(ParameterList *P, ClosureExpr *CE,
1390-
AnyFunctionType *FN);
1389+
bool coerceParameterListToType(ParameterList *P, DeclContext *dc, Type type);
13911390

13921391

13931392
/// Type-check an initialized variable pattern declaration.

stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ self.test("\(testNamePrefix)._preprocessingPass/semantics") {
18071807
let s = makeWrappedSequence(test.sequence.map(OpaqueValue.init))
18081808
var wasInvoked = false
18091809
let result = s._preprocessingPass {
1810-
() -> OpaqueValue<Int> in
1810+
(sequence) -> OpaqueValue<Int> in
18111811
wasInvoked = true
18121812

18131813
expectEqualSequence(
@@ -1830,7 +1830,7 @@ self.test("\(testNamePrefix)._preprocessingPass/semantics") {
18301830
var result: OpaqueValue<Int>? = nil
18311831
do {
18321832
result = try s._preprocessingPass {
1833-
() -> OpaqueValue<Int> in
1833+
(sequence) -> OpaqueValue<Int> in
18341834
wasInvoked = true
18351835
throw TestError.error2
18361836
}

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,7 @@ public func expectEqualsUnordered<
23872387
let x: [(T, T)] =
23882388
expected.sorted(by: comparePairLess)
23892389
let y: [(T, T)] =
2390-
actual.map { ($0, $1) }
2390+
actual.map { ($0.0, $0.1) }
23912391
.sorted(by: comparePairLess)
23922392

23932393
func comparePairEquals(_ lhs: (T, T), rhs: (key: T, value: T)) -> Bool {

stdlib/private/StdlibUnittest/TypeIndexed.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public func expectEqual<V: Comparable>(
105105
file: String = #file, line: UInt = #line
106106
) {
107107
expectEqualsUnordered(
108-
expected.map { (key: TypeIdentifier($0), value: $1) },
108+
expected.map { (key: TypeIdentifier($0.0), value: $0.1) },
109109
actual.byType,
110110
message(), stackTrace: stackTrace) { $0 <=> $1 }
111111
}

stdlib/public/SDK/AppKit/AppKit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ extension NSView : _DefaultCustomPlaygroundQuickLookable {
5454
public extension NSGradient {
5555
convenience init?(colorsAndLocations objects: (NSColor, CGFloat)...) {
5656
self.init(
57-
colors: objects.map { c, _ in c },
58-
atLocations: objects.map { _, l in l },
57+
colors: objects.map { $0.0 },
58+
atLocations: objects.map { $0.1 },
5959
colorSpace: NSColorSpace.genericRGB())
6060
}
6161
}

stdlib/public/SDK/Foundation/Foundation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,8 @@ extension NSDictionary : ExpressibleByDictionaryLiteral {
570570
dictionaryLiteral elements: (NSCopying, AnyObject)...
571571
) {
572572
self.init(
573-
objects: elements.map { _, v in v },
574-
forKeys: elements.map { k, _ in k },
573+
objects: elements.map { $0.1 },
574+
forKeys: elements.map { $0.0 },
575575
count: elements.count)
576576
}
577577
}

stdlib/public/SDK/WatchKit/WatchKit.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ extension WKInterfaceController {
2929
withNamesAndContexts namesAndContexts: [(name: String, context: AnyObject)]
3030
) {
3131
WKInterfaceController.reloadRootControllers(
32-
withNames: namesAndContexts.map { name, _ in name },
33-
contexts: namesAndContexts.map { _, context in context })
32+
withNames: namesAndContexts.map { $0.name },
33+
contexts: namesAndContexts.map { $0.context })
3434
}
3535

3636
@available(*, deprecated,
@@ -47,8 +47,8 @@ extension WKInterfaceController {
4747
withNamesAndContexts namesAndContexts: [(name: String, context: AnyObject)]
4848
) {
4949
self.presentController(
50-
withNames: namesAndContexts.map { name, _ in name },
51-
contexts: namesAndContexts.map { _, context in context })
50+
withNames: namesAndContexts.map { $0.name },
51+
contexts: namesAndContexts.map { $0.context })
5252
}
5353
}
5454

0 commit comments

Comments
 (0)