Skip to content

Commit b2285f5

Browse files
committed
ASTPrinter: Fix printing variadic BoundGenericTypes
1 parent 77a67e1 commit b2285f5

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5282,15 +5282,20 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
52825282
Optional<llvm::DenseMap<const clang::Module *, ModuleDecl *>>
52835283
VisibleClangModules;
52845284

5285-
void printGenericArgs(ArrayRef<Type> Args) {
5286-
if (Args.empty())
5287-
return;
5288-
5285+
void printGenericArgs(PackType *flatArgs) {
52895286
Printer << "<";
5290-
interleave(Args, [&](Type Arg) { visit(Arg); }, [&] { Printer << ", "; });
5287+
interleave(flatArgs->getElementTypes(),
5288+
[&](Type arg) { visit(arg); },
5289+
[&] { Printer << ", "; });
52915290
Printer << ">";
52925291
}
52935292

5293+
void printGenericArgs(ASTContext &ctx,
5294+
TypeArrayView<GenericTypeParamType> params,
5295+
ArrayRef<Type> args) {
5296+
printGenericArgs(PackType::get(ctx, params, args));
5297+
}
5298+
52945299
/// Helper function for printing a type that is embedded within a larger type.
52955300
///
52965301
/// This is necessary whenever the inner type may not normally be represented
@@ -5626,7 +5631,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
56265631
}
56275632

56285633
printQualifiedType(T);
5629-
printGenericArgs(T->getDirectGenericArgs());
5634+
5635+
auto *typeAliasDecl = T->getDecl();
5636+
if (typeAliasDecl->isGeneric()) {
5637+
printGenericArgs(T->getExpandedGenericArgsPack());
5638+
}
56305639
}
56315640

56325641
void visitParenType(ParenType *T) {
@@ -5711,7 +5720,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
57115720
}
57125721
}
57135722
printQualifiedType(T);
5714-
printGenericArgs(T->getGenericArgs());
5723+
5724+
printGenericArgs(T->getExpandedGenericArgsPack());
57155725
}
57165726

57175727
void visitParentType(Type T) {
@@ -6525,6 +6535,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
65256535
return false;
65266536
};
65276537

