swift: "Stored value type does not match pointer operand type!" compiler crash

Describe the bug The following function crashes the compiler:

func setEnumOverrideIfExists<T: RawRepresentable>(for key: String, in override: [String: Any], target: inout T) where T.RawValue == UInt {
    if let overrideValue = override[key] as? UInt, let overrideValue = T(rawValue: overrideValue) {
        target = overrideValue
    }
}

If, however, I make the following change, there is no crash:

func setEnumOverrideIfExists<T: RawRepresentable>(for key: String, in override: [String: Any], target: inout T) where T.RawValue == UInt {
    if let overrideValue = override[key] as? UInt, let overrideValueEnum = T(rawValue: overrideValue) {
        target = overrideValueEnum
    }
}

To Reproduce Steps to reproduce the behavior:

  1. Add the function above in a Swift file
  2. Compile

Expected behavior Compiler should not crash.

Environment (please complete the following information):

  • OS: macOS 12.5 (21G5046c)
  • Xcode Version/Tag/Branch: Xcode 13.2

Additional context

Stored value type does not match pointer operand type!
  store i64 %95, i8** %9, align 8, !dbg !393
 i8*in function $s6Panels22PrototypeOverrideModelV07setEnumC8IfExists3for2in0E0ySS_SDySSypGxztSYRzSu8RawValueRtzlF
<unknown>:0: error: fatal error encountered during compilation; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
<unknown>:0: note: Broken function found, compilation aborted!
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c /Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Panels/Plugins/Sync/Prototyping/PrototypeOverridesEditors.swift -primary-file /Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Panels/Plugins/Sync/Prototyping/Models/PrototypeOverrideModel.swift /Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Panels/Plugins/Sync/Prototyping/SwiftUIUtils.swift -emit-module-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel~partial.swiftmodule -emit-module-doc-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel~partial.swiftdoc -emit-module-source-info-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel~partial.swiftsourceinfo -emit-dependencies-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel.d -emit-reference-dependencies-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel.swiftdeps -serialize-diagnostics-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel.dia -target x86_64-apple-macos11.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -I /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Products/Debug -F /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Products/Debug -enable-testing -g -import-underlying-module -module-cache-path /Users/lnatan/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -serialize-debugging-options -Xcc -working-directory -Xcc /Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Panels-generated-files.hmap -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Panels-own-target-headers.hmap -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Panels-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Panels-project-headers.hmap -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Products/Debug/include -Xcc -I/Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Pods/Headers/Public -Xcc -I/Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Pods/Headers/Public/CocoaLumberjack -Xcc -I/Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Pods/Headers/Public/RNCryptor-objc -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/DerivedSources-normal/x86_64 -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/DerivedSources/x86_64 -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DCURRENT_USER=@\"lnatan\" -Xcc -DCOCOAPODS=1 -Xcc -ivfsoverlay -Xcc /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/unextended-module-overlay.yaml -module-name Panels -target-sdk-version 12.1.0 -o /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel.o -index-store-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Index/DataStore -index-system-modules
1.	Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)
2.	
3.	Running pass 'Module Verifier' on function '@"$s6Panels22PrototypeOverrideModelV07setEnumC8IfExists3for2in0E0ySS_SDySSypGxztSYRzSu8RawValueRtzlF"'
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           0x000000010f1d7c27 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x000000010f1d6bb8 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x000000010f1d8236 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007ff80790adfd _sigtramp + 29
4  libsystem_platform.dylib 0x0000000000000003 _sigtramp + 18446603370454012451
5  libsystem_c.dylib        0x00007ff807840d24 abort + 123
6  swift-frontend           0x000000010a2e9502 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_2::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 978
7  swift-frontend           0x000000010f12169e llvm::report_fatal_error(llvm::Twine const&, bool) + 286
8  swift-frontend           0x000000010f12157b llvm::report_fatal_error(char const*, bool) + 43
9  swift-frontend           0x000000010f0b815f (anonymous namespace)::VerifierLegacyPass::runOnFunction(llvm::Function&) + 111
10 swift-frontend           0x000000010f05236a llvm::FPPassManager::runOnFunction(llvm::Function&) + 1354
11 swift-frontend           0x000000010f051651 llvm::legacy::FunctionPassManagerImpl::run(llvm::Function&) + 113
12 swift-frontend           0x000000010f058b75 llvm::legacy::FunctionPassManager::run(llvm::Function&) + 341
13 swift-frontend           0x000000010a7deb61 swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*) + 1585
14 swift-frontend           0x000000010a7df9c7 swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, swift::UnifiedStatsReporter*) + 2055
15 swift-frontend           0x000000010a2f4a9d 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*) + 3581
16 swift-frontend           0x000000010a2e6346 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 13830
17 swift-frontend           0x000000010a226b08 main + 1032
18 dyld                     0x000000011c3fa52e start + 462
error: Abort trap: 6 (in target 'Panels' from project 'Panels')

About this issue

Commits related to this issue

Most upvoted comments

@max-christian Here’s how I described figuring out which file was causing the issue in a private thread, in case you want to try a similar approach:

