swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67k stars 10.32k forks source link

Extension Macro fails with nonzero exit code on compile #67620

Open JaapWijnen opened 11 months ago

JaapWijnen commented 11 months ago

I'm creating an extension macro in a swift package. On compiling the executable target that is using my Macro I get the following: Command SwiftEmitModule failed with a nonzero exit code

SwiftEmitModule normal arm64 Emitting\ module\ for\ MyMacroClient (in target 'MyMacroClient' from project 'KeyPathIterable')
    cd /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/SourcePackages
    builtin-swiftTaskExecution -- /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-23-a.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/jaap/Developer/PassiveLogic/KeyPathIterable/Sources/MyMacroClient/main.swift -target arm64-apple-macos10.15 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -I /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug -I /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug/PackageFrameworks -F /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path /Users/jaap/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 -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins\#/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins\#/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -serialize-debugging-options -load-plugin-executable /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug/KeyPathIterableMacro\#KeyPathIterableMacro -package-name keypathiterable -const-gather-protocols-file /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient_const_extract_protocols.json -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/jaap/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/SourcePackages -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-23-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I/Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug/include -Xcc -I/Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/DerivedSources-normal/arm64 -Xcc -I/Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/DerivedSources/arm64 -Xcc -I/Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG\=1 -module-name MyMacroClient -disable-clang-spi -target-sdk-version 14.0 -target-sdk-name macosx14.0 -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/lib/swift/host/plugins\#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/local/lib/swift/host/plugins\#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins\#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins\#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-23-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-23-a.xctoolchain/usr/local/lib/swift/host/plugins -emit-module-doc-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient.swiftdoc -emit-module-source-info-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient.swiftsourceinfo -serialize-diagnostics-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient-master-emit-module.dia -emit-dependencies-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient-master-emit-module.d -o /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient.swiftmodule -emit-abi-descriptor-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient.abi.json

AttributeSyntax
├─atSign: atSign
╰─attributeName: SimpleTypeIdentifierSyntax
  ╰─name: identifier("KeyPathIterable")
StructDeclSyntax
├─attributes: AttributeListSyntax
│ ╰─[0]: AttributeSyntax
│   ├─atSign: atSign
│   ╰─attributeName: SimpleTypeIdentifierSyntax
│     ╰─name: identifier("KeyPathIterable")
├─structKeyword: keyword(SwiftSyntax.Keyword.struct)
├─name: identifier("Thing")
╰─memberBlock: MemberDeclBlockSyntax
  ├─leftBrace: leftBrace
  ├─members: MemberDeclListSyntax
  │ ├─[0]: MemberDeclListItemSyntax
  │ │ ╰─decl: VariableDeclSyntax
  │ │   ├─bindingSpecifier: keyword(SwiftSyntax.Keyword.var)
  │ │   ╰─bindings: PatternBindingListSyntax
  │ │     ╰─[0]: PatternBindingSyntax
  │ │       ├─pattern: IdentifierPatternSyntax
  │ │       │ ╰─identifier: identifier("foo")
  │ │       ╰─typeAnnotation: TypeAnnotationSyntax
  │ │         ├─colon: colon
  │ │         ╰─type: SimpleTypeIdentifierSyntax
  │ │           ╰─name: identifier("Double")
  │ ╰─[1]: MemberDeclListItemSyntax
  │   ╰─decl: VariableDeclSyntax
  │     ├─bindingSpecifier: keyword(SwiftSyntax.Keyword.var)
  │     ╰─bindings: PatternBindingListSyntax
  │       ╰─[0]: PatternBindingSyntax
  │         ├─pattern: IdentifierPatternSyntax
  │         │ ╰─identifier: identifier("bar")
  │         ╰─typeAnnotation: TypeAnnotationSyntax
  │           ├─colon: colon
  │           ╰─type: SimpleTypeIdentifierSyntax
  │             ╰─name: identifier("Double")
  ╰─rightBrace: rightBrace
