Skip to content

Commit b05df02

Browse files
committed
AST: An opaque generic parameter is not 'simple' for purposes of type printing
1 parent ce2a345 commit b05df02

File tree

4 files changed

+53
-41
lines changed

4 files changed

+53
-41
lines changed

include/swift/AST/Types.h

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6752,38 +6752,6 @@ inline TypeBase *TypeBase::getDesugaredType() {
67526752
return cast<SugarType>(this)->getSinglyDesugaredType()->getDesugaredType();
67536753
}
67546754

6755-
inline bool TypeBase::hasSimpleTypeRepr() const {
6756-
// NOTE: Please keep this logic in sync with TypeRepr::isSimple().
6757-
switch (getKind()) {
6758-
case TypeKind::Function:
6759-
case TypeKind::GenericFunction:
6760-
return false;
6761-
6762-
case TypeKind::Metatype:
6763-
return !cast<const AnyMetatypeType>(this)->hasRepresentation();
6764-
6765-
case TypeKind::ExistentialMetatype:
6766-
case TypeKind::Existential:
6767-
return false;
6768-
6769-
case TypeKind::OpaqueTypeArchetype:
6770-
case TypeKind::OpenedArchetype:
6771-
return false;
6772-
6773-
case TypeKind::ProtocolComposition: {
6774-
// 'Any', 'AnyObject' and single protocol compositions are simple
6775-
auto composition = cast<const ProtocolCompositionType>(this);
6776-
auto memberCount = composition->getMembers().size();
6777-
if (composition->hasExplicitAnyObject())
6778-
return memberCount == 0;
6779-
return memberCount <= 1;
6780-
}
6781-
6782-
default:
6783-
return true;
6784-
}
6785-
}
6786-
67876755
} // end namespace swift
67886756

67896757
namespace llvm {

lib/AST/Type.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6222,6 +6222,46 @@ bool TypeBase::isForeignReferenceType() {
62226222
return false;
62236223
}
62246224

6225+
bool TypeBase::hasSimpleTypeRepr() const {
6226+
// NOTE: Please keep this logic in sync with TypeRepr::isSimple().
6227+
switch (getKind()) {
6228+
case TypeKind::Function:
6229+
case TypeKind::GenericFunction:
6230+
return false;
6231+
6232+
case TypeKind::Metatype:
6233+
return !cast<const AnyMetatypeType>(this)->hasRepresentation();
6234+
6235+
case TypeKind::ExistentialMetatype:
6236+
case TypeKind::Existential:
6237+
return false;
6238+
6239+
case TypeKind::OpaqueTypeArchetype:
6240+
case TypeKind::OpenedArchetype:
6241+
return false;
6242+
6243+
case TypeKind::ProtocolComposition: {
6244+
// 'Any', 'AnyObject' and single protocol compositions are simple
6245+
auto composition = cast<const ProtocolCompositionType>(this);
6246+
auto memberCount = composition->getMembers().size();
6247+
if (composition->hasExplicitAnyObject())
6248+
return memberCount == 0;
6249+
return memberCount <= 1;
6250+
}
6251+
6252+
case TypeKind::GenericTypeParam: {
6253+
if (auto *decl = cast<const GenericTypeParamType>(this)->getDecl()) {
6254+
return !decl->isOpaqueType();
6255+
}
6256+
6257+
return true;
6258+
}
6259+
6260+
default:
6261+
return true;
6262+
}
6263+
}
6264+
62256265
bool CanType::isForeignReferenceType() {
62266266
if (auto *classDecl = getPointer()->lookThroughAllOptionalTypes()->getClassOrBoundGenericClass())
62276267
return classDecl->isForeignReferenceType();

test/type/parameterized_existential.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@ func saturation(_ dry: any Sponge, _ wet: any Sponge<Int, Int>) {
7373
// expected-note@-1 {{did you mean to use 'as!' to force downcast?}}
7474
}
7575

76-
protocol Pair<X, Y> where Self.X == Self.Y {
77-
associatedtype X
78-
associatedtype Y
79-
}
80-
81-
func splay(_ x: some Pair<Int, String>) -> (Int, String) { fatalError() }
82-
// expected-error@-1 {{no type for 'some Pair<Int, String>.X' can satisfy both 'some Pair<Int, String>.X == String' and 'some Pair<Int, String>.X == Int'}}
83-
8476
func typeExpr() {
8577
_ = Sequence<Int>.self
8678
// expected-error@-1 {{use of protocol 'Sequence' as a type must be written 'any Sequence'}}

test/type/parameterized_protocol.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ func testConstraintAlias1<T : SequenceOfInt>(_: T) {}
193193
// CHECK-NEXT: Generic signature: <T where T : Sequence, T.[Sequence]Element == String>
194194
func testConstraintAlias2<T : SequenceOf<String>>(_: T) {}
195195

196+
196197
/// Protocol compositions
197198

198199
// CHECK-LABEL: .testComposition1@
@@ -212,4 +213,15 @@ protocol TestCompositionProtocol1 {
212213

213214
struct TestStructComposition : Sequence<Int> & Sendable {}
214215
// expected-error@-1 {{cannot inherit from protocol type with generic argument 'Sequence<Int>'}}
215-
// expected-error@-2 {{type 'TestStructComposition' does not conform to protocol 'Sequence'}}
216+
// expected-error@-2 {{type 'TestStructComposition' does not conform to protocol 'Sequence'}}
217+
218+
219+
/// Conflicts
220+
221+
protocol Pair<X, Y> where Self.X == Self.Y {
222+
associatedtype X
223+
associatedtype Y
224+
}
225+
226+
func splay(_ x: some Pair<Int, String>) -> (Int, String) { fatalError() }
227+
// expected-error@-1 {{no type for '(some Pair<Int, String>).X' can satisfy both '(some Pair<Int, String>).X == String' and '(some Pair<Int, String>).X == Int'}}

0 commit comments

Comments
 (0)