Skip to content

Commit 2c4f28b

Browse files
committed
[Diagnostics] Suppress printing explicit pack types in the ASTPrinter instead of
stripping PackType out of diagnostic arguments. There are places in the type printing code that assume the substitution for a type parameter pack is always a pack, and violating that invariant will crash the compiler. We also never want to print 'Pack{...}' in diagnostics anyway, so the print option is a better approach and fixes a few existing tests that still contained 'Pack{...}' in error messages.
1 parent d6fe625 commit 2c4f28b

File tree

7 files changed

+25
-16
lines changed

7 files changed

+25
-16
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,12 @@ struct PrintOptions {
553553
/// Whether to always desugar optional types from `base_type?` to `Optional<base_type>`
554554
bool AlwaysDesugarOptionalTypes = false;
555555

556+
/// Whether to always print explicit `Pack{...}` around pack
557+
/// types.
558+
///
559+
/// This is set to \c false for diagnostic arguments.
560+
bool PrintExplicitPackTypes = true;
561+
556562
/// \see ShouldQualifyNestedDeclarations
557563
enum class QualifyNestedDeclarations {
558564
Never,
@@ -606,6 +612,7 @@ struct PrintOptions {
606612
/// The print options used for formatting diagnostic arguments.
607613
static PrintOptions forDiagnosticArguments() {
608614
PrintOptions result;
615+
result.PrintExplicitPackTypes = false;
609616
return result;
610617
}
611618

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6051,7 +6051,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60516051
}
60526052

60536053
void visitPackType(PackType *T) {
6054-
Printer << "Pack{";
6054+
if (Options.PrintExplicitPackTypes)
6055+
Printer << "Pack{";
60556056

60566057
auto Fields = T->getElementTypes();
60576058
for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
@@ -6060,7 +6061,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60606061
Type EltType = Fields[i];
60616062
visit(EltType);
60626063
}
6063-
Printer << "}";
6064+
6065+
if (Options.PrintExplicitPackTypes)
6066+
Printer << "}";
60646067
}
60656068

60666069
void visitSILPackType(SILPackType *T) {

lib/Sema/CSDiagnostics.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,6 @@ Type FailureDiagnostic::resolveType(Type rawType, bool reconstituteSugar,
120120
return env->mapElementTypeIntoPackContext(type);
121121
}
122122

123-
if (auto *packType = type->getAs<PackType>()) {
124-
if (packType->getNumElements() == 1) {
125-
auto eltType = resolveType(packType->getElementType(0));
126-
if (auto expansion = eltType->getAs<PackExpansionType>())
127-
return expansion->getPatternType();
128-
}
129-
}
130-
131123
return type->isPlaceholder() ? Type(type->getASTContext().TheUnresolvedType)
132124
: type;
133125
});

test/Constraints/pack-expansion-expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func tupleExpansion<each T, each U>(
138138
_ = zip(repeat each tuple1, with: repeat each tuple1.element) // legacy syntax
139139

140140
_ = zip(repeat each tuple1, with: repeat each tuple2)
141-
// expected-error@-1 {{global function 'zip(_:with:)' requires the type packs 'each T' and 'each U' have the same shape}}
141+
// expected-error@-1 {{global function 'zip(_:with:)' requires the type packs 'repeat each T' and 'repeat each U' have the same shape}}
142142

143143
_ = forward(repeat each tuple3)
144144
}

test/Constraints/pack_expansion_types.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ func patternInstantiationConcreteValid() {
240240

241241
func patternInstantiationConcreteInvalid() {
242242
let _: Set<Int> = patternInstantiationTupleTest1()
243-
// expected-error@-1 {{cannot convert value of type '(repeat Array<Pack{_}>)' to specified type 'Set<Int>'}}
243+
// expected-error@-1 {{cannot convert value of type '(repeat Array<_>)' to specified type 'Set<Int>'}}
244244

245-
let _: (Array<Int>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<Pack{Int, _}>)' is not convertible to '(Array<Int>, Set<String>)', tuples have a different number of elements}}
245+
let _: (Array<Int>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<Int, _>)' is not convertible to '(Array<Int>, Set<String>)', tuples have a different number of elements}}
246246
}
247247

248248
func patternInstantiationGenericValid<each T, each U>(t: repeat each T, u: repeat each U)
@@ -272,7 +272,7 @@ func patternInstantiationGenericInvalid<each T: Hashable>(t: repeat each T) {
272272
let _: (repeat Set<each T>) = patternInstantiationTupleTest1() // expected-error {{cannot convert value of type '(repeat Array<each T>)' to specified type '(repeat Set<each T>)}}
273273
// expected-error@-1 {{generic parameter 'each T' could not be inferred}}
274274

275-
let _: (repeat Array<each T>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<Pack{repeat each T, _}>)' is not convertible to '(repeat Array<each T>, Set<String>)', tuples have a different number of elements}}
275+
let _: (repeat Array<each T>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<repeat each T, _>)' is not convertible to '(repeat Array<each T>, Set<String>)', tuples have a different number of elements}}
276276
}
277277

278278
// rdar://107996926 - Vanishing metatype of tuple not supported

test/Constraints/variadic_generic_constraints.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ let _ = zip(t: 1, u: "hi") // ok
6565
let _ = zip(t: 1, 2, u: "hi", "hello") // ok
6666
let _ = zip(t: 1, 2, 3, u: "hi", "hello", "greetings") // ok
6767
let _ = zip(t: 1, u: "hi", "hello", "greetings") // expected-error {{extra arguments at positions #3, #4 in call}}
68-
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'Pack{Int}' and 'Pack{String, String, String}' have the same shape}}
68+
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'Int' and 'String, String, String' have the same shape}}
6969

7070
func goodCallToZip<each T, each U>(t: repeat each T, u: repeat each U) where (repeat (each T, each U)): Any {
7171
_ = zip(t: repeat each t, u: repeat each u)
7272
}
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 'each T' and 'each U' have the same shape}}
76+
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'repeat each T' and 'repeat each U' have the same shape}}
7777
}

test/Constraints/variadic_generic_types.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ func g<each T>(_: repeat each T) {
2222
// expected-error@-1 {{pack expansion 'Int' must contain at least one pack reference}}
2323
// expected-error@-2 {{'each' cannot be applied to non-pack type 'Int'}}{{15-19=}}
2424
}
25+
26+
struct MissingMemberError<each T> {
27+
init() {
28+
self.doesNotExist = 1
29+
// expected-error@-1 {{value of type 'MissingMemberError<repeat each T>' has no member 'doesNotExist'}}
30+
}
31+
}

0 commit comments

Comments
 (0)