You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Peer宏添加含其他Peer宏的属性致Swift编译器崩溃问题排查

Swift编译器崩溃:Peer宏生成带其他Peer宏标记的属性时触发错误

当使用Peer宏生成带有另一个Peer宏标记的属性时,Swift编译器会直接崩溃并抛出如下错误:

Command SwiftCompile failed with a nonzero exit code

移除生成代码中的Peer宏标记后,编译可正常完成。

问题复现步骤

  • 定义两个Peer宏:
    第一个宏SecretNumberMacro用于生成普通属性:
    public struct SecretNumberMacro: PeerMacro {
        public static func expansion(of node: AttributeSyntax,
                                     providingPeersOf declaration: some DeclSyntaxProtocol,
                                     in context: some MacroExpansionContext) throws -> [DeclSyntax] {
    
            return ["let secretNumber = 5"]  // 必须生成属性类声明
        }
    }
    
    第二个宏SecretWordAndNumberMacro生成带有@SecretNumber标记的属性:
    public struct SecretWordAndNumberMacro: PeerMacro {
        public static func expansion(of node: AttributeSyntax,
                                     providingPeersOf declaration: some DeclSyntaxProtocol,
                                     in context: some MacroExpansionContext) throws -> [DeclSyntax] {
    
            return ["@SecretNumber let secretWord = \"Hello\""]  // 此处使用了另一个Peer宏
        }
    }
    
  • 在结构体中使用宏触发崩溃:
    struct Person { 
        @SecretWordAndNumber var name = "John Doe"
    }
    

错误详情

编译器报错核心内容:

  1. While emitting IR SIL function "@$s16ResettableClient6PersonC12secretNumberSivg".
    for getter for secretNumber (at @swiftmacro_16ResettableClient6PersonC10secretWord0A0fMp.swift:1:5)
    Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var LLVM_SYMBOLIZER_PATH to point to it):

注:"ResettableClient"为主.swift文件所在的模块名称

根源推测

在@StoreInitial与@Observable宏兼容的场景中,曾触发类似错误:

'self' used in property access '$_name' before 'self.init' call

重实现@Observable宏后发现,@ObservationTracked生成的@StoreInitial @ObservationIgnored var _name = "John Doe"属性是问题诱因。由此推断,本次编译器崩溃的根源是:宏生成的代码在self初始化完成前就访问了self,导致编译器在生成IR SIL阶段出现逻辑错误。

内容的提问来源于stack exchange,提问作者rayaantaneja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 15:10:05