Skip to content

Commit df5d531

Browse files
committed
ASTPrinter: Fix printing of pack requirements
Also remove the flag for printing 'each' and make it always on.
1 parent 563ebde commit df5d531

21 files changed

+221
-222
lines changed

include/swift/AST/PrintOptions.h

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

294294
bool PrintImplicitAttrs = true;
295295

296-
/// Whether to print the \c each keyword for pack archetypes.
297-
bool PrintExplicitEach = false;
298-
299296
/// Whether to print the \c any keyword for existential
300297
/// types.
301298
bool PrintExplicitAny = false;

lib/AST/ASTPrinter.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,11 +1732,11 @@ void PrintAST::printSingleDepthOfGenericSignature(
17321732
llvm::interleave(
17331733
genericParams,
17341734
[&](GenericTypeParamType *param) {
1735-
if (param->isParameterPack())
1736-
Printer << "each ";
17371735
if (!subMap.empty()) {
17381736
printType(substParam(param));
17391737
} else if (auto *GP = param->getDecl()) {
1738+
if (param->isParameterPack())
1739+
Printer << "each ";
17401740
Printer.callPrintStructurePre(PrintStructureKind::GenericParameter,
17411741
GP);
17421742
Printer.printName(GP->getName(),
@@ -1835,9 +1835,9 @@ void PrintAST::printSingleDepthOfGenericSignature(
18351835
void PrintAST::printRequirement(const Requirement &req) {
18361836
switch (req.getKind()) {
18371837
case RequirementKind::SameShape:
1838-
Printer << "(repeat (each ";
1838+
Printer << "(repeat (";
18391839
printTransformedType(req.getFirstType());
1840-
Printer << ", each ";
1840+
Printer << ", ";
18411841
printTransformedType(req.getSecondType());
18421842
Printer << ")) : Any";
18431843
return;
@@ -5714,10 +5714,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
57145714
if (!Options.PrintExplicitAny)
57155715
return isSimpleUnderPrintOptions(existential->getInstanceType());
57165716
} else if (auto param = dyn_cast<GenericTypeParamType>(T.getPointer())) {
5717-
if (param->isParameterPack() && Options.PrintExplicitEach)
5717+
if (param->isParameterPack())
57185718
return false;
57195719
} else if (auto archetype = dyn_cast<ArchetypeType>(T.getPointer())) {
5720-
if (archetype->isParameterPack() && Options.PrintExplicitEach)
5720+
if (archetype->isParameterPack())
57215721
return false;
57225722
if (Options.PrintForSIL && isa<LocalArchetypeType>(archetype))
57235723
return false;
@@ -6053,11 +6053,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60536053
}
60546054

60556055
void visitPackExpansionType(PackExpansionType *T) {
6056-
PrintOptions innerOptions = Options;
6057-
innerOptions.PrintExplicitEach = true;
6058-
60596056
Printer << "repeat ";
6060-
TypePrinter(Printer, innerOptions).visit(T->getPatternType());
6057+
visit(T->getPatternType());
60616058
}
60626059

60636060
void visitTupleType(TupleType *T) {
@@ -6967,8 +6964,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
69676964
}
69686965

69696966
void printEach() {
6970-
if (Options.PrintExplicitEach)
6971-
Printer << "each ";
6967+
Printer << "each ";
69726968
}
69736969

69746970
void printArchetypeCommon(ArchetypeType *T) {

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4634,7 +4634,8 @@ NeverNullType TypeResolver::resolvePackElement(PackElementTypeRepr *repr,
46344634
return packReference;
46354635
}
46364636

4637-
if (!options.contains(TypeResolutionFlags::AllowPackReferences)) {
4637+
if (!options.contains(TypeResolutionFlags::AllowPackReferences) &&
4638+
!options.contains(TypeResolutionFlags::SILMode)) {
46384639
ctx.Diags.diagnose(repr->getLoc(),
46394640
diag::pack_reference_outside_expansion,
46404641
packReference);

test/Constraints/pack-expansion-expressions.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ func typeReprPacks<each T: ExpressibleByIntegerLiteral>(_ t: repeat each T) {
8383
}
8484

8585
func sameShapeDiagnostics<each T, each U>(t: repeat each T, u: repeat each U) {
86-
_ = (repeat (each t, each u)) // expected-error {{pack expansion requires that 'U' and 'T' have the same shape}}
87-
_ = (repeat Array<(each T, each U)>()) // expected-error {{pack expansion requires that 'U' and 'T' have the same shape}}
88-
_ = (repeat (Array<each T>(), each u)) // expected-error {{pack expansion requires that 'U' and 'T' have the same shape}}
86+
_ = (repeat (each t, each u)) // expected-error {{pack expansion requires that 'each U' and 'each T' have the same shape}}
87+
_ = (repeat Array<(each T, each U)>()) // expected-error {{pack expansion requires that 'each U' and 'each T' have the same shape}}
88+
_ = (repeat (Array<each T>(), each u)) // expected-error {{pack expansion requires that 'each U' and 'each T' have the same shape}}
8989
}
9090

9191
func returnPackExpansionType<each T>(_ t: repeat each T) -> repeat each T { // expected-error {{pack expansion 'repeat each T' can only appear in a function parameter list, tuple element, or generic argument list}}
@@ -133,7 +133,7 @@ func tupleExpansion<each T, each U>(
133133
_ = zip(repeat each tuple1.element, with: repeat each tuple1.element)
134134

135135
_ = zip(repeat each tuple1.element, with: repeat each tuple2.element)
136-
// expected-error@-1 {{global function 'zip(_:with:)' requires the type packs 'U' and 'T' have the same shape}}
136+
// expected-error@-1 {{global function 'zip(_:with:)' requires the type packs 'each U' and 'each T' have the same shape}}
137137
}
138138

139139
protocol Generatable {
@@ -258,24 +258,24 @@ func forwardFunctionPack<each T>(functions: repeat (each T) -> Bool) {
258258

259259
func packOutsideExpansion<each T>(_ t: repeat each T) {
260260
_ = t
261-
// expected-error@-1{{pack reference 'T' can only appear in pack expansion}}
261+
// expected-error@-1{{pack reference 'each T' can only appear in pack expansion}}
262262

263263
forward(t)
264-
// expected-error@-1{{pack reference 'T' can only appear in pack expansion}}
264+
// expected-error@-1{{pack reference 'each T' can only appear in pack expansion}}
265265

266266
_ = each t
267-
// expected-error@-1{{pack reference 'T' can only appear in pack expansion}}
267+
// expected-error@-1{{pack reference 'each T' can only appear in pack expansion}}
268268

269269
forward(each t)
270-
// expected-error@-1{{pack reference 'T' can only appear in pack expansion}}
270+
// expected-error@-1{{pack reference 'each T' can only appear in pack expansion}}
271271

272272
let tuple = (repeat each t)
273273

274274
_ = tuple.element
275-
// expected-error@-1{{pack reference 'T' can only appear in pack expansion}}
275+
// expected-error@-1{{pack reference 'each T' can only appear in pack expansion}}
276276

277277
_ = each tuple.element
278-
// expected-error@-1{{pack reference 'T' can only appear in pack expansion}}
278+
// expected-error@-1{{pack reference 'each T' can only appear in pack expansion}}
279279
}
280280

281281
func identity<T>(_ t: T) -> T { t }

test/Constraints/pack_expansion_types.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ func concreteReturnTupleValid() {
2929
// FIXME: consider propagating 'Int' through the conversion constraint
3030
// as a binding for the parameter pack expanded in the tuple return type.
3131
let _: Int = returnTuple1()
32-
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
32+
// expected-error@-1 {{generic parameter 'each T' could not be inferred}}
3333
// expected-error@-2 {{cannot convert value of type '(repeat each T)' to specified type 'Int'}}
3434
let _: (Int, String) = returnTuple1()
3535

3636
let _: Int = returnTuple2()
37-
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
37+
// expected-error@-1 {{generic parameter 'each T' could not be inferred}}
3838
// expected-error@-2 {{cannot convert value of type '(Int, repeat each T)' to specified type 'Int'}}
3939
let _: (Int, String) = returnTuple2()
4040
let _: (Int, String, Float) = returnTuple2()
@@ -73,15 +73,15 @@ func concreteReturnTupleValid() {
7373
func concreteReturnTypeInvalid() {
7474
let _: Int = returnTuple1()
7575
// expected-error@-1 {{cannot convert value of type '(repeat each T)' to specified type 'Int'}}
76-
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
76+
// expected-error@-2 {{generic parameter 'each T' could not be inferred}}
7777

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

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

8686
let _: (Int, Int, y: Float) = returnTupleLabel4() // error at declaration
8787
let _: () = returnTupleLabel5() // error at declaration
@@ -129,14 +129,14 @@ func genericReturnTupleInvalid<each T>(_: repeat each T) {
129129

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

134134
let _: (Int, x: repeat each T) = returnTuple2()
135135
// expected-error@-1 {{cannot use label with pack expansion tuple element}}
136136

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

141141
let _: (y: repeat each T) = returnTupleLabel1()
142142
// expected-error@-1 {{cannot use label with pack expansion tuple element}}
@@ -150,11 +150,11 @@ func genericReturnTupleInvalid<each T>(_: repeat each T) {
150150

151151
let _: (repeat each T, y: Float) = returnTupleLabel3()
152152
// 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}}
153-
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
153+
// expected-error@-2 {{generic parameter 'each T' could not be inferred}}
154154

155155
let _: (Int, String, repeat each T, x: Float) = returnTupleLabel3()
156156
// 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}}
157-
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
157+
// expected-error@-2 {{generic parameter 'each T' could not be inferred}}
158158

159159
let _: (repeat each T, y: Float) = returnTupleLabel4() // error at declaration
160160

@@ -197,22 +197,22 @@ func concreteReturnFunctionValid() {
197197

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

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

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

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

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

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

217217
let _: (Float, Int) -> () = returnFunction4() // expected-error {{cannot convert value of type '(Int, Float) -> ()' to specified type '(Float, Int) -> ()'}}
218218
}
@@ -229,16 +229,16 @@ func patternInstantiationConcreteValid() {
229229
let _: () = patternInstantiationTupleTest1()
230230
// FIXME
231231
let _: Array<Int> = patternInstantiationTupleTest1()
232-
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
232+
// expected-error@-1 {{generic parameter 'each T' could not be inferred}}
233233
// expected-error@-2 {{cannot convert value of type '(repeat Array<each T>)' to specified type 'Array<Int>'}}
234234
let _: (Array<Int>, Array<String>) = patternInstantiationTupleTest1()
235235
let _: (Array<Int>, Array<String>, Array<Float>) = patternInstantiationTupleTest1()
236236

237237
let _: () = patternInstantiationTupleTest2()
238238
// FIXME
239239
let _: Dictionary<Int, String> = patternInstantiationTupleTest2()
240-
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
241-
// expected-error@-2 {{generic parameter 'U' could not be inferred}}
240+
// expected-error@-1 {{generic parameter 'each T' could not be inferred}}
241+
// expected-error@-2 {{generic parameter 'each U' could not be inferred}}
242242
// expected-error@-3 {{cannot convert value of type '(repeat Dictionary<each T, each U>)' to specified type 'Dictionary<Int, String>'}}
243243
let _: (Dictionary<Int, String>, Dictionary<Float, Bool>) = patternInstantiationTupleTest2()
244244
let _: (Dictionary<Int, String>, Dictionary<Float, Bool>, Dictionary<Double, Character>) = patternInstantiationTupleTest2()
@@ -256,7 +256,7 @@ func patternInstantiationConcreteValid() {
256256

257257
func patternInstantiationConcreteInvalid() {
258258
let _: Set<Int> = patternInstantiationTupleTest1()
259-
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
259+
// expected-error@-1 {{generic parameter 'each T' could not be inferred}}
260260
// expected-error@-2 {{cannot convert value of type '(repeat Array<each T>)' to specified type 'Set<Int>'}}
261261

262262
let _: (Array<Int>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{type of expression is ambiguous without more context}}
@@ -287,7 +287,7 @@ func patternInstantiationGenericValid<each T, each U>(t: repeat each T, u: repea
287287

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

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

test/Constraints/variadic_generic_constraints.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
protocol P {}
99

10-
func takesP<each T: P>(_: repeat each T) {} // expected-note {{where 'T' = 'DoesNotConformToP'}}
10+
func takesP<each T: P>(_: repeat each T) {} // expected-note {{where 'each T' = 'DoesNotConformToP'}}
1111

1212
struct ConformsToP: P {}
1313
struct DoesNotConformToP {}
@@ -24,7 +24,7 @@ class C {}
2424
class SubclassOfC: C {}
2525
class NotSubclassOfC {}
2626

27-
func takesC<each T: C>(_: repeat each T) {} // expected-note {{where 'T' = 'NotSubclassOfC'}}
27+
func takesC<each T: C>(_: repeat each T) {} // expected-note {{where 'each T' = 'NotSubclassOfC'}}
2828

2929
takesC() // ok
3030
takesC(SubclassOfC(), SubclassOfC(), SubclassOfC()) // ok
@@ -45,7 +45,7 @@ takesAnyObject(C(), S(), C()) // expected-error {{type of expression is ambiguo
4545

4646
// Same-type requirements
4747

48-
// expected-note@+1 {{where 'T.Element' = 'String', 'U.Element' = 'Int'}}
48+
// expected-note@+1 {{where '(each T).Element' = 'String', '(each U).Element' = 'Int'}}
4949
func takesParallelSequences<each T, each U>(t: repeat each T, u: repeat each U)
5050
where repeat each T: Sequence,
5151
repeat each U: Sequence,
@@ -73,5 +73,5 @@ func goodCallToZip<each T, each U>(t: repeat each T, u: repeat each U) where (re
7373

7474
func badCallToZip<each T, each U>(t: repeat each T, u: repeat each U) {
7575
_ = zip(t: repeat each t, u: repeat each u)
76-
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'U' and 'T' have the same shape}}
76+
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'each U' and 'each T' have the same shape}}
7777
}

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
}
1313

1414
// CHECK-LABEL: desugarSameShape(ts:us:)
15-
// CHECK-NEXT: Generic signature: <each T, each U where repeat T : P, (repeat (each T, each U)) : Any, repeat U : P>
15+
// CHECK-NEXT: Generic signature: <each T, each U where repeat each T : P, (repeat (each T, each U)) : Any, repeat each U : P>
1616
func desugarSameShape<each T, each U>(ts t: repeat each T, us u: repeat each U)
1717
where repeat each T: P, repeat each U: P, (repeat (each T.A, each U.A)): Any {}
1818

@@ -49,13 +49,13 @@ func multipleSameShape6<each T, each U, each V>(ts t: repeat each T, us u: repea
4949
struct Ts<each T> {
5050
struct Us<each U> {
5151
// CHECK-LABEL: Ts.Us.packEquality()
52-
// CHECK-NEXT: Generic signature: <each T, each U where repeat T == U>
52+
// CHECK-NEXT: Generic signature: <each T, each U where repeat each T == each U>
5353
func packEquality() where repeat each T == each U, (repeat (each T, each U)): Any {
5454
}
5555

5656
struct Vs<each V> {
5757
// CHECK-LABEL: Ts.Us.Vs.packEquality()
58-
// CHECK-NEXT: Generic signature: <each T, each U, each V where repeat T == U, (repeat (each T, each V)) : Any>
58+
// CHECK-NEXT: Generic signature: <each T, each U, each V where repeat each T == each U, (repeat (each T, each V)) : Any>
5959
func packEquality() where repeat each T == each U, (repeat (each U, each V)): Any {
6060
}
6161
}

test/Generics/variadic_generic_requirements.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ class Class {}
1111
class OtherClass {}
1212
class Subclass: Class {}
1313

14-
struct Superclass<each T: Class> {} // expected-note {{requirement specified as 'T' : 'Class' [with each T = OtherClass]}}
14+
struct Superclass<each T: Class> {} // expected-note {{requirement specified as 'each T' : 'Class' [with each T = OtherClass]}}
1515

1616
_ = Superclass<Class, Subclass>.self // ok
1717
_ = Superclass<OtherClass>.self // expected-error {{'Superclass' requires that 'OtherClass' inherit from 'Class'}}
1818

19-
struct Layout<each T: AnyObject> {} // expected-note {{requirement specified as 'T' : 'AnyObject' [with each T = Int, String]}}
19+
struct Layout<each T: AnyObject> {} // expected-note {{requirement specified as 'each T' : 'AnyObject' [with each T = Int, String]}}
2020

2121
_ = Layout<Class, Subclass>.self // ok
2222
_ = Layout<Int, String>.self // expected-error {{'Layout' requires that 'Int' be a class type}}
2323

2424
struct Outer<each T: Sequence> {
2525
struct Inner<each U: Sequence> where repeat each T.Element == each U.Element {}
26-
// expected-note@-1 {{requirement specified as 'T.Element' == 'U.Element' [with each T = Array<Int>, Array<String>; each U = Set<String>, Set<Int>]}}
27-
// expected-note@-2 {{requirement specified as 'T.Element' == 'U.Element' [with each T = Array<Int>; each U = Set<Int>, Set<String>]}}
26+
// 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>]}}
27+
// expected-note@-2 {{requirement specified as '(each T).Element' == '(each U).Element' [with each T = Array<Int>; each U = Set<Int>, Set<String>]}}
2828

2929
struct InnerShape<each U: Sequence> where (repeat (each T, each U)): Any {}
30-
// expected-note@-1 {{same-shape requirement inferred between 'T' and 'U' [with each T = Array<Int>; each U = Set<Int>, Set<String>]}}
30+
// expected-note@-1 {{same-shape requirement inferred between 'each T' and 'each U' [with each T = Array<Int>; each U = Set<Int>, Set<String>]}}
3131

3232
}
3333

0 commit comments

Comments
 (0)