SimpleTypeIdentifierSyntax
╰─name: identifier("Thing")
Assertion failed: (conformance->isComplete() || allowCompilerErrors()), function writeLocalNormalProtocolConformance, file Serialization.cpp, line 1669.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-23-a.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/jaap/Developer/PassiveLogic/KeyPathIterable/Sources/MyMacroClient/main.swift -target arm64-apple-macos10.15 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -I /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug -I /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug/PackageFrameworks -F /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path /Users/jaap/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 -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -serialize-debugging-options -load-plugin-executable /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug/KeyPathIterableMacro#KeyPathIterableMacro -package-name keypathiterable -const-gather-protocols-file /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient_const_extract_protocols.json -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/jaap/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/SourcePackages -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-23-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I/Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Products/Debug/include -Xcc -I/Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/DerivedSources-normal/arm64 -Xcc -I/Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/DerivedSources/arm64 -Xcc -I/Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name MyMacroClient -disable-clang-spi -target-sdk-version 14.0 -target-sdk-name macosx14.0 -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-23-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-23-a.xctoolchain/usr/local/lib/swift/host/plugins -emit-module-doc-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient.swiftdoc -emit-module-source-info-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient.swiftsourceinfo -serialize-diagnostics-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient-master-emit-module.dia -emit-dependencies-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient-master-emit-module.d -o /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient.swiftmodule -emit-abi-descriptor-path /Users/jaap/Library/Developer/Xcode/DerivedData/KeyPathIterable-fowbqqyxaenyttetrxumhxoechph/Build/Intermediates.noindex/KeyPathIterable.build/Debug/MyMacroClient.build/Objects-normal/arm64/MyMacroClient.abi.json
1.  Apple Swift version 5.9-dev (LLVM 9b562f55c38e378, Swift b4ee68bd37c4f7d)
2.  Compiling with the current language version
3.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { Non-Diagnostic Mandatory Optimizations, Serialization, Rest of Onone } on SIL for MyMacroClient)
4.  While running pass #27 SILModuleTransform "SerializeSILPass".
5.  While serializing protocol conformance to 'KeyPathIterable' (in module 'KeyPathIterable') for type 'Thing' (declared at [/Users/jaap/Developer/PassiveLogic/KeyPathIterable/Sources/MyMacroClient/main.swift:6:1 - line:9:1] RangeText="struct Thing {
    var foo: Double
    var bar: Double
")
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):
0  swift-frontend           0x0000000107c870bc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107c86460 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107c876fc SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001a847aa24 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a844bc28 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a8359ae8 abort + 180
6  libsystem_c.dylib        0x00000001a8358e44 err + 0
7  swift-frontend           0x0000000107f485bc swift::serialization::Serializer::addConformanceRef(swift::ProtocolConformance*) (.cold.1) + 0
8  swift-frontend           0x0000000103a8dc90 swift::serialization::Serializer::writeLocalNormalProtocolConformance(swift::NormalProtocolConformance*) + 2008
9  swift-frontend           0x0000000103aa0ba0 swift::serialization::Serializer::writeAllDeclsAndTypes() + 65204
10 swift-frontend           0x0000000103aa2944 swift::serialization::Serializer::writeAST(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>) + 3768
11 swift-frontend           0x0000000103aab570 swift::serialization::Serializer::writeToStream(llvm::raw_ostream&, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SILModule const*, swift::SerializationOptions const&, swift::fine_grained_dependencies::SourceFileDepGraph const*) + 2056
12 swift-frontend           0x00000001030a7ea0 bool llvm::function_ref<bool (llvm::raw_pwrite_stream&)>::callback_fn<swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::symbolgraphgen::SymbolGraphOptions const&, swift::SILModule const*, swift::fine_grained_dependencies::SourceFileDepGraph const*)::$_3>(long, llvm::raw_pwrite_stream&) + 96
13 swift-frontend           0x0000000102d1534c swift::withOutputPath(swift::DiagnosticEngine&, llvm::vfs::OutputBackend&, llvm::StringRef, llvm::function_ref<bool (llvm::raw_pwrite_stream&)>) + 168
14 swift-frontend           0x00000001030a7aac swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::symbolgraphgen::SymbolGraphOptions const&, swift::SILModule const*, swift::fine_grained_dependencies::SourceFileDepGraph const*) + 192
15 swift-frontend           0x0000000102eae048 bool llvm::function_ref<bool (swift::fine_grained_dependencies::SourceFileDepGraph&&)>::callback_fn<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*)::$_33::operator()() const::'lambda'(swift::fine_grained_dependencies::SourceFileDepGraph&&)>(long, swift::fine_grained_dependencies::SourceFileDepGraph&&) + 40
16 swift-frontend           0x00000001043f0b20 swift::fine_grained_dependencies::withReferenceDependencies(llvm::PointerUnion<swift::ModuleDecl const*, swift::SourceFile const*>, swift::DependencyTracker const&, llvm::vfs::OutputBackend&, llvm::StringRef, bool, llvm::function_ref<bool (swift::fine_grained_dependencies::SourceFileDepGraph&&)>) + 164
17 swift-frontend           0x0000000102eadfbc std::__1::__function::__func<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*)::$_33, std::__1::allocator<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*)::$_33>, void ()>::operator()() + 296
18 swift-frontend           0x0000000103b9f8cc swift::SILModule::serialize() + 44
19 swift-frontend           0x00000001038bcfe0 SerializeSILPass::run() + 156
20 swift-frontend           0x000000010376fa5c swift::SILPassManager::runModulePass(unsigned int) + 980
21 swift-frontend           0x00000001037759e8 swift::SILPassManager::execute() + 624
22 swift-frontend           0x000000010376c9e0 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
23 swift-frontend           0x000000010376c964 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
24 swift-frontend           0x00000001037abe18 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
25 swift-frontend           0x0000000103795210 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
26 swift-frontend           0x000000010376cbd4 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
27 swift-frontend           0x0000000103797574 swift::runSILPassesForOnone(swift::SILModule&) + 80
28 swift-frontend           0x000000010307a784 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 252
29 swift-frontend           0x0000000102eaa358 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*) + 792
30 swift-frontend           0x0000000102ea9c7c swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1160
31 swift-frontend           0x0000000102eb8dfc withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
32 swift-frontend           0x0000000102eac5d0 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 740
33 swift-frontend           0x0000000102eab530 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2504
34 swift-frontend           0x0000000102cf2760 swift::mainEntry(int, char const**) + 2144
35 dyld                     0x00000001a80f3f28 start + 2236
LLVM Profile Error: Failed to write file "default.profraw": Operation not permitted
Command SwiftEmitModule failed with a nonzero exit code

