Skip to content

Commit d88e701

Browse files
committed
AST: Print parameterized protocols in opaque return types
The primary change was the refactoring of ArchetypeType::getExistentialType(); this commit just fixes the type sugar. Fixes rdar://problem/100911362.
1 parent 3bfc997 commit d88e701

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6473,6 +6473,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
64736473
if (auto existential = constraint->getAs<ExistentialType>())
64746474
constraint = existential->getConstraintType();
64756475

6476+
// Opaque archetype substitutions are always canonical, so re-sugar the
6477+
// constraint type using the owning declaration's generic parameter names.
6478+
auto genericSig = T->getDecl()->getNamingDecl()->getInnermostDeclContext()
6479+
->getGenericSignatureOfContext();
6480+
if (genericSig)
6481+
constraint = genericSig->getSugaredType(constraint);
6482+
64766483
visit(constraint);
64776484
return;
64786485
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-emit-module-interface(%t/ParameterizedProtocols.swiftinterface) %s -module-name ParameterizedProtocols -disable-availability-checking
3+
// RUN: %target-swift-typecheck-module-from-interface(%t/ParameterizedProtocols.swiftinterface) -module-name ParameterizedProtocols -disable-availability-checking
4+
// RUN: %FileCheck %s < %t/ParameterizedProtocols.swiftinterface
5+
6+
public protocol P<T> {
7+
associatedtype T
8+
}
9+
10+
public protocol Q<T>: P {}
11+
12+
struct S<T>: Q {}
13+
14+
15+
// CHECK-LABEL: public func returnsP() -> some ParameterizedProtocols.P
16+
public func returnsP() -> some P { return S<Int>() }
17+
18+
// CHECK-LABEL: public func returnsPInt() -> some ParameterizedProtocols.P<Swift.Int>
19+
public func returnsPInt() -> some P<Int> { return S<Int>() }
20+
21+
// CHECK-LABEL: public func returnsPT<T>(_: T) -> some ParameterizedProtocols.P<T>
22+
public func returnsPT<T>(_: T) -> some P<T> { return S<T>() }
23+
24+
// CHECK-LABEL: public func returnsPArrayT<T>(_: T) -> some ParameterizedProtocols.P<Swift.Array<T>>
25+
public func returnsPArrayT<T>(_: T) -> some P<Array<T>> { return S<Array<T>>() }
26+
27+
28+
// CHECK-LABEL: public func returnsQ() -> some ParameterizedProtocols.Q
29+
public func returnsQ() -> some Q { return S<Int>() }
30+
31+
// CHECK-LABEL: public func returnsQInt() -> some ParameterizedProtocols.Q<Swift.Int>
32+
public func returnsQInt() -> some Q<Int> { return S<Int>() }
33+
34+
// CHECK-LABEL: public func returnsQT<T>(_: T) -> some ParameterizedProtocols.Q<T>
35+
public func returnsQT<T>(_: T) -> some Q<T> { return S<T>() }
36+
37+
// CHECK-LABEL: public func returnsQArrayT<T>(_: T) -> some ParameterizedProtocols.Q<Swift.Array<T>>
38+
public func returnsQArrayT<T>(_: T) -> some Q<Array<T>> { return S<Array<T>>() }

0 commit comments

Comments
 (0)