6538+
OpaqueTypeDecl *decl = T->getDecl();
6539+
auto *namingDecl = decl->getNamingDecl();
6540+
auto genericSig = namingDecl->getInnermostDeclContext()
6541+
->getGenericSignatureOfContext();
6542+
65286543
switch (Options.OpaqueReturnTypePrinting) {
65296544
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
65306545
if (printNamedOpaque())
@@ -6542,8 +6557,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
65426557

65436558
// Opaque archetype substitutions are always canonical, so re-sugar the
65446559
// constraint type using the owning declaration's generic parameter names.
6545-
auto genericSig = T->getDecl()->getNamingDecl()->getInnermostDeclContext()
6546-
->getGenericSignatureOfContext();
65476560
if (genericSig)
65486561
constraint = genericSig->getSugaredType(constraint);
65496562

@@ -6556,7 +6569,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
65566569
// turn this back into a reference to the naming decl for the opaque
65576570
// type.
65586571
Printer << "@_opaqueReturnTypeOf(";
6559-
OpaqueTypeDecl *decl = T->getDecl();
65606572

65616573
Printer.printEscapedStringLiteral(
65626574
decl->getOpaqueReturnTypeIdentifier().str());
@@ -6570,22 +6582,24 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
65706582
// attribute to apply to, but the attribute alone references the opaque
65716583
// type.
65726584
Printer << ") __";
6573-
printGenericArgs(T->getSubstitutions().getReplacementTypes());
6585+
6586+
if (genericSig) {
6587+
printGenericArgs(decl->getASTContext(),
6588+
genericSig.getGenericParams(),
6589+
T->getSubstitutions().getReplacementTypes());
6590+
}
65746591
return;
65756592
}
65766593
case PrintOptions::OpaqueReturnTypePrintingMode::Description: {
65776594
// TODO(opaque): present opaque types with user-facing syntax. we should
65786595
// probably print this as `some P` and record the fact that we printed that
65796596
// so that diagnostics can add followup notes.
6580-
Printer << "(return type of " << T->getDecl()->getNamingDecl()->printRef();
6597+
Printer << "(return type of " << namingDecl->printRef();
65816598
Printer << ')';
6582-
if (!T->getSubstitutions().empty()) {
6583-
Printer << '<';
6584-
auto replacements = T->getSubstitutions().getReplacementTypes();
6585-
llvm::interleave(
6586-
replacements.begin(), replacements.end(), [&](Type t) { visit(t); },
6587-
[&] { Printer << ", "; });
6588-
Printer << '>';
6599+
if (genericSig) {
6600+
printGenericArgs(decl->getASTContext(),
6601+
genericSig.getGenericParams(),
6602+
T->getSubstitutions().getReplacementTypes());
65896603
}
65906604
return;
65916605
}

test/ModuleInterface/pack_expansion_type.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,29 @@ public func variadicFunction<T..., U...>(t: T..., u: U...) -> ((T, U)...) {}
1212
public struct VariadicType<T...> {
1313
// CHECK: public func variadicMethod<U...>(t: T..., u: U...) -> ((T, U)...) where ((T, U)...) : Any
1414
public func variadicMethod<U...>(t: T..., u: U...) -> ((T, U)...) {}
15+
16+
// CHECK: public func returnsSelf() -> PackExpansionType.VariadicType<T...>
17+
public func returnsSelf() -> Self {}
1518
}
1619
// CHECK: }
20+
21+
// CHECK: public func returnsVariadicType() -> PackExpansionType.VariadicType<>
22+
public func returnsVariadicType() -> VariadicType< > {}
23+
24+
// CHECK: public func returnsVariadicType() -> PackExpansionType.VariadicType<Swift.Int, Swift.String, Swift.Float>
25+
public func returnsVariadicType() -> VariadicType<Int, String, Float> {}
26+
27+
// CHECK: public func returnsVariadicType<T...>() -> PackExpansionType.VariadicType<T...>
28+
public func returnsVariadicType<T...>() -> VariadicType<T... > {}
29+
30+
// CHECK: public typealias VariadicAlias<T...> = PackExpansionType.VariadicType<Swift.Array<T>...>
31+
public typealias VariadicAlias<T...> = VariadicType<Array<T>... >
32+
33+
// CHECK: public func returnsVariadicAlias() -> PackExpansionType.VariadicAlias<>
34+
public func returnsVariadicAlias() -> VariadicAlias< > {}
35+
36+
// CHECK: public func returnsVariadicAlias() -> PackExpansionType.VariadicAlias<Swift.Int, Swift.String, Swift.Float>
37+
public func returnsVariadicAlias() -> VariadicAlias<Int, String, Float> {}
38+
39+
// CHECK: public func returnsVariadicAlias<T...>() -> PackExpansionType.VariadicAlias<T...>
40+
public func returnsVariadicAlias<T...>() -> VariadicAlias<T... > {}

test/SILGen/pack_expansion_type.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public func variadicFunction<T..., U...>(t: T..., u: U...) -> ((T, U)...) {}
99

1010
public struct VariadicType<T...> {
11-
// CHECK-LABEL: sil [ossa] @$s19pack_expansion_type12VariadicTypeV14variadicMethod1t1ux_qd__txQp_txxQp_qd__qd__Qptqd__RhzlF : $@convention(method) <T...><U... where ((T, U)...) : Any> (@in_guaranteed T..., @in_guaranteed U..., VariadicType<T>) -> @out (T, U)... {
12-
// CHECK: bb0(%0 : $*(T, U)..., %1 : $*T..., %2 : $*U..., %3 : $VariadicType<T>):
11+
// CHECK-LABEL: sil [ossa] @$s19pack_expansion_type12VariadicTypeV14variadicMethod1t1ux_qd__txQp_txxQp_qd__qd__Qptqd__RhzlF : $@convention(method) <T...><U... where ((T, U)...) : Any> (@in_guaranteed T..., @in_guaranteed U..., VariadicType<T...>) -> @out (T, U)... {
12+
// CHECK: bb0(%0 : $*(T, U)..., %1 : $*T..., %2 : $*U..., %3 : $VariadicType<T...>):
1313
public func variadicMethod<U...>(t: T..., u: U...) -> ((T, U)...) {}
1414
}

0 commit comments

Comments
 (0)