Skip to content

Commit 7017c11

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

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;
@@ -5710,10 +5710,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
57105710
if (!Options.PrintExplicitAny)
57115711
return isSimpleUnderPrintOptions(existential->getInstanceType());
57125712
} else if (auto param = dyn_cast<GenericTypeParamType>(T.getPointer())) {
5713-
if (param->isParameterPack() && Options.PrintExplicitEach)
5713+
if (param->isParameterPack())
57145714
return false;
57155715
} else if (auto archetype = dyn_cast<ArchetypeType>(T.getPointer())) {
5716-
if (archetype->isParameterPack() && Options.PrintExplicitEach)
5716+
if (archetype->isParameterPack())
57175717
return false;
57185718
if (Options.PrintForSIL && isa<LocalArchetypeType>(archetype))
57195719
return false;
@@ -6049,11 +6049,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60496049
}
60506050

60516051
void visitPackExpansionType(PackExpansionType *T) {
6052-
PrintOptions innerOptions = Options;
6053-
innerOptions.PrintExplicitEach = true;
6054-
60556052
Printer << "repeat ";
6056-
TypePrinter(Printer, innerOptions).visit(T->getPatternType());
6053+
visit(T->getPatternType());
60576054
}
60586055

60596056
void visitTupleType(TupleType *T) {
@@ -6963,8 +6960,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
69636960
}
69646961

69656962
void printEach() {
6966-
if (Options.PrintExplicitEach)
6967-
Printer << "each ";
6963+
Printer << "each ";
69686964
}
69696965

69706966
void printArchetypeCommon(ArchetypeType *T) {

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4626,7 +4626,8 @@ NeverNullType TypeResolver::resolvePackElement(PackElementTypeRepr *repr,
46264626
return packReference;
46274627
}
46284628

4629-
if (!options.contains(TypeResolutionFlags::AllowPackReferences)) {
4629+
if (!options.contains(TypeResolutionFlags::AllowPackReferences) &&
4630+
!options.contains(TypeResolutionFlags::SILMode)) {
46304631
ctx.Diags.diagnose(repr->getLoc(),
46314632
diag::pack_reference_outside_expansion,
46324633
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 {
@@ -257,24 +257,24 @@ func forwardFunctionPack<each T>(functions: repeat (each T) -> Bool) {
257257

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

262262
forward(t)
263-
// expected-error@-1{{pack reference 'T' can only appear in pack expansion}}
263+
// expected-error@-1{{pack reference 'each T' can only appear in pack expansion}}
264264

265265
_ = each t
266-
// expected-error@-1{{pack reference 'T' can only appear in pack expansion}}
266+
// expected-error@-1{{pack reference 'each T' can only appear in pack expansion}}
267267

268268
forward(each t)
269-
// expected-error@-1{{pack reference 'T' can only appear in pack expansion}}
269+
// expected-error@-1{{pack reference 'each T' can only appear in pack expansion}}
270270

271271
let tuple = (repeat each t)
272272

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

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

280280
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)