HaxeFoundation / HaxeFoundation/haxe

[js] Analyzer fails to optimize constant conditional inside abstract when StringBuf is used

Open
#12,466 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

The analyzer optimizer fails to fold a constant conditional expression inside an abstract type when `StringBuf` is used in the same function. The condition `this == 0` where `this` is known to be `0` at compile-time should be evaluated to `true`, but instead generates a runtime conditional `0 == 0 ? 65 : 66`.

Removing the new `StringBuf().addChar(10);` line causes the analyzer to correctly optimize the conditional.

### Minimal Reproduction
```haxe
abstract W(Int) from Int {
public inline function test() {
trace(this == 0 ? 65 : 66);
new StringBuf().addChar(10);
}
}

class Test {
static function main() {
final w:W = 0;
w.test();
}
}
```
### Actual Output (JS)
```js
class Test {
static main() {
console.log("Test.hx:3:", 0 == 0 ? 65 : 66); // Not optimized!
let _this_b = "";
_this_b += String.fromCodePoint(10);
}
}
```

### Expected Output (JS)
```js
class Test {
static main() {
console.log("Test.hx:3:", 65); // Should be constant-folded
let _this_b = "";
_this_b += String.fromCodePoint(10);
}
}
```

### Notes
- Removing `new StringBuf().addChar(10);` causes the analyzer to correctly optimize to `65`
- The same code outside an abstract is optimized correctly
- The value of w is known at compile-time `(w:W = 0`)

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.