[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 14:20:05 CST 2016


Taking what you said in mind, I switched “let upperBits = lhs.value.lowerBits << (rhs.value.lowerBits - 64)” to "let upperBits = lhs.value.lowerBits << UInt64(rhs - 64)” and that compile error goes away. I wonder if Swift is just having an issue with dealing with the conversion when it involves a tuple.

Speaking of which, there is one other circumstance where a release build gives the same error, in my IntegerLiteralConvertible initializer!:

extension UInt128: IntegerLiteralConvertible {
    public init(integerLiteral value: IntegerLiteralType) {
        self.init()
        self.value.lowerBits = UInt64(value)
    }
    public init(_builtinIntegerLiteral value: _MaxBuiltinIntegerType) {
        self.init()
        // Gives “Shift amount is greater than or equal to type size in bits” error.
        self.value.lowerBits = UInt64(_builtinIntegerLiteral: value)
    }
}

I broke down my init(_bulterinIntegerLiteral:) function like so and it gave the same error:

extension UInt128: IntegerLiteralConvertible {
    public init(integerLiteral value: IntegerLiteralType) {
        self.init()
        self.value.lowerBits = UInt64(value)
    }
    public init(_builtinIntegerLiteral value: _MaxBuiltinIntegerType) {
        self.init()
        // Error happens on the below line.
        let temporaryHolder = UInt64(_builtinIntegerLiteral: value)
        self.value.lowerBits = temporaryHolder
    }
}

In this case, temporaryHolder seems to be resolving to a UInt64 type, so I have no clue why the compiler would have a problem with it.

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