Skip to content

[5.9][Diagnostics] Suppress printing explicit pack types in the ASTPrinter instead of stripping PackType out of diagnostic arguments. #66653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,12 @@ struct PrintOptions {
/// Whether to always desugar optional types from `base_type?` to `Optional<base_type>`
bool AlwaysDesugarOptionalTypes = false;

/// Whether to always print explicit `Pack{...}` around pack
/// types.
///
/// This is set to \c false for diagnostic arguments.
bool PrintExplicitPackTypes = true;

/// \see ShouldQualifyNestedDeclarations
enum class QualifyNestedDeclarations {
Never,
Expand Down Expand Up @@ -611,6 +617,7 @@ struct PrintOptions {
static PrintOptions forDiagnosticArguments() {
PrintOptions result;
result.PrintExplicitAny = true;
result.PrintExplicitPackTypes = false;
return result;
}

Expand Down
7 changes: 5 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6021,7 +6021,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}

void visitPackType(PackType *T) {
Printer << "Pack{";
if (Options.PrintExplicitPackTypes)
Printer << "Pack{";

auto Fields = T->getElementTypes();
for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Expand All @@ -6030,7 +6031,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
Type EltType = Fields[i];
visit(EltType);
}
Printer << "}";

if (Options.PrintExplicitPackTypes)
Printer << "}";
}

void visitSILPackType(SILPackType *T) {
Expand Down
8 changes: 0 additions & 8 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,6 @@ Type FailureDiagnostic::resolveType(Type rawType, bool reconstituteSugar,
return env->mapElementTypeIntoPackContext(type);
}

if (auto *packType = type->getAs<PackType>()) {
if (packType->getNumElements() == 1) {
auto eltType = resolveType(packType->getElementType(0));
if (auto expansion = eltType->getAs<PackExpansionType>())
return expansion->getPatternType();
}
}

return type->isPlaceholder() ? Type(type->getASTContext().TheUnresolvedType)
: type;
});
Expand Down
16 changes: 3 additions & 13 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,8 @@ static std::string gatherGenericParamBindingsText(

SmallString<128> result;
llvm::raw_svector_ostream OS(result);
auto options = PrintOptions::forDiagnosticArguments();
options.PrintExplicitAny = false;

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

if (auto *packType = type->getAs<PackType>()) {
bool first = true;
for (auto eltType : packType->getElementTypes()) {
if (first)
first = false;
else
OS << ", ";

OS << eltType;
}
} else {
OS << type.getString();
}
type->print(OS, options);
}

OS << "]";
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func tupleExpansion<each T, each U>(
_ = zip(repeat each tuple1, with: repeat each tuple1.element) // legacy syntax

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

_ = forward(repeat each tuple3)
}
Expand Down
6 changes: 3 additions & 3 deletions test/Constraints/pack_expansion_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ func patternInstantiationConcreteValid() {

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

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

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

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

// rdar://107996926 - Vanishing metatype of tuple not supported
Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/variadic_generic_constraints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ let _ = zip(t: 1, u: "hi") // ok
let _ = zip(t: 1, 2, u: "hi", "hello") // ok
let _ = zip(t: 1, 2, 3, u: "hi", "hello", "greetings") // ok
let _ = zip(t: 1, u: "hi", "hello", "greetings") // expected-error {{extra arguments at positions #3, #4 in call}}
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'Pack{Int}' and 'Pack{String, String, String}' have the same shape}}
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'Int' and 'String, String, String' have the same shape}}

func goodCallToZip<each T, each U>(t: repeat each T, u: repeat each U) where (repeat (each T, each U)): Any {
_ = zip(t: repeat each t, u: repeat each u)
}

func badCallToZip<each T, each U>(t: repeat each T, u: repeat each U) {
_ = zip(t: repeat each t, u: repeat each u)
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'each T' and 'each U' have the same shape}}
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'repeat each T' and 'repeat each U' have the same shape}}
}
7 changes: 7 additions & 0 deletions test/Constraints/variadic_generic_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ func g<each T>(_: repeat each T) {
// expected-error@-1 {{pack expansion 'Int' must contain at least one pack reference}}
// expected-error@-2 {{'each' cannot be applied to non-pack type 'Int'}}
}

struct MissingMemberError<each T> {
init() {
self.doesNotExist = 1
// expected-error@-1 {{value of type 'MissingMemberError<repeat each T>' has no member 'doesNotExist'}}
}
}
6 changes: 3 additions & 3 deletions test/Generics/variadic_generic_requirements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ struct Outer<each T: Sequence> {
}

_ = Outer<Array<Int>, Array<String>>.Inner<Set<Int>, Set<String>>.self // ok
_ = 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}}
_ = 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}}
_ = 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}}
_ = Outer<Array<Int>>.Inner<Set<Int>, Set<String>>.self // expected-error {{'Outer<Array<Int>>.Inner' requires the types 'Int' and 'Int, String' be equivalent}}

_ = Outer<Array<Int>, Array<String>>.InnerShape<Set<String>, Set<Int>>.self // ok
_ = 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}}
_ = 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}}