Steps to reproduce This can be reproduced as follows:

Package.swift file with the following:

// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import CompilerPluginSupport
import PackageDescription

let package = Package(
    name: "KeyPathIterable",
    platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
    products: [
        // Products define the executables and libraries a package produces, making them visible to other packages.
        .library(
            name: "KeyPathIterable",
            targets: ["KeyPathIterable"]
        ),
        .executable(
            name: "MyMacroClient",
            targets: ["MyMacroClient"]
        ),
    ],
    dependencies: [
        // Depend on the latest Swift 5.9 prerelease of SwiftSyntax
        .package(url: "https://github.com/apple/swift-syntax.git", revision: "95b2677"),
    ],
    targets: [
        .macro(
            name: "KeyPathIterableMacro",
            dependencies: [
                .product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
                .product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
                .product(name: "SwiftSyntax", package: "swift-syntax"),
                .product(name: "SwiftDiagnostics", package: "swift-syntax"),
                .product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
            ]
        ),

        // Library that exposes a macro as part of its API, which is used in client programs.
        .target(
            name: "KeyPathIterable",
            dependencies: [
                "KeyPathIterableMacro",
            ]
        ),

        // A client of the library, which is able to use the macro in its own code.
        .executableTarget(name: "MyMacroClient", dependencies: ["KeyPathIterable"]),
    ]
)

Sources/MyMacroClient/main.swift:

import KeyPathIterable

@KeyPathIterable
struct Thing {
    var foo: Double
    var bar: Double
}

Sources/KeyPathIterable/KeyPathIterable.swift

import KeyPathIterableMacro

@attached(extension, conformances: KeyPathIterable, names: named(keyPaths))
public macro KeyPathIterable() = #externalMacro(module: "KeyPathIterableMacro", type: "KeyPathIterableMacro")

public protocol KeyPathIterable {
    static var keyPaths: [PartialKeyPath<Self>] { get }
}

Sources/KeyPathIterableMacro/KeyPathIterableMacro.swift

import SwiftCompilerPlugin
import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

@main
struct MyMacroPlugin: CompilerPlugin {
    let providingMacros: [Macro.Type] = [
        KeyPathIterableMacro.self,
    ]
}

public struct KeyPathIterableMacro: ExtensionMacro {
    public static func expansion(
        of node: AttributeSyntax,
        attachedTo declaration: some DeclGroupSyntax,
        providingExtensionsOf type: some TypeSyntaxProtocol,
        conformingTo protocols: [TypeSyntax],
        in context: some MacroExpansionContext
    ) throws -> [ExtensionDeclSyntax] {
        print(node.debugDescription)
        print(declaration.debugDescription)
        print(type.debugDescription)
        return try protocols.map { prot in
            try ExtensionDeclSyntax(
                extendedType: type,
                inheritanceClause:  .init() {
                    for prot in protocols {
                        InheritedTypeSyntax(type: prot)
                    }
                }
            ) {
                try generateKeyPathMemberDeclaration(of: node, attachedTo: declaration)
            }
        }
    }

