[swift-users] What are the various causes of "shift amount is greater than or equal to type size in bits"

Jens Alfke jens at mooseyard.com
Sat Feb 13 13:47:43 CST 2016


> On Feb 13, 2016, at 11:11 AM, Joel Gerber via swift-users <swift-users at swift.org> wrote:
> 
>    switch rhs {
…
>    case 65...127:
>        // This causes a "shift amount is greater than or equal to type size in bits" error
>        // on release build but not debug build.
>        let upperBits = lhs.value.lowerBits << (rhs.value.lowerBits - 64)

I suspect the problem is that you’re switching on `rhs` instead of `rhs.value.lowerBits`. You haven’t shown the conversion method that lets your struct be compared with an integer literal, but I’m guessing that the compiler isn’t smart enough to deduce that when rhs<127, then rhs.value.lowerBits must also be <127.

As for why this only happens in release builds, it’s probably because only release builds perform the control-flow analysis that’s necessary for detecting these types of errors when the RHS of `<<` isn’t a constant.

—Jens


More information about the swift-users mailing list