strange bug

95 views
Skip to first unread message

robert engels

unread,
Apr 28, 2019, 10:33:07 AM4/28/19
to golang-nuts
I accepted a PR for my fixed project. It was done by an expectedly experienced developer and it includes a test case.

I find it very hard to believe that the author didn’t run and verify the test case, so that leaves me to believe that something in Go has changed, since the code won’t compile.

The offending code is in decomposer.go

var c uint64
for i, v := range coefficient {
if i < 8 {
c |= uint64(v) << (i * 8)
} else if v != 0 {
return fmt.Errorf("coefficent too large")
}
}

and it fails to compile with:

./decomposer.go:62:19: invalid operation: uint64(v) << (i * 8) (shift count type int, must be unsigned integer)

and changing the code to 

c |= uint64(v) << (uint(i) * 8)

causes it to work. coefficient is defined as

coefficient []byte

So, the question is: why ‘i’ isn’t treated as unsigned, since it is a range index - won't it always be positive?

andrey mirtchovski

unread,
Apr 28, 2019, 11:04:30 AM4/28/19
to robert engels, golang-nuts
> So, the question is: why ‘i’ isn’t treated as unsigned, since it is a range index - won't it always be positive?

The author of the PR was most likely working on Go's tip (what will
become 1.13), where the requirement that the right operator in a shift
is an unsigned integer has been lifted.

compare the third paragraph between these versions:

https://tip.golang.org/ref/spec#Operators
https://golang.org/ref/spec#Operators

robert engels

unread,
Apr 28, 2019, 11:05:58 AM4/28/19
to andrey mirtchovski, golang-nuts
Yes, I heard back from the author that he was using tip. Thanks for the help.
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

robert engels

unread,
Apr 28, 2019, 11:10:19 AM4/28/19
to andrey mirtchovski, golang-nuts
But still, it would seem the range index should be unsigned - what would be the purpose of it being signed? Similarly, the slice indexing should be unsigned as well. Just thinking about it...

> On Apr 28, 2019, at 10:03 AM, andrey mirtchovski <mirtc...@gmail.com> wrote:
>

andrey mirtchovski

unread,
Apr 28, 2019, 11:22:16 AM4/28/19
to robert engels, golang-nuts
> But still, it would seem the range index should be unsigned - what would be the purpose of it being signed? Similarly, the slice indexing should be unsigned as well. Just thinking about it...

not the first time this has come up. here are a couple of references:

https://groups.google.com/d/msg/golang-nuts/QqTCTFwspcM/HLBxhqJKuMQJ
https://groups.google.com/forum/#!msg/golang-nuts/jJWAAMdquwQ/jhWhxJJbzVYJ

robert engels

unread,
Apr 28, 2019, 12:55:37 PM4/28/19
to andrey mirtchovski, golang-nuts
Thanks. Interesting that in all my time with Go I didn’t really think about it.
Reply all
Reply to author
Forward
0 new messages