Intermittent(!) error "lifetime-dependent variable escapes its scope"

In my project that uses the swift-binary-parsing package, I get three intermittent lifetime-related compile errors in one source file; “intermittent” in the sense that, if I build again (with no changes!) they go away for a while, then reappear a few hours later. As far as I can tell the errors are bogus.

Just upgraded to Swift 6.3 (Xcode 26.3 to 26.4) and it’s still happening, so I want to ask if there’s any known cause for this or whether I should file a bug report on swiftc.

The three errors are nearly identical, on different methods in the source file, and look like:

Container.swift:192:21: error: lifetime-dependent value escapes its scope
        return data.bytes.unsafeLoadUnaligned(fromByteOffset: Int(offset), as: UInt16.self).byteSwapped
                    ^
Container.swift:46:16: note: it depends on the lifetime of variable 'data'
    public let data: Data
               ^
Container.swift:192:27: note: this use of the lifetime-dependent value is out of scope
        return data.bytes.unsafeLoadUnaligned(fromByteOffset: Int(offset), as: UInt16.self).byteSwapped
                          ^

A highly abbreviated listing of the source:

import Foundation
import BinaryParsing

public final class Container : Sendable {
    public let data: Data      // line 46

    func getUInt16BE(_ offset: Offset) -> UInt16 {
        precondition(offset + 2 <= data.count)
        return data.bytes.unsafeLoadUnaligned(fromByteOffset: Int(offset), as: UInt16.self).byteSwapped    // line 192
    }

    ...
}

The target has .enableExperimentalFeature("Lifetimes").

Is this worth a compiler bug report?

Any other targets/schemes involved (including testing targets)? If so try to build them one by one.

Are you building for Intel or Apple Silicon?

I’m not having trouble building. As I said, I just hit Build again and the errors go away for a while. So this isn’t a real problem, but it’s been annoying me for two months.

Apple Silicon.

You might want to try Intel to see if it is nondeterministic on both architectures. Maybe also try compilers with and without assertions enabled to see if that makes it nondeterministic.

It could well be different in your case – I saw something very similar with Xcode showing errors, then hiding them, while the build itself was marked "successful", then showing errors again, often times without listing them in the error list – in my case that was due to Xcode "asynchronously" building other targets / configurations seemingly unrelated to what I was currently building or just built.

1 Like

It’s not Xcode-related – I’ve now seen the exact same thing happen when using swift build. One build lists the above errors and fails; then I re-run the command and don’t get the errors.