closure-compiler: Numbers 2^31 < x < 2^31-1 should not raise BITWISE_OPERAND_OUT_OF_RANGE

Take this input:

function toUint32(x) {
  if (x < 0) {
    x += 0x100000000;  // 2^32
  }
  return x;
}

console.log(toUint32(~~0xe0000000).toString(16));

Closure Compiler returns the error:

hello.js:12: WARNING - Operand out of range, bitwise operation will lose information: NUMBER 3.758096384E9 12 [source_file: hello.js]
console.log(toUint32(~~0xe0000000).toString(16));

And yet the output of this code is e0000000, which illustrates that no information is in fact lost.

I think this warning should only fire for numbers > 2^32 - 1, not > 2^31 - 1.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 27 (25 by maintainers)

Commits related to this issue

Most upvoted comments

I would say that the unsettled feeling was completely appropriate. And a comment like so would have made it go away:

/**
 * @suppress {whateverClassThisIs} the toUInt32 call here protects
 *    us from this unsafe manipulation
 */

I actually prefer less configuration if possible. So, in this case, I’d rather check for the higher bound and miss warning in some cases.