iOS Development

ios – Macros with lazy var

Spread the love


I have been exploring Macros and was making an attempt to create one which solely returns a lazy var, utilizing the next code:

public struct SubjectPublisherMacro: PeerMacro {

    public static func growth(of node: AttributeSyntax,
                                 providingPeersOf declaration: some DeclSyntaxProtocol,
                                 in context: some MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax] {

        return [
            "lazy var state: AnyPublisher<String, Never> = { subject.eraseToAnyPublisher() }()"
        ]
    }
}

When utilized to a Topic, like the next:

@SubjectPublisher
personal var topic = PassthroughSubject<String, By no means>()

If we broaden the macro, the code generated is successfully:

lazy var state: AnyPublisher<String, By no means> = {
    topic.eraseToAnyPublisher()
}()

Nevertheless, after we compile, it provides an error, and we have now the next Stack dump:

0.  Program arguments: /Functions/Xcode-15.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Customers/xpto/macros/FFMacros/Sources/FFMacrosClient/major.swift -emit-dependencies-path /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Intermediates.noindex/FFMacros.construct/Debug/FFMacrosClient.construct/Objects-normal/arm64/major.d -emit-const-values-path /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Intermediates.noindex/FFMacros.construct/Debug/FFMacrosClient.construct/Objects-normal/arm64/major.swiftconstvalues -emit-reference-dependencies-path /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Intermediates.noindex/FFMacros.construct/Debug/FFMacrosClient.construct/Objects-normal/arm64/major.swiftdeps -serialize-diagnostics-path /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Intermediates.noindex/FFMacros.construct/Debug/FFMacrosClient.construct/Objects-normal/arm64/major.dia -target arm64-apple-macos10.15 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -I /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Merchandise/Debug -I /Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Merchandise/Debug/PackageFrameworks -F /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Merchandise/Debug -F /Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path /Customers/xpto/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -profile-generate -profile-coverage-mapping -swift-version 5 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -serialize-debugging-options -load-plugin-executable /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Merchandise/Debug/FFMacrosMacros#FFMacrosMacros -package-name ffmacros -const-gather-protocols-file /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Intermediates.noindex/FFMacros.construct/Debug/FFMacrosClient.construct/Objects-normal/arm64/FFMacrosClient_const_extract_protocols.json -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Customers/xpto/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Customers/xpto/macros/FFMacros -resource-dir /Functions/Xcode-15.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -ivfsstatcache -Xcc /Customers/xpto/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx14.2-23C53-ddea79c7890ab41bba4a63928c655a96.sdkstatcache -Xcc -I/Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Merchandise/Debug/embody -Xcc -I/Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Intermediates.noindex/FFMacros.construct/Debug/FFMacrosClient.construct/DerivedSources-normal/arm64 -Xcc -I/Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Intermediates.noindex/FFMacros.construct/Debug/FFMacrosClient.construct/DerivedSources/arm64 -Xcc -I/Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Intermediates.noindex/FFMacros.construct/Debug/FFMacrosClient.construct/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name FFMacrosClient -frontend-parseable-output -disable-clang-spi -target-sdk-version 14.2 -target-sdk-name macosx14.2 -external-plugin-path /Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/lib/swift/host/plugins#/Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/native/lib/swift/host/plugins#/Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/native/lib/swift/host/plugins#/Functions/Xcode-15.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Functions/Xcode-15.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Functions/Xcode-15.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/native/lib/swift/host/plugins -o /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Construct/Intermediates.noindex/FFMacros.construct/Debug/FFMacrosClient.construct/Objects-normal/arm64/major.o -index-unit-output-path /FFMacros.construct/Debug/FFMacrosClient.construct/Objects-normal/arm64/major.o -index-store-path /Customers/xpto/Library/Developer/Xcode/DerivedData/FFMacros-byrxxptdunfedzeunapzrisyulor/Index.noindex/DataStore -index-system-modules
1.  Apple Swift model 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
2.  Compiling with the present language model
3.  Whereas evaluating request IRGenRequest(IR Era for file "/Customers/macros/FFMacros/Sources/FFMacrosClient/major.swift")
4.  Whereas emitting IR SIL perform "@$s14FFMacrosClient4TestC5state7Combine12AnyPublisherVySSs5NeverOGvg".
 for getter for state (at @__swiftmacro_14FFMacrosClient4TestC7subject33_32E41C858868D91DD4497E3BF955E2AFLL16SubjectPublisherfMp_.swift:1:10)
Stack dump with out image names (guarantee you've got llvm-symbolizer in your PATH or set the atmosphere var `LLVM_SYMBOLIZER_PATH` to level to it):
0  swift-frontend           0x000000010728dabc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109ef3cb0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109c5d054 SignalHandler(int) + 352
3  libsystem_platform.dylib 0x0000000186829a24 _sigtramp + 56
4  swift-frontend           0x00000001098a52a4 swift::irgen::projectPhysicalClassMemberAddress(swift::irgen::IRGenFunction&, llvm::Worth*, swift::SILType, swift::SILType, swift::VarDecl*, swift::GenericSignature) + 1036
5  swift-frontend           0x00000001098a52a4 swift::irgen::projectPhysicalClassMemberAddress(swift::irgen::IRGenFunction&, llvm::Worth*, swift::SILType, swift::SILType, swift::VarDecl*, swift::GenericSignature) + 1036
6  swift-frontend           0x0000000106e90ee4 (nameless namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 79544
7  swift-frontend           0x0000000109611fdc swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 11680
8  swift-frontend           0x00000001072109c8 swift::irgen::IRGenerator::emitGlobalTopLevel(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>,
std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>> const&) + 2516
9  swift-frontend           0x00000001058b59d0 swift::IRGenRequest::consider(swift::Evaluator&, swift::IRGenDescriptor) const + 6532
10 swift-frontend           0x0000000105188adc swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 176
11 swift-frontend           0x0000000106b59f64 llvm::Anticipated<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 1480
12 swift-frontend           0x0000000108e00fb8 swift::performIRGeneration(swift::FileUnit*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**) + 264
13 swift-frontend           0x00000001097cd99c generateIR(swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, llvm::GlobalVariable*&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>) + 156
14 swift-frontend           0x00000001097d56d4 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1596
15 swift-frontend           0x00000001097d0a08 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1748
16 swift-frontend           0x00000001097d4854 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4568
17 swift-frontend           0x000000010983bd44 swift::mainEntry(int, char const**) + 4408
18 dyld                     0x00000001864790e0 begin + 2360
LLVM Profile Error: Failed to write down file "default.profraw": Operation not permitted

If we modify our macro to return a computed var as a substitute of a lazy var, we will compile efficiently. Nevertheless, our aim was to return the lazy var. Does anybody have an concept of what the difficulty may be?

Thanks on your assist!

Leave a Reply

Your email address will not be published. Required fields are marked *