|
| 1 | +// REQUIRES: swift_swift_parser |
| 2 | + |
| 3 | +// RUN: %empty-directory(%t/src) |
| 4 | +// RUN: %empty-directory(%t/plugins) |
| 5 | +// RUN: %empty-directory(%t/plugins_local) |
| 6 | + |
| 7 | +// RUN: split-file %s %t/src |
| 8 | + |
| 9 | + |
| 10 | +//#-- Prepare the macro dylib plugin. |
| 11 | +// RUN: %host-build-swift \ |
| 12 | +// RUN: -swift-version 5 \ |
| 13 | +// RUN: -emit-library -o %t/plugins/%target-library-name(MacroDefinition) \ |
| 14 | +// RUN: -module-name MacroDefinition \ |
| 15 | +// RUN: %t/src/MacroDefinition.float.swift \ |
| 16 | +// RUN: -g -no-toolchain-stdlib-rpath |
| 17 | + |
| 18 | +//#-- Prepare the macro dylib plugin. |
| 19 | +// RUN: %host-build-swift \ |
| 20 | +// RUN: -swift-version 5 \ |
| 21 | +// RUN: -emit-library -o %t/plugins_local/%target-library-name(MacroDefinition) \ |
| 22 | +// RUN: -module-name MacroDefinition \ |
| 23 | +// RUN: %t/src/MacroDefinition.int.swift \ |
| 24 | +// RUN: -g -no-toolchain-stdlib-rpath |
| 25 | + |
| 26 | +//#-- Check '-load-plugin-library' takes precedence over '-plugin-path'. |
| 27 | +// RUN: %target-swift-frontend -typecheck -verify -swift-version 5 \ |
| 28 | +// RUN: -plugin-path %t/plugins \ |
| 29 | +// RUN: -load-plugin-library %t/plugins_local/%target-library-name(MacroDefinition) \ |
| 30 | +// RUN: %t/src/test.swift |
| 31 | + |
| 32 | +//#-- Same, but with different argument order. |
| 33 | +// RUN: %target-swift-frontend -typecheck -verify -swift-version 5 \ |
| 34 | +// RUN: -load-plugin-library %t/plugins_local/%target-library-name(MacroDefinition) \ |
| 35 | +// RUN: -plugin-path %t/plugins \ |
| 36 | +// RUN: %t/src/test.swift |
| 37 | + |
| 38 | +//--- test.swift |
| 39 | +@freestanding(expression) macro constInt() -> Int = #externalMacro(module: "MacroDefinition", type: "ConstMacro") |
| 40 | + |
| 41 | +func foo() { |
| 42 | + let _: Int = #constInt |
| 43 | + // If 'MacroDefinition.float.swift' is loaded, type checking this fails because it expands to '4.2' which is a float literal. |
| 44 | +} |
| 45 | + |
| 46 | +//--- MacroDefinition.float.swift |
| 47 | +import SwiftSyntax |
| 48 | +import SwiftSyntaxBuilder |
| 49 | +import SwiftSyntaxMacros |
| 50 | + |
| 51 | +public struct ConstMacro: ExpressionMacro { |
| 52 | + public static func expansion( |
| 53 | + of macro: some FreestandingMacroExpansionSyntax, |
| 54 | + in context: some MacroExpansionContext |
| 55 | + ) -> ExprSyntax { |
| 56 | + |
| 57 | + return "4.2" |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +//--- MacroDefinition.int.swift |
| 62 | +import SwiftSyntax |
| 63 | +import SwiftSyntaxBuilder |
| 64 | +import SwiftSyntaxMacros |
| 65 | + |
| 66 | +public struct ConstMacro: ExpressionMacro { |
| 67 | + public static func expansion( |
| 68 | + of macro: some FreestandingMacroExpansionSyntax, |
| 69 | + in context: some MacroExpansionContext |
| 70 | + ) -> ExprSyntax { |
| 71 | + |
| 72 | + return "42" |
| 73 | + } |
| 74 | +} |
0 commit comments