Skip to content

Commit 5a9a654

Browse files
committed
Adopt @freestanding(expression) for all @expression macros
1 parent 9b93576 commit 5a9a654

File tree

11 files changed

+52
-52
lines changed

11 files changed

+52
-52
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6860,8 +6860,8 @@ WARNING(macro_type_access_warn,none,
68606860
(bool, AccessLevel, AccessLevel, bool))
68616861
ERROR(macro_in_nested,none,
68626862
"macro %0 can only be declared at file scope", (DeclName))
6863-
ERROR(macro_without_context,none,
6864-
"macro %0 must declare its applicable contexts (e.g., '@expression')",
6863+
ERROR(macro_without_role,none,
6864+
"macro %0 must declare its applicable roles via '@freestanding' or @attached'",
68656865
(DeclName))
68666866
ERROR(macro_expansion_missing_pound,none,
68676867
"expansion of macro %0 requires leading '#'", (DeclName))

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20122012
if (!MD->getDeclContext()->isModuleScopeContext())
20132013
MD->diagnose(diag::macro_in_nested, MD->getName());
20142014
if (!MD->getMacroRoles())
2015-
MD->diagnose(diag::macro_without_context, MD->getName());
2015+
MD->diagnose(diag::macro_without_role, MD->getName());
20162016

20172017
// Check the macro definition.
20182018
switch (auto macroDef = MD->getDefinition()) {

stdlib/public/core/Macros.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if compiler(>=5.8) && hasAttribute(expression)
13+
#if compiler(>=5.8) && hasAttribute(freestanding)
1414
/// Specifies the module and type name for an externally-defined macro, which
1515
/// must conform to the appropriate set of `Macro` protocols.
1616
///
@@ -20,7 +20,7 @@
2020
/// #externalMacro(module: "ExampleMacros", type :"StringifyMacro")
2121
///
2222
/// Use of this macro in any other context is an error.
23-
@expression
23+
@freestanding(expression)
2424
public macro externalMacro<T>(module: String, type: String) -> T =
2525
Builtin.ExternalMacro
2626

test/Macros/attached_macros_diags.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@attached(accessor) macro m3(message: String) -> Void = #externalMacro(module: "MyMacros", type: "Macro3")
1818
// expected-warning@-1{{external macro implementation type 'MyMacros.Macro3' could not be found for macro 'm3(message:)'}}
1919

20-
@expression macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MyMacros", type: "StringifyMacro")
20+
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MyMacros", type: "StringifyMacro")
2121
// expected-warning@-1{{external macro implementation type 'MyMacros.StringifyMacro' could not be found for macro 'stringify'}}
2222
// expected-note@-2{{'stringify' declared here}}
2323

test/Macros/macro_availability_macosx.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
@available(macOS 12.0, *)
55
struct X { }
66

7-
@expression macro m1: X = #externalMacro(module: "A", type: "B") // expected-error{{'X' is only available in macOS 12.0 or newer}}
7+
@freestanding(expression) macro m1: X = #externalMacro(module: "A", type: "B") // expected-error{{'X' is only available in macOS 12.0 or newer}}
88
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'm1'}}
99
1010
@available(macOS 12.0, *)
11-
@expression macro m2: X = #externalMacro(module: "A", type: "B")
11+
@freestanding(expression) macro m2: X = #externalMacro(module: "A", type: "B")
1212
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'm2'}}

test/Macros/macro_expand.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
// FIXME: Swift parser is not enabled on Linux CI yet.
2020
// REQUIRES: OS=macosx
2121

22-
@expression macro customFileID: String = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
23-
@expression macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
24-
@expression macro fileID<T: ExpressibleByStringLiteral>: T = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
25-
@expression macro recurse(_: Bool) = #externalMacro(module: "MacroDefinition", type: "RecursiveMacro")
22+
@freestanding(expression) macro customFileID: String = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
23+
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
24+
@freestanding(expression) macro fileID<T: ExpressibleByStringLiteral>: T = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
25+
@freestanding(expression) macro recurse(_: Bool) = #externalMacro(module: "MacroDefinition", type: "RecursiveMacro")
2626

