# Macros

**URL:** https://forums.swift.org/c/development/macros/96.md?page=1

[Latest](https://forums.swift.org/latest.md) · [Categories](https://forums.swift.org/categories.md) · [Tags](https://forums.swift.org/tags.md)

**Page:** 2

---

## ['body' macro cannot be attached to property](https://forums.swift.org/t/body-macro-cannot-be-attached-to-property/73487)

<div class="topic-metadata">

**Author:** [@joshw](https://forums.swift.org/u/joshw)\
**Replies:** 10\
**Last updated:** [June 26, 2025, 7:47pm UTC](https://forums.swift.org/t/body-macro-cannot-be-attached-to-property/73487 "2025-06-26T19:47:45Z")

</div>

I'm trying out the new function body macros. I'm attempting to attach the macro to a computed variable, which I believe was part of the proposal: When using the shorthand syntax for get-only properties, a function body…

---

## [Limits of macro automatic formatting](https://forums.swift.org/t/limits-of-macro-automatic-formatting/80597)

<div class="topic-metadata">

**Author:** [@cocoaphony](https://forums.swift.org/u/cocoaphony)\
**Replies:** 1\
**Last updated:** [June 22, 2025, 7:07pm UTC](https://forums.swift.org/t/limits-of-macro-automatic-formatting/80597 "2025-06-22T19:07:31Z")

</div>

I'm having trouble determining the rules for macro auto-formatting. I'm not sure what's expected and what's a bug. For example, modifying the stringify macro this way: return ExprSyntax( FunctionCallExprSynt…

---

## [Compilation extremely slow since macros adoption](https://forums.swift.org/t/compilation-extremely-slow-since-macros-adoption/67921)

<div class="topic-metadata">

**Author:** [@LyricApps](https://forums.swift.org/u/LyricApps)\
**Replies:** 140\
**Last updated:** [May 30, 2025, 10:48am UTC](https://forums.swift.org/t/compilation-extremely-slow-since-macros-adoption/67921 "2025-05-30T10:48:05Z")

</div>

Hi everyone, I've adopted macros to generate boilerplate code for my model classes (it's a bit like what Swift Data does) and first compilation now takes an horrible amount of time : it has gone from maybe 30 seconds to…

---

## [Freestanding function declaration macro](https://forums.swift.org/t/freestanding-function-declaration-macro/79341)

<div class="topic-metadata">

**Author:** [@noahsmartin](https://forums.swift.org/u/noahsmartin)\
**Replies:** 6\
**Last updated:** [May 7, 2025, 7:07pm UTC](https://forums.swift.org/t/freestanding-function-declaration-macro/79341 "2025-05-07T19:07:15Z")

</div>

Hi, I'm working on a freestanding macro that should create a function definition. For example @MacroExample("funcA") // Expands to: func MacroFunc\_funcA() { } I've declared the macro like this: @freestanding(declara…

---

## [PeerMacro on Protocol causes EXC\_BAD\_ACCESS during runtime](https://forums.swift.org/t/peermacro-on-protocol-causes-exc-bad-access-during-runtime/79484)

<div class="topic-metadata">

**Author:** [@shp-philip-sieberg](https://forums.swift.org/u/shp-philip-sieberg)\
**Replies:** 0\
**Last updated:** [April 24, 2025, 10:01am UTC](https://forums.swift.org/t/peermacro-on-protocol-causes-exc-bad-access-during-runtime/79484 "2025-04-24T10:01:03Z")

</div>

Hello, I created a macro layer to generate boiler code. Initially I used @Published to have both variables and a publisher to subscribe to if needed. When I introduced a Protocol abstraction layer i needed have an add…

---

## [Macro Package failing test runs on Linux when \`swift-syntax\` is 510.0.0](https://forums.swift.org/t/macro-package-failing-test-runs-on-linux-when-swift-syntax-is-510-0-0/79448)

<div class="topic-metadata">

**Author:** [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Replies:** 3\
**Last updated:** [April 23, 2025, 6:36pm UTC](https://forums.swift.org/t/macro-package-failing-test-runs-on-linux-when-swift-syntax-is-510-0-0/79448 "2025-04-23T18:36:23Z")

</div>

I recently expanded the testing matrix of my macro package. I have some GitHub actions that run on every push to main. I started testing Linux builds and I am seeing some strange failures. They seem to be on some narrow …

---

## [Macro parameter is compiled before expansion](https://forums.swift.org/t/macro-parameter-is-compiled-before-expansion/79154)

<div class="topic-metadata">

**Author:** [@samkudr](https://forums.swift.org/u/samkudr)\
**Replies:** 7\
**Last updated:** [April 9, 2025, 6:48pm UTC](https://forums.swift.org/t/macro-parameter-is-compiled-before-expansion/79154 "2025-04-09T18:48:54Z")

</div>

Hello! I have a macro that converts expression into a string literal, e.g.: #toString(variable) -\> "variable" #toString(TypeName) -\> "TypeName" #toString(\\TypeName.property) -\> "property" In Xcode 16.3 #toString(TypeN…

---

## [Macro diagnostic position ignored?](https://forums.swift.org/t/macro-diagnostic-position-ignored/78640)

<div class="topic-metadata">

**Author:** [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Replies:** 4\
**Last updated:** [March 19, 2025, 5:37am UTC](https://forums.swift.org/t/macro-diagnostic-position-ignored/78640 "2025-03-19T05:37:39Z")

</div>

I'm trying to add some static analysis to a string passed to a macro, and am explicitly giving the associated diagnostic a position in the string, but Xcode appears to ignore it: In the above example, the diagnostic …

---

## [Use a macro to modify nested structs?](https://forums.swift.org/t/use-a-macro-to-modify-nested-structs/78586)

<div class="topic-metadata">

**Author:** [@MisterDrgn](https://forums.swift.org/u/MisterDrgn)\
**Replies:** 3\
**Last updated:** [March 18, 2025, 2:14pm UTC](https://forums.swift.org/t/use-a-macro-to-modify-nested-structs/78586 "2025-03-18T14:14:29Z")

</div>

Suppose I have a setup like this (I'm simplifying things a lot). @Domain struct SimpleDomain { struct Properties { var color: Color var area: Int } struct ObjectProperties { var …

---

## [Adding 'Test diamonds' to Xcode with a macro](https://forums.swift.org/t/adding-test-diamonds-to-xcode-with-a-macro/78197)

<div class="topic-metadata">

**Author:** [@adammcarter](https://forums.swift.org/u/adammcarter)\
**Replies:** 3\
**Last updated:** [March 1, 2025, 1:17am UTC](https://forums.swift.org/t/adding-test-diamonds-to-xcode-with-a-macro/78197 "2025-03-01T01:17:39Z")

</div>

I'm currently writing a macro which outputs Swift Testing code: // Macro code ... @MainActor @SnapshotSuite struct MySuite { func makeView() -\> some View { Text("a view") } } which expands to... // Ex…

---

## ["Recursive expansion of macro"?](https://forums.swift.org/t/recursive-expansion-of-macro/68347)

<div class="topic-metadata">

**Author:** [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Replies:** 2\
**Last updated:** [February 26, 2025, 1:52am UTC](https://forums.swift.org/t/recursive-expansion-of-macro/68347 "2025-02-26T01:52:46Z")

</div>

I have a macro that, when applied to a type, applies the same macro to nested types. E.g., @MyMacro struct Foo { struct Bar { } } Should expand to: -@MyMacro struct Foo { + @MyMacro struct Bar { } } And …

---

## [Is there a way to programmatically allow (trust) the compiler plugin to run from the command line?](https://forums.swift.org/t/is-there-a-way-to-programmatically-allow-trust-the-compiler-plugin-to-run-from-the-command-line/65690)

<div class="topic-metadata">

**Author:** [@kishikawakatsumi](https://forums.swift.org/u/kishikawakatsumi)\
**Replies:** 6\
**Last updated:** [February 13, 2025, 6:05pm UTC](https://forums.swift.org/t/is-there-a-way-to-programmatically-allow-trust-the-compiler-plugin-to-run-from-the-command-line/65690 "2025-02-13T18:05:18Z")

</div>

When I try to run code that uses a community-created macro library on CI (Xcode Cloud), it fails because I cannot interact with the compiler plugin to allow its use. The error message looks like this: Target 'PowerAsse…

---

## [Macro-generated code isn't documented](https://forums.swift.org/t/macro-generated-code-isnt-documented/77855)

<div class="topic-metadata">

**Author:** [@MisterDrgn](https://forums.swift.org/u/MisterDrgn)\
**Replies:** 0\
**Last updated:** [February 13, 2025, 3:55pm UTC](https://forums.swift.org/t/macro-generated-code-isnt-documented/77855 "2025-02-13T15:55:18Z")

</div>

I've noticed that when I build documentation in XCode, any code produced by macros (e.g., attached or peer macros on a structure) is ignored. It doesn't show up in the documentation at all. This includes functions and st…

---

## [Automate the expansion of Swift macros](https://forums.swift.org/t/automate-the-expansion-of-swift-macros/76337)

<div class="topic-metadata">

**Author:** [@achernenko-sch](https://forums.swift.org/u/achernenko-sch)\
**Replies:** 9\
**Last updated:** [December 11, 2024, 4:48pm UTC](https://forums.swift.org/t/automate-the-expansion-of-swift-macros/76337 "2024-12-11T16:48:03Z")

</div>

I'm troubleshooting indexing issues in a Swift project I'm working on: Xcode 16.1: Swift macro killing in… | Apple Developer Forums tl;dr the project has several dozen targets depending on a macro, and Xcode spends a lo…

---

## [How to create a xcframework with macros inside?](https://forums.swift.org/t/how-to-create-a-xcframework-with-macros-inside/74989)

<div class="topic-metadata">

**Author:** [@timoninas](https://forums.swift.org/u/timoninas)\
**Replies:** 1\
**Last updated:** [November 28, 2024, 2:37pm UTC](https://forums.swift.org/t/how-to-create-a-xcframework-with-macros-inside/74989 "2024-11-28T14:37:01Z")

</div>

I need to distribute my code with xcframeworks. There is should be distributed macros How can I do this?

---

## [Macro expression with compiler control statements](https://forums.swift.org/t/macro-expression-with-compiler-control-statements/65816)

<div class="topic-metadata">

**Author:** [@joey.gm](https://forums.swift.org/u/joey.gm)\
**Replies:** 3\
**Last updated:** [November 27, 2024, 10:47pm UTC](https://forums.swift.org/t/macro-expression-with-compiler-control-statements/65816 "2024-11-27T22:47:39Z")

</div>

Hi, I was wondering if it is possible to incorporate compiler directives into the expression syntax. This could greatly reduce the amount of repetitive code caused by compiler control statements or preprocessor macros (…

---

## [Debug Code wrapped by Macro](https://forums.swift.org/t/debug-code-wrapped-by-macro/76135)

<div class="topic-metadata">

**Author:** [@johannesd](https://forums.swift.org/u/johannesd)\
**Replies:** 0\
**Last updated:** [November 20, 2024, 9:54am UTC](https://forums.swift.org/t/debug-code-wrapped-by-macro/76135 "2024-11-20T09:54:26Z")

</div>

Hi, I'm developing a macro that takes an async function as a parameter and wraps it in a Task. My problem is that the execution does not halt at breakpoints that I set in the block: Breakpoints that I set in the exp…

---

## [PeerMacro Role does not work together with AccessorMacro](https://forums.swift.org/t/peermacro-role-does-not-work-together-with-accessormacro/75899)

<div class="topic-metadata">

**Author:** [@johannesd](https://forums.swift.org/u/johannesd)\
**Replies:** 0\
**Last updated:** [November 8, 2024, 10:12am UTC](https://forums.swift.org/t/peermacro-role-does-not-work-together-with-accessormacro/75899 "2024-11-08T10:12:42Z")

</div>

Hi, I'm developing an attached macro that is supposed to add an accessor to the type, as well as a peer. The code is very similar to the Clamping example in the Attached Macro Proposal. However, the peer code is not ge…

---

## [How to access the outermost type name in a PeerMacro?](https://forums.swift.org/t/how-to-access-the-outermost-type-name-in-a-peermacro/75226)

<div class="topic-metadata">

**Author:** [@fatbobman](https://forums.swift.org/u/fatbobman)\
**Replies:** 1\
**Last updated:** [October 9, 2024, 12:49am UTC](https://forums.swift.org/t/how-to-access-the-outermost-type-name-in-a-peermacro/75226 "2024-10-09T00:49:03Z")

</div>

Hello everyone, I am currently developing a Swift macro where I can retrieve the name of the outermost type (as a string) in a MemberMacro. However, I also need to access and use this type name in a PeerMacro. For exam…

---

## [Compiler unable to infer type in case of multiple parameter packs](https://forums.swift.org/t/compiler-unable-to-infer-type-in-case-of-multiple-parameter-packs/75209)

<div class="topic-metadata">

**Author:** [@soumyamahunt](https://forums.swift.org/u/soumyamahunt)\
**Replies:** 0\
**Last updated:** [October 7, 2024, 4:38pm UTC](https://forums.swift.org/t/compiler-unable-to-infer-type-in-case-of-multiple-parameter-packs/75209 "2024-10-07T16:38:36Z")

</div>

I have a macro with following definition: @attached(peer) @available(swift 5.9) public macro CodedBy\<Parent, Helper: HelperCoder, each Argument, each Property\>( \_ helperAction: (repeat each Argument, repeat each Pro…

---

## [How to import macros using methods other than SwiftPM](https://forums.swift.org/t/how-to-import-macros-using-methods-other-than-swiftpm/66645)

<div class="topic-metadata">

**Author:** [@ssly](https://forums.swift.org/u/ssly)\
**Replies:** 33\
**Last updated:** [October 7, 2024, 11:05am UTC](https://forums.swift.org/t/how-to-import-macros-using-methods-other-than-swiftpm/66645 "2024-10-07T11:05:30Z")

</div>

I'm trying to import macro functionality using CocoaPods, but Xcode is giving an error: External macro implementation type 'XXX' could not be found for macro 'XXX'; the type must be public and provided by a macro targe…

---

## [How to get access level of a type a macro is attached to?](https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876)

<div class="topic-metadata">

**Author:** [@Dev42](https://forums.swift.org/u/Dev42)\
**Replies:** 5\
**Last updated:** [September 23, 2024, 7:48pm UTC](https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876 "2024-09-23T19:48:34Z")

</div>

Let's look at the following case where I want to use macros to extend an enum type as follows: Source: enum MyEnum { case intValue(Int) case stringValue(string: String) } Destination: enum MyEnum { case i…

---

## [Xcode unable to expand macro when macro name matches name of existing type?](https://forums.swift.org/t/xcode-unable-to-expand-macro-when-macro-name-matches-name-of-existing-type/69997)

<div class="topic-metadata">

**Author:** [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Replies:** 3\
**Last updated:** [September 10, 2024, 7:07pm UTC](https://forums.swift.org/t/xcode-unable-to-expand-macro-when-macro-name-matches-name-of-existing-type/69997 "2024-09-10T19:07:25Z")

</div>

Hi! I'm running into problems expanding a macro when that macro name matches the name of an existing type. It's possible this is an Xcode issue (not a swift-syntax issue)… but I don't see anyway to expand a macro outside…

---

## [How to call macros with module as a "namespace"?](https://forums.swift.org/t/how-to-call-macros-with-module-as-a-namespace/74405)

<div class="topic-metadata">

**Author:** [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Replies:** 4\
**Last updated:** [September 6, 2024, 6:38am UTC](https://forums.swift.org/t/how-to-call-macros-with-module-as-a-namespace/74405 "2024-09-06T06:38:10Z")

</div>

Hi! I am wondering if there is any kind of correct syntax for importing a macro package and then calling one of those macros along with the module as a "namespace". For example: import MyMacro let a = 17 let b = 25 le…

---

## [How to build Package and Depend on Macros in Embedded Swift?](https://forums.swift.org/t/how-to-build-package-and-depend-on-macros-in-embedded-swift/74098)

<div class="topic-metadata">

**Author:** [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Replies:** 0\
**Last updated:** [August 21, 2024, 9:49pm UTC](https://forums.swift.org/t/how-to-build-package-and-depend-on-macros-in-embedded-swift/74098 "2024-08-21T21:49:10Z")

</div>

Hi! I'm just now getting started with Embedded Swift. I attempted to build the stringify macro template from Xcode in embedded mode: .executableTarget( name: "MyMacroClient", dependencies: \["MyMacro"\], swiftSettin…

---

## [How does the SwiftUI Entry macro get information about extension?](https://forums.swift.org/t/how-does-the-swiftui-entry-macro-get-information-about-extension/74061)

<div class="topic-metadata">

**Author:** [@Gotyanov](https://forums.swift.org/u/Gotyanov)\
**Replies:** 6\
**Last updated:** [August 20, 2024, 8:44pm UTC](https://forums.swift.org/t/how-does-the-swiftui-entry-macro-get-information-about-extension/74061 "2024-08-20T20:44:11Z")

</div>

The code generated by the Entry macro depends on the extension in which the macro is applied. extension EnvironmentValues { @Entry var value: Int = 42 } expands to extension EnvironmentValues { var value: Int …

---

## [How is SwiftUI entry macro possible?](https://forums.swift.org/t/how-is-swiftui-entry-macro-possible/74018)

<div class="topic-metadata">

**Author:** [@yapryntsev](https://forums.swift.org/u/yapryntsev)\
**Replies:** 2\
**Last updated:** [August 19, 2024, 6:08am UTC](https://forums.swift.org/t/how-is-swiftui-entry-macro-possible/74018 "2024-08-19T06:08:53Z")

</div>

Hey everyone! Since the computed property requires explicit type annotation, how is the code produced by SwiftUI Entry macro valid? The result from macro expansion written by hand gets a compiler error.

---

## [Accessor macros and lazy properties](https://forums.swift.org/t/accessor-macros-and-lazy-properties/73869)

<div class="topic-metadata">

**Author:** [@Gotyanov](https://forums.swift.org/u/Gotyanov)\
**Replies:** 5\
**Last updated:** [August 12, 2024, 8:02pm UTC](https://forums.swift.org/t/accessor-macros-and-lazy-properties/73869 "2024-08-12T20:02:59Z")

</div>

I'm trying to implement a macro that expands the following code class Klass { @ThreadSafe lazy var value: Int = compute() func compute() -\> Int { 42 } } to class Klass { var value: Int { \_read { …

---

## [Macro protocols in swift-syntax lack async from expansion function requirements despite proposals stating they should have it](https://forums.swift.org/t/macro-protocols-in-swift-syntax-lack-async-from-expansion-function-requirements-despite-proposals-stating-they-should-have-it/73750)

<div class="topic-metadata">

**Author:** [@roopekv](https://forums.swift.org/u/roopekv)\
**Replies:** 1\
**Last updated:** [August 13, 2024, 6:47pm UTC](https://forums.swift.org/t/macro-protocols-in-swift-syntax-lack-async-from-expansion-function-requirements-despite-proposals-stating-they-should-have-it/73750 "2024-08-13T18:47:08Z")

</div>

Swift's concurrency features cannot be fully utilized in macros without being able to implement macro expansion functions as async. The Swift Evolution proposals clearly state that the expansion functions that the diffe…

---

## [Body Macro formatted despite formatMode disabled](https://forums.swift.org/t/body-macro-formatted-despite-formatmode-disabled/73488)

<div class="topic-metadata">

**Author:** [@mtsrodrigues](https://forums.swift.org/u/mtsrodrigues)\
**Replies:** 4\
**Last updated:** [July 26, 2024, 8:49pm UTC](https://forums.swift.org/t/body-macro-formatted-despite-formatmode-disabled/73488 "2024-07-26T20:49:46Z")

</div>

I'm having trouble using #sourceLocation inside a body macro expansion because the first level of nodes is always formatted. Consider the following macro: public struct SourceLocationMacro: BodyMacro { public …

[Previous page](https://forums.swift.org/c/development/macros/96.md)

[Next page](https://forums.swift.org/c/development/macros/96.md?page=2)
