-
Notifications
You must be signed in to change notification settings - Fork 300
Open
Description
Problem
Swift 6.2 introduced a new Data.bytes property that returns RawSpan (via SE-0456). This collides with UniFFI's generated Swift code which uses bytes as a parameter name.
Generated code that causes the issue
fileprivate extension RustBuffer {
// Allocate a new buffer, copying the contents of a `UInt8` array.
init(bytes: [UInt8]) {
let rbuf = bytes.withUnsafeBufferPointer { ptr in
RustBuffer.from(ptr)
}
self.init(capacity: rbuf.capacity, len: rbuf.len, data: rbuf.data)
}
// ...
}Compilation errors
When building with Xcode 26.x / Swift 6.2.x:
Cannot convert value of type 'RawSpan' to expected argument type 'UnsafePointer<UInt8>'Ambiguous use of 'bytes'
The Swift compiler gets confused between:
- The local parameter
bytes: [UInt8] - The new
Data.bytesproperty returningRawSpan
Suggested Fix
Rename the bytes parameter to something that won't collide, like byteArray:
fileprivate extension RustBuffer {
init(byteArray: [UInt8]) {
let rbuf = byteArray.withUnsafeBufferPointer { ptr in
RustBuffer.from(ptr)
}
self.init(capacity: rbuf.capacity, len: rbuf.len, data: rbuf.data)
}
}And update the call site:
// From:
return RustBuffer(bytes: writer)
// To:
return RustBuffer(byteArray: writer)Workaround
We've implemented a post-processing sed script that patches the generated Swift code:
sed -i '' 's/init(bytes: \[UInt8\])/init(byteArray: [UInt8])/g' "$swift_file"
sed -i '' 's/let rbuf = bytes\.withUnsafeBufferPointer/let rbuf = byteArray.withUnsafeBufferPointer/g' "$swift_file"
sed -i '' 's/RustBuffer(bytes: writer)/RustBuffer(byteArray: writer)/g' "$swift_file"This is backwards compatible and works on both Swift 5.x and Swift 6.x.
Environment
- UniFFI version: 0.31.0
- Swift version: 6.2.3 (Xcode 26.2)
- Platform: iOS/macOS
Related
- Swift Evolution SE-0456: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0456-stdlib-span-properties.md
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels