Skip to content

Commit 3b1a8ac

Browse files
committed
[Type Resolution] Only allow each applied directly to a type parameter pack.
(cherry picked from commit 3de3402)
1 parent 68c464f commit 3b1a8ac

12 files changed

+51
-41
lines changed

include/swift/AST/Types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,10 @@ class alignas(1 << TypeAlignInBits) TypeBase
767767
/// include type parameters in nested positions e.g. \c X<T...>.
768768
bool isParameterPack();
769769

770+
/// Determine whether this type is directly a type parameter pack, which
771+
/// can only be a GenericTypeParamType.
772+
bool isRootParameterPack();
773+
770774
/// Determine whether this type can dynamically be an optional type.
771775
///
772776
/// \param includeExistential Whether an existential type should be considered

lib/AST/ParameterPack.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ bool TypeBase::isParameterPack() {
191191
while (auto *memberTy = t->getAs<DependentMemberType>())
192192
t = memberTy->getBase();
193193

194+
return t->isRootParameterPack();
195+
}
196+
197+
bool TypeBase::isRootParameterPack() {
198+
Type t(this);
199+
194200
return t->is<GenericTypeParamType>() &&
195201
t->castTo<GenericTypeParamType>()->isParameterPack();
196202
}

lib/Sema/TypeCheckType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4672,7 +4672,7 @@ NeverNullType TypeResolver::resolvePackElement(PackElementTypeRepr *repr,
46724672
if (packReference->hasError())
46734673
return ErrorType::get(ctx);
46744674

4675-
if (!packReference->isParameterPack()) {
4675+
if (!packReference->isRootParameterPack()) {
46764676
auto diag =
46774677
ctx.Diags.diagnose(repr->getLoc(), diag::each_non_pack, packReference);
46784678
if (auto *packIdent = dyn_cast<IdentTypeRepr>(repr->getPackType())) {

test/Constraints/pack-expansion-expressions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extension P {
5858

5959

6060
func outerArchetype<each T, U>(t: repeat each T, u: U) where repeat each T: P {
61-
let _: (repeat (each T.A, U)) = (repeat ((each t).value, u))
61+
let _: (repeat ((each T).A, U)) = (repeat ((each t).value, u))
6262
}
6363

6464
func sameElement<each T, U>(t: repeat each T, u: U) where repeat each T: P, repeat each T == U {
@@ -476,11 +476,11 @@ do {
476476

477477
// rdar://107675464 - misplaced `each` results in `type of expression is ambiguous without a type annotation`
478478
do {
479-
func test_correct_each<each T: P>(_ value: repeat each T) -> (repeat each T.A) {
479+
func test_correct_each<each T: P>(_ value: repeat each T) -> (repeat (each T).A) {
480480
return (repeat (each value).makeA()) // Ok
481481
}
482482

483-
func test_misplaced_each<each T: P>(_ value: repeat each T) -> (repeat each T.A) {
483+
func test_misplaced_each<each T: P>(_ value: repeat each T) -> (repeat (each T).A) {
484484
return (repeat each value.makeA())
485485
// expected-error@-1 {{value pack 'each T' must be referenced with 'each'}} {{25-25=(each }} {{30-30=)}}
486486
}

test/IRGen/bind_element_archetype.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public protocol Q {
88

99
public func f<T: P>(_: T) {}
1010

11-
public func foo1<each T: Q>(t: repeat each T, u: repeat each T.A) {
11+
public func foo1<each T: Q>(t: repeat each T, u: repeat (each T).A) {
1212
repeat f(each u)
1313
}
1414

test/IRGen/run_variadic_generics.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,18 +626,18 @@ entry(%intIndex : $Builtin.Word):
626626
return %t : $()
627627
}
628628

629-
sil @unwrap_from_PA : $<each T_1 : PA where repeat each T_1.A : P> (Builtin.Word) -> () {
629+
sil @unwrap_from_PA : $<each T_1 : PA where repeat (each T_1).A : P> (Builtin.Word) -> () {
630630
entry(%intIndex : $Builtin.Word):
631631
%direct_access_from_parameter_with_conformance = function_ref @direct_access_from_parameter_with_conformance : $@convention(thin) <each T_1: P> (Builtin.Word) -> ()
632-
apply %direct_access_from_parameter_with_conformance<Pack{repeat GenFwdP<each T_1.A>}>(%intIndex) : $@convention(thin) <each T_1: P> (Builtin.Word) -> ()
632+
apply %direct_access_from_parameter_with_conformance<Pack{repeat GenFwdP<(each T_1).A>}>(%intIndex) : $@convention(thin) <each T_1: P> (Builtin.Word) -> ()
633633
%t = tuple ()
634634
return %t : $()
635635
}
636636

637637
sil @extract_associatedtype_with_conformance : $<each T_1 : P> (Builtin.Word) -> () {
638638
entry(%intIndex : $Builtin.Word):
639639
%innerIndex = dynamic_pack_index %intIndex of $Pack{repeat each T_1}
640-
%token = open_pack_element %innerIndex of <each U_1 : PA where repeat each U_1.A : P> at <Pack{repeat GenAssocPA<GenFwdP<each T_1>>}>, shape $U_1, uuid "01234567-89AB-CDEF-0123-000000000005"
640+
%token = open_pack_element %innerIndex of <each U_1 : PA where repeat (each U_1).A : P> at <Pack{repeat GenAssocPA<GenFwdP<each T_1>>}>, shape $U_1, uuid "01234567-89AB-CDEF-0123-000000000005"
641641
%metatype_1 = metatype $@thick (@pack_element("01234567-89AB-CDEF-0123-000000000005") U_1).Type
642642
%printGenericType = function_ref @printGenericType : $@convention(thin) <T> (@thick T.Type) -> ()
643643
apply %printGenericType<(@pack_element("01234567-89AB-CDEF-0123-000000000005") U_1)>(%metatype_1) : $@convention(thin) <T> (@thick T.Type) -> ()
@@ -653,7 +653,7 @@ sil @extract_associatedtype_with_conformance2 : $<each T_1 : P, Tee : P, each T_
653653
entry(%intIndex : $Builtin.Word):
654654
%innerIndex = dynamic_pack_index %intIndex of $Pack{repeat GenAssocPA<GenAssocPA<GenAssocPA<GenFwdP<each T_1>>>>, repeat GenAssocPA<GenAssocPA<GenAssocPA<GenFwdP<each T_1>>>>}
655655
%token = open_pack_element %innerIndex
656-
of <each U_1 : PA, Ewe : PA, each U_2 : PA where repeat each U_1.A : PA, repeat each U_1.A.A : PA, repeat each U_1.A.A.A : P, Ewe.A : PA, Ewe.A.A : PA, Ewe.A.A.A : P, repeat each U_2.A : PA, repeat each U_2.A.A : PA, repeat each U_2.A.A.A : P, (repeat (each U_1, each U_2)): Any>
656+
of <each U_1 : PA, Ewe : PA, each U_2 : PA where repeat (each U_1).A : PA, repeat (each U_1).A.A : PA, repeat (each U_1).A.A.A : P, Ewe.A : PA, Ewe.A.A : PA, Ewe.A.A.A : P, repeat (each U_2).A : PA, repeat (each U_2).A.A : PA, repeat (each U_2).A.A.A : P, (repeat (each U_1, each U_2)): Any>
657657
at <
658658
Pack{repeat GenAssocPA<GenAssocPA<GenAssocPA<GenFwdP<each T_1>>>>, repeat GenAssocPA<GenAssocPA<GenAssocPA<GenFwdP<each T_1>>>>},
659659
GenAssocPA<GenAssocPA<GenAssocPA<GenFwdP<Tee>>>>,

test/IRGen/variadic_generic_captures.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ public func has_witness_table_pack<each T: Sequence>(t: repeat each T) -> () ->
8080
}
8181

8282
public func has_witness_table_pack2<each T: Sequence>(t: repeat each T) -> () -> ()
83-
where repeat each T.Element: Sequence {
83+
where repeat (each T).Element: Sequence {
8484
return { _ = (repeat (each T).Element.Element).self }
8585
}

test/IRGen/variadic_generic_functions.sil

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ sil @fc : $<each T : P> () -> () {}
7373
// CHECK-SAME: i8*** %"each T.PA",
7474
// CHECK-SAME: i8*** %"each T.A.P")
7575
// CHECK: call swiftcc void @f1c([[INT]] %0, %swift.type** %"each T", i8*** %"each T.PA", i8*** %"each T.A.P")
76-
sil @f1 : $<each T : PA where repeat each T.A : P> () -> () {
77-
%f1c = function_ref @f1c : $@convention(thin) <each T : PA where repeat each T.A : P> () -> ()
78-
apply %f1c<Pack{repeat each T}>() : $@convention(thin) <each T : PA where repeat each T.A : P> () -> ()
76+
sil @f1 : $<each T : PA where repeat (each T).A : P> () -> () {
77+
%f1c = function_ref @f1c : $@convention(thin) <each T : PA where repeat (each T).A : P> () -> ()
78+
apply %f1c<Pack{repeat each T}>() : $@convention(thin) <each T : PA where repeat (each T).A : P> () -> ()
7979
%ret = tuple ()
8080
return %ret : $()
8181
}
8282

83-
sil @f1c : $<each T : PA where repeat each T.A : P> () -> () {}
83+
sil @f1c : $<each T : PA where repeat (each T).A : P> () -> () {}
8484

8585
// Construct associatedtype metadata pack, forward root wtable pack.
8686
// CHECK-LABEL: define {{.*}}@associatedtype_with_added_conformance(
@@ -93,9 +93,9 @@ sil @f1c : $<each T : PA where repeat each T.A : P> () -> () {}
9393
// CHECK-SAME: [[INT]] [[SHAPE]],
9494
// CHECK-SAME: %swift.type** [[ASSOCIATEDTYPES]],
9595
// CHECK-SAME: i8*** [[ASSOCIATEDTYPES_CONFORMANCES_TO_Q]])
96-
sil @associatedtype_with_added_conformance : $<each T : PA where repeat each T.A : Q> () -> () {
96+
sil @associatedtype_with_added_conformance : $<each T : PA where repeat (each T).A : Q> () -> () {
9797
%callee = function_ref @associatedtype_with_added_conformance_callee : $@convention(thin) <each T : Q> () -> ()
98-
apply %callee<Pack{repeat each T.A}>() : $@convention(thin) <each T : Q> () -> ()
98+
apply %callee<Pack{repeat (each T).A}>() : $@convention(thin) <each T : Q> () -> ()
9999
%ret = tuple ()
100100
return %ret : $()
101101
}
@@ -113,9 +113,9 @@ sil @associatedtype_with_added_conformance_callee : $<each T : Q> () -> () {}
113113
// CHECK-SAME: [[INT]] [[SHAPE]],
114114
// CHECK-SAME: %swift.type** [[ASSOCIATEDTYPES]],
115115
// CHECK-SAME: i8*** [[ASSOCIATEDTYPES_CONFORMANCES_TO_Q]])
116-
sil @associatedtype_with_added_conformance_2 : $<each T : PA where repeat each T.A : QA, repeat each T.A.A : R> () -> () {
116+
sil @associatedtype_with_added_conformance_2 : $<each T : PA where repeat (each T).A : QA, repeat (each T).A.A : R> () -> () {
117117
%j = function_ref @associatedtype_with_added_conformance_2_callee : $@convention(thin) <each T : R> () -> ()
118-
apply %j<Pack{repeat each T.A.A}>() : $@convention(thin) <each T : R> () -> ()
118+
apply %j<Pack{repeat (each T).A.A}>() : $@convention(thin) <each T : R> () -> ()
119119
%ret = tuple ()
120120
return %ret : $()
121121
}
@@ -158,13 +158,13 @@ sil @associatedtype_with_forwarded_conformance_1_callee : $<each T : Q> () -> ()
158158
// CHECK-SAME: i8*** [[GEN1_CONFORMANCES_TO_PA]],
159159
// CHECK-SAME: i8*** %"each T.A.Q")
160160
// CHECK: call void @llvm.stackrestore(i8* [[STORED_STACK_LOC]])
161-
sil @generictype_with_forwarded_conformance_2 : $<each T : PA where repeat each T.A : Q>() -> () {
162-
%callee = function_ref @generictype_with_forwarded_conformance_2_callee : $@convention(thin) <each T : PA where repeat each T.A : Q> () -> ()
163-
apply %callee<Pack{repeat Gen1<each T>}>() : $@convention(thin) <each T : PA where repeat each T.A : Q> () -> ()
161+
sil @generictype_with_forwarded_conformance_2 : $<each T : PA where repeat (each T).A : Q>() -> () {
162+
%callee = function_ref @generictype_with_forwarded_conformance_2_callee : $@convention(thin) <each T : PA where repeat (each T).A : Q> () -> ()
163+
apply %callee<Pack{repeat Gen1<each T>}>() : $@convention(thin) <each T : PA where repeat (each T).A : Q> () -> ()
164164
%ret = tuple ()
165165
return %ret : $()
166166
}
167-
sil @generictype_with_forwarded_conformance_2_callee : $<each T : PA where repeat each T.A : Q> () -> () {}
167+
sil @generictype_with_forwarded_conformance_2_callee : $<each T : PA where repeat (each T).A : Q> () -> () {}
168168

169169
// Construct a pack of generic types of generic types which "forward" conformance to a protocol with an associatedtype which itself conforms to a protocol.
170170
// CHECK-LABEL: define {{.*}}@generic_with_forwarded_conformance_3(
@@ -181,12 +181,12 @@ sil @generictype_with_forwarded_conformance_2_callee : $<each T : PA where repea
181181
// CHECK-SAME: i8*** [[GEN1_GEN1_CONFORMANCES_TO_PA]],
182182
// CHECK-SAME: i8*** %"each T.A.Q")
183183
// CHECK: call void @llvm.stackrestore(i8* [[STORED_STACK_LOC]])
184-
sil @generic_with_forwarded_conformance_3 : $<each T : PA where repeat each T.A : Q> () -> () {
185-
%callee = function_ref @generic_with_forwarded_conformance_3_callee : $@convention(thin) <each T : PA where repeat each T.A : Q> () -> ()
186-
apply %callee<Pack{repeat Gen1<Gen1<each T>>}>() : $@convention(thin) <each T : PA where repeat each T.A : Q> () -> ()
184+
sil @generic_with_forwarded_conformance_3 : $<each T : PA where repeat (each T).A : Q> () -> () {
185+
%callee = function_ref @generic_with_forwarded_conformance_3_callee : $@convention(thin) <each T : PA where repeat (each T).A : Q> () -> ()
186+
apply %callee<Pack{repeat Gen1<Gen1<each T>>}>() : $@convention(thin) <each T : PA where repeat (each T).A : Q> () -> ()
187187
%ret = tuple ()
188188
return %ret : $()
189189
}
190-
sil @generic_with_forwarded_conformance_3_callee : $<each T : PA where repeat each T.A : Q> () -> () {
190+
sil @generic_with_forwarded_conformance_3_callee : $<each T : PA where repeat (each T).A : Q> () -> () {
191191
}
192192

test/Interpreter/variadic_generic_conformances.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,29 @@ extension Int: Q {}
5656
extension Bool: Q {}
5757

5858
protocol HasPackRequirements {
59-
func doStuff1<each T: Q>(_ value: repeat each T) -> (repeat each T.A)
60-
func doStuff2<each T: Q>(_ value: repeat each T) -> (repeat each T.A)
59+
func doStuff1<each T: Q>(_ value: repeat each T) -> (repeat (each T).A)
60+
func doStuff2<each T: Q>(_ value: repeat each T) -> (repeat (each T).A)
6161
}
6262

6363
extension HasPackRequirements {
64-
func doStuff1<each T: Q>(_ value: repeat each T) -> (repeat each T.A) {
64+
func doStuff1<each T: Q>(_ value: repeat each T) -> (repeat (each T).A) {
6565
return (repeat (each value).makeA())
6666
}
6767
}
6868

6969
struct ConformsPackRequirements: HasPackRequirements {
70-
func doStuff2<each T: Q>(_ value: repeat each T) -> (repeat each T.A) {
70+
func doStuff2<each T: Q>(_ value: repeat each T) -> (repeat (each T).A) {
7171
return (repeat (each value).makeA())
7272
}
7373
}
7474

7575
func testPackRequirements1<T: HasPackRequirements, each U: Q>(_ t: T, _ u: repeat each U)
76-
-> (repeat each U.A) {
76+
-> (repeat (each U).A) {
7777
return t.doStuff1(repeat each u)
7878
}
7979

8080
func testPackRequirements2<T: HasPackRequirements, each U: Q>(_ t: T, _ u: repeat each U)
81-
-> (repeat each U.A) {
81+
-> (repeat (each U).A) {
8282
return t.doStuff2(repeat each u)
8383
}
8484

test/Interpreter/variadic_generic_type_witnesses.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ struct G<each T> {}
2020

2121
struct TupleWitnesses<each T: Sequence>: P {
2222
typealias A = (Bool, repeat each T)
23-
typealias B = (repeat each T.Element, x: Bool)
24-
typealias C = (x: Bool, repeat H<each T.Element>)
23+
typealias B = (repeat (each T).Element, x: Bool)
24+
typealias C = (x: Bool, repeat H<(each T).Element>)
2525
}
2626

2727
struct SingletonTupleWitnesses<each T>: P {
@@ -32,14 +32,14 @@ struct SingletonTupleWitnesses<each T>: P {
3232

3333
struct FunctionWitnesses<each T: Sequence>: P {
3434
typealias A = (Bool, repeat each T) -> ()
35-
typealias B = (repeat each T.Element, Bool) -> ()
36-
typealias C = (Bool, repeat H<each T.Element>) -> ()
35+
typealias B = (repeat (each T).Element, Bool) -> ()
36+
typealias C = (Bool, repeat H<(each T).Element>) -> ()
3737
}
3838

3939
struct NominalWitnesses<each T: Sequence>: P {
4040
typealias A = G<Bool, repeat each T>
41-
typealias B = G<repeat each T.Element, Bool>
42-
typealias C = G<Bool, repeat H<each T.Element>>
41+
typealias B = G<repeat (each T).Element, Bool>
42+
typealias C = G<Bool, repeat H<(each T).Element>>
4343
}
4444

4545
func getA<T: P>(_: T.Type) -> Any.Type {

0 commit comments

Comments
 (0)