Perhaps an unconventional approach, but I looked inside Intermediates.noindex/xxx.build/Debug/xxx.build/Objects-normal/arm64, sorted by size, and looked into the source files corresponding to the object files with zero bytes in length, assuming those were the files that likely triggered the crash. Commented out as much as possible from the affected files until the target built successfully. I’ve narrowed it down to a single file, will slowly reintroduce the code in that file until I can find which code exactly is causing the compiler to crash

@osrufung Please provide a reproducer. I could not get the original example to crash with a recent revision, and I would not expect it to in Xcode 15 beta either since the regression test had already been added by the time the 5.9 branch was cut.

Here you can find a Swift 5.9 Package that reproduce the issue in Xcode 15

MyLibrary.zip

@AnthonyLatsis I happened to run into what I believe to be this same issue in one of my projects when building with Xcode 15 beta 1, and I managed to narrow it down and create a sample project that reproduces it.

Let me know if I should file mine separately, in case it’s not directly related to this one.

Here’s the reproduction case:

import SwiftUI

public enum EnumWithAssociatedValues: Hashable, Codable {

    public enum NestedEnum: Int, Codable {
        case success
        case failure
    }

    case caseWithoutValues
    case caseWithNestedOptionalEnum(NestedEnum?)
    /// If this enum case is removed, then the `let result = result` no longer causes a compiler crash.
    case caseWithTwoValues(id: String, value: Double)
}

struct ContentView: View {
    @State private var enumValue: EnumWithAssociatedValues?

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
        .onChange(of: enumValue) { newValue in
            /// Note: the compiler crash occurs even if the call is not made within a SwiftUI view modifier block.
            performAction(for: newValue)
        }
    }

    private func performAction(for newValue: EnumWithAssociatedValues?) {
        guard let ring = newValue else { return }

        /// The line below crashes the Swift compiler in Xcode 15 if using `let result = result` and the enum includes the `caseWithTwoValues` case.
        guard case .caseWithNestedOptionalEnum(let result) = ring, let result = result else { return }

        switch result {
        case .success: print("success")
        case .failure: print("failure")
        }
    }
}

Here’s the stack dump:

Stored value type does not match pointer operand type!
  store i1 %44, i8* %12, align 8, !dbg !750
 i1Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
1.	Apple Swift version 5.9 (swiftlang-5.9.0.114.6 clang-1500.0.27.1)
2.	Compiling with the current language version
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           0x0000000109eef91c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109eeeae0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109eeff1c SignalHandler(int) + 352
3  libsystem_platform.dylib 0x00000001a18a6a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a1877c28 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a1785ae8 abort + 180
6  swift-frontend           0x0000000104928b14 PrettyStackTraceFrontend::~PrettyStackTraceFrontend() + 0
7  swift-frontend           0x0000000109e3c6fc llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x0000000109e3c5e4 llvm::report_fatal_error(llvm::Twine const&, bool) + 0
9  swift-frontend           0x0000000104e95670 llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::printPipeline(llvm::raw_ostream&, llvm::function_ref<llvm::StringRef (llvm::StringRef)>) + 0
10 swift-frontend           0x0000000109bf7064 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) + 224
11 swift-frontend           0x0000000104e84114 swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*, llvm::raw_pwrite_stream*) + 4252
12 swift-frontend           0x0000000104e85b20 swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, swift::UnifiedStatsReporter*) + 2736
13 swift-frontend           0x00000001049239dc generateCode(swift::CompilerInstance&, llvm::StringRef, llvm::Module*, llvm::GlobalVariable*) + 252
14 swift-frontend           0x000000010491e108 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*) + 1708
15 swift-frontend           0x0000000104920d48 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1748
16 swift-frontend           0x000000010491f1e0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4216
17 swift-frontend           0x00000001048e4a58 swift::mainEntry(int, char const**) + 4112
18 dyld                     0x00000001a151ff28 start + 2236

Here’s an Xcode project with the reproduction case: Swift-59572.zip

@max-christian Here’s how I described figuring out which file was causing the issue in a private thread, in case you want to try a similar approach:

Perhaps an unconventional approach, but I looked inside Intermediates.noindex/xxx.build/Debug/xxx.build/Objects-normal/arm64, sorted by size, and looked into the source files corresponding to the object files with zero bytes in length, assuming those were the files that likely triggered the crash. Commented out as much as possible from the affected files until the target built successfully. I’ve narrowed it down to a single file, will slowly reintroduce the code in that file until I can find which code exactly is causing the compiler to crash

Thank you so much, I would have never discovered my issue without doing this! It helped me to discover that my code

var networkError: some Error {
   NSError(...)
}

was not triggering any compiler errors/warnings, but it was crashing the compiler. All I had to do was change it to any Error.

Huh, what an elusive bug. Have you tried an older snapshot? One other option that might catch the broken invariant before stuff is handed over to LLVM is -sil-verify-all frontend flag.

Perhaps not elusive but just a bug that’s been fixed between Xcode 15.0 beta 2 and the snapshot I used? If there’s a possibility it’s a bug that needs attention I can open a separate issue, as the line of code that crashed the compiler isn’t an exact match for this issue:

 ARScanViewController.queueModelUpdateJob { [n = liveSymbol?.node] in
                n?.removeFromParentNode()
  }

Thanks to @insidegui for the zero byte object file trick; that successfully narrowed it down to one source file.