    internal static func generateKeyPathMemberDeclaration(
        of node: AttributeSyntax,
        attachedTo declaration: some DeclGroupSyntax
    ) throws -> DeclSyntax {
        guard declaration.is(StructDeclSyntax.self) || declaration.is(ClassDeclSyntax.self) else {
            throw DiagnosticsError(diagnostics: [])
        }

        let storedPropertyIdentifiers = declaration.storedProperties
            .map { $0.identifier }

        return DeclSyntax(
            """
            static var keyPaths: [PartialKeyPath<Self>] {
                [\(raw: storedPropertyIdentifiers.map { "\\.\($0.text)" }.joined(separator: ", "))]
            }
            """
        )
    }
}

extension DeclGroupSyntax {
    public var properties: [VariableDeclSyntax] {
        self.memberBlock.members.compactMap { $0.decl.as(VariableDeclSyntax.self) }
    }

    public var storedProperties: [VariableDeclSyntax] {
        self.properties.filter { $0.isStored }
    }
}

extension VariableDeclSyntax {
    var isStored: Bool {
        self.bindings.contains { $0.accessors == nil }
    }

    public var identifier: TokenSyntax {
        self.bindings
            .compactMap { binding in binding.pattern.as(IdentifierPatternSyntax.self) }
            .first! 
            .identifier
    }
}

Expected behavior Macro would compile or give me better compiler errors to fix my code.

Environment

JaapWijnen commented 11 months ago

Still running into this on the newest toolchain 2023-08-03

Viranchee commented 10 months ago

I was able to reproduce this issue on the nightly Swift release 2023-08-12

Running swift run first time results in the same error as @JaapWijnen mentioned While successive builds compile just fine.

eg. a modified main.swift

import KeyPathIterable

@KeyPathIterable
struct Thing {
   var foo: Double
   var bar: Double
}

var thing = Thing(foo: 1, bar: 20)
for keyPath in Thing.keyPaths {
    debugPrint(keyPath)
}
print("Hello")

swift run

