HaxeFoundation / HaxeFoundation/haxe

[cpp] wierd addition results upon int overflow

Open
#8,879 6 comments 0 reactions 0 assignees View on GitHub
platform-cpp
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.