Skip to content

Commit b7c6348

Browse files
committed
[Sema] Implement type join for existential types.
1 parent d971d48 commit b7c6348

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/AST/TypeJoinMeet.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct TypeJoin : CanTypeVisitor<TypeJoin, CanType> {
6666
CanType visitBoundGenericStructType(CanType second);
6767
CanType visitMetatypeType(CanType second);
6868
CanType visitExistentialMetatypeType(CanType second);
69+
CanType visitExistentialType(CanType second);
6970
CanType visitModuleType(CanType second);
7071
CanType visitDynamicSelfType(CanType second);
7172
CanType visitArchetypeType(CanType second);
@@ -271,6 +272,29 @@ CanType TypeJoin::visitExistentialMetatypeType(CanType second) {
271272
return ExistentialMetatypeType::get(joinInstance)->getCanonicalType();
272273
}
273274

275+
CanType TypeJoin::visitExistentialType(CanType second) {
276+
assert(First != second);
277+
278+
if (First->getKind() != second->getKind())
279+
return TheAnyType;
280+
281+
auto firstConstraint = First->castTo<ExistentialType>()
282+
->getConstraintType()->getCanonicalType();
283+
auto secondConstraint = second->castTo<ExistentialType>()
284+
->getConstraintType()->getCanonicalType();
285+
286+
auto joinInstance = join(firstConstraint, secondConstraint);
287+
if (!joinInstance)
288+
return CanType();
289+
290+
if (joinInstance->is<ExistentialMetatypeType>() ||
291+
joinInstance->isAny() ||
292+
joinInstance->isAnyObject())
293+
return joinInstance;
294+
295+
return ExistentialType::get(joinInstance)->getCanonicalType();
296+
}
297+
274298
CanType TypeJoin::visitModuleType(CanType second) {
275299
assert(First != second);
276300

test/Sema/type_join.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-typecheck-verify-swift -parse-stdlib
2+
// RUN: %target-typecheck-verify-swift -parse-stdlib -enable-explicit-existential-types
23

34
import Swift
45

0 commit comments

Comments
 (0)