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

Joel Gerber joel at grrbrr.ca
Sat Feb 13 13:53:42 CST 2016


This whole library is at https://github.com/Jitsusama/UInt128. I tried simplifying the library down to the bare necessities and I couldn’t replicate the issue, so I’m guessing that it does have to do with integer literals somehow now that you mention it.

Here is my integerLiteralConvertible implementation:

extension UInt128: IntegerLiteralConvertible {
    public init(integerLiteral value: IntegerLiteralType) {
        self.init()
        self.value.lowerBits = UInt64(value)
    }
    public init(_builtinIntegerLiteral value: _MaxBuiltinIntegerType) {
        self.init()
        self.value.lowerBits = UInt64(_builtinIntegerLiteral: value)
    }
}

Joel Gerber
joel at grrbrr.ca



> On Feb 13, 2016, at 2:47 PM, Jens Alfke <jens at mooseyard.com> wrote:
> 
> 
>> 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