Skip to content

Commit cd84df5

Browse files
committed
[Sema] Don't wrap class types in existential types.
This could happen previously when computing the existential type for a given archetype. In cases where an archetype is only constrained to a class, return the class type directly rather than wrapping it in an existential type.
1 parent c7fd60f commit cd84df5

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,6 +4281,8 @@ Type ExistentialType::get(Type constraint) {
42814281
if (constraint->is<ExistentialMetatypeType>())
42824282
return constraint;
42834283

4284+
assert(constraint->isConstraintType());
4285+
42844286
auto properties = constraint->getRecursiveProperties();
42854287
auto arena = getArena(properties);
42864288

lib/AST/Type.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3362,6 +3362,13 @@ Type ArchetypeType::getExistentialType() const {
33623362
auto constraint = ProtocolCompositionType::get(
33633363
ctx, constraintTypes, requiresClass());
33643364

3365+
// If the archetype is only constrained to a class type,
3366+
// return the class type directly.
3367+
if (!constraint->isConstraintType()) {
3368+
assert(constraint->getClassOrBoundGenericClass());
3369+
return constraint;
3370+
}
3371+
33653372
return ExistentialType::get(constraint);
33663373
}
33673374

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-frontend -emit-ir %s
2+
3+
import Swift
4+
5+
class C {}
6+
sil_vtable C {}
7+
8+
sil @repo: $@convention(c) @pseudogeneric <S, I where S : AnyObject, S : Hashable, I : C> (@inout_aliasable @block_storage @callee_guaranteed (@guaranteed S, Int, UnsafeMutablePointer<Bool>) -> (), S, Int, UnsafeMutablePointer<Bool>) -> () {
9+
bb0(%0 : $*@block_storage @callee_guaranteed (@guaranteed S, Int, UnsafeMutablePointer<Bool>) -> (), %1 : $S, %2 : $Int, %3 : $UnsafeMutablePointer<Bool>):
10+
%9 = tuple () // user: %10
11+
return %9 : $() // id: %10
12+
}

0 commit comments

Comments
 (0)