@@ -45,6 +45,10 @@ struct TypeJoin : CanTypeVisitor<TypeJoin, CanType> {
4545 // For convenience, TheAnyType from ASTContext;
4646 CanType TheAnyType;
4747
48+ CanType getAnyExistentialType () {
49+ return ExistentialType::get (TheAnyType)->getCanonicalType ();
50+ }
51+
4852 TypeJoin (CanType First) : First(First), Unimplemented(CanType()) {
4953 assert (First && " Unexpected null type!" );
5054 TheAnyType = First->getASTContext ().TheAnyType ;
@@ -112,10 +116,10 @@ struct TypeJoin : CanTypeVisitor<TypeJoin, CanType> {
112116
113117 // Likewise, rather than making every visitor deal with Any,
114118 // always dispatch to the protocol composition side of the join.
115- if (first->is <ProtocolCompositionType>())
119+ if (first->is <ProtocolCompositionType>() || first-> is <ExistentialType>() )
116120 return TypeJoin (second).visit (first);
117121
118- if (second->is <ProtocolCompositionType>())
122+ if (second->is <ProtocolCompositionType>() || second-> is <ExistentialType>() )
119123 return TypeJoin (first).visit (second);
120124
121125 // Otherwise the first type might be an optional (or not), so
@@ -166,7 +170,7 @@ CanType TypeJoin::visitErrorType(CanType second) {
166170CanType TypeJoin::visitTupleType (CanType second) {
167171 assert (First != second);
168172
169- return TheAnyType ;
173+ return getAnyExistentialType () ;
170174}
171175
172176CanType TypeJoin::visitEnumType (CanType second) {
@@ -184,7 +188,7 @@ CanType TypeJoin::visitStructType(CanType second) {
184188
185189 // FIXME: When possible we should return a protocol or protocol
186190 // composition.
187- return TheAnyType ;
191+ return getAnyExistentialType () ;
188192}
189193
190194CanType TypeJoin::visitClassType (CanType second) {
@@ -240,7 +244,7 @@ CanType TypeJoin::visitMetatypeType(CanType second) {
240244 assert (First != second);
241245
242246 if (First->getKind () != second->getKind ())
243- return TheAnyType ;
247+ return getAnyExistentialType () ;
244248
245249 auto firstInstance =
246250 First->castTo <AnyMetatypeType>()->getInstanceType ()->getCanonicalType ();
@@ -258,7 +262,7 @@ CanType TypeJoin::visitExistentialMetatypeType(CanType second) {
258262 assert (First != second);
259263
260264 if (First->getKind () != second->getKind ())
261- return TheAnyType ;
265+ return getAnyExistentialType () ;
262266
263267 auto firstInstance =
264268 First->castTo <AnyMetatypeType>()->getInstanceType ()->getCanonicalType ();
@@ -276,7 +280,7 @@ CanType TypeJoin::visitExistentialType(CanType second) {
276280 assert (First != second);
277281
278282 if (First->getKind () != second->getKind ())
279- return TheAnyType ;
283+ return getAnyExistentialType () ;
280284
281285 auto firstConstraint = First->castTo <ExistentialType>()
282286 ->getConstraintType ()->getCanonicalType ();
@@ -293,7 +297,7 @@ CanType TypeJoin::visitExistentialType(CanType second) {
293297CanType TypeJoin::visitModuleType (CanType second) {
294298 assert (First != second);
295299
296- return TheAnyType ;
300+ return getAnyExistentialType () ;
297301}
298302
299303CanType TypeJoin::visitDynamicSelfType (CanType second) {
@@ -312,7 +316,7 @@ CanType TypeJoin::visitDependentMemberType(CanType second) {
312316 assert (First != second);
313317
314318 if (First->getKind () != second->getKind ())
315- return TheAnyType ;
319+ return getAnyExistentialType () ;
316320
317321 return Unimplemented;
318322}
@@ -326,7 +330,7 @@ CanType TypeJoin::visitFunctionType(CanType second) {
326330 if (secondFnTy->getExtInfo ().isNoEscape ()) {
327331 return Nonexistent;
328332 } else {
329- return TheAnyType ;
333+ return getAnyExistentialType () ;
330334 }
331335 }
332336
@@ -362,7 +366,7 @@ CanType TypeJoin::visitGenericFunctionType(CanType second) {
362366 assert (First != second);
363367
364368 if (First->getKind () != second->getKind ())
365- return TheAnyType ;
369+ return getAnyExistentialType () ;
366370
367371 return Unimplemented;
368372}
@@ -482,7 +486,7 @@ CanType TypeJoin::visitBuiltinType(CanType second) {
482486 assert (First != second);
483487
484488 // BuiltinType with any non-equal type results in Any.
485- return TheAnyType ;
489+ return getAnyExistentialType () ;
486490}
487491
488492} // namespace
0 commit comments