Crash with ExtensionMacro

Hi im experiencing a crash when applying a extension macro.

I cannot share the actual source code since its from my job and i did not manage to extract a reproducible example (the macro is applied to a lot of types and i dont know where to start searching)

I suspect that this is a compiler bug, since when i inline the macros in some places it builds without problems

With Xcode 15 Beta 7

Report
1.	Apple Swift version 5.9 (swiftlang-5.9.0.128.106 clang-1500.0.40.1)
2.	Compiling with the current language version
3.	While evaluating request IRGenRequest(IR Generation for file "/Users/smon/Developer/<redacted>.swift")
4.	While emitting IR SIL function "@$s15WFAPIDefinition19CreateTripsEndpointV4POSTV9responses18OpenAPIDescription9ResponsesVvgZ".
 for getter for responses (at /Users/smon/Developer/<redacted>.swift:19:20)
5.	While reading from module 'WFModels', builder version '5.9(5.9)/Apple Swift version 5.9 (swiftlang-5.9.0.128.106 clang-1500.0.40.1)', built from source, non-resilient, loaded from '/Users/smon/Library/Developer/Xcode/DerivedData/wegfinder-djyismktrinblhakuopxifgxjqid/Build/Products/Debug-iphonesimulator/WFModels.swiftmodule/arm64-apple-ios-simulator.swiftmodule'
6.	While finishing conformance for protocol conformance to 'APIModel' (in module 'OpenAPIModels') for type 'Ticket'
7.	*** DESERIALIZATION FAILURE ***
*** If any module named here was modified in the SDK, please delete the ***
*** new swiftmodule files from the SDK and keep only swiftinterfaces.   ***
module 'WFModels', builder version '5.9(5.9)/Apple Swift version 5.9 (swiftlang-5.9.0.128.106 clang-1500.0.40.1)', built from source, non-resilient, loaded from '/Users/smon/Library/Developer/Xcode/DerivedData/wegfinder-djyismktrinblhakuopxifgxjqid/Build/Products/Debug-iphonesimulator/WFModels.swiftmodule/arm64-apple-ios-simulator.swiftmodule'
serialized conformances do not match requirement signature

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           0x0000000105a5f7b4 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001085ddf0c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010431f720 SignalHandler(int) + 352
3  libsystem_platform.dylib 0x00000001952b6a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000195287c28 pthread_kill + 288
5  libsystem_c.dylib        0x0000000195195ae8 abort + 180
6  swift-frontend           0x0000000103c7805c clang::IdentifierResolver::tryAddTopLevelDecl(clang::NamedDecl*, clang::DeclarationName) + 0
7  swift-frontend           0x00000001049c760c clang::NamedDecl* (anonymous namespace)::AttrReader::GetLocalDeclAs<clang::NamedDecl>(unsigned int) + 0
8  swift-frontend           0x00000001065215e8 swift::ModuleFile::fatal(llvm::Error) const + 44
9  swift-frontend           0x0000000104a286c8 llvm::detail::DenseSetPair<clang::DeclContext const*>* llvm::DenseMapBase<llvm::SmallDenseMap<clang::DeclContext const*, llvm::detail::DenseSetEmpty, 16u, llvm::DenseMapInfo<clang::DeclContext const*, void>, llvm::detail::DenseSetPair<clang::DeclContext const*>>, clang::DeclContext const*, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<clang::DeclContext const*, void>, llvm::detail::DenseSetPair<clang::DeclContext const*>>::InsertIntoBucket<clang::DeclContext const* const&, llvm::detail::DenseSetEmpty&>(llvm::detail::DenseSetPair<clang::DeclContext const*>*, clang::DeclContext const* const&, llvm::detail::DenseSetEmpty&) + 0
10 swift-frontend           0x000000010658e52c swift::NormalProtocolConformance::getAssociatedConformance(swift::Type, swift::ProtocolDecl*) const + 72
11 swift-frontend           0x0000000106bc4254 isDependentConformance(swift::irgen::IRGenModule&, swift::RootProtocolConformance const*, llvm::SmallPtrSet<swift::NormalProtocolConformance const*, 4u>&) (.llvm.12130313198954639950) + 848
12 swift-frontend           0x0000000104e0746c swift::irgen::IRGenModule::getConformanceInfo(swift::ProtocolDecl const*, swift::ProtocolConformance const*) + 288
13 swift-frontend           0x0000000103d69808 swift::irgen::emitWitnessTableRef(swift::irgen::IRGenFunction&, swift::CanType, llvm::Value**, swift::ProtocolConformanceRef) + 264
14 swift-frontend           0x0000000104e419a4 bool llvm::function_ref<bool (unsigned int, swift::CanType, swift::ProtocolDecl*)>::callback_fn<emitConditionalConformancesBuffer(swift::irgen::IRGenFunction&, swift::ProtocolConformance const*)::$_13>(long, unsigned int, swift::CanType, swift::ProtocolDecl*) + 200
15 swift-frontend           0x0000000106c4e794 swift::SILWitnessTable::enumerateWitnessTableConditionalConformances(swift::ProtocolConformance const*, llvm::function_ref<bool (unsigned int, swift::CanType, swift::ProtocolDecl*)>) + 384
16 swift-frontend           0x0000000106c290d0 emitWitnessTableAccessorCall(swift::irgen::IRGenFunction&, swift::ProtocolConformance const*, llvm::Value**) + 492
17 swift-frontend           0x0000000106c2bb34 swift::irgen::MetadataResponse llvm::function_ref<swift::irgen::MetadataResponse (swift::irgen::IRGenFunction&, swift::irgen::Explosion&)>::callback_fn<getWitnessTableLazyAccessFunction(swift::irgen::IRGenModule&, swift::ProtocolConformance const*)::$_14>(long, swift::irgen::IRGenFunction&, swift::irgen::Explosion&) + 48
18 swift-frontend           0x00000001059303a0 swift::irgen::emitCacheAccessFunction(swift::irgen::IRGenModule&, llvm::Function*, llvm::Constant*, llvm::Type*, swift::irgen::CacheStrategy, llvm::function_ref<swift::irgen::MetadataResponse (swift::irgen::IRGenFunction&, swift::irgen::Explosion&)>, bool) + 1376
19 swift-frontend           0x0000000106c25f48 (anonymous namespace)::AccessorConformanceInfo::getTable(swift::irgen::IRGenFunction&, llvm::Value**) const + 380
20 swift-frontend           0x0000000103d6981c swift::irgen::emitWitnessTableRef(swift::irgen::IRGenFunction&, swift::CanType, llvm::Value**, swift::ProtocolConformanceRef) + 284
21 swift-frontend           0x0000000104617ce4 forEachProtocolWitnessTable(swift::irgen::IRGenFunction&, swift::CanType, llvm::Value**, swift::CanType, llvm::ArrayRef<swift::ProtocolDecl const*>, llvm::ArrayRef<swift::ProtocolConformanceRef>, llvm::function_ref<void (unsigned int, llvm::Value*)>) + 420
22 swift-frontend           0x000000010348b364 swift::irgen::emitExistentialMetatypeContainer(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::SILType, llvm::Value*, swift::SILType, llvm::ArrayRef<swift::ProtocolConformanceRef>) + 684
23 swift-frontend           0x0000000107c384a0 (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 51192
24 swift-frontend           0x0000000107c07ca0 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 11680
25 swift-frontend           0x000000010594f748 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
26 swift-frontend           0x000000010740bd80 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 6532
27 swift-frontend           0x00000001055eed5c swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 176
28 swift-frontend           0x00000001052db260 llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 1480
29 swift-frontend           0x0000000103f2ebe8 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
30 swift-frontend           0x0000000108591284 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
31 swift-frontend           0x0000000107e73588 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
32 swift-frontend           0x00000001085958fc performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1748
33 swift-frontend           0x0000000108599454 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4568
34 swift-frontend           0x0000000107edf4c0 swift::mainEntry(int, char const**) + 4116
35 dyld                     0x0000000194f2ff28 start + 2236
LLVM Profile Error: Failed to write file "default.profraw": Operation not permitted

With swift-5.9-DEVELOPMENT-SNAPSHOT-2023-08-24-a toolchain

Report
Assertion failed: (conformance->isComplete()), function emitType, file SILGenType.cpp, line 1135.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: <redacted>
1.	Apple Swift version 5.9-dev (LLVM 46ac717ed0ec21c, Swift 05fe36c7b352712)
2.	Compiling with the current language version
3.	Contents of /Users/smon/Library/Developer/Xcode/DerivedData/<redacted>-djyismktrinblhakuopxifgxjqid/Build/Intermediates.noindex/WFApp.build/Debug-iphonesimulator/WFModels.build/Objects-normal/arm64/sources-1:
---
4.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/smon/Developer/<redacted>.swift")
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           0x0000000108f1eb08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000108f1deac llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000108f1f148 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001952b6a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000195287c28 pthread_kill + 288
5  libsystem_c.dylib        0x0000000195195ae8 abort + 180
6  libsystem_c.dylib        0x0000000195194e44 err + 0
7  swift-frontend           0x0000000109040fbc SILGenExtension::emitExtension(swift::ExtensionDecl*) (.cold.1) + 0
8  swift-frontend           0x000000010486e750 (anonymous namespace)::SILGenType::emitType() + 1128
9  swift-frontend           0x0000000104870fa8 swift::ASTVisitor<(anonymous namespace)::SILGenType, void, void, void, void, void, void>::visit(swift::Decl*) + 96
10 swift-frontend           0x000000010486e374 (anonymous namespace)::SILGenType::emitType() + 140
11 swift-frontend           0x000000010486e2dc swift::Lowering::SILGenModule::visitNominalTypeDecl(swift::NominalTypeDecl*) + 24
12 swift-frontend           0x0000000104793d74 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1004
13 swift-frontend           0x000000010486296c swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 148
14 swift-frontend           0x0000000104797bbc llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 396
15 swift-frontend           0x0000000104795038 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 100
16 swift-frontend           0x000000010426fd58 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 524
17 swift-frontend           0x000000010427ed2c withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
18 swift-frontend           0x0000000104271920 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2848
19 swift-frontend           0x00000001040f69a4 swift::mainEntry(int, char const**) + 3220
20 dyld                     0x0000000194f2ff28 start + 2236

For extra context, I have a macro that is both a member and extension macro.
it is supposed to work roughly like this allowing me to extract a open-api spec from my swift types.


protocol APIModel: Codable {
   public static func resolveSchema(in registry: Registry) -> JSONSchema 
}

@APIModel
struct MyType {
   let int: Int
   let string: String
}

// expands to

struct MyType {
   let int: Int
   let string: String

   // generated by member macro
   public init(from decoder: Decoder) throws { // decode }
   public func encode(to encoder: Encoder) throws { // decode }

   public static func resolveSchema(in registry: Registry) -> JSONSchema {
       // generate json-schema describing this type and store it in the registry
   }
}

 // generated by extension macro
extension MyType: APIModel {}

the macro is defined like this

@attached(extension, conformances: APIModel)
// @attached(conformance, conformances: APIModel)
@attached(
    member,
    names:
    named(init(from:)),
    named(encode(to:)),
    named(resolveSchema(in:))
)
public macro APIModel() = #externalMacro(
    module: "OpenAPIMacros",
    type: "APIModelMacro"
)

Hoping this helps resolve the issue or looking for tips how to resolve it.

Happy to provide extra info if needed via a private channel

This APIModel protocol---is it inheriting from Codable and/or other protocols? I would have expected this issue to be fixed in the August 24th snapshot build, but clearly it isn't.

Doug

Yes that’s exactly what it does.

The protocol is defined like this

protocol APIModel: Codable {
   public static func resolveSchema(in registry: Registry) -> JSONSchema 
}

I can still reproduce this with the latest toolchains

  • Xcode 15 Beta 8
  • swift-5.9-DEVELOPMENT-SNAPSHOT-2023-09-01-a
  • swift-5.9-DEVELOPMENT-SNAPSHOT-2023-09-03-a

Is there anything i can do to help move this along?

I would really love let this macro loose on our codebase :heart_eyes:

Simon