Skip to content

Commit 81a1f64

Browse files
committed
[Macros] Allow @attached(extension) attributes to be suppressed in
module interfaces.
1 parent c3bdae6 commit 81a1f64

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,11 @@ struct PrintOptions {
578578
/// If false, we print them as ordinary associated types.
579579
bool PrintPrimaryAssociatedTypes = true;
580580

581+
/// Whether or not to print \c @attached(extension) attributes on
582+
/// macro declarations. This is used for feature suppression in
583+
/// Swift interface printing.
584+
bool PrintExtensionMacroAttributes = true;
585+
581586
/// If this is not \c nullptr then function bodies (including accessors
582587
/// and constructors) will be printed by this function.
583588
std::function<void(const ValueDecl *, ASTPrinter &)> FunctionBody;

include/swift/Basic/Features.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ EXPERIMENTAL_FEATURE(StaticAssert, false)
119119
EXPERIMENTAL_FEATURE(NamedOpaqueTypes, false)
120120
EXPERIMENTAL_FEATURE(FlowSensitiveConcurrencyCaptures, false)
121121
EXPERIMENTAL_FEATURE(CodeItemMacros, true)
122-
EXPERIMENTAL_FEATURE(ExtensionMacros, false)
123122
EXPERIMENTAL_FEATURE(TupleConformances, false)
124123
EXPERIMENTAL_FEATURE(InitAccessors, false)
125124

125+
EXPERIMENTAL_FEATURE(ExtensionMacros, false)
126+
SUPPRESSIBLE_LANGUAGE_FEATURE(ExtensionMacroAttr, 0, "@attached(extension)", true)
127+
126128
// Whether to enable @_used and @_section attributes
127129
EXPERIMENTAL_FEATURE(SymbolLinkageMarkers, true)
128130

lib/AST/ASTPrinter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,6 +2860,18 @@ static bool usesFeatureExtensionMacros(Decl *decl) {
28602860
return macro->getMacroRoles().contains(MacroRole::Extension);
28612861
}
28622862

2863+
static bool usesFeatureExtensionMacroAttr(Decl *decl) {
2864+
return usesFeatureExtensionMacros(decl);
2865+
}
2866+
2867+
static void suppressingFeatureExtensionMacroAttr(PrintOptions &options,
2868+
llvm::function_ref<void()> action) {
2869+
bool originalPrintExtensionMacroAttrs = options.PrintExtensionMacroAttributes;
2870+
options.PrintExtensionMacroAttributes = false;
2871+
action();
2872+
options.PrintExtensionMacroAttributes = originalPrintExtensionMacroAttrs;
2873+
}
2874+
28632875
static bool usesFeatureAttachedMacros(Decl *decl) {
28642876
auto macro = dyn_cast<MacroDecl>(decl);
28652877
if (!macro)

lib/AST/Attr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,13 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
13711371

13721372
case DAK_MacroRole: {
13731373
auto Attr = cast<MacroRoleAttr>(this);
1374+
1375+
// Suppress @attached(extension) if needed.
1376+
if (!Options.PrintExtensionMacroAttributes &&
1377+
Attr->getMacroRole() == MacroRole::Extension) {
1378+
break;
1379+
}
1380+
13741381
switch (Attr->getMacroSyntax()) {
13751382
case MacroSyntax::Freestanding:
13761383
Printer.printAttrName("@freestanding");

test/ModuleInterface/macros.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// RUN: %empty-directory(%t)
77

8-
// RUN: %target-swift-emit-module-interface(%t/Macros.swiftinterface) -module-name Macros %s
8+
// RUN: %target-swift-emit-module-interface(%t/Macros.swiftinterface) -enable-experimental-feature ExtensionMacros -module-name Macros %s
99
// RUN: %FileCheck %s < %t/Macros.swiftinterface --check-prefix CHECK
1010
// RUN: %target-swift-frontend -compile-module-from-interface %t/Macros.swiftinterface -o %t/Macros.swiftmodule
1111

@@ -49,5 +49,12 @@
4949
// CHECK-NEXT: #endif
5050
@attached(accessor, names: named(init)) public macro AccessorInitFunc() = #externalMacro(module: "SomeModule", type: "AccessorInitFuncMacro")
5151

52+
// CHECK: #if compiler(>=5.3) && $Macros && $AttachedMacros
53+
// CHECK: @attached(extension) @attached(conformance) public macro AddSendable() = #externalMacro(module: "SomeModule", type: "SendableExtensionMacro")
54+
// CHECK-NEXT: #else
55+
// CHECK: @attached(conformance) public macro AddSendable() = #externalMacro(module: "SomeModule", type: "SendableExtensionMacro")
56+
// CHECK-NEXT: #endif
57+
@attached(extension) @attached(conformance) public macro AddSendable() = #externalMacro(module: "SomeModule", type: "SendableExtensionMacro")
58+
5259
// CHECK-NOT: internalStringify
5360
@freestanding(expression) macro internalStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")

0 commit comments

Comments
 (0)