@@ -7862,7 +7862,24 @@ swift::getInheritedForPrinting(
78627862 // Collect explicit inherited types.
78637863 for (auto i : inherited.getIndices ()) {
78647864 if (auto ty = inherited.getResolvedType (i)) {
7865+ // Preserve inverses separately, because the `foundUnprintable` logic
7866+ // doesn't handle compositions with a mix of printable and unprintable
7867+ // types! That's handled later by `InheritedProtocolCollector`.
7868+ //
7869+ // Generally speaking, `getInheritedForPrinting` needs to be
7870+ // querying `InheritedProtocolCollector` to find out what protocols it
7871+ // should print in the inheritance clause, to reduce code duplication
7872+ // in the printer.
7873+ InvertibleProtocolSet printableInverses;
7874+
78657875 bool foundUnprintable = ty.findIf ([&](Type subTy) {
7876+ {
7877+ // We canonicalize the composition to ensure no inverses are missed.
7878+ auto subCanTy = subTy->getCanonicalType ();
7879+ if (auto PCT = subCanTy->getAs <ProtocolCompositionType>()) {
7880+ printableInverses.insertAll (PCT->getInverses ());
7881+ }
7882+ }
78667883 if (auto aliasTy = dyn_cast<TypeAliasType>(subTy.getPointer ()))
78677884 return !options.shouldPrint (aliasTy->getDecl ());
78687885 if (auto NTD = subTy->getAnyNominal ()) {
@@ -7871,8 +7888,22 @@ swift::getInheritedForPrinting(
78717888 }
78727889 return false ;
78737890 });
7874- if (foundUnprintable)
7891+
7892+ // Preserve any inverses that appeared in the unprintable type.
7893+ if (foundUnprintable) {
7894+ if (printableInverses.contains (InvertibleProtocolKind::Copyable)
7895+ && options.SuppressNoncopyableGenerics )
7896+ printableInverses.remove (InvertibleProtocolKind::Copyable);
7897+
7898+ if (!printableInverses.empty ()) {
7899+ auto inversesTy = ProtocolCompositionType::get (decl->getASTContext (),
7900+ /* members=*/ {},
7901+ printableInverses,
7902+ /* anyObject=*/ false );
7903+ Results.push_back (InheritedEntry (TypeLoc::withoutLoc (inversesTy)));
7904+ }
78757905 continue ;
7906+ }
78767907
78777908 // Suppress Copyable and ~Copyable.
78787909 if (options.SuppressNoncopyableGenerics ) {
0 commit comments