diff --git a/include/swift/AST/Types.h b/include/swift/AST/Types.h index e24eca73ad1a3..50adc888245e8 100644 --- a/include/swift/AST/Types.h +++ b/include/swift/AST/Types.h @@ -769,6 +769,10 @@ class alignas(1 << TypeAlignInBits) TypeBase /// include type parameters in nested positions e.g. \c X. bool isParameterPack(); + /// Determine whether this type is directly a type parameter pack, which + /// can only be a GenericTypeParamType. + bool isRootParameterPack(); + /// Determine whether this type can dynamically be an optional type. /// /// \param includeExistential Whether an existential type should be considered diff --git a/lib/AST/ASTMangler.cpp b/lib/AST/ASTMangler.cpp index fe6dcac73a0ec..0083ded9c0d37 100644 --- a/lib/AST/ASTMangler.cpp +++ b/lib/AST/ASTMangler.cpp @@ -653,8 +653,7 @@ std::string ASTMangler::mangleAutoDiffGeneratedDeclaration( static Type getTypeForDWARFMangling(Type t) { return t.subst( [](SubstitutableType *t) -> Type { - if (isa(t) && - cast(t)->isParameterPack()) { + if (t->isRootParameterPack()) { return PackType::getSingletonPackExpansion(t->getCanonicalType()); } return t->getCanonicalType(); diff --git a/lib/AST/ASTVerifier.cpp b/lib/AST/ASTVerifier.cpp index 12dedbb7d4d8b..443282b031b29 100644 --- a/lib/AST/ASTVerifier.cpp +++ b/lib/AST/ASTVerifier.cpp @@ -614,8 +614,7 @@ class Verifier : public ASTWalker { auto countType = expansion->getCountType(); if (!(countType->is() || countType->is() || - (countType->is() && - countType->castTo()->isParameterPack()))) { + countType->isRootParameterPack())) { Out << "non-pack shape type" << countType->getString() << "\n"; abort(); } diff --git a/lib/AST/GenericSignature.cpp b/lib/AST/GenericSignature.cpp index b0af8625d92b6..6108f2dd5e41a 100644 --- a/lib/AST/GenericSignature.cpp +++ b/lib/AST/GenericSignature.cpp @@ -875,7 +875,7 @@ void GenericSignature::verify(ArrayRef reqts) const { abort(); } - if (!reqt.getFirstType()->castTo()->isParameterPack()) { + if (!reqt.getFirstType()->isRootParameterPack()) { llvm::errs() << "Left hand side is not a parameter pack: "; reqt.dump(llvm::errs()); llvm::errs() << "\n"; @@ -889,7 +889,7 @@ void GenericSignature::verify(ArrayRef reqts) const { abort(); } - if (!reqt.getSecondType()->castTo()->isParameterPack()) { + if (!reqt.getSecondType()->isRootParameterPack()) { llvm::errs() << "Right hand side is not a parameter pack: "; reqt.dump(llvm::errs()); llvm::errs() << "\n"; diff --git a/lib/AST/ParameterPack.cpp b/lib/AST/ParameterPack.cpp index 7b35b93e95d5a..992ee692bb86a 100644 --- a/lib/AST/ParameterPack.cpp +++ b/lib/AST/ParameterPack.cpp @@ -66,8 +66,7 @@ static Type transformTypeParameterPacksRec( if (auto *paramType = dyn_cast(t)) { if (expansionLevel == 0 && (isa(paramType) || - (isa(paramType) && - cast(paramType)->isParameterPack()))) { + paramType->isRootParameterPack())) { return fn(paramType); } @@ -186,6 +185,12 @@ bool TypeBase::isParameterPack() { while (auto *memberTy = t->getAs()) t = memberTy->getBase(); + return t->isRootParameterPack(); +} + +bool TypeBase::isRootParameterPack() { + Type t(this); + return t->is() && t->castTo()->isParameterPack(); } diff --git a/lib/AST/TypeSubstitution.cpp b/lib/AST/TypeSubstitution.cpp index 011b7b1d9c44c..9496a304dc80c 100644 --- a/lib/AST/TypeSubstitution.cpp +++ b/lib/AST/TypeSubstitution.cpp @@ -56,9 +56,7 @@ QueryTypeSubstitutionMapOrIdentity::operator()(SubstitutableType *type) const { if (known != substitutions.end() && known->second) return known->second; - if (isa(type) || - (isa(type) && - cast(type)->isParameterPack())) { + if (isa(type) || type->isRootParameterPack()) { return PackType::getSingletonPackExpansion(type); } diff --git a/lib/Sema/TypeCheckType.cpp b/lib/Sema/TypeCheckType.cpp index bff0b3c1e9cb7..6070386100cd9 100644 --- a/lib/Sema/TypeCheckType.cpp +++ b/lib/Sema/TypeCheckType.cpp @@ -4676,7 +4676,7 @@ NeverNullType TypeResolver::resolvePackElement(PackElementTypeRepr *repr, if (packReference->hasError()) return ErrorType::get(ctx); - if (!packReference->isParameterPack()) { + if (!packReference->isRootParameterPack()) { auto diag = ctx.Diags.diagnose(repr->getLoc(), diag::each_non_pack, packReference); bool addEachFixitApplied = false; diff --git a/test/Constraints/pack-expansion-expressions.swift b/test/Constraints/pack-expansion-expressions.swift index 383fa44bcd6f4..1d32efb5aca24 100644 --- a/test/Constraints/pack-expansion-expressions.swift +++ b/test/Constraints/pack-expansion-expressions.swift @@ -59,7 +59,7 @@ extension P { func outerArchetype(t: repeat each T, u: U) where repeat each T: P { - let _: (repeat (each T.A, U)) = (repeat ((each t).value, u)) + let _: (repeat ((each T).A, U)) = (repeat ((each t).value, u)) } func sameElement(t: repeat each T, u: U) where repeat each T: P, repeat each T == U { @@ -478,11 +478,11 @@ do { // rdar://107675464 - misplaced `each` results in `type of expression is ambiguous without a type annotation` do { - func test_correct_each(_ value: repeat each T) -> (repeat each T.A) { + func test_correct_each(_ value: repeat each T) -> (repeat (each T).A) { return (repeat (each value).makeA()) // Ok } - func test_misplaced_each(_ value: repeat each T) -> (repeat each T.A) { + func test_misplaced_each(_ value: repeat each T) -> (repeat (each T).A) { return (repeat each value.makeA()) // expected-error@-1 {{value pack 'each T' must be referenced with 'each'}} {{25-25=(each }} {{30-30=)}} } diff --git a/test/Generics/pack-shape-requirements.swift b/test/Generics/pack-shape-requirements.swift index e6950a145aeab..acb25b0cc7dfa 100644 --- a/test/Generics/pack-shape-requirements.swift +++ b/test/Generics/pack-shape-requirements.swift @@ -12,7 +12,7 @@ func inferSameShape(ts t: repeat each T, us u: repeat each U) wh // CHECK-LABEL: desugarSameShape(ts:us:) // CHECK-NEXT: Generic signature: func desugarSameShape(ts t: repeat each T, us u: repeat each U) - where repeat each T: P, repeat each U: P, (repeat (each T.A, each U.A)): Any {} + where repeat each T: P, repeat each U: P, (repeat ((each T).A, (each U).A)): Any {} // CHECK-LABEL: multipleSameShape1(ts:us:vs:) // CHECK-NEXT: Generic signature: @@ -75,11 +75,11 @@ func expandedParameters(_ t: repeat each T, transform: repe // CHECK-LABEL: sameType1 // CHECK-NEXT: Generic signature: -func sameType1(_: repeat (each T, each U)) where repeat each T: P, repeat each U: P, repeat each T.A == each U.A {} +func sameType1(_: repeat (each T, each U)) where repeat each T: P, repeat each U: P, repeat (each T).A == (each U).A {} // Make sure inherited associated types are handled protocol Q: P where A: Q {} // CHECK-LABEL: sameType2 // CHECK-NEXT: Generic signature: -func sameType2(_: repeat (each T, each U)) where repeat each T: Q, repeat each U: Q, repeat each T.A.A == each U.A.A {} \ No newline at end of file +func sameType2(_: repeat (each T, each U)) where repeat each T: Q, repeat each U: Q, repeat (each T).A.A == (each U).A.A {} diff --git a/test/Generics/tuple-conformances.swift b/test/Generics/tuple-conformances.swift index b2120b4ec12a4..e35320c419b9f 100644 --- a/test/Generics/tuple-conformances.swift +++ b/test/Generics/tuple-conformances.swift @@ -12,7 +12,7 @@ protocol P { } extension Builtin.TheTupleType: P where repeat each Elements: P { - typealias A = (repeat each Elements.A) + typealias A = (repeat (each Elements).A) typealias B = Float func f() {} } diff --git a/test/Generics/variadic_generic_requirements.swift b/test/Generics/variadic_generic_requirements.swift index 6be7b41be8fd3..c6a12aca47065 100644 --- a/test/Generics/variadic_generic_requirements.swift +++ b/test/Generics/variadic_generic_requirements.swift @@ -20,7 +20,7 @@ _ = Layout.self // ok _ = Layout.self // expected-error {{'Layout' requires that 'Int' be a class type}} struct Outer { - struct Inner where repeat each T.Element == each U.Element {} + struct Inner where repeat (each T).Element == (each U).Element {} // expected-note@-1 {{requirement specified as '(each T).Element' == '(each U).Element' [with each T = Array, Array; each U = Set, Set]}} // expected-note@-2 {{requirement specified as '(each T).Element' == '(each U).Element' [with each T = Array; each U = Set, Set]}} diff --git a/test/IRGen/bind_element_archetype.swift b/test/IRGen/bind_element_archetype.swift index 4f764e478ce93..363e2a7f5fe0a 100644 --- a/test/IRGen/bind_element_archetype.swift +++ b/test/IRGen/bind_element_archetype.swift @@ -8,7 +8,7 @@ public protocol Q { public func f(_: T) {} -public func foo1(t: repeat each T, u: repeat each T.A) { +public func foo1(t: repeat each T, u: repeat (each T).A) { repeat f(each u) } diff --git a/test/IRGen/run_variadic_generics.sil b/test/IRGen/run_variadic_generics.sil index c271348a4d5b7..9319853b8733e 100644 --- a/test/IRGen/run_variadic_generics.sil +++ b/test/IRGen/run_variadic_generics.sil @@ -626,10 +626,10 @@ entry(%intIndex : $Builtin.Word): return %t : $() } -sil @unwrap_from_PA : $ (Builtin.Word) -> () { +sil @unwrap_from_PA : $ (Builtin.Word) -> () { entry(%intIndex : $Builtin.Word): %direct_access_from_parameter_with_conformance = function_ref @direct_access_from_parameter_with_conformance : $@convention(thin) (Builtin.Word) -> () - apply %direct_access_from_parameter_with_conformance}>(%intIndex) : $@convention(thin) (Builtin.Word) -> () + apply %direct_access_from_parameter_with_conformance}>(%intIndex) : $@convention(thin) (Builtin.Word) -> () %t = tuple () return %t : $() } @@ -637,7 +637,7 @@ entry(%intIndex : $Builtin.Word): sil @extract_associatedtype_with_conformance : $ (Builtin.Word) -> () { entry(%intIndex : $Builtin.Word): %innerIndex = dynamic_pack_index %intIndex of $Pack{repeat each T_1} - %token = open_pack_element %innerIndex of at >}>, shape $U_1, uuid "01234567-89AB-CDEF-0123-000000000005" + %token = open_pack_element %innerIndex of at >}>, shape $U_1, uuid "01234567-89AB-CDEF-0123-000000000005" %metatype_1 = metatype $@thick (@pack_element("01234567-89AB-CDEF-0123-000000000005") U_1).Type %printGenericType = function_ref @printGenericType : $@convention(thin) (@thick T.Type) -> () apply %printGenericType<(@pack_element("01234567-89AB-CDEF-0123-000000000005") U_1)>(%metatype_1) : $@convention(thin) (@thick T.Type) -> () @@ -653,7 +653,7 @@ sil @extract_associatedtype_with_conformance2 : $>>>, repeat GenAssocPA>>>} %token = open_pack_element %innerIndex - of + of at < Pack{repeat GenAssocPA>>>, repeat GenAssocPA>>>}, GenAssocPA>>>, diff --git a/test/IRGen/variadic_generic_captures.swift b/test/IRGen/variadic_generic_captures.swift index 0957e0d2a6f6e..0ce897108c4c4 100644 --- a/test/IRGen/variadic_generic_captures.swift +++ b/test/IRGen/variadic_generic_captures.swift @@ -62,6 +62,6 @@ public func has_witness_table_pack(t: repeat each T) -> () -> } public func has_witness_table_pack2(t: repeat each T) -> () -> () - where repeat each T.Element: Sequence { + where repeat (each T).Element: Sequence { return { _ = (repeat (each T).Element.Element).self } } diff --git a/test/IRGen/variadic_generic_functions.sil b/test/IRGen/variadic_generic_functions.sil index a1650123f1559..db7e0da3e08c5 100644 --- a/test/IRGen/variadic_generic_functions.sil +++ b/test/IRGen/variadic_generic_functions.sil @@ -72,14 +72,14 @@ sil @fc : $ () -> () {} // CHECK-SAME: ptr %"each T.PA", // CHECK-SAME: ptr %"each T.A.P") // CHECK: call swiftcc void @f1c([[INT]] %0, ptr %"each T", ptr %"each T.PA", ptr %"each T.A.P") -sil @f1 : $ () -> () { - %f1c = function_ref @f1c : $@convention(thin) () -> () - apply %f1c() : $@convention(thin) () -> () +sil @f1 : $ () -> () { + %f1c = function_ref @f1c : $@convention(thin) () -> () + apply %f1c() : $@convention(thin) () -> () %ret = tuple () return %ret : $() } -sil @f1c : $ () -> () {} +sil @f1c : $ () -> () {} // Construct associatedtype metadata pack, forward root wtable pack. // CHECK-LABEL: define {{.*}}@associatedtype_with_added_conformance( @@ -92,9 +92,9 @@ sil @f1c : $ () -> () {} // CHECK-SAME: [[INT]] [[SHAPE]], // CHECK-SAME: ptr [[ASSOCIATEDTYPES]], // CHECK-SAME: ptr [[ASSOCIATEDTYPES_CONFORMANCES_TO_Q]]) -sil @associatedtype_with_added_conformance : $ () -> () { +sil @associatedtype_with_added_conformance : $ () -> () { %callee = function_ref @associatedtype_with_added_conformance_callee : $@convention(thin) () -> () - apply %callee() : $@convention(thin) () -> () + apply %callee() : $@convention(thin) () -> () %ret = tuple () return %ret : $() } @@ -112,9 +112,9 @@ sil @associatedtype_with_added_conformance_callee : $ () -> () {} // CHECK-SAME: [[INT]] [[SHAPE]], // CHECK-SAME: ptr [[ASSOCIATEDTYPES]], // CHECK-SAME: ptr [[ASSOCIATEDTYPES_CONFORMANCES_TO_Q]]) -sil @associatedtype_with_added_conformance_2 : $ () -> () { +sil @associatedtype_with_added_conformance_2 : $ () -> () { %j = function_ref @associatedtype_with_added_conformance_2_callee : $@convention(thin) () -> () - apply %j() : $@convention(thin) () -> () + apply %j() : $@convention(thin) () -> () %ret = tuple () return %ret : $() } @@ -157,13 +157,13 @@ sil @associatedtype_with_forwarded_conformance_1_callee : $ () -> () // CHECK-SAME: ptr [[GEN1_CONFORMANCES_TO_PA]], // CHECK-SAME: ptr %"each T.A.Q") // CHECK: call void @llvm.stackrestore(ptr [[STORED_STACK_LOC]]) -sil @generictype_with_forwarded_conformance_2 : $() -> () { - %callee = function_ref @generictype_with_forwarded_conformance_2_callee : $@convention(thin) () -> () - apply %callee}>() : $@convention(thin) () -> () +sil @generictype_with_forwarded_conformance_2 : $() -> () { + %callee = function_ref @generictype_with_forwarded_conformance_2_callee : $@convention(thin) () -> () + apply %callee}>() : $@convention(thin) () -> () %ret = tuple () return %ret : $() } -sil @generictype_with_forwarded_conformance_2_callee : $ () -> () {} +sil @generictype_with_forwarded_conformance_2_callee : $ () -> () {} // Construct a pack of generic types of generic types which "forward" conformance to a protocol with an associatedtype which itself conforms to a protocol. // CHECK-LABEL: define {{.*}}@generic_with_forwarded_conformance_3( @@ -180,12 +180,12 @@ sil @generictype_with_forwarded_conformance_2_callee : $ () -> () { - %callee = function_ref @generic_with_forwarded_conformance_3_callee : $@convention(thin) () -> () - apply %callee>}>() : $@convention(thin) () -> () +sil @generic_with_forwarded_conformance_3 : $ () -> () { + %callee = function_ref @generic_with_forwarded_conformance_3_callee : $@convention(thin) () -> () + apply %callee>}>() : $@convention(thin) () -> () %ret = tuple () return %ret : $() } -sil @generic_with_forwarded_conformance_3_callee : $ () -> () { +sil @generic_with_forwarded_conformance_3_callee : $ () -> () { } diff --git a/test/Interpreter/variadic_generic_conformances.swift b/test/Interpreter/variadic_generic_conformances.swift index 130b801bb3c6e..c3d863ce57506 100644 --- a/test/Interpreter/variadic_generic_conformances.swift +++ b/test/Interpreter/variadic_generic_conformances.swift @@ -56,29 +56,29 @@ extension Int: Q {} extension Bool: Q {} protocol HasPackRequirements { - func doStuff1(_ value: repeat each T) -> (repeat each T.A) - func doStuff2(_ value: repeat each T) -> (repeat each T.A) + func doStuff1(_ value: repeat each T) -> (repeat (each T).A) + func doStuff2(_ value: repeat each T) -> (repeat (each T).A) } extension HasPackRequirements { - func doStuff1(_ value: repeat each T) -> (repeat each T.A) { + func doStuff1(_ value: repeat each T) -> (repeat (each T).A) { return (repeat (each value).makeA()) } } struct ConformsPackRequirements: HasPackRequirements { - func doStuff2(_ value: repeat each T) -> (repeat each T.A) { + func doStuff2(_ value: repeat each T) -> (repeat (each T).A) { return (repeat (each value).makeA()) } } func testPackRequirements1(_ t: T, _ u: repeat each U) - -> (repeat each U.A) { + -> (repeat (each U).A) { return t.doStuff1(repeat each u) } func testPackRequirements2(_ t: T, _ u: repeat each U) - -> (repeat each U.A) { + -> (repeat (each U).A) { return t.doStuff2(repeat each u) } diff --git a/test/Interpreter/variadic_generic_type_witnesses.swift b/test/Interpreter/variadic_generic_type_witnesses.swift index 8ae00a2081e60..c6c3226e6311d 100644 --- a/test/Interpreter/variadic_generic_type_witnesses.swift +++ b/test/Interpreter/variadic_generic_type_witnesses.swift @@ -20,8 +20,8 @@ struct G {} struct TupleWitnesses: P { typealias A = (Bool, repeat each T) - typealias B = (repeat each T.Element, x: Bool) - typealias C = (x: Bool, repeat H) + typealias B = (repeat (each T).Element, x: Bool) + typealias C = (x: Bool, repeat H<(each T).Element>) } struct SingletonTupleWitnesses: P { @@ -32,14 +32,14 @@ struct SingletonTupleWitnesses: P { struct FunctionWitnesses: P { typealias A = (Bool, repeat each T) -> () - typealias B = (repeat each T.Element, Bool) -> () - typealias C = (Bool, repeat H) -> () + typealias B = (repeat (each T).Element, Bool) -> () + typealias C = (Bool, repeat H<(each T).Element>) -> () } struct NominalWitnesses: P { typealias A = G - typealias B = G - typealias C = G> + typealias B = G + typealias C = G> } func getA(_: T.Type) -> Any.Type { diff --git a/test/Interpreter/variadic_generic_types.swift b/test/Interpreter/variadic_generic_types.swift index 4bae5a9a6a596..43557066daa31 100644 --- a/test/Interpreter/variadic_generic_types.swift +++ b/test/Interpreter/variadic_generic_types.swift @@ -101,7 +101,7 @@ types.test("LayoutReq") { } public struct OuterSeq { - public struct InnerSeq where repeat each T.Element == each U.Element {} + public struct InnerSeq where repeat (each T).Element == (each U).Element {} } types.test("SameTypeReq") { diff --git a/test/SILGen/variadic_generic_conformances.swift b/test/SILGen/variadic_generic_conformances.swift index 11411a1d34a10..090cce39ff5f5 100644 --- a/test/SILGen/variadic_generic_conformances.swift +++ b/test/SILGen/variadic_generic_conformances.swift @@ -8,6 +8,6 @@ struct S: P { typealias A = S } -func f(_: repeat each T) -> (repeat each T.A.A.A.A) {} +func f(_: repeat each T) -> (repeat (each T).A.A.A.A) {} -f(S(), S(), S()) \ No newline at end of file +f(S(), S(), S()) diff --git a/test/type/pack_expansion.swift b/test/type/pack_expansion.swift index 5ca21f26833e5..f36308c542993 100644 --- a/test/type/pack_expansion.swift +++ b/test/type/pack_expansion.swift @@ -64,7 +64,7 @@ struct Outer { func packRef(_: repeat each T) where repeat each T: P {} -func packMemberRef(_: repeat each T.T) where repeat each T: P {} +func packMemberRef(_: repeat (each T).T) where repeat each T: P {} // expected-error@+1 {{'each' cannot be applied to non-pack type 'Int'}}{{31-35=}} func invalidPackRefEachInt(_: each Int) {} diff --git a/validation-test/compiler_crashers_2_fixed/rdar108319167.swift b/validation-test/compiler_crashers_2_fixed/rdar108319167.swift index 87c57402280f3..71f747ded08f1 100644 --- a/validation-test/compiler_crashers_2_fixed/rdar108319167.swift +++ b/validation-test/compiler_crashers_2_fixed/rdar108319167.swift @@ -13,6 +13,6 @@ public func foo1(t: repeat each T, u: repeat each U) repeat f(each u) } -public func foo2(t: repeat each T, u: repeat each T.A) { +public func foo2(t: repeat each T, u: repeat (each T).A) { repeat f(each u) } diff --git a/validation-test/compiler_crashers_2_fixed/rdar110363503.swift b/validation-test/compiler_crashers_2_fixed/rdar110363503.swift index 95c605602b149..ea9e48cf2b26f 100644 --- a/validation-test/compiler_crashers_2_fixed/rdar110363503.swift +++ b/validation-test/compiler_crashers_2_fixed/rdar110363503.swift @@ -6,11 +6,11 @@ struct ZipCollection { extension ZipCollection: Collection { struct Element { - var elt: (repeat each C.Element) + var elt: (repeat (each C).Element) } struct Index { - let i: (repeat each C.Index) + let i: (repeat (each C).Index) } var startIndex: Index { diff --git a/validation-test/compiler_crashers_2_fixed/rdar112108253.swift b/validation-test/compiler_crashers_2_fixed/rdar112108253.swift index b8882b0e2442c..d490b54c8be1c 100644 --- a/validation-test/compiler_crashers_2_fixed/rdar112108253.swift +++ b/validation-test/compiler_crashers_2_fixed/rdar112108253.swift @@ -10,7 +10,7 @@ public protocol Q { init() } -public struct S: Q where repeat T.A == G { +public struct S: Q where repeat T.A == G { public init() {} public init(predicate: repeat each U) {} }