Skip to content

Commit 747fda0

Browse files
committed
AST: Adjust declaration printing when NoncopyableGenerics2 is suppressed.
When printing declarations with `NoncopyableGenerics2` suppressed we must avoid printing the `@_preInverseGenerics` attribute and any `borrowing` or `consuming` parameter ownership modifiers.
1 parent 9c37687 commit 747fda0

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ struct PrintOptions {
385385
/// Suppress Noncopyable generics.
386386
bool SuppressNoncopyableGenerics = false;
387387

388+
/// Suppress printing of `borrowing` and `consuming`.
389+
bool SuppressNoncopyableOwnershipModifiers = false;
390+
388391
/// List of attribute kinds that should not be printed.
389392
std::vector<AnyAttrKind> ExcludeAttrList = {
390393
DeclAttrKind::Transparent, DeclAttrKind::Effects,

lib/AST/ASTPrinter.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,16 @@ class PrintAST : public ASTVisitor<PrintAST> {
11541154
Printer.callPrintDeclPre(D, Options.BracketOptions);
11551155

11561156
if (Options.PrintCompatibilityFeatureChecks) {
1157-
printWithCompatibilityFeatureChecks(Printer, Options, D, [&]{
1157+
printWithCompatibilityFeatureChecks(Printer, Options, D, [&] {
1158+
// If we are in a scope where non-copyable generics are being suppressed
1159+
// and we are also printing a decl that has @_preInverseGenerics, make
1160+
// sure we also suppress printing ownership modifiers that were added
1161+
// to satisfy the requirements of non-copyability.
1162+
llvm::SaveAndRestore<bool> scope(
1163+
Options.SuppressNoncopyableOwnershipModifiers,
1164+
Options.SuppressNoncopyableGenerics &&
1165+
D->getAttrs().hasAttribute<PreInverseGenericsAttr>());
1166+
11581167
ASTVisitor::visit(D);
11591168
});
11601169
} else {
@@ -3139,9 +3148,12 @@ static void suppressingFeatureAssociatedTypeImplements(PrintOptions &options,
31393148
static void suppressingFeatureNoncopyableGenerics(
31403149
PrintOptions &options,
31413150
llvm::function_ref<void()> action) {
3151+
unsigned originalExcludeAttrCount = options.ExcludeAttrList.size();
3152+
options.ExcludeAttrList.push_back(DeclAttrKind::PreInverseGenerics);
31423153
llvm::SaveAndRestore<bool> scope(
31433154
options.SuppressNoncopyableGenerics, true);
31443155
action();
3156+
options.ExcludeAttrList.resize(originalExcludeAttrCount);
31453157
}
31463158

31473159
/// Suppress the printing of a particular feature.
@@ -3696,10 +3708,14 @@ static void printParameterFlags(ASTPrinter &printer,
36963708
printer.printKeyword("inout", options, " ");
36973709
break;
36983710
case ParamSpecifier::Borrowing:
3699-
printer.printKeyword("borrowing", options, " ");
3711+
if (!options.SuppressNoncopyableOwnershipModifiers) {
3712+
printer.printKeyword("borrowing", options, " ");
3713+
}
37003714
break;
37013715
case ParamSpecifier::Consuming:
3702-
printer.printKeyword("consuming", options, " ");
3716+
if (!options.SuppressNoncopyableOwnershipModifiers) {
3717+
printer.printKeyword("consuming", options, " ");
3718+
}
37033719
break;
37043720
case ParamSpecifier::LegacyShared:
37053721
printer.printKeyword("__shared", options, " ");

lib/AST/FeatureSet.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ static bool usesFeatureRawLayout(Decl *decl) {
506506
UNINTERESTING_FEATURE(Embedded)
507507

508508
static bool usesFeatureNoncopyableGenerics(Decl *decl) {
509+
if (decl->getAttrs().hasAttribute<PreInverseGenericsAttr>())
510+
return true;
511+
509512
if (auto *valueDecl = dyn_cast<ValueDecl>(decl)) {
510513
if (isa<StructDecl, EnumDecl, ClassDecl>(decl)) {
511514
auto *nominalDecl = cast<NominalTypeDecl>(valueDecl);

test/ModuleInterface/Inputs/NoncopyableGenerics_Misc.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,6 @@ extension Outer.InnerStruct {
106106

107107
@_preInverseGenerics
108108
public func old_swap<T: ~Copyable>(_ a: inout T, _ b: inout T) {}
109+
110+
@_preInverseGenerics
111+
public func borrowsNoncopyable<T: ~Copyable>(_ t: borrowing T) {}

test/ModuleInterface/noncopyable_generics.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,18 @@ import NoncopyableGenerics_Misc
144144

145145
// CHECK-MISC: #if compiler(>=5.3) && $NoncopyableGenerics
146146
// CHECK-MISC-NEXT: @_preInverseGenerics public func old_swap<T>(_ a: inout T, _ b: inout T) where T : ~Copyable
147+
// CHECK-MISC-NEXT: #else
148+
// CHECK-MISC-NOT: @_preInverseGenerics
149+
// CHECK-MISC-NEXT: public func old_swap<T>(_ a: inout T, _ b: inout T)
147150
// CHECK-MISC: #endif
148151

152+
// CHECK-MISC: #if compiler(>=5.3) && $NoncopyableGenerics
153+
// CHECK-MISC-NEXT: @_preInverseGenerics public func borrowsNoncopyable<T>(_ t: borrowing T) where T : ~Copyable
154+
// CHECK-MISC-NEXT: #else
155+
// CHECK-MISC-NOT: @_preInverseGenerics
156+
// CHECK-MISC-NEXT: public func borrowsNoncopyable<T>(_ t: T)
157+
// CHECK-MISC-NEXT: #endif
158+
149159
import Swiftskell
150160

151161
// CHECK: #if compiler(>=5.3) && $NoncopyableGenerics

0 commit comments

Comments
 (0)