2727
func testFileID(a: Int, b: Int) {
2828
// CHECK: MacroUser/macro_expand.swift
@@ -79,7 +79,7 @@ public struct Outer {
7979
// CHECK: (2, "a + b")
8080
testStringify(a: 1, b: 1)
8181

82-
@expression macro addBlocker<T>(_ value: T) -> T = #externalMacro(module: "MacroDefinition", type: "AddBlocker")
82+
@freestanding(expression) macro addBlocker<T>(_ value: T) -> T = #externalMacro(module: "MacroDefinition", type: "AddBlocker")
8383

8484
struct OnlyAdds {
8585
static func +(lhs: OnlyAdds, rhs: OnlyAdds) -> OnlyAdds { lhs }
@@ -116,7 +116,7 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
116116
}
117117

118118
// Make sure we don't crash with declarations produced by expansions.
119-
@expression macro nestedDeclInExpr: () -> Void = #externalMacro(module: "MacroDefinition", type: "NestedDeclInExprMacro")
119+
@freestanding(expression) macro nestedDeclInExpr: () -> Void = #externalMacro(module: "MacroDefinition", type: "NestedDeclInExprMacro")
120120

121121
func testNestedDeclInExpr() {
122122
let _: () -> Void = #nestedDeclInExpr

test/Macros/macro_expand_variadic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// FIXME: Swift parser is not enabled on Linux CI yet.
1010
// REQUIRES: OS=macosx
1111

12-
@expression macro print<Value...>(_ value: repeat each Value) = #externalMacro(module: "MacroDefinition", type: "PrintMacro")
12+
@freestanding(expression) macro print<Value...>(_ value: repeat each Value) = #externalMacro(module: "MacroDefinition", type: "PrintMacro")
1313

1414
func testIt() {
1515
// CHECK: hello

test/Macros/macros_diagnostics.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
// RUN: %target-typecheck-verify-swift -enable-experimental-feature Macros -module-name MacrosTest
22

3-
@expression macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
3+
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
44
// expected-note@-1 2{{'stringify' declared here}}
55
// expected-warning@-2{{external macro implementation type}}
6-
@expression macro missingMacro1(_: Any) = MissingModule.MissingType // expected-note{{'missingMacro1' declared here}}
7-
// expected-warning@-1{{external macro definitions are now written using #externalMacro}}{{43-68=#externalMacro(module: "MissingModule", type: "MissingType")}}
6+
@freestanding(expression) macro missingMacro1(_: Any) = MissingModule.MissingType // expected-note{{'missingMacro1' declared here}}
7+
// expected-warning@-1{{external macro definitions are now written using #externalMacro}}{{57-82=#externalMacro(module: "MissingModule", type: "MissingType")}}
88
// expected-warning@-2{{external macro implementation type}}
9-
@expression macro missingMacro2(_: Any) = #externalMacro(module: "MissingModule", type: "MissingType")
9+
@freestanding(expression) macro missingMacro2(_: Any) = #externalMacro(module: "MissingModule", type: "MissingType")
1010
// expected-warning@-1{{external macro implementation type}}
1111

1212
protocol P { }
1313

14-
@expression macro tryToHide<T: P>(_: T) -> some P = #externalMacro(module: "BuiltinMacros", type: "Blah")
14+
@freestanding(expression) macro tryToHide<T: P>(_: T) -> some P = #externalMacro(module: "BuiltinMacros", type: "Blah")
1515
// expected-error@-1{{some' types are only permitted in properties, subscripts, and functions}}
1616
// expected-error@-2{{generic parameter 'T' could not be inferred}}
1717

1818
internal struct X { } // expected-note{{type declared here}}
1919

20-
@expression public macro createAnX: X = #externalMacro(module: "BuiltinMacros", type: "Blah")
20+
@freestanding(expression) public macro createAnX: X = #externalMacro(module: "BuiltinMacros", type: "Blah")
2121
// expected-error@-1{{macro cannot be declared public because its result type uses an internal type}}
2222
// expected-warning@-2{{external macro implementation type}}
2323

24-
@expression macro m1: Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
24+
@freestanding(expression) macro m1: Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
2525
// expected-warning@-1{{external macro implementation type}}
26-
@expression macro m1: Float = #externalMacro(module: "BuiltinMacros", type: "Blah")
26+
@freestanding(expression) macro m1: Float = #externalMacro(module: "BuiltinMacros", type: "Blah")
2727
// expected-warning@-1{{external macro implementation type}}
2828

29-
@expression macro m2: Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-note{{'m2' previously declared here}}
29+
@freestanding(expression) macro m2: Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-note{{'m2' previously declared here}}
3030
// expected-warning@-1{{external macro implementation type}}
31-
@expression macro m2: Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-error{{invalid redeclaration of 'm2'}}
31+
@freestanding(expression) macro m2: Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-error{{invalid redeclaration of 'm2'}}
3232
// expected-warning@-1{{external macro implementation type}}
3333

34-
@expression macro m3(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
34+
@freestanding(expression) macro m3(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
3535
// expected-warning@-1{{external macro implementation type}}
36-
@expression macro m3(_: Int) -> Float = #externalMacro(module: "BuiltinMacros", type: "Blah")
36+
@freestanding(expression) macro m3(_: Int) -> Float = #externalMacro(module: "BuiltinMacros", type: "Blah")
3737
// expected-warning@-1{{external macro implementation type}}
3838

39-
@expression macro m4(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-note{{'m4' previously declared here}}
39+
@freestanding(expression) macro m4(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-note{{'m4' previously declared here}}
4040
// expected-warning@-1{{external macro implementation type}}
41-
@expression macro m4(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-error{{invalid redeclaration of 'm4'}}
41+
@freestanding(expression) macro m4(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-error{{invalid redeclaration of 'm4'}}
4242
// expected-warning@-1{{external macro implementation type}}
4343

4444
struct ZZZ {
4545
macro m5: Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
4646
// expected-error@-1{{macro 'm5' can only be declared at file scope}}
47-
// expected-error@-2{{macro 'm5' must declare its applicable contexts (e.g., '@expression')}}
47+
// expected-error@-2{{macro 'm5' must declare its applicable roles}}
4848
// expected-warning@-3{{external macro implementation type}}
4949
}
5050

51-
@expression macro multiArgMacro(_: Any, second: Any) = #externalMacro(module: "MissingModule", type: "MissingType")
51+
@freestanding(expression) macro multiArgMacro(_: Any, second: Any) = #externalMacro(module: "MissingModule", type: "MissingType")
5252
// expected-note@-1{{'multiArgMacro(_:second:)' declared here}}
5353
// expected-warning@-2{{external macro implementation type}}
5454

55-
@expression macro overloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingType")
55+
@freestanding(expression) macro overloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingType")
5656
// expected-warning@-1{{external macro implementation type}}
5757

5858
func overloaded1(_ p: Any) { }
5959

60-
@expression macro notOverloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingType") // expected-note{{'notOverloaded1' previously declared here}}
60+
@freestanding(expression) macro notOverloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingType") // expected-note{{'notOverloaded1' previously declared here}}
6161
// expected-warning@-1{{external macro implementation type}}
62-
@expression macro notOverloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingOtherType") // expected-error{{invalid redeclaration of 'notOverloaded1'}}
62+
@freestanding(expression) macro notOverloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingOtherType") // expected-error{{invalid redeclaration of 'notOverloaded1'}}
6363
// expected-warning@-1{{external macro implementation type}}
6464

65-
@expression macro intIdentity(value: Int, _: Float) -> Int = #externalMacro(module: "MissingModule", type: "MissingType")
65+
@freestanding(expression) macro intIdentity(value: Int, _: Float) -> Int = #externalMacro(module: "MissingModule", type: "MissingType")
6666
// expected-note@-1{{macro 'intIdentity(value:_:)' declared here}}
6767
// expected-warning@-2{{external macro implementation type}}
6868

@@ -114,7 +114,7 @@ func testMissing() {
114114
#missingMacro1("hello") // expected-error{{external macro implementation type 'MissingModule.MissingType' could not be found for macro 'missingMacro1'; the type must be public and provided via '-load-plugin-library'}}
115115
}
116116

117-
@expression macro undefined() // expected-error{{macro 'undefined()' requires a definition}}
117+
@freestanding(expression) macro undefined() // expected-error{{macro 'undefined()' requires a definition}}
118118

119119
func testExternalMacroOutOfPlace() {
120120
let _: Int = #externalMacro(module: "A", type: "B")

test/Macros/parsing.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22
protocol P { }
33
protocol Q { associatedtype Assoc }
44

5-
@expression macro m1: Int = #externalMacro(module: "A", type: "M1")
5+
@freestanding(expression) macro m1: Int = #externalMacro(module: "A", type: "M1")
66
// expected-warning@-1{{external macro implementation type 'A.M1' could not be found for macro 'm1'; the type must be public and provided via '-load-plugin-library'}}
7-
@expression macro m2(_: Int) = #externalMacro(module: "A", type: "M2")
7+
@freestanding(expression) macro m2(_: Int) = #externalMacro(module: "A", type: "M2")
88
// expected-warning@-1{{external macro implementation type 'A.M2' could not be found for macro 'm2'; the type must be public and provided via '-load-plugin-library'}}
9-
@expression macro m3(a b: Int) -> Int = #externalMacro(module: "A", type: "M3")
9+
@freestanding(expression) macro m3(a b: Int) -> Int = #externalMacro(module: "A", type: "M3")
1010
// expected-warning@-1{{external macro implementation type 'A.M3' could not be found for macro 'm3(a:)'; the type must be public and provided via '-load-plugin-library'}}
11-
@expression macro m4<T: Q>: T = #externalMacro(module: "A", type: "M4") where T.Assoc: P
11+
@freestanding(expression) macro m4<T: Q>: T = #externalMacro(module: "A", type: "M4") where T.Assoc: P
1212
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm4'; the type must be public and provided via '-load-plugin-library'}}
13-
@expression macro m5<T: P>(_: T) = #externalMacro(module: "A", type: "M4")
13+
@freestanding(expression) macro m5<T: P>(_: T) = #externalMacro(module: "A", type: "M4")
1414
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm5'; the type must be public and provided via '-load-plugin-library'}}
1515

16-
@expression macro m6 = A // expected-error{{expected '(' for macro parameters or ':' for a value-like macro}}
16+
@freestanding(expression) macro m6 = A // expected-error{{expected '(' for macro parameters or ':' for a value-like macro}}
1717
// expected-error@-1{{macro must itself be defined by a macro expansion such as '#externalMacro(...)'}}
1818

1919
// expected-error @+2 {{expected '('}}
20-
// expected-error @+1 {{macro 'm7' must declare its applicable contexts (e.g., '@expression')}}
20+
// expected-error @+1 {{macro 'm7' must declare its applicable roles}}
2121
@declaration macro m7(_: String) = #externalMacro(module: "A", type: "M4")
2222
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm7'; the type must be public and provided via '-load-plugin-library'}}
2323
// expected-error @+2 {{expected a declaration macro kind ('freestanding' or 'attached')}}
24-
// expected-error @+1 {{macro 'm8' must declare its applicable contexts (e.g., '@expression')}}
24+
// expected-error @+1 {{macro 'm8' must declare its applicable roles}}
2525
@declaration(abc) macro m8(_: String) = #externalMacro(module: "A", type: "M4")
2626
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm8'; the type must be public and provided via '-load-plugin-library'}}
2727
@declaration(freestanding) macro m9(_: String) = #externalMacro(module: "A", type: "M4")
2828
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm9'; the type must be public and provided via '-load-plugin-library'}}
2929

30-
@expression @declaration(freestanding) @attached(accessor)
30+
@freestanding(expression) @declaration(freestanding) @attached(accessor)
3131
macro m10(_: String) = #externalMacro(module: "A", type: "M4")
3232
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm10'; the type must be public and provided via '-load-plugin-library'}}
3333

test/ModuleInterface/macros.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
// RUN: %target-swift-frontend -compile-module-from-interface %t/Macros.swiftinterface -o %t/Macros.swiftmodule
88

99
// CHECK: #if compiler(>=5.3) && $Macros
10-
// CHECK-NEXT: @expression public macro publicStringify<T>(_ value: T) -> (T, Swift.String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")
10+
// CHECK-NEXT: @freestanding(expression) public macro publicStringify<T>(_ value: T) -> (T, Swift.String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")
1111
// CHECK-NEXT: #endif
12-
@expression public macro publicStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")
12+
@freestanding(expression) public macro publicStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")
1313

1414
// CHECK: #if compiler(>=5.3) && $Macros
15-
// CHECK: @expression public macro publicLine<T>: T = #externalMacro(module: "SomeModule", type: "Line") where T : Swift.ExpressibleByIntegerLiteral
15+
// CHECK: @freestanding(expression) public macro publicLine<T>: T = #externalMacro(module: "SomeModule", type: "Line") where T : Swift.ExpressibleByIntegerLiteral
1616
// CHECK-NEXT: #endif
17-
@expression public macro publicLine<T: ExpressibleByIntegerLiteral>: T = #externalMacro(module: "SomeModule", type: "Line")
17+
@freestanding(expression) public macro publicLine<T: ExpressibleByIntegerLiteral>: T = #externalMacro(module: "SomeModule", type: "Line")
1818

1919
// CHECK: #if compiler(>=5.3) && $Macros
2020
// CHECK: @attached(accessor) public macro myWrapper: Swift.Void = #externalMacro(module: "SomeModule", type: "Wrapper")
2121
// CHECK-NEXT: #endif
2222
@attached(accessor) public macro myWrapper: Void = #externalMacro(module: "SomeModule", type: "Wrapper")
2323

2424
// CHECK-NOT: internalStringify
25-
@expression macro internalStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")
25+
@freestanding(expression) macro internalStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")

0 commit comments

Comments
 (0)