Skip to content

Commit c16e9c1

Browse files
committed
Fix the pretty-printing of pack expansion types to match the current design
1 parent 43da3fd commit c16e9c1

File tree

7 files changed

+74
-50
lines changed

7 files changed

+74
-50
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ struct PrintOptions {
285285

286286
bool PrintImplicitAttrs = true;
287287

288+
/// Whether to print the \c each keyword for pack archetypes.
289+
bool PrintExplicitEach = false;
290+
288291
/// Whether to print the \c any keyword for existential
289292
/// types.
290293
bool PrintExplicitAny = false;

lib/AST/ASTPrinter.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5416,6 +5416,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
54165416
} else if (auto existential = dyn_cast<ExistentialMetatypeType>(T.getPointer())) {
54175417
if (!Options.PrintExplicitAny)
54185418
return isSimpleUnderPrintOptions(existential->getInstanceType());
5419+
} else if (auto param = dyn_cast<GenericTypeParamType>(T.getPointer())) {
5420+
return !param->isParameterPack() || !Options.PrintExplicitEach;
5421+
} else if (auto archetype = dyn_cast<ArchetypeType>(T.getPointer())) {
5422+
return !archetype->isParameterPack() || !Options.PrintExplicitEach;
54195423
}
54205424
return T->hasSimpleTypeRepr();
54215425
}
@@ -5731,8 +5735,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
57315735
}
57325736

57335737
void visitPackExpansionType(PackExpansionType *T) {
5734-
visit(T->getPatternType());
5735-
Printer << "...";
5738+
PrintOptions innerOptions = Options;
5739+
innerOptions.PrintExplicitEach = true;
5740+
5741+
Printer << "repeat ";
5742+
TypePrinter(Printer, innerOptions).visit(T->getPatternType());
57365743
}
57375744

57385745
void visitTupleType(TupleType *T) {
@@ -6567,10 +6574,16 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
65676574
}
65686575
}
65696576

