Lately I've run into a few cases where the compiler gives a completely nonsense diagnostic for a compile error. I don't remember any of the previous ones, but I just ran into one that's especially strange and I thought someone working on generics or something might find it useful. It may be related to a previous post of mine about another odd diagnostic (although that one was happening for code that should compile perfectly fine as far as I could see).
Here's the diagnostic:
CompressionUtil.swift:45:15: error: type '(SIMD2<Double>) -> Double' cannot conform to 'BinaryInteger'
if length != decompressedLength {
^
The line that that's occurring on is in a function that has nothing to do with protocol conformances or simd types. I do use simd and some generics related to BinaryInteger in other unrelated files but they're compiling perfectly fine.
The actual issue with that line of code is just that there is no such variable called length
(I accidentally renamed the wrong thing). Here's the function that this is occurring in:
static func decompress(_ bytes: [UInt8], decompressedLength: Int) throws -> [UInt8] {
let compressedData = Data(bytes: bytes, count: bytes.count)
// Decompression only works if the first two bytes are removed no idea why, it's probably
// something to do with headers or magic numbers
var compressedBytes = [UInt8](compressedData[2...])
var decompressedLength = UInt(decompressedLength)
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: decompressedLength)
let err = zlib.uncompress(
buffer,
&decompressedLength,
&compressedBytes,
UInt(compressedBytes.count)
)
if err != Z_OK {
throw CompressionError.decompressionFailed(error: Int(err))
}
if length != decompressedLength { // This is the line that has the weird diagnostic
log.warning("actual decompressed length does not match length in packet")
}
let data = Data(bytes: buffer, count: length)
buffer.deallocate()
return [UInt8](data)
}
I'm happy to provide more context if it would be useful in resolving whatever is causing this clearly incorrect diagnostic.