Skip to content

Commit 28d4c80

Browse files
authored
Merge pull request #67436 from hborla/5.9-dependent-type-pack-syntax
[5.9][Type Resolution] Only allow `each` applied directly to a type parameter pack.
2 parents 4cef53f + ae702e8 commit 28d4c80

19 files changed

+61
-51
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/Generics/pack-shape-requirements.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func inferSameShape<each T, each U>(ts t: repeat each T, us u: repeat each U) wh
1212
// CHECK-LABEL: desugarSameShape(ts:us:)
1313
// CHECK-NEXT: Generic signature: <each T, each U where repeat each T : P, (repeat (each T, each U)) : Any, repeat each U : P>
1414
func desugarSameShape<each T, each U>(ts t: repeat each T, us u: repeat each U)
15-
where repeat each T: P, repeat each U: P, (repeat (each T.A, each U.A)): Any {}
15+
where repeat each T: P, repeat each U: P, (repeat ((each T).A, (each U).A)): Any {}
1616

1717
// CHECK-LABEL: multipleSameShape1(ts:us:vs:)
1818
// CHECK-NEXT: Generic signature: <each T, each U, each V where (repeat (each T, each U)) : Any, (repeat (each U, each V)) : Any>
@@ -75,11 +75,11 @@ func expandedParameters<each T, each Result>(_ t: repeat each T, transform: repe
7575

7676
// CHECK-LABEL: sameType1
7777
// CHECK-NEXT: Generic signature: <each T, each U where repeat each T : P, repeat each U : P, repeat (each T).[P]A == (each U).[P]A>
78-
func sameType1<each T, each U>(_: repeat (each T, each U)) where repeat each T: P, repeat each U: P, repeat each T.A == each U.A {}
78+
func sameType1<each T, each U>(_: repeat (each T, each U)) where repeat each T: P, repeat each U: P, repeat (each T).A == (each U).A {}
7979

8080
// Make sure inherited associated types are handled
8181
protocol Q: P where A: Q {}
8282

8383
// CHECK-LABEL: sameType2
8484
// CHECK-NEXT: Generic signature: <each T, each U where repeat each T : Q, repeat each U : Q, repeat (each T).[P]A.[P]A == (each U).[P]A.[P]A>
85-
func sameType2<each T, each U>(_: repeat (each T, each U)) where repeat each T: Q, repeat each U: Q, repeat each T.A.A == each U.A.A {}
85+
func sameType2<each T, each U>(_: repeat (each T, each U)) where repeat each T: Q, repeat each U: Q, repeat (each T).A.A == (each U).A.A {}

test/Generics/tuple-conformances.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protocol P {
1212
}
1313

1414
extension Builtin.TheTupleType: P where repeat each Elements: P {
15-
typealias A = (repeat each Elements.A)
15+
typealias A = (repeat (each Elements).A)
1616
typealias B = Float
1717
func f() {}
1818
}

test/Generics/variadic_generic_requirements.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ _ = Layout<Class, Subclass>.self // ok
2020
_ = Layout<Int, String>.self // expected-error {{'Layout' requires that 'Int' be a class type}}
2121

2222
struct Outer<each T: Sequence> {
23-
struct Inner<each U: Sequence> where repeat each T.Element == each U.Element {}
23+
struct Inner<each U: Sequence> where repeat (each T).Element == (each U).Element {}
2424
// expected-note@-1 {{requirement specified as '(each T).Element' == '(each U).Element' [with each T = Array<Int>, Array<String>; each U = Set<String>, Set<Int>]}}
2525
// expected-note@-2 {{requirement specified as '(each T).Element' == '(each U).Element' [with each T = Array<Int>; each U = Set<Int>, Set<String>]}}
2626

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
}

0 commit comments

Comments
 (0)