theheraldproject/herald-for-ios

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

codeanticode opened this issue · 2 comments

Describe the bug

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

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
    }

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

@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: #174