Skip to content

Don't print conformance attributes redundantly in -preserve-types-as-written mode #71687

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
32 changes: 22 additions & 10 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,8 +887,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
printTransformedTypeWithOptions(T, Options);
}

void printTypeLocWithOptions(const TypeLoc &TL, const PrintOptions &options) {
void printTypeLocWithOptions(const TypeLoc &TL, const PrintOptions &options,
std::optional<llvm::function_ref<void()>> printBeforeType = std::nullopt) {
if (CurrentType && TL.getType()) {
if (printBeforeType) (*printBeforeType)();
printTransformedTypeWithOptions(TL.getType(), options);
return;
}
Expand All @@ -901,11 +903,21 @@ class PrintAST : public ASTVisitor<PrintAST> {
return;
}

if (printBeforeType) (*printBeforeType)();
TL.getType().print(Printer, options);
}

void printTypeLoc(const TypeLoc &TL) { printTypeLocWithOptions(TL, Options); }

/// Print a TypeLoc. If we decide to print based on the type, rather than
/// based on the TypeRepr, call the given function before printing the type;
/// this is useful if there are attributes in the TypeRepr which don't end
/// up being part of the type, such as `@unchecked` in inheritance clauses.
void printTypeLoc(const TypeLoc &TL,
llvm::function_ref<void()> printBeforeType) {
printTypeLocWithOptions(TL, Options, printBeforeType);
}

void printTypeLocForImplicitlyUnwrappedOptional(TypeLoc TL, bool IUO) {
PrintOptions options = Options;
options.PrintOptionalAsImplicitlyUnwrapped = IUO;
Expand Down Expand Up @@ -2710,15 +2722,15 @@ void PrintAST::printInherited(const Decl *decl) {
Printer << ": ";

interleave(TypesToPrint, [&](InheritedEntry inherited) {
if (inherited.isUnchecked())
Printer << "@unchecked ";
if (inherited.isRetroactive() &&
!llvm::is_contained(Options.ExcludeAttrList, TypeAttrKind::Retroactive))
Printer << "@retroactive ";
if (inherited.isPreconcurrency())
Printer << "@preconcurrency ";

printTypeLoc(inherited);
printTypeLoc(inherited, [&] {
if (inherited.isUnchecked())
Printer << "@unchecked ";
if (inherited.isRetroactive() &&
!llvm::is_contained(Options.ExcludeAttrList, TypeAttrKind::Retroactive))
Printer << "@retroactive ";
if (inherited.isPreconcurrency())
Printer << "@preconcurrency ";
});
}, [&]() {
Printer << ", ";
});
Expand Down
13 changes: 11 additions & 2 deletions test/ModuleInterface/concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public struct InferredPreconcurrencyMainActor: PreconcurrencyMainProtocol {
public func requirement() {}
}

// rdar://122965951
public struct UncheckedSendable: @unchecked Sendable {}
extension UnsafePointer : @retroactive @unchecked Sendable {}

// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency -I %t

#else
Expand All @@ -54,7 +58,7 @@ func callFn() async {
}
#endif

// RUN: %FileCheck %s < %t/Library.swiftinterface
// RUN: %FileCheck --check-prefix=CHECK --check-prefix=CHECK-TYPECHECKED %s < %t/Library.swiftinterface
// CHECK: // swift-module-flags:{{.*}} -enable-experimental-concurrency
// CHECK: public func fn() async
// CHECK: public func reasyncFn(_: () async -> ()) reasync
Expand All @@ -77,7 +81,12 @@ func callFn() async {
// CHECK-NEXT: @_Concurrency.MainActor @preconcurrency public func requirement()
// CHECK-NEXT: }

// CHECK-TYPECHECKED: public struct UncheckedSendable : @unchecked Swift.Sendable
// CHECK-ASWRITTEN: public struct UncheckedSendable : @unchecked Sendable

// CHECK-TYPECHECKED: extension Swift.UnsafePointer : @unchecked @retroactive Swift.Sendable
// CHECK-ASWRITTEN: extension UnsafePointer : @retroactive @unchecked Sendable

// RUN: %target-swift-emit-module-interface(%t/LibraryPreserveTypesAsWritten.swiftinterface) %s -enable-experimental-concurrency -DLIBRARY -module-name LibraryPreserveTypesAsWritten -module-interface-preserve-types-as-written
// RUN: %target-swift-typecheck-module-from-interface(%t/LibraryPreserveTypesAsWritten.swiftinterface) -enable-experimental-concurrency
// RUN: %FileCheck %s < %t/LibraryPreserveTypesAsWritten.swiftinterface
// RUN: %FileCheck --check-prefix=CHECK --check-prefix=CHECK-ASWRITTEN %s < %t/LibraryPreserveTypesAsWritten.swiftinterface