Skip to content

Commit b412c6c

Browse files
committed
[Macros] Treat FreestandingExpressionMacros as a suppressible feature
When the feature isn't available, use the older `@expression` syntax to work around limitations of older compilers.
1 parent e202801 commit b412c6c

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ struct PrintOptions {
315315
/// for class layout
316316
bool PrintClassLayoutName = false;
317317

318+
/// Replace @freestanding(expression) with @expression.
319+
bool SuppressingFreestandingExpression = false;
320+
318321
/// Suppress emitting @available(*, noasync)
319322
bool SuppressNoAsyncAvailabilityAttr = false;
320323

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ SUPPRESSIBLE_LANGUAGE_FEATURE(UnavailableFromAsync, 0, "@_unavailableFromAsync",
9494
SUPPRESSIBLE_LANGUAGE_FEATURE(NoAsyncAvailability, 340, "@available(*, noasync)", true)
9595
LANGUAGE_FEATURE(BuiltinIntLiteralAccessors, 368, "Builtin.IntLiteral accessors", true)
9696
LANGUAGE_FEATURE(Macros, 0, "Macros", true)
97-
LANGUAGE_FEATURE(
97+
SUPPRESSIBLE_LANGUAGE_FEATURE(
9898
FreestandingExpressionMacros, 382, "Expression macros",
9999
true)
100100
LANGUAGE_FEATURE(AttachedMacros, 389, "Attached macros", true)

lib/AST/ASTPrinter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,6 +3237,14 @@ static bool usesFeatureFreestandingExpressionMacros(Decl *decl) {
32373237
return macro->getMacroRoles().contains(MacroRole::Expression);
32383238
}
32393239

3240+
static void
3241+
suppressingFeatureFreestandingExpressionMacros(PrintOptions &options,
3242+
llvm::function_ref<void()> action) {
3243+
llvm::SaveAndRestore<PrintOptions> orignalOptions(options);
3244+
options.SuppressingFreestandingExpression = true;
3245+
action();
3246+
}
3247+
32403248
static void
32413249
suppressingFeatureNoAsyncAvailability(PrintOptions &options,
32423250
llvm::function_ref<void()> action) {

lib/AST/Attr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,14 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
13471347

13481348
case DAK_MacroRole: {
13491349
auto Attr = cast<MacroRoleAttr>(this);
1350+
1351+
if (Options.SuppressingFreestandingExpression &&
1352+
Attr->getMacroSyntax() == MacroSyntax::Freestanding &&
1353+
Attr->getMacroRole() == MacroRole::Expression) {
1354+
Printer.printAttrName("@expression");
1355+
break;
1356+
}
1357+
13501358
switch (Attr->getMacroSyntax()) {
13511359
case MacroSyntax::Freestanding:
13521360
Printer.printAttrName("@freestanding");

test/ModuleInterface/macros.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66
// RUN: %FileCheck %s < %t/Macros.swiftinterface --check-prefix CHECK
77
// RUN: %target-swift-frontend -compile-module-from-interface %t/Macros.swiftinterface -o %t/Macros.swiftmodule
88

9-
// CHECK: #if compiler(>=5.3) && $Macros && $FreestandingExpressionMacros
9+
// CHECK: #if compiler(>=5.3) && $Macros
10+
// CHECK-NEXT: #if $FreestandingExpressionMacros
1011
// CHECK-NEXT: @freestanding(expression) public macro publicStringify<T>(_ value: T) -> (T, Swift.String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")
12+
// CHECK-NEXT: #else
13+
// CHECK-NEXT: @expression public macro publicStringify<T>(_ value: T) -> (T, Swift.String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")
14+
// CHECK-NEXT: #endif
1115
// CHECK-NEXT: #endif
1216
@freestanding(expression) public macro publicStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")
1317

14-
// CHECK: #if compiler(>=5.3) && $Macros && $FreestandingExpressionMacros
18+
// CHECK: #if compiler(>=5.3) && $Macros
19+
// CHECK-NEXT: #if $FreestandingExpressionMacros
1520
// CHECK: @freestanding(expression) public macro publicLine<T>() -> T = #externalMacro(module: "SomeModule", type: "Line") where T : Swift.ExpressibleByIntegerLiteral
21+
// CHECK-NEXT: #else
22+
// CHECK: @expression public macro publicLine<T>() -> T = #externalMacro(module: "SomeModule", type: "Line") where T : Swift.ExpressibleByIntegerLiteral
23+
// CHECK-NEXT: #endif
1624
// CHECK-NEXT: #endif
1725
@freestanding(expression) public macro publicLine<T: ExpressibleByIntegerLiteral>() -> T = #externalMacro(module: "SomeModule", type: "Line")
1826

0 commit comments

Comments
 (0)