HaxeFoundation / HaxeFoundation/haxe
[cpp] wierd addition results upon int overflow
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
```haxe
class Main {
static var tmp:Any;
public static function main():Void {
tmp = new Int32(-2147483648) + new Int32(-1);
}
}
abstract Int32(Int) {
public inline function new(v:Int) {
this = v;
}
public inline function toInt():Int {
return this;
}
@:op(A + B) inline function addition(b:Int32):Int32 {
var result = this + b.toInt();
trace(result); // <----------- 2147483647
trace(result >= 0); // <----------- false
if((this < 0 && b.toInt() < 0 && result >= 0) || (this > 0 && b.toInt() > 0 && result <= 0)) {
throw '($this + ${b.toInt()}) overflows Int32';
}
return new Int32(result);
}
}
```
```
$ haxe -main Main -dce full -cpp bin/cpp -D analyzer-optimize && ./bin/cpp/Main
Main.hx:19: 2147483647
Main.hx:20: false
```
Making any one of the following changes fixes it:
1. Remove `inline` accessor from `addition` function;
2. Remove `if` expression;
3. Disable `analyzer-optimize`.
Contributor guide
Assessment
This issue has not been assessed yet.