Skip to content

Commit 89732fc

Browse files
committed
[Macros] Allow @attached(extension) attributes to be suppressed in
module interfaces. (cherry picked from commit 81a1f64)
1 parent 202d3f5 commit 89732fc

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
@@ -582,6 +582,11 @@ struct PrintOptions {
582582
/// If false, we print them as ordinary associated types.
583583
bool PrintPrimaryAssociatedTypes = true;
584584

585+
/// Whether or not to print \c @attached(extension) attributes on
586+
/// macro declarations. This is used for feature suppression in
587+
/// Swift interface printing.
588+
bool PrintExtensionMacroAttributes = true;
589+
585590
/// If this is not \c nullptr then function bodies (including accessors
586591
/// and constructors) will be printed by this function.
587592
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
@@ -118,10 +118,12 @@ EXPERIMENTAL_FEATURE(StaticAssert, false)
118118
EXPERIMENTAL_FEATURE(NamedOpaqueTypes, false)
119119
EXPERIMENTAL_FEATURE(FlowSensitiveConcurrencyCaptures, false)
120120
EXPERIMENTAL_FEATURE(CodeItemMacros, true)
121-
EXPERIMENTAL_FEATURE(ExtensionMacros, false)
122121
EXPERIMENTAL_FEATURE(TupleConformances, false)
123122
EXPERIMENTAL_FEATURE(InitAccessors, false)
124123

124+
EXPERIMENTAL_FEATURE(ExtensionMacros, false)
125+
SUPPRESSIBLE_LANGUAGE_FEATURE(ExtensionMacroAttr, 0, "@attached(extension)", true)
126+
125127
// FIXME: MoveOnlyClasses is not intended to be in production,
126128
// but our tests currently rely on it, and we want to run those
127129
// tests in non-asserts builds too.

lib/AST/ASTPrinter.cpp

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

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

lib/AST/Attr.cpp

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

13661366
case DAK_MacroRole: {
13671367
auto Attr = cast<MacroRoleAttr>(this);
1368+
1369+
// Suppress @attached(extension) if needed.
1370+
if (!Options.PrintExtensionMacroAttributes &&
1371+
Attr->getMacroRole() == MacroRole::Extension) {
1372+
break;
1373+
}
1374+
13681375
switch (Attr->getMacroSyntax()) {
13691376
case MacroSyntax::Freestanding:
13701377
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)