Skip to content

Commit 29415df

Browse files
committed
ASTPrinter: Fix logic to determine if a requirement needs 'repeat' prefix
1 parent ddeab73 commit 29415df

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
803803

804804
void printType(Type T) { printTypeWithOptions(T, Options); }
805805

806-
void printTransformedTypeWithOptions(Type T, PrintOptions options) {
806+
Type getTransformedType(Type T) {
807807
if (CurrentType && Current && CurrentType->mayHaveMembers()) {
808808
auto *M = Current->getDeclContext()->getParentModule();
809809
SubstitutionMap subMap;
@@ -824,9 +824,16 @@ class PrintAST : public ASTVisitor<PrintAST> {
824824
}
825825

826826
T = T.subst(subMap, SubstFlags::DesugarMemberTypes);
827+
}
828+
829+
return T;
830+
}
827831

832+
void printTransformedTypeWithOptions(Type T, PrintOptions options) {
833+
T = getTransformedType(T);
834+
835+
if (CurrentType && Current && CurrentType->mayHaveMembers())
828836
options.TransformContext = TypeTransformContext(CurrentType);
829-
}
830837

831838
printTypeWithOptions(T, options);
832839
}
@@ -1832,6 +1839,11 @@ void PrintAST::printSingleDepthOfGenericSignature(
18321839
}
18331840

18341841
void PrintAST::printRequirement(const Requirement &req) {
1842+
SmallVector<Type, 2> rootParameterPacks;
1843+
getTransformedType(req.getFirstType())
1844+
->getTypeParameterPacks(rootParameterPacks);
1845+
bool isPackRequirement = !rootParameterPacks.empty();
1846+
18351847
switch (req.getKind()) {
18361848
case RequirementKind::SameShape:
18371849
Printer << "(repeat (";
@@ -1841,22 +1853,21 @@ void PrintAST::printRequirement(const Requirement &req) {
18411853
Printer << ")) : Any";
18421854
return;
18431855
case RequirementKind::Layout:
1844-
if (req.getFirstType()->hasParameterPack())
1856+
if (isPackRequirement)
18451857
Printer << "repeat ";
18461858
printTransformedType(req.getFirstType());
18471859
Printer << " : ";
18481860
req.getLayoutConstraint()->print(Printer, Options);
18491861
return;
18501862
case RequirementKind::Conformance:
18511863
case RequirementKind::Superclass:
1852-
if (req.getFirstType()->hasParameterPack())
1864+
if (isPackRequirement)
18531865
Printer << "repeat ";
18541866
printTransformedType(req.getFirstType());
18551867
Printer << " : ";
18561868
break;
18571869
case RequirementKind::SameType:
1858-
if (req.getFirstType()->hasParameterPack() ||
1859-
req.getSecondType()->hasParameterPack())
1870+
if (isPackRequirement)
18601871
Printer << "repeat ";
18611872
printTransformedType(req.getFirstType());
18621873
Printer << " == ";

test/ModuleInterface/pack_expansion_type.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// RUN: %target-swift-emit-module-interface(%t/PackExpansionType.swiftinterface) %s -module-name PackExpansionType -disable-availability-checking
33
// RUN: %FileCheck %s < %t/PackExpansionType.swiftinterface
44

5+
/// Requirements
6+
57
// CHECK: #if compiler(>=5.3) && $ParameterPacks
68
// CHECK-NEXT: public func variadicFunction<each T, each U>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) where (repeat (each T, each U)) : Any
79
public func variadicFunction<each T, each U>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) {}
@@ -24,6 +26,13 @@ public struct VariadicType<each T> {
2426
// CHECK: }
2527
// CHECK-NEXT: #endif
2628

29+
// The second requirement should not be prefixed with 'repeat'
30+
// CHECK: public struct SameTypeReq<T, each U> where T : Swift.Sequence, T.Element == PackExpansionType.VariadicType<repeat each U> {
31+
public struct SameTypeReq<T: Sequence, each U> where T.Element == VariadicType<repeat each U> {}
32+
// CHECK: }
33+
34+
/// Pack expansion types
35+
2736
// CHECK: public func returnsVariadicType() -> PackExpansionType.VariadicType<>
2837
public func returnsVariadicType() -> VariadicType< > {}
2938

0 commit comments

Comments
 (0)