|
| 1 | +// REQUIRES: swift_swift_parser, executable_test, OS=macosx |
| 2 | + |
| 3 | +// RUN: %empty-directory(%t) |
| 4 | +// RUN: mkdir -p %t/src |
| 5 | +// RUN: mkdir -p %t/plugins |
| 6 | + |
| 7 | +// RUN: split-file %s %t/src |
| 8 | + |
| 9 | +//#-- Prepare the macro dylib plugin. |
| 10 | +// RUN: %host-build-swift \ |
| 11 | +// RUN: -swift-version 5 \ |
| 12 | +// RUN: -emit-library -o %t/plugins/%target-library-name(MacroDefinition) \ |
| 13 | +// RUN: -module-name MacroDefinition \ |
| 14 | +// RUN: %t/src/MacroDefinition.swift \ |
| 15 | +// RUN: -g -no-toolchain-stdlib-rpath |
| 16 | + |
| 17 | +// RUN: %target-swift-frontend \ |
| 18 | +// RUN: -typecheck -verify \ |
| 19 | +// RUN: -enable-experimental-feature FreestandingMacros \ |
| 20 | +// RUN: -parse-as-library \ |
| 21 | +// RUN: -dump-macro-expansions \ |
| 22 | +// RUN: -plugin-path %t/plugins \ |
| 23 | +// RUN: %t/src/test.swift |
| 24 | + |
| 25 | + |
| 26 | +//--- MacroDefinition.swift |
| 27 | +import SwiftDiagnostics |
| 28 | +import SwiftOperators |
| 29 | +import SwiftSyntax |
| 30 | +import SwiftSyntaxBuilder |
| 31 | +import SwiftSyntaxMacros |
| 32 | + |
| 33 | +public struct GlobalFuncAndVarMacro: DeclarationMacro { |
| 34 | + public static func expansion( |
| 35 | + of node: some FreestandingMacroExpansionSyntax, |
| 36 | + in context: some MacroExpansionContext |
| 37 | + ) throws -> [DeclSyntax] { |
| 38 | + return ["func globalFunc() {}", "var globalVar: Int { 1 }"] |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +public struct MemberFuncAndVarMacro: DeclarationMacro { |
| 43 | + public static func expansion( |
| 44 | + of node: some FreestandingMacroExpansionSyntax, |
| 45 | + in context: some MacroExpansionContext |
| 46 | + ) throws -> [DeclSyntax] { |
| 47 | + return ["func memberFunc() {}", "var memberVar: Int { 1 }"] |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +public struct LocalFuncAndVarMacro: DeclarationMacro { |
| 52 | + public static func expansion( |
| 53 | + of node: some FreestandingMacroExpansionSyntax, |
| 54 | + in context: some MacroExpansionContext |
| 55 | + ) throws -> [DeclSyntax] { |
| 56 | + return ["func LocalFunc() {}", "var LocalVar: Int { 1 }"] |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +//--- test.swift |
| 61 | + |
| 62 | +@freestanding(declaration, names: named(globalFunc), named(globalVar)) macro globalDecls() = #externalMacro(module: "MacroDefinition", type: "GlobalFuncAndVarMacro") |
| 63 | +@freestanding(declaration, names: named(memberFunc), named(memberVar)) macro memberDecls() = #externalMacro(module: "MacroDefinition", type: "MemberFuncAndVarMacro") |
| 64 | +@freestanding(declaration, names: named(localFunc), named(localVar)) macro localDecls() = #externalMacro(module: "MacroDefinition", type: "LocalFuncAndVarMacro") |
| 65 | + |
| 66 | +@available(SwiftStdlib 9999, *) |
| 67 | +#globalDecls |
| 68 | + |
| 69 | +func testGlobal() { // expected-note {{add @available attribute to enclosing global function}} |
| 70 | + globalFunc() // expected-error {{'globalFunc()' is only available in macOS 9999 or newer}} expected-note {{add 'if #available' version check}} |
| 71 | + // FIXME(109376568): Global variable introduced by macro expansion not found |
| 72 | + _ = globalVar // expected-error {{cannot find 'globalVar' in scope}} |
| 73 | +} |
| 74 | + |
| 75 | +struct S { |
| 76 | + @available(SwiftStdlib 9999, *) |
| 77 | + #memberDecls |
| 78 | +} |
| 79 | +func testMember(value: S) { // expected-note 2 {{add @available attribute to enclosing global function}} |
| 80 | + value.memberFunc() // expected-error {{'memberFunc()' is only available in macOS 9999 or newer}} expected-note {{add 'if #available' version check}} |
| 81 | + _ = value.memberVar // expected-error {{'memberVar' is only available in macOS 9999 or newer}} expected-note {{add 'if #available' version check}} |
| 82 | +} |
| 83 | + |
| 84 | +struct T { |
| 85 | + static #memberDecls |
| 86 | +} |
| 87 | +func testStatic() { |
| 88 | + T.memberFunc() // OK |
| 89 | + _ = T.memberVar // OK |
| 90 | +} |
| 91 | + |
| 92 | +func testLocal() { |
| 93 | +// FIXME(109376102): Local vars with freestanding macro is not supported yet. |
| 94 | +#if false |
| 95 | + #localDecls |
| 96 | + do { |
| 97 | + } |
| 98 | +#endif |
| 99 | +} |
0 commit comments