Crash in `outlined init with copy of` when run in Release Mode

Hey there,

When I run the following 50 lines of code in release mode, or turn Optimization on in Build-Settings Swift Compiler - Code Generation I will get the following crash.

Anyone any idea why that happens? (Xcode 13.4.1, happens on Device as well as simulator on iOS 15.5 and 15.6)

Example Project: https://github.com/Bersaelor/ResourceCrashMinimalDemo

#0	0x000000010265dd58 in assignWithCopy for Resource ()
#1	0x000000010265d73c in outlined init with copy of Resource<VoidPayload, String> ()
#2	0x000000010265d5dc in specialized Resource<>.init(url:method:query:authToken:headers:) [inlined] at /Users/konradfeiler/Source/ResourceCrashMinimalDemo/ResourceCrashMinimalDemo/ContentView.swift:51
#3	0x000000010265d584 in specialized ContentView.crash() at /Users/konradfeiler/Source/ResourceCrashMinimalDemo/ResourceCrashMinimalDemo/ContentView.swift:18

Code needed:

import SwiftUI



struct ContentView: View {

    var body: some View {
        Button(action: { crash() }, label: { Text("Create Resouce") })
    }

    /// crashes in `outlined init with copy of Resource<VoidPayload, String>`
    func crash() {
        let testURL = URL(string: "https://www.google.com")!
        let r = Resource<VoidPayload, String>(url: testURL, method: .get, authToken: nil)
        print("r: \(r)")
    }
}

struct VoidPayload {}

enum HTTPMethod<Payload> {
    case get
    case post(Payload)
    case patch(Payload)
}

struct Resource<Payload, Response> {
    let url: URL
    let method: HTTPMethod<Payload>
    let query: [(String, String)]
    let authToken: String?
    let parse: (Data) throws -> Response
}

extension Resource where Response: Decodable {

    init(
        url: URL,
        method: HTTPMethod<Payload>,
        query: [(String, String)] = [],
        authToken: String?,
        headers: [String: String] = [:]
    ) {

        self.url = url
        self.method = method
        self.query = query
        self.authToken = authToken
        self.parse = {
            return try JSONDecoder().decode(Response.self, from: $0)
        }
    }
}
Post not yet marked as solved Up vote post of Bersaelor Down vote post of Bersaelor
2.6k views

Replies

PS: If we remove one of the fields from Resource , say delete the authToken field, the method will no longer crash. I believe that would reduce the size of the struct Resource to where the init method would no longer be outlined

PS: If I change

enum HTTPMethod<Payload> {
    case get
    case post(Payload)
    case patch(Payload)
}

to

enum HTTPMethod<Payload> {
    case get(Payload)
    case post(Payload)
    case patch(Payload)
}

the problem also goes away. Filed a bug report using the Feedback Assistent, there shouldn't be reason why the .get case has an associated value.