Building for debugging...
error: emit-module command failed due to signal 6 (use -v to see invocation)
...
Assertion failed: (conformance->isComplete() || allowCompilerErrors()), function writeLocalNormalProtocolConformance, file Serialization.cpp, line 1669.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.      Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-08-12-a.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/viranchee/Documents/KeyPathIterable/Sources/MyMacroClient/main.swift -target arm64-apple-macosx10.15 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -I /Users/viranchee/Documents/KeyPathIterable/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/viranchee/Documents/KeyPathIterable/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-08-12-a.xctoolchain/usr/bin/swift-driver -entry-point-function-name MyMacroClient_main -load-plugin-executable /Users/viranchee/Documents/KeyPathIterable/.build/arm64-apple-macosx/debug/KeyPathIterableMacro#KeyPathIterableMacro -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-08-12-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -isysroot -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC -Xcc -g -module-name MyMacroClient -package-name keypathiterable -target-sdk-version 13.3 -target-sdk-name macosx13.3 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-08-12-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-08-12-a.xctoolchain/usr/local/lib/swift/host/plugins -emit-module-doc-path /Users/viranchee/Documents/KeyPathIterable/.build/arm64-apple-macosx/debug/MyMacroClient.swiftdoc -emit-module-source-info-path /Users/viranchee/Documents/KeyPathIterable/.build/arm64-apple-macosx/debug/MyMacroClient.swiftsourceinfo -emit-dependencies-path /Users/viranchee/Documents/KeyPathIterable/.build/arm64-apple-macosx/debug/MyMacroClient.build/MyMacroClient.emit-module.d -o /Users/viranchee/Documents/KeyPathIterable/.build/arm64-apple-macosx/debug/MyMacroClient.swiftmodule -emit-abi-descriptor-path /Users/viranchee/Documents/KeyPathIterable/.build/arm64-apple-macosx/debug/MyMacroClient.abi.json
1.      Apple Swift version 5.9-dev (LLVM fd38736063c15cd, Swift a533c63d783f5b8)
2.      Compiling with the current language version
3.      While evaluating request ExecuteSILPipelineRequest(Run pipelines { Non-Diagnostic Mandatory Optimizations, Serialization, Rest of Onone } on SIL for MyMacroClient)
4.      While running pass #48 SILModuleTransform "SerializeSILPass".
5.      While serializing protocol conformance to 'KeyPathIterable' (in module 'KeyPathIterable') for type 'Thing' (declared at [/Users/viranchee/Documents/KeyPathIterable/Sources/MyMacroClient/main.swift:4:1 - line:7:1] RangeText="struct Thing {
var foo: Double
var bar: Double
")
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):
0  swift-frontend           0x0000000105df0d80 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000105df0124 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000105df13c0 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001a0a9aa24 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a0a6bc28 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a0979ae8 abort + 180
6  libsystem_c.dylib        0x00000001a0978e44 err + 0
7  swift-frontend           0x00000001060b4ee8 swift::serialization::Serializer::addConformanceRef(swift::ProtocolConformance*) (.cold.1) + 0
8  swift-frontend           0x0000000101bdb088 swift::serialization::Serializer::writeLocalNormalProtocolConformance(swift::NormalProtocolConformance*) + 2008
9  swift-frontend           0x0000000101bee058 swift::serialization::Serializer::writeAllDeclsAndTypes() + 65532
10 swift-frontend           0x0000000101beff9c swift::serialization::Serializer::writeAST(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>) + 3768
11 swift-frontend           0x0000000101bf8768 swift::serialization::Serializer::writeToStream(llvm::raw_ostream&, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SILModule const*, swift::SerializationOptions const&, swift::fine_grained_dependencies::SourceFileDepGraph const*) + 936
12 swift-frontend           0x00000001011e2f4c bool llvm::function_ref<bool (llvm::raw_pwrite_stream&)>::callback_fn<swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::symbolgraphgen::SymbolGraphOptions const&, swift::SILModule const*, swift::fine_grained_dependencies::SourceFileDepGraph const*)::$_3>(long, llvm::raw_pwrite_stream&) + 96
13 swift-frontend           0x0000000100e42b98 swift::withOutputPath(swift::DiagnosticEngine&, llvm::vfs::OutputBackend&, llvm::StringRef, llvm::function_ref<bool (llvm::raw_pwrite_stream&)>) + 168
14 swift-frontend           0x00000001011e2b58 swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::symbolgraphgen::SymbolGraphOptions const&, swift::SILModule const*, swift::fine_grained_dependencies::SourceFileDepGraph const*) + 192
15 swift-frontend           0x0000000100fe8ffc bool llvm::function_ref<bool (swift::fine_grained_dependencies::SourceFileDepGraph&&)>::callback_fn<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*)::$_33::operator()() const::'lambda'(swift::fine_grained_dependencies::SourceFileDepGraph&&)>(long, swift::fine_grained_dependencies::SourceFileDepGraph&&) + 40
16 swift-frontend           0x0000000102546090 swift::fine_grained_dependencies::withReferenceDependencies(llvm::PointerUnion<swift::ModuleDecl const*, swift::SourceFile const*>, swift::DependencyTracker const&, llvm::vfs::OutputBackend&, llvm::StringRef, bool, llvm::function_ref<bool (swift::fine_grained_dependencies::SourceFileDepGraph&&)>) + 164
17 swift-frontend           0x0000000100fe8f70 std::__1::__function::__func<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*)::$_33, std::__1::allocator<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*)::$_33>, void ()>::operator()() + 296
18 swift-frontend           0x0000000101cecab4 swift::SILModule::serialize() + 44
19 swift-frontend           0x0000000101a096a8 SerializeSILPass::run() + 156
20 swift-frontend           0x00000001018b77ac swift::SILPassManager::runModulePass(unsigned int) + 980
21 swift-frontend           0x00000001018bd6c8 swift::SILPassManager::execute() + 624
22 swift-frontend           0x00000001018b472c swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
23 swift-frontend           0x00000001018b46b0 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
24 swift-frontend           0x00000001018f5058 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
25 swift-frontend           0x00000001018de5a8 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
26 swift-frontend           0x00000001018b4920 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
27 swift-frontend           0x00000001018e08fc swift::runSILPassesForOnone(swift::SILModule&) + 80
28 swift-frontend           0x00000001011b606c swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 260
29 swift-frontend           0x0000000100fe5338 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*) + 792
30 swift-frontend           0x0000000100fe4c5c swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1164
31 swift-frontend           0x0000000100ff3dd4 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
32 swift-frontend           0x0000000100fe7584 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 740
33 swift-frontend           0x0000000100fe6508 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2496
34 swift-frontend           0x0000000100e1ff04 swift::mainEntry(int, char const**) + 2144
35 dyld                     0x00000001a0713f28 start + 2236

Running again:

swift run


Building for debugging...
warning: Could not open '/Users/viranchee/Documents/KeyPathIterable/.build/arm64-apple-macosx/debug/MyMacroClient.swiftmodule'

[2/2] Linking MyMacroClient
Build complete! (0.65s)
\Thing.foo
\Thing.bar
Hello
Viranchee commented 10 months ago

This is fixed in nightly 2023-08-27