Skip to content

Commit 994e1b3

Browse files
authored
Merge pull request #66653 from hborla/5.9-pack-diagnostics
[5.9][Diagnostics] Suppress printing explicit pack types in the ASTPrinter instead of stripping `PackType` out of diagnostic arguments.
2 parents 6033bad + 20dc12c commit 994e1b3

9 files changed

+31
-32
lines changed

include/swift/AST/PrintOptions.h

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

560+
/// Whether to always print explicit `Pack{...}` around pack
561+
/// types.
562+
///
563+
/// This is set to \c false for diagnostic arguments.
564+
bool PrintExplicitPackTypes = true;
565+
560566
/// \see ShouldQualifyNestedDeclarations
561567
enum class QualifyNestedDeclarations {
562568
Never,
@@ -611,6 +617,7 @@ struct PrintOptions {
611617
static PrintOptions forDiagnosticArguments() {
612618
PrintOptions result;
613619
result.PrintExplicitAny = true;
620+
result.PrintExplicitPackTypes = false;
614621
return result;
615622
}
616623

lib/AST/ASTPrinter.cpp

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

60236023
void visitPackType(PackType *T) {
6024-
Printer << "Pack{";
6024+
if (Options.PrintExplicitPackTypes)
6025+
Printer << "Pack{";
60256026

60266027
auto Fields = T->getElementTypes();
60276028
for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
@@ -6030,7 +6031,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60306031
Type EltType = Fields[i];
60316032
visit(EltType);
60326033
}
6033-
Printer << "}";
6034+
6035+
if (Options.PrintExplicitPackTypes)
6036+
Printer << "}";
60346037
}
60356038

60366039
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
});

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,8 @@ static std::string gatherGenericParamBindingsText(
838838

839839
SmallString<128> result;
840840
llvm::raw_svector_ostream OS(result);
841+
auto options = PrintOptions::forDiagnosticArguments();
842+
options.PrintExplicitAny = false;
841843

842844
for (auto gp : genericParams) {
843845
auto canonGP = gp->getCanonicalType()->castTo<GenericTypeParamType>();
@@ -859,19 +861,7 @@ static std::string gatherGenericParamBindingsText(
859861
if (!type)
860862
return "";
861863

862-
if (auto *packType = type->getAs<PackType>()) {
863-
bool first = true;
864-
for (auto eltType : packType->getElementTypes()) {
865-
if (first)
866-
first = false;
867-
else
868-
OS << ", ";
869-
870-
OS << eltType;
871-
}
872-
} else {
873-
OS << type.getString();
874-
}
864+
type->print(OS, options);
875865
}
876866

877867
OS << "]";

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'}}
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+
}

test/Generics/variadic_generic_requirements.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ struct Outer<each T: Sequence> {
3030
}
3131

3232
_ = Outer<Array<Int>, Array<String>>.Inner<Set<Int>, Set<String>>.self // ok
33-
_ = Outer<Array<Int>, Array<String>>.Inner<Set<String>, Set<Int>>.self // expected-error {{'Outer<Array<Int>, Array<String>>.Inner' requires the types 'Pack{Int, String}' and 'Pack{String, Int}' be equivalent}}
34-
_ = Outer<Array<Int>>.Inner<Set<Int>, Set<String>>.self // expected-error {{'Outer<Array<Int>>.Inner' requires the types 'Pack{Int}' and 'Pack{Int, String}' be equivalent}}
33+
_ = Outer<Array<Int>, Array<String>>.Inner<Set<String>, Set<Int>>.self // expected-error {{'Outer<Array<Int>, Array<String>>.Inner' requires the types 'Int, String' and 'String, Int' be equivalent}}
34+
_ = Outer<Array<Int>>.Inner<Set<Int>, Set<String>>.self // expected-error {{'Outer<Array<Int>>.Inner' requires the types 'Int' and 'Int, String' be equivalent}}
3535

3636
_ = Outer<Array<Int>, Array<String>>.InnerShape<Set<String>, Set<Int>>.self // ok
37-
_ = Outer<Array<Int>>.InnerShape<Set<Int>, Set<String>>.self // expected-error {{'Outer<Array<Int>>.InnerShape' requires the type packs 'Pack{Array<Int>}' and 'Pack{Set<Int>, Set<String>}' have the same shape}}
37+
_ = Outer<Array<Int>>.InnerShape<Set<Int>, Set<String>>.self // expected-error {{'Outer<Array<Int>>.InnerShape' requires the type packs 'Array<Int>' and 'Set<Int>, Set<String>' have the same shape}}

0 commit comments

Comments
 (0)