Skip to content

Commit 4723e55

Browse files
committed
ASTPrinter: Fix logic to determine if a requirement needs 'repeat' prefix
1 parent 756b03a commit 4723e55

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
@@ -804,7 +804,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
804804

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

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

827827
T = T.subst(subMap, SubstFlags::DesugarMemberTypes);
828+
}
829+
830+
return T;
831+
}
828832

833+
void printTransformedTypeWithOptions(Type T, PrintOptions options) {
834+
T = getTransformedType(T);
835+
836+
if (CurrentType && Current && CurrentType->mayHaveMembers())
829837
options.TransformContext = TypeTransformContext(CurrentType);
830-
}
831838

832839
printTypeWithOptions(T, options);
833840
}
@@ -1833,6 +1840,11 @@ void PrintAST::printSingleDepthOfGenericSignature(
18331840
}
18341841

18351842
void PrintAST::printRequirement(const Requirement &req) {
1843+
SmallVector<Type, 2> rootParameterPacks;
1844+
getTransformedType(req.getFirstType())
1845+
->getTypeParameterPacks(rootParameterPacks);
1846+
bool isPackRequirement = !rootParameterPacks.empty();
1847+
18361848
switch (req.getKind()) {
18371849
case RequirementKind::SameShape:
18381850
Printer << "(repeat (";
@@ -1842,22 +1854,21 @@ void PrintAST::printRequirement(const Requirement &req) {
18421854
Printer << ")) : Any";
18431855
return;
18441856
case RequirementKind::Layout:
1845-
if (req.getFirstType()->hasParameterPack())
1857+
if (isPackRequirement)
18461858
Printer << "repeat ";
18471859
printTransformedType(req.getFirstType());
18481860
Printer << " : ";
18491861
req.getLayoutConstraint()->print(Printer, Options);
18501862
return;
18511863
case RequirementKind::Conformance:
18521864
case RequirementKind::Superclass:
1853-
if (req.getFirstType()->hasParameterPack())
1865+
if (isPackRequirement)
18541866
Printer << "repeat ";
18551867
printTransformedType(req.getFirstType());
18561868
Printer << " : ";
18571869
break;
18581870
case RequirementKind::SameType:
1859-
if (req.getFirstType()->hasParameterPack() ||
1860-
req.getSecondType()->hasParameterPack())
1871+
if (isPackRequirement)
18611872
Printer << "repeat ";
18621873
printTransformedType(req.getFirstType());
18631874
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)