Skip to content

ASTPrinter: handle inverses in compositions #73770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7861,7 +7861,24 @@ swift::getInheritedForPrinting(
// Collect explicit inherited types.
for (auto i : inherited.getIndices()) {
if (auto ty = inherited.getResolvedType(i)) {
// Preserve inverses separately, because the `foundUnprintable` logic
// doesn't handle compositions with a mix of printable and unprintable
// types! That's handled later by `InheritedProtocolCollector`.
//
// Generally speaking, `getInheritedForPrinting` needs to be
// querying `InheritedProtocolCollector` to find out what protocols it
// should print in the inheritance clause, to reduce code duplication
// in the printer.
InvertibleProtocolSet printableInverses;

bool foundUnprintable = ty.findIf([&](Type subTy) {
{
// We canonicalize the composition to ensure no inverses are missed.
auto subCanTy = subTy->getCanonicalType();
if (auto PCT = subCanTy->getAs<ProtocolCompositionType>()) {
printableInverses.insertAll(PCT->getInverses());
}
}
if (auto aliasTy = dyn_cast<TypeAliasType>(subTy.getPointer()))
return !options.shouldPrint(aliasTy->getDecl());
if (auto NTD = subTy->getAnyNominal()) {
Expand All @@ -7870,8 +7887,22 @@ swift::getInheritedForPrinting(
}
return false;
});
if (foundUnprintable)

// Preserve any inverses that appeared in the unprintable type.
if (foundUnprintable) {
if (printableInverses.contains(InvertibleProtocolKind::Copyable)
&& options.SuppressNoncopyableGenerics)
printableInverses.remove(InvertibleProtocolKind::Copyable);

if (!printableInverses.empty()) {
auto inversesTy = ProtocolCompositionType::get(decl->getASTContext(),
/*members=*/{},
printableInverses,
/*anyObject=*/false);
Results.push_back(InheritedEntry(TypeLoc::withoutLoc(inversesTy)));
}
continue;
}

// Suppress Copyable and ~Copyable.
if (options.SuppressNoncopyableGenerics) {
Expand Down
10 changes: 10 additions & 0 deletions test/ModuleInterface/Inputs/NoncopyableGenerics_Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,13 @@ public func substCopyable(_ t: String?) {}
public func substGenericCopyable<T>(_ t: T?) {}
public func substNC(_ t: borrowing NoCopyPls?) {}
public func substGenericNC<T: ~Copyable>(_ t: borrowing T?) {}

// coverage for rdar://126090425
protocol P : ~Copyable {} // NOTE: it's important that this is NOT public.
protocol Q: ~Copyable {} // NOTE: it's important that this is NOT public.
public protocol Publik: ~Copyable {}
public struct Concrete : (P & ~Copyable) {}
public struct Generic<T: Publik & ~Copyable> : (P & ~Copyable) {}
public struct VeryNested: (P & (Q & ~Copyable & Publik) & (P & ~Copyable)) {}
public struct Twice: P & ~Copyable, Q & ~Copyable {}
public struct RegularTwice: ~Copyable, ~Copyable {}
25 changes: 25 additions & 0 deletions test/ModuleInterface/noncopyable_generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,31 @@ import NoncopyableGenerics_Misc
// CHECK-MISC-NEXT: public func substGenericNC<T>(_ t: borrowing T?)
// CHECK-MISC-NEXT: #endif

// CHECK-MISC: #if compiler(>=5.3) && $NoncopyableGenerics
// CHECK-MISC-NEXT: public protocol Publik : ~Copyable {
// CHECK-MISC-NEXT: }
// CHECK-MISC-NEXT: #else
// CHECK-MISC-NEXT: public protocol Publik {
// CHECK-MISC-NEXT: }
// CHECK-MISC-NEXT: #endif
// CHECK-MISC-NEXT: public struct Concrete : ~Copyable {
// CHECK-MISC-NEXT: }
// CHECK-MISC-NEXT: #if compiler(>=5.3) && $NoncopyableGenerics
// CHECK-MISC-NEXT: public struct Generic<T> : ~Copyable where T : {{.*}}.Publik, T : ~Copyable {
// CHECK-MISC-NEXT: }
// CHECK-MISC-NEXT: #else
// CHECK-MISC-NEXT: public struct Generic<T> where T : {{.*}}.Publik {
// CHECK-MISC-NEXT: }
// CHECK-MISC-NEXT: #endif
// CHECK-MISC-NEXT: public struct VeryNested : ~Copyable {
// CHECK-MISC-NEXT: }
// CHECK-MISC-NEXT: public struct Twice : ~Copyable, ~Copyable {
// CHECK-MISC-NEXT: }
// CHECK-MISC-NEXT: public struct RegularTwice : ~Swift.Copyable, ~Swift.Copyable {
// CHECK-MISC-NEXT: }

// NOTE: below are extensions emitted at the end of NoncopyableGenerics_Misc.swift
// CHECK-MISC: extension {{.*}}.VeryNested : {{.*}}.Publik {}

import Swiftskell

Expand Down