Skip to content

Commit 6bca9f7

Browse files
author
ematejska
authored
Merge pull request #7102 from jrose-apple/3.1-unsplat
In Swift 3 mode, allow "tuple unsplatting" in certain cases.
2 parents d7df768 + 9d642b0 commit 6bca9f7

File tree

5 files changed

+244
-9
lines changed

5 files changed

+244
-9
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,21 @@ matchCallArguments(ConstraintSystem &cs, ConstraintKind kind,
653653
getCalleeDeclAndArgs(cs, locator, argLabelsScratch);
654654
auto params = decomposeParamType(paramType, callee, calleeLevel);
655655

656+
if (callee && cs.getASTContext().isSwiftVersion3()
657+
&& argType->is<TupleType>()) {
658+
// Hack: In Swift 3 mode, accept `foo(x, y)` for `foo((x, y))` when the
659+
// callee is a function-typed property or an enum constructor whose
660+
// argument is a single unlabeled type parameter.
661+
if (auto *prop = dyn_cast<VarDecl>(callee)) {
662+
auto *fnType = prop->getInterfaceType()->getAs<AnyFunctionType>();
663+
if (fnType && fnType->getInput()->isTypeParameter())
664+
argType = ParenType::get(cs.getASTContext(), argType);
665+
} else if (auto *enumCtor = dyn_cast<EnumElementDecl>(callee)) {
666+
if (enumCtor->getArgumentInterfaceType()->isTypeParameter())
667+
argType = ParenType::get(cs.getASTContext(), argType);
668+
}
669+
}
670+
656671
// Extract the arguments.
657672
auto args = decomposeArgType(argType, argLabels);
658673

test/Compatibility/tuple_arguments.swift

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@
1919
// but now crash. These are bugs in Swift 3 mode that should be fixed.
2020

2121
func concrete(_ x: Int) {}
22+
func concreteLabeled(x: Int) {}
2223
func concreteTwo(_ x: Int, _ y: Int) {} // expected-note 3 {{'concreteTwo' declared here}}
2324
func concreteTuple(_ x: (Int, Int)) {}
2425

2526
do {
2627
concrete(3)
2728
concrete((3))
2829

30+
concreteLabeled(x: 3)
31+
concreteLabeled(x: (3))
32+
concreteLabeled((x: 3)) // expected-error {{missing argument label 'x:' in call}}
33+
2934
concreteTwo(3, 4)
3035
concreteTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
3136

@@ -72,6 +77,7 @@ do {
7277
}
7378

7479
func generic<T>(_ x: T) {}
80+
func genericLabeled<T>(x: T) {}
7581
func genericTwo<T, U>(_ x: T, _ y: U) {} // expected-note 5 {{'genericTwo' declared here}}
7682
func genericTuple<T, U>(_ x: (T, U)) {}
7783

@@ -81,6 +87,11 @@ do {
8187
generic((3))
8288
generic((3, 4))
8389

90+
genericLabeled(x: 3)
91+
genericLabeled(x: 3, 4) // expected-error {{extra argument in call}}
92+
genericLabeled(x: (3))
93+
genericLabeled(x: (3, 4))
94+
8495
genericTwo(3, 4)
8596
genericTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
8697

@@ -250,6 +261,7 @@ do {
250261

251262
extension Concrete {
252263
func generic<T>(_ x: T) {}
264+
func genericLabeled<T>(x: T) {}
253265
func genericTwo<T, U>(_ x: T, _ y: U) {} // expected-note 5 {{'genericTwo' declared here}}
254266
func genericTuple<T, U>(_ x: (T, U)) {}
255267
}
@@ -262,6 +274,11 @@ do {
262274
s.generic((3))
263275
s.generic((3, 4))
264276

277+
s.genericLabeled(x: 3)
278+
s.genericLabeled(x: 3, 4) // expected-error {{extra argument in call}}
279+
s.genericLabeled(x: (3))
280+
s.genericLabeled(x: (3, 4))
281+
265282
s.genericTwo(3, 4)
266283
s.genericTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
267284

@@ -378,6 +395,7 @@ do {
378395

379396
extension Concrete {
380397
mutating func mutatingGeneric<T>(_ x: T) {}
398+
mutating func mutatingGenericLabeled<T>(x: T) {}
381399
mutating func mutatingGenericTwo<T, U>(_ x: T, _ y: U) {} // expected-note 5 {{'mutatingGenericTwo' declared here}}
382400
mutating func mutatingGenericTuple<T, U>(_ x: (T, U)) {}
383401
}
@@ -390,6 +408,11 @@ do {
390408
s.mutatingGeneric((3))
391409
s.mutatingGeneric((3, 4))
392410

411+
s.mutatingGenericLabeled(x: 3)
412+
s.mutatingGenericLabeled(x: 3, 4) // expected-error {{extra argument in call}}
413+
s.mutatingGenericLabeled(x: (3))
414+
s.mutatingGenericLabeled(x: (3, 4))
415+
393416
s.mutatingGenericTwo(3, 4)
394417
s.mutatingGenericTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
395418

@@ -512,12 +535,19 @@ struct InitTuple {
512535
init(_ x: (Int, Int)) {}
513536
}
514537

538+
struct InitLabeledTuple {
539+
init(x: (Int, Int)) {}
540+
}
541+
515542
do {
516543
_ = InitTwo(3, 4)
517544
_ = InitTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
518545

519546
_ = InitTuple(3, 4) // expected-error {{extra argument in call}}
520547
_ = InitTuple((3, 4))
548+
549+
_ = InitLabeledTuple(x: 3, 4) // expected-error {{extra argument in call}}
550+
_ = InitLabeledTuple(x: (3, 4))
521551
}
522552

523553
do {
@@ -556,6 +586,10 @@ struct SubscriptTuple {
556586
subscript(_ x: (Int, Int)) -> Int { get { return 0 } set { } }
557587
}
558588

589+
struct SubscriptLabeledTuple {
590+
subscript(x x: (Int, Int)) -> Int { get { return 0 } set { } }
591+
}
592+
559593
do {
560594
let s1 = SubscriptTwo()
561595
_ = s1[3, 4]
@@ -564,6 +598,10 @@ do {
564598
let s2 = SubscriptTuple()
565599
_ = s2[3, 4] // expected-error {{extra argument in call}}
566600
_ = s2[(3, 4)]
601+
602+
let s3 = SubscriptLabeledTuple()
603+
_ = s3[x: 3, 4] // expected-error {{extra argument in call}}
604+
_ = s3[x: (3, 4)]
567605
}
568606

569607
do {
@@ -601,6 +639,7 @@ do {
601639
enum Enum {
602640
case two(Int, Int) // expected-note 3 {{'two' declared here}}
603641
case tuple((Int, Int))
642+
case labeledTuple(x: (Int, Int))
604643
}
605644

606645
do {
@@ -609,6 +648,9 @@ do {
609648

610649
_ = Enum.tuple(3, 4) // expected-error {{extra argument in call}}
611650
_ = Enum.tuple((3, 4))
651+
652+
_ = Enum.labeledTuple(x: 3, 4) // expected-error {{extra argument in call}}
653+
_ = Enum.labeledTuple(x: (3, 4))
612654
}
613655

614656
do {
@@ -643,6 +685,7 @@ struct Generic<T> {}
643685

644686
extension Generic {
645687
func generic(_ x: T) {}
688+
func genericLabeled(x: T) {}
646689
func genericTwo(_ x: T, _ y: T) {} // expected-note 2 {{'genericTwo' declared here}}
647690
func genericTuple(_ x: (T, T)) {}
648691
}
@@ -653,6 +696,9 @@ do {
653696
s.generic(3.0)
654697
s.generic((3.0))
655698

699+
s.genericLabeled(x: 3.0)
700+
s.genericLabeled(x: (3.0))
701+
656702
s.genericTwo(3.0, 4.0)
657703
s.genericTwo((3.0, 4.0)) // expected-error {{missing argument for parameter #2 in call}}
658704

@@ -663,6 +709,9 @@ do {
663709

664710
sTwo.generic(3.0, 4.0) // expected-error {{extra argument in call}}
665711
sTwo.generic((3.0, 4.0))
712+
713+
sTwo.genericLabeled(x: 3.0, 4.0) // expected-error {{extra argument in call}}
714+
sTwo.genericLabeled(x: (3.0, 4.0))
666715
}
667716

668717
do {
@@ -717,6 +766,7 @@ do {
717766

718767
extension Generic {
719768
mutating func mutatingGeneric(_ x: T) {}
769+
mutating func mutatingGenericLabeled(x: T) {}
720770
mutating func mutatingGenericTwo(_ x: T, _ y: T) {} // expected-note 2 {{'mutatingGenericTwo' declared here}}
721771
mutating func mutatingGenericTuple(_ x: (T, T)) {}
722772
}
@@ -727,6 +777,9 @@ do {
727777
s.mutatingGeneric(3.0)
728778
s.mutatingGeneric((3.0))
729779

780+
s.mutatingGenericLabeled(x: 3.0)
781+
s.mutatingGenericLabeled(x: (3.0))
782+
730783
s.mutatingGenericTwo(3.0, 4.0)
731784
s.mutatingGenericTwo((3.0, 4.0)) // expected-error {{missing argument for parameter #2 in call}}
732785

@@ -737,6 +790,9 @@ do {
737790

738791
sTwo.mutatingGeneric(3.0, 4.0) // expected-error {{extra argument in call}}
739792
sTwo.mutatingGeneric((3.0, 4.0))
793+
794+
sTwo.mutatingGenericLabeled(x: 3.0, 4.0) // expected-error {{extra argument in call}}
795+
sTwo.mutatingGenericLabeled(x: (3.0, 4.0))
740796
}
741797

742798
do {
@@ -809,7 +865,7 @@ do {
809865

810866
let sTwo = Generic<(Double, Double)>()
811867

812-
sTwo.genericFunction(3.0, 4.0) // FIXME: Diagnoses in Swift 3 mode // expected-error {{extra argument in call}}
868+
sTwo.genericFunction(3.0, 4.0)
813869
sTwo.genericFunction((3.0, 4.0)) // Does not diagnose in Swift 3 mode
814870
}
815871

@@ -858,7 +914,7 @@ do {
858914

859915
var sTwo = Generic<(Double, Double)>()
860916

861-
sTwo.genericFunction(a, b) // FIXME: Diagnoses in Swift 3 mode // expected-error {{extra argument in call}}
917+
sTwo.genericFunction(a, b)
862918
sTwo.genericFunction((a, b)) // Does not diagnose in Swift 3 mode
863919
sTwo.genericFunction(d) // Does not diagnose in Swift 3 mode
864920
}
@@ -867,6 +923,10 @@ struct GenericInit<T> { // expected-note 2 {{'T' declared as parameter to type '
867923
init(_ x: T) {}
868924
}
869925

926+
struct GenericInitLabeled<T> {
927+
init(x: T) {}
928+
}
929+
870930
struct GenericInitTwo<T> {
871931
init(_ x: T, _ y: T) {} // expected-note 8 {{'init' declared here}}
872932
}
@@ -875,26 +935,42 @@ struct GenericInitTuple<T> {
875935
init(_ x: (T, T)) {}
876936
}
877937

938+
struct GenericInitLabeledTuple<T> {
939+
init(x: (T, T)) {}
940+
}
941+
878942
do {
879943
_ = GenericInit(3, 4)
880944
_ = GenericInit((3, 4))
881945

946+
_ = GenericInitLabeled(x: 3, 4) // expected-error {{extra argument in call}}
947+
_ = GenericInitLabeled(x: (3, 4))
948+
882949
_ = GenericInitTwo(3, 4)
883950
_ = GenericInitTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
884951

885952
_ = GenericInitTuple(3, 4) // expected-error {{extra argument in call}}
886953
_ = GenericInitTuple((3, 4))
954+
955+
_ = GenericInitLabeledTuple(x: 3, 4) // expected-error {{extra argument in call}}
956+
_ = GenericInitLabeledTuple(x: (3, 4))
887957
}
888958

889959
do {
890960
_ = GenericInit<(Int, Int)>(3, 4)
891961
_ = GenericInit<(Int, Int)>((3, 4)) // expected-error {{expression type 'GenericInit<(Int, Int)>' is ambiguous without more context}}
892962

963+
_ = GenericInitLabeled<(Int, Int)>(x: 3, 4) // expected-error {{extra argument in call}}
964+
_ = GenericInitLabeled<(Int, Int)>(x: (3, 4))
965+
893966
_ = GenericInitTwo<Int>(3, 4)
894967
_ = GenericInitTwo<Int>((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
895968

896969
_ = GenericInitTuple<Int>(3, 4) // expected-error {{extra argument in call}}
897970
_ = GenericInitTuple<Int>((3, 4))
971+
972+
_ = GenericInitLabeledTuple<Int>(x: 3, 4) // expected-error {{extra argument in call}}
973+
_ = GenericInitLabeledTuple<Int>(x: (3, 4))
898974
}
899975

900976
do {
@@ -973,6 +1049,10 @@ struct GenericSubscript<T> {
9731049
subscript(_ x: T) -> Int { get { return 0 } set { } }
9741050
}
9751051

1052+
struct GenericSubscriptLabeled<T> {
1053+
subscript(x x: T) -> Int { get { return 0 } set { } }
1054+
}
1055+
9761056
struct GenericSubscriptTwo<T> {
9771057
subscript(_ x: T, _ y: T) -> Int { get { return 0 } set { } } // expected-note {{'subscript' declared here}}
9781058
}
@@ -981,18 +1061,30 @@ struct GenericSubscriptTuple<T> {
9811061
subscript(_ x: (T, T)) -> Int { get { return 0 } set { } }
9821062
}
9831063

1064+
struct GenericSubscriptLabeledTuple<T> {
1065+
subscript(x x: (T, T)) -> Int { get { return 0 } set { } }
1066+
}
1067+
9841068
do {
9851069
let s1 = GenericSubscript<(Double, Double)>()
9861070
_ = s1[3.0, 4.0]
9871071
_ = s1[(3.0, 4.0)] // expected-error {{expression type 'Int' is ambiguous without more context}}
9881072

1073+
let s1a = GenericSubscriptLabeled<(Double, Double)>()
1074+
_ = s1a [x: 3.0, 4.0] // expected-error {{extra argument in call}}
1075+
_ = s1a [x: (3.0, 4.0)]
1076+
9891077
let s2 = GenericSubscriptTwo<Double>()
9901078
_ = s2[3.0, 4.0]
9911079
_ = s2[(3.0, 4.0)] // expected-error {{missing argument for parameter #2 in call}}
9921080

9931081
let s3 = GenericSubscriptTuple<Double>()
9941082
_ = s3[3.0, 4.0] // expected-error {{extra argument in call}}
9951083
_ = s3[(3.0, 4.0)]
1084+
1085+
let s3a = GenericSubscriptLabeledTuple<Double>()
1086+
_ = s3a[x: 3.0, 4.0] // expected-error {{extra argument in call}}
1087+
_ = s3a[x: (3.0, 4.0)]
9961088
}
9971089

9981090
do {
@@ -1039,6 +1131,7 @@ do {
10391131

10401132
enum GenericEnum<T> {
10411133
case one(T)
1134+
case labeled(x: T)
10421135
case two(T, T) // expected-note 8 {{'two' declared here}}
10431136
case tuple((T, T))
10441137
}
@@ -1047,6 +1140,11 @@ do {
10471140
_ = GenericEnum.one(3, 4)
10481141
_ = GenericEnum.one((3, 4))
10491142

1143+
_ = GenericEnum.labeled(x: 3, 4) // expected-error {{extra argument in call}}
1144+
_ = GenericEnum.labeled(x: (3, 4))
1145+
_ = GenericEnum.labeled(3, 4) // expected-error {{extra argument in call}}
1146+
_ = GenericEnum.labeled((3, 4)) // expected-error {{missing argument label 'x:' in call}}
1147+
10501148
_ = GenericEnum.two(3, 4)
10511149
_ = GenericEnum.two((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
10521150

@@ -1055,9 +1153,14 @@ do {
10551153
}
10561154

10571155
do {
1058-
_ = GenericEnum<(Int, Int)>.one(3, 4) // FIXME: Diagnoses in Swift 3 mode // expected-error {{extra argument in call}}
1156+
_ = GenericEnum<(Int, Int)>.one(3, 4)
10591157
_ = GenericEnum<(Int, Int)>.one((3, 4)) // Does not diagnose in Swift 3 mode
10601158

1159+
_ = GenericEnum<(Int, Int)>.labeled(x: 3, 4) // expected-error {{extra argument in call}}
1160+
_ = GenericEnum<(Int, Int)>.labeled(x: (3, 4))
1161+
_ = GenericEnum<(Int, Int)>.labeled(3, 4) // expected-error {{extra argument in call}}
1162+
_ = GenericEnum<(Int, Int)>.labeled((3, 4)) // expected-error {{missing argument label 'x:' in call}}
1163+
10611164
_ = GenericEnum<Int>.two(3, 4)
10621165
_ = GenericEnum<Int>.two((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
10631166

@@ -1143,6 +1246,7 @@ protocol Protocol {
11431246

11441247
extension Protocol {
11451248
func requirement(_ x: Element) {}
1249+
func requirementLabeled(x: Element) {}
11461250
func requirementTwo(_ x: Element, _ y: Element) {} // expected-note 2 {{'requirementTwo' declared here}}
11471251
func requirementTuple(_ x: (Element, Element)) {}
11481252
}
@@ -1157,6 +1261,9 @@ do {
11571261
s.requirement(3.0)
11581262
s.requirement((3.0))
11591263

1264+
s.requirementLabeled(x: 3.0)
1265+
s.requirementLabeled(x: (3.0))
1266+
11601267
s.requirementTwo(3.0, 4.0)
11611268
s.requirementTwo((3.0, 4.0)) // expected-error {{missing argument for parameter #2 in call}}
11621269

@@ -1167,6 +1274,9 @@ do {
11671274

11681275
sTwo.requirement(3.0, 4.0) // expected-error {{extra argument in call}}
11691276
sTwo.requirement((3.0, 4.0))
1277+
1278+
sTwo.requirementLabeled(x: 3.0, 4.0) // expected-error {{extra argument in call}}
1279+
sTwo.requirementLabeled(x: (3.0, 4.0))
11701280
}
11711281

11721282
do {

0 commit comments

Comments
 (0)