@@ -314,6 +314,7 @@ bool CanType::isReferenceTypeImpl(CanType type, const GenericSignatureImpl *sig,
314314 case TypeKind::PackElement:
315315 case TypeKind::SILPack:
316316 case TypeKind::BuiltinTuple:
317+ case TypeKind::ErrorUnion:
317318#define REF_STORAGE (Name, ...) \
318319 case TypeKind::Name##Storage:
319320#include " swift/AST/ReferenceStorage.def"
@@ -1875,6 +1876,15 @@ CanType TypeBase::computeCanonicalType() {
18751876 Result = BoundGenericType::get (BGT->getDecl (), parentTy, CanGenericArgs);
18761877 break ;
18771878 }
1879+ case TypeKind::ErrorUnion: {
1880+ SmallVector<Type, 2 > newTerms;
1881+ for (auto term : cast<ErrorUnionType>(this )->getTerms ()) {
1882+ newTerms.push_back (term->getCanonicalType ());
1883+ }
1884+ ASTContext &ctx = newTerms[0 ]->getASTContext ();
1885+ Result = ErrorUnionType::get (ctx, newTerms).getPointer ();
1886+ break ;
1887+ }
18781888 }
18791889
18801890 // Cache the canonical type for future queries.
@@ -4709,6 +4719,48 @@ case TypeKind::Id:
47094719 return ParenType::get (Ptr->getASTContext (), underlying);
47104720 }
47114721
4722+ case TypeKind::ErrorUnion: {
4723+ auto errorUnion = cast<ErrorUnionType>(base);
4724+ bool anyChanged = false ;
4725+ SmallVector<Type, 4 > terms;
4726+ unsigned Index = 0 ;
4727+ for (Type term : errorUnion->getTerms ()) {
4728+ Type transformedTerm =
4729+ term.transformWithPosition (TypePosition::Invariant, fn);
4730+ if (!transformedTerm)
4731+ return Type ();
4732+
4733+ // If nothing has changed, just keep going.
4734+ if (!anyChanged &&
4735+ transformedTerm.getPointer () == term.getPointer ()) {
4736+ ++Index;
4737+ continue ;
4738+ }
4739+
4740+ // If this is the first change we've seen, copy all of the previous
4741+ // elements.
4742+ if (!anyChanged) {
4743+ // Copy all of the previous elements.
4744+ terms.append (errorUnion->getTerms ().begin (),
4745+ errorUnion->getTerms ().begin () + Index);
4746+ anyChanged = true ;
4747+ }
4748+
4749+ // If the transformed type is a pack, immediately expand it.
4750+ if (auto termPack = getTransformedPack (transformedTerm)) {
4751+ auto termElements = termPack->getElementTypes ();
4752+ terms.append (termElements.begin (), termElements.end ());
4753+ } else {
4754+ terms.push_back (transformedTerm);
4755+ }
4756+ }
4757+
4758+ if (!anyChanged)
4759+ return *this ;
4760+
4761+ return ErrorUnionType::get (Ptr->getASTContext (), terms);
4762+ }
4763+
47124764 case TypeKind::Pack: {
47134765 auto pack = cast<PackType>(base);
47144766 bool anyChanged = false ;
@@ -5407,6 +5459,7 @@ ReferenceCounting TypeBase::getReferenceCounting() {
54075459 case TypeKind::PackElement:
54085460 case TypeKind::SILPack:
54095461 case TypeKind::BuiltinTuple:
5462+ case TypeKind::ErrorUnion:
54105463#define REF_STORAGE (Name, ...) \
54115464 case TypeKind::Name##Storage:
54125465#include " swift/AST/ReferenceStorage.def"
@@ -5640,6 +5693,13 @@ bool TypeBase::hasSimpleTypeRepr() const {
56405693 }
56415694}
56425695
5696+ bool CanType::isErrorExistentialType () const {
5697+ if (!isExistentialTypeImpl (*this ))
5698+ return false ;
5699+
5700+ return const_cast <CanType *>(this )->getExistentialLayout ().isErrorExistential ();
5701+ }
5702+
56435703bool CanType::isForeignReferenceType () {
56445704 if (auto *classDecl = getPointer ()->lookThroughAllOptionalTypes ()->getClassOrBoundGenericClass ())
56455705 return classDecl->isForeignReferenceType ();
0 commit comments