I thought I was getting pretty savvy with ~Escapable types, but today I’ve run into a compiler error that doesn’t make sense and I cannot find a workaround for.
import SQLite3
struct SQLBlob: ~Escapable {
@_lifetime(borrow val)
init(_ val: SQLFnValue) {
self.bytes = RawSpan(_unsafeStart: sqlite3_value_blob(val._val),
byteCount: Int(sqlite3_value_bytes(val._val)))
}
let bytes: RawSpan
var data: Data {Data(rawSpan: bytes)}
}
struct SQLFnValue {
fileprivate let _val: OpaquePointer?
// (rest of implementation omitted)
}
In Swift 6.3 (Xcode 26.4) this invariably barfs compiling the init method:
SQLiteFns.swift:240:5: error: lifetime-dependent variable 'self' escapes its scope
init(_ val: SQLFnValue) {
^
SQLiteFns.swift:240:5: note: error in compiler-generated 'init'
init(_ val: SQLFnValue) {
^
SQLiteFns.swift:241:44: note: it depends on the lifetime of this parent value
self.bytes = RawSpan(_unsafeStart: sqlite3_value_blob(val._val),
^
SQLiteFns.swift:243:5: note: this use causes the lifetime-dependent value to escape
}
- How can
selfescape its scope in a struct declaration that has no consuming methods? - What’s the “compiler-generated ‘init’” it’s talking about? The hints point to an explicit init; why would there be an implicit one?
- How does anything depend on the lifetime of the result of
sqlite3_value_blob()? That’s an external C function that returns anUnsafeRawPointer. Unsafe pointers don’t have a lifetime, and<sqlite3.h>has no Swift-specific annotations.