Skip to content

Commit bf00a71

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

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
@@ -6781,38 +6781,6 @@ inline TypeBase *TypeBase::getDesugaredType() {
67816781
return cast<SugarType>(this)->getSinglyDesugaredType()->getDesugaredType();
67826782
}
67836783

6784-
inline bool TypeBase::hasSimpleTypeRepr() const {
6785-
// NOTE: Please keep this logic in sync with TypeRepr::isSimple().
6786-
switch (getKind()) {
6787-
case TypeKind::Function:
6788-
case TypeKind::GenericFunction:
6789-
return false;
6790-
6791-
case TypeKind::Metatype:
6792-
return !cast<const AnyMetatypeType>(this)->hasRepresentation();
6793-
6794-
case TypeKind::ExistentialMetatype:
6795-
case TypeKind::Existential:
6796-
return false;
6797-
6798-
case TypeKind::OpaqueTypeArchetype:
6799-
case TypeKind::OpenedArchetype:
6800-
return false;
6801-
6802-
case TypeKind::ProtocolComposition: {
6803-
// 'Any', 'AnyObject' and single protocol compositions are simple
6804-
auto composition = cast<const ProtocolCompositionType>(this);
6805-
auto memberCount = composition->getMembers().size();
6806-
if (composition->hasExplicitAnyObject())
6807-
return memberCount == 0;
6808-
return memberCount <= 1;
6809-
}
6810-
6811-
default:
6812-
return true;
6813-
}
6814-
}
6815-
68166784
} // end namespace swift
68176785

68186786
namespace llvm {

lib/AST/Type.cpp

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

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