求助:moon check提示字面量0x8000_0000越界

版本:

$ moon version
moon 0.1.0 (a13731f 2023-10-08)
moonc 66309f47a /home/foo/.moon/bin/moonc

问题:

$ moon check
failed: moonc check /home/foo/tmp/hashtable/lib/rhtable/rhtable.mbt -o /home/foo/tmp/hashtable/target/check/lib/rhtable/rhtable.mi -pkg RHtable/lib/rhtable -pkg-sources RHtable/lib/rhtable:/home/foo/tmp/hashtable/lib/rhtable
......
/home/foo/tmp/hashtable/lib/rhtable/rhtable.mbt:42:20-42:31 Integer literal 0x8000_0000 is out of range.

有字面量范围检查固然不错,可是0x8000_0000应该是int32的最小值,并没有越界吧…

代码中使用此字面量的地方

func abs(self:Int) -> Int {
  let min_of_i32 = 0x8000_0000
  assert(self != min_of_i32, "abs() : 参数不应该为i32类型最小值")
  if(self < 0){
    0 - self
  } else {
    self
  }
}

0x8000_0000 实际上已经超出了 Int 的范围,具体可以参考下面这段Rust代码的报错提示 Rust Playground

fn main (){
  let a: i32 = 0x8000_0000;
  let _ = a;
}
   Compiling playground v0.0.1 (/playground)
error: literal out of range for `i32`
 --> src/main.rs:2:16
  |
2 |   let a: i32 = 0x8000_0000;
  |                ^^^^^^^^^^^
  |
  = note: the literal `0x8000_0000` (decimal `2147483648`) does not fit into the type `i32` and will become `-2147483648i32`
  = help: consider using the type `u32` instead
  = note: `#[deny(overflowing_literals)]` on by default
help: to use as a negative number (decimal `-2147483648`), consider using the type `u32` for the literal and cast it to `i32`
  |
2 |   let a: i32 = 0x8000_0000u32 as i32;
  |                ~~~~~~~~~~~~~~~~~~~~~

error: could not compile `playground` (bin "playground") due to previous error
1 个赞

惊了,竟然已经超过了吗,谢谢指导

C时代的坏习惯.jpg【至少在javascript里面虽然位运算都把数字处理成(u)int32,用十六进制写0x80000000也是正整数,代表的是double范围内的最接近的。
没写负号却表示了负数,这合理么【

更新:

考虑到短期内MoonBit的内置类型不会添加无符号整数,现在把这个检查去掉了 :rofl:,不然使用起来比较麻烦

1 个赞

啊,什么当场倒车【
真心的,别吧……-0x80000000又不是什么冒天下之大不韪的禁忌……