theheraldproject / herald-for-ios

Herald for iOS - Reliable mobile Bluetooth communications - iOS library & test app
https://heraldprox.io
Apache License 2.0
24 stars 24 forks source link

Compiler in Xcode cannot type-check long bit-shifting expression #171

Closed codeanticode closed 2 years ago

codeanticode commented 2 years ago

Describe the bug

Latest version of the XCode compiler (tested on XCode Version 13.3 (13E113)) give an error on this line:

https://github.com/theheraldproject/herald-for-ios/blob/f227f4d5c8281168f65f6e86df7e41bb89a047b1/Herald/Herald/Sensor/Extensions/DataExtensions.swift#L239

saying that the line cannot be type-checked because the expression is too long… See the screenshot from XCode highlighting the error message:

image

A simple solution seems to break up the bit-shifting operation into two separate operations, as suggested by the compiler:

    func uint64(_ index: Int) -> UInt64? {
        let bytes = [UInt8](self)
        guard index < (bytes.count - 7) else {
            return nil
        }

        let a = UInt64(bytes[index]) |
            UInt64(bytes[index + 1]) << 8 |
            UInt64(bytes[index + 2]) << 16 |
            UInt64(bytes[index + 3]) << 24 |
            UInt64(bytes[index + 4]) << 32

        let b = UInt64(bytes[index + 5]) << 40 |
            UInt64(bytes[index + 6]) << 48 |
            UInt64(bytes[index + 7]) << 56

        return a | b
    }
adamfowleruk commented 2 years ago

How very bizarre. Thanks for the bug report. I'll verify on my Mac soon.

codeanticode commented 2 years ago

@adamfowleruk Although it seems rare, people have reported it (for other complex instructions, not bitwise operations):

https://stackoverflow.com/questions/52382645/the-compiler-is-unable-to-type-check-this-expression-swift-4

and there is some more info about it here:

https://www.hackingwithswift.com/example-code/language/how-to-fix-the-error-expression-was-too-complex-to-be-solved-in-reasonable-time

I created a PR with the fix: https://github.com/theheraldproject/herald-for-ios/pull/174/