Skip to content

Commit ee73ff7

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 2813fbe commit ee73ff7

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
@@ -1138,7 +1138,16 @@ class PrintAST : public ASTVisitor<PrintAST> {
11381138
Printer.callPrintDeclPre(D, Options.BracketOptions);
11391139

11401140
if (Options.PrintCompatibilityFeatureChecks) {
1141-
printWithCompatibilityFeatureChecks(Printer, Options, D, [&]{
1141+
printWithCompatibilityFeatureChecks(Printer, Options, D, [&] {
1142+
// If we are in a scope where non-copyable generics are being suppressed
1143+
// and we are also printing a decl that has @_preInverseGenerics, make
1144+
// sure we also suppress printing ownership modifiers that were added
1145+
// to satisfy the requirements of non-copyability.
1146+
llvm::SaveAndRestore<bool> scope(
1147+
Options.SuppressNoncopyableOwnershipModifiers,
1148+
Options.SuppressNoncopyableGenerics &&
1149+
D->getAttrs().hasAttribute<PreInverseGenericsAttr>());
1150+
11421151
ASTVisitor::visit(D);
11431152
});
11441153
} else {
@@ -3123,9 +3132,12 @@ static void suppressingFeatureAssociatedTypeImplements(PrintOptions &options,
31233132
static void suppressingFeatureNoncopyableGenerics(
31243133
PrintOptions &options,
31253134
llvm::function_ref<void()> action) {
3135+
unsigned originalExcludeAttrCount = options.ExcludeAttrList.size();
3136+
options.ExcludeAttrList.push_back(DeclAttrKind::PreInverseGenerics);
31263137
llvm::SaveAndRestore<bool> scope(
31273138
options.SuppressNoncopyableGenerics, true);
31283139
action();
3140+
options.ExcludeAttrList.resize(originalExcludeAttrCount);
31293141
}
31303142

31313143
/// Suppress the printing of a particular feature.
@@ -3680,10 +3692,14 @@ static void printParameterFlags(ASTPrinter &printer,
36803692
printer.printKeyword("inout", options, " ");
36813693
break;
36823694
case ParamSpecifier::Borrowing:
3683-
printer.printKeyword("borrowing", options, " ");
3695+
if (!options.SuppressNoncopyableOwnershipModifiers) {
3696+
printer.printKeyword("borrowing", options, " ");
3697+
}
36843698
break;
36853699
case ParamSpecifier::Consuming:
3686-
printer.printKeyword("consuming", options, " ");
3700+
if (!options.SuppressNoncopyableOwnershipModifiers) {
3701+
printer.printKeyword("consuming", options, " ");
3702+
}
36873703
break;
36883704
case ParamSpecifier::LegacyShared:
36893705
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)