6577+
void printEach() {
6578+
if (Options.PrintExplicitEach)
6579+
Printer << "each ";
6580+
}
6581+
65706582
void printArchetypeCommon(ArchetypeType *T) {
65716583
if (Options.AlternativeTypeNames) {
65726584
auto found = Options.AlternativeTypeNames->find(T->getCanonicalType());
65736585
if (found != Options.AlternativeTypeNames->end()) {
6586+
if (T->isParameterPack()) printEach();
65746587
Printer << found->second.str();
65756588
return;
65766589
}
@@ -6683,12 +6696,17 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
66836696
}
66846697

66856698
void visitGenericTypeParamType(GenericTypeParamType *T) {
6699+
auto printPrefix = [&]{
6700+
if (T->isParameterPack()) printEach();
6701+
};
6702+
66866703
auto decl = T->getDecl();
66876704
if (!decl) {
66886705
// If we have an alternate name for this type, use it.
66896706
if (Options.AlternativeTypeNames) {
66906707
auto found = Options.AlternativeTypeNames->find(T->getCanonicalType());
66916708
if (found != Options.AlternativeTypeNames->end()) {
6709+
printPrefix();
66926710
Printer << found->second.str();
66936711
return;
66946712
}
@@ -6705,6 +6723,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
67056723
// If we have and should print based on the type representation, do so.
67066724
if (auto opaqueRepr = decl->getOpaqueTypeRepr()) {
67076725
if (willUseTypeReprPrinting(opaqueRepr, Type(), Options)) {
6726+
printPrefix();
67086727
opaqueRepr->print(Printer, Options);
67096728
return;
67106729
}
@@ -6722,6 +6741,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
67226741
return;
67236742
}
67246743

6744+
printPrefix();
6745+
67256746
const auto Name = T->getName();
67266747
if (Name.empty()) {
67276748
Printer << "<anonymous>";

test/Constraints/pack_expansion_types.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ func concreteReturnTupleValid() {
6868

6969
func concreteReturnTypeInvalid() {
7070
let _: (x: Int) = returnTuple1()
71-
// expected-error@-1 {{cannot convert value of type '(T...)' to specified type '(x: Int)'}}
71+
// expected-error@-1 {{cannot convert value of type '(repeat each T)' to specified type '(x: Int)'}}
7272
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
7373

7474
let _: () = returnTuple2()
75-
// expected-error@-1 {{'(Int, T...)' is not convertible to '()', tuples have a different number of elements}}
75+
// expected-error@-1 {{'(Int, repeat each T)' is not convertible to '()', tuples have a different number of elements}}
7676
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
7777

7878
let _: (x: Int) = returnTupleLabel3()
79-
// expected-error@-1 {{'(Int, T..., y: Float)' is not convertible to '(x: Int)', tuples have a different number of elements}}
79+
// expected-error@-1 {{'(Int, repeat each T, y: Float)' is not convertible to '(x: Int)', tuples have a different number of elements}}
8080
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
8181

8282
let _: (Int, Int, y: Float) = returnTupleLabel4()
83-
// expected-error@-1 {{cannot convert value of type '(Int, x: T..., y: Float)' to specified type '(Int, Int, y: Float)'}}
83+
// expected-error@-1 {{cannot convert value of type '(Int, x: repeat each T, y: Float)' to specified type '(Int, Int, y: Float)'}}
8484
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
8585

8686
let _: () = returnTupleLabel5()
87-
// expected-error@-1 {{'(Int, T..., y: U...)' is not convertible to '()', tuples have a different number of elements}}
87+
// expected-error@-1 {{'(Int, repeat each T, y: repeat each U)' is not convertible to '()', tuples have a different number of elements}}
8888
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
8989
// expected-error@-3 {{generic parameter 'U' could not be inferred}}
9090
}
@@ -117,61 +117,61 @@ func genericReturnTupleValid<T...>(_: repeat each T) {
117117

118118
func genericReturnTupleInvalid<T...>(_: repeat each T) {
119119
let _: (x: repeat each T) = returnTuple1()
120-
// expected-error@-1 {{cannot convert value of type '(T...)' to specified type '(x: T...)'}}
120+
// expected-error@-1 {{cannot convert value of type '(repeat each T)' to specified type '(x: repeat each T)'}}
121121
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
122122

123123
let _: (x: Int, repeat each T) = returnTuple1()
124-
// expected-error@-1 {{'(T...)' is not convertible to '(x: Int, T...)', tuples have a different number of elements}}
124+
// expected-error@-1 {{'(repeat each T)' is not convertible to '(x: Int, repeat each T)', tuples have a different number of elements}}
125125
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
126126

127127
let _: (Int, x: repeat each T) = returnTuple2()
128-
// expected-error@-1 {{cannot convert value of type '(Int, T...)' to specified type '(Int, x: T...)'}}
128+
// expected-error@-1 {{cannot convert value of type '(Int, repeat each T)' to specified type '(Int, x: repeat each T)'}}
129129
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
130130

131131
let _: (Int, x: String, repeat each T) = returnTuple2()
132-
// expected-error@-1 {{'(Int, T...)' is not convertible to '(Int, x: String, T...)', tuples have a different number of elements}}
132+
// expected-error@-1 {{'(Int, repeat each T)' is not convertible to '(Int, x: String, repeat each T)', tuples have a different number of elements}}
133133
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
134134

135135
let _: (y: repeat each T) = returnTupleLabel1()
136-
// expected-error@-1 {{cannot convert value of type '(x: T...)' to specified type '(y: T...)'}}
136+
// expected-error@-1 {{cannot convert value of type '(x: repeat each T)' to specified type '(y: repeat each T)'}}
137137
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
138138

139139
let _: (y: Int, repeat each T) = returnTupleLabel1()
140-
// expected-error@-1 {{'(x: T...)' is not convertible to '(y: Int, T...)', tuples have a different number of elements}}
140+
// expected-error@-1 {{'(x: repeat each T)' is not convertible to '(y: Int, repeat each T)', tuples have a different number of elements}}
141141
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
142142

143143
let _: (x: repeat each T) = returnTupleLabel2()
144-
// expected-error@-1 {{'(Int, x: T...)' is not convertible to '(x: T...)', tuples have a different number of elements}}
144+
// expected-error@-1 {{'(Int, x: repeat each T)' is not convertible to '(x: repeat each T)', tuples have a different number of elements}}
145145
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
146146

147147
let _: (Int, y: String, repeat each T) = returnTupleLabel2()
148-
// expected-error@-1 {{'(Int, x: T...)' is not convertible to '(Int, y: String, T...)', tuples have a different number of elements}}
148+
// expected-error@-1 {{'(Int, x: repeat each T)' is not convertible to '(Int, y: String, repeat each T)', tuples have a different number of elements}}
149149
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
150150

151151
let _: (repeat each T, y: Float) = returnTupleLabel3()
152-
// expected-error@-1 {{'(Int, T..., y: Float)' is not convertible to '(T..., y: Float)', tuples have a different number of elements}}
152+
// expected-error@-1 {{'(Int, repeat each T, y: Float)' is not convertible to '(repeat each T, y: Float)', tuples have a different number of elements}}
153153
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
154154

155155
let _: (Int, String, repeat each T, x: Float) = returnTupleLabel3()
156-
// expected-error@-1 {{'(Int, T..., y: Float)' is not convertible to '(Int, String, T..., x: Float)', tuples have a different number of elements}}
156+
// expected-error@-1 {{'(Int, repeat each T, y: Float)' is not convertible to '(Int, String, repeat each T, x: Float)', tuples have a different number of elements}}
157157
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
158158

159-
let _: (repeat each T, y: Float) = returnTupleLabel4() // expected-error {{'(Int, x: T..., y: Float)' is not convertible to '(T..., y: Float)', tuples have a different number of elements}}
159+
let _: (repeat each T, y: Float) = returnTupleLabel4() // expected-error {{'(Int, x: repeat each T, y: Float)' is not convertible to '(repeat each T, y: Float)', tuples have a different number of elements}}
160160
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
161161

162-
let _: (Int, x: String, y: repeat each T) = returnTupleLabel4() // expected-error {{cannot convert value of type '(Int, x: String, y: Float)' to specified type '(Int, x: String, y: T...)'}}
162+
let _: (Int, x: String, y: repeat each T) = returnTupleLabel4() // expected-error {{cannot convert value of type '(Int, x: String, y: Float)' to specified type '(Int, x: String, y: repeat each T)'}}
163163

164164
let _: (Int, repeat each T, x: repeat each T) = returnTupleLabel5()
165-
// expected-error@-1 {{cannot convert value of type '(Int, T..., y: U...)' to specified type '(Int, T..., x: T...)'}}
165+
// expected-error@-1 {{cannot convert value of type '(Int, repeat each T, y: repeat each U)' to specified type '(Int, repeat each T, x: repeat each T)'}}
166166
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
167167
// expected-error@-3 {{generic parameter 'U' could not be inferred}}
168168

169169
let _: (repeat each T, y: Float, repeat each T) = returnTupleLabel5()
170-
// expected-error@-1 {{cannot convert value of type '(Int, T..., y: U...)' to specified type '(T..., y: Float, T...)'}}
170+
// expected-error@-1 {{cannot convert value of type '(Int, repeat each T, y: repeat each U)' to specified type '(repeat each T, y: Float, repeat each T)'}}
171171
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
172172
// expected-error@-3 {{generic parameter 'U' could not be inferred}}
173173

174-
let _: (repeat each T, y: Int) = returnTupleLabel6() // expected-error {{'(Int, x: T..., y: U...)' is not convertible to '(T..., y: Int)', tuples have a different number of elements}}
174+
let _: (repeat each T, y: Int) = returnTupleLabel6() // expected-error {{'(Int, x: repeat each T, y: repeat each U)' is not convertible to '(repeat each T, y: Int)', tuples have a different number of elements}}
175175
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
176176
// expected-error@-2 {{generic parameter 'U' could not be inferred}}
177177
}
@@ -203,22 +203,22 @@ func concreteReturnFunctionValid() {
203203
}
204204

205205
func concreteReturnFunctionInvalid() {
206-
let _: () -> () = returnFunction2() // expected-error {{cannot convert value of type '(Int, T...) -> ()' to specified type '() -> ()'}}
206+
let _: () -> () = returnFunction2() // expected-error {{cannot convert value of type '(Int, repeat each T) -> ()' to specified type '() -> ()'}}
207207
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
208208

209209
let _: (String) -> () = returnFunction2() // expected-error {{cannot convert value of type '(Int) -> ()' to specified type '(String) -> ()'}}
210210
let _: (String, Int) -> () = returnFunction2() // expected-error {{cannot convert value of type '(Int, Int) -> ()' to specified type '(String, Int) -> ()'}}
211211

212-
let _: () -> () = returnFunction3() // expected-error {{cannot convert value of type '(T..., Float) -> ()' to specified type '() -> ()'}}
212+
let _: () -> () = returnFunction3() // expected-error {{cannot convert value of type '(repeat each T, Float) -> ()' to specified type '() -> ()'}}
213213
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
214214

215215
let _: (Float, Int) -> () = returnFunction3() // expected-error {{cannot convert value of type '(Float, Float) -> ()' to specified type '(Float, Int) -> ()'}}
216216
let _: (Float, Double, String) -> () = returnFunction3() // expected-error {{cannot convert value of type '(Float, Double, Float) -> ()' to specified type '(Float, Double, String) -> ()'}}
217217

218-
let _: () -> () = returnFunction4() // expected-error {{cannot convert value of type '(Int, T..., Float) -> ()' to specified type '() -> ()'}}
218+
let _: () -> () = returnFunction4() // expected-error {{cannot convert value of type '(Int, repeat each T, Float) -> ()' to specified type '() -> ()'}}
219219
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
220220

221-
let _: (Int) -> () = returnFunction4() // expected-error {{cannot convert value of type '(Int, T..., Float) -> ()' to specified type '(Int) -> ()'}}
221+
let _: (Int) -> () = returnFunction4() // expected-error {{cannot convert value of type '(Int, repeat each T, Float) -> ()' to specified type '(Int) -> ()'}}
222222
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
223223

224224
let _: (Float, Int) -> () = returnFunction4() // expected-error {{cannot convert value of type '(Int, Float) -> ()' to specified type '(Float, Int) -> ()'}}
@@ -280,7 +280,7 @@ func patternInstantiationGenericValid<T..., U...>(t: repeat each T, u: repeat ea
280280
}
281281

282282
func patternInstantiationGenericInvalid<T...>(t: repeat each T) where T: Hashable {
283-
let _: (repeat Set<each T>) = patternInstantiationTupleTest1() // expected-error {{cannot convert value of type '(Array<T>...)' to specified type '(Set<T>...)}}
283+
let _: (repeat Set<each T>) = patternInstantiationTupleTest1() // expected-error {{cannot convert value of type '(repeat Array<each T>)' to specified type '(repeat Set<each T>)}}
284284
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
285285

286286
let _: (repeat Array<each T>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{type of expression is ambiguous without more context}}

test/Constraints/variadic_generic_functions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func call() {
3838

3939
func multipleSequences<T..., U...>(xs: repeat each T, ys: repeat each U) -> (repeat each T) {
4040
return (repeat each ys)
41-
// expected-error@-1 {{cannot convert return expression of type '(U...)' to return type '(T...)'}}
41+
// expected-error@-1 {{cannot convert return expression of type '(repeat each U)' to return type '(repeat each T)'}}
4242
}
4343

4444
multipleSequences()

test/ModuleInterface/pack_expansion_type.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
// Experimental features require an asserts compiler
66
// REQUIRES: asserts
77

8-
// CHECK: public func variadicFunction<T..., U...>(t: T..., u: U...) -> ((T, U)...) where ((T, U)...) : Any
9-
public func variadicFunction<T..., U...>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) {}
8+
// CHECK: public func variadicFunction<T..., U...>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) where ((T, U)...) : Any
9+
public func variadicFunction<T..., U...>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) {}
1010

1111
// CHECK: public struct VariadicType<T...> {
1212
public struct VariadicType<T...> {
13-
// CHECK: public func variadicMethod<U...>(t: T..., u: U...) -> ((T, U)...) where ((T, U)...) : Any
13+
// CHECK: public func variadicMethod<U...>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) where ((T, U)...) : Any
1414
public func variadicMethod<U...>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) {}
1515

16-
// CHECK: public func returnsSelf() -> PackExpansionType.VariadicType<T...>
16+
// CHECK: public func returnsSelf() -> PackExpansionType.VariadicType<repeat each T>
1717
public func returnsSelf() -> Self {}
1818
}
1919
// CHECK: }
@@ -24,10 +24,10 @@ public func returnsVariadicType() -> VariadicType< > {}
2424
// CHECK: public func returnsVariadicType() -> PackExpansionType.VariadicType<Swift.Int, Swift.String, Swift.Float>
2525
public func returnsVariadicType() -> VariadicType<Int, String, Float> {}
2626

27-
// CHECK: public func returnsVariadicType<T...>() -> PackExpansionType.VariadicType<T...>
27+
// CHECK: public func returnsVariadicType<T...>() -> PackExpansionType.VariadicType<repeat each T>
2828
public func returnsVariadicType<T...>() -> VariadicType<repeat each T> {}
2929

30-
// CHECK: public typealias VariadicAlias<T...> = PackExpansionType.VariadicType<Swift.Array<T>...>
30+
// CHECK: public typealias VariadicAlias<T...> = PackExpansionType.VariadicType<repeat Swift.Array<each T>>
3131
public typealias VariadicAlias<T...> = VariadicType<repeat Array<each T>>
3232

3333
// CHECK: public func returnsVariadicAlias() -> PackExpansionType.VariadicAlias<>
@@ -36,5 +36,5 @@ public func returnsVariadicAlias() -> VariadicAlias< > {}
3636
// CHECK: public func returnsVariadicAlias() -> PackExpansionType.VariadicAlias<Swift.Int, Swift.String, Swift.Float>
3737
public func returnsVariadicAlias() -> VariadicAlias<Int, String, Float> {}
3838

39-
// CHECK: public func returnsVariadicAlias<T...>() -> PackExpansionType.VariadicAlias<T...>
39+
// CHECK: public func returnsVariadicAlias<T...>() -> PackExpansionType.VariadicAlias<repeat each T>
4040
public func returnsVariadicAlias<T...>() -> VariadicAlias<repeat each T> {}

0 commit comments

Comments
 (0)