HaxeFoundation / HaxeFoundation/haxe
Definite assignment analysis for if-cases and while-loops fail to conclude that value is initialized before usage
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
The compiler is missing or is not precise enough in its definite assignment analysis of variables that are definitely assigned before a point of usage of the variable. This leads to the compiler giving an error for programs that are otherwise correct.
In both below failing cases, there is no way of the program to reach the `trace()`-call without the variable `value` being initialized beforehand:
```haxe
class Test {
static function main() {
var value: Int;
if (true) {
value = 3;
}
trace(value); // ERROR: Local variable value used without being initialized
}
}
```
```haxe
class Test {
static function main() {
var value: Int;
while (true) {
var random = Math.round(Math.random() * 10);
if (random != 3 {
value = random;
break;
}
}
trace(value); // ERROR: Local variable value used without being initialized
}
}
```
This is something that is handled in Clang [(link)](https://godbolt.org/z/s66aGao34), but not in Rust [(link)](https://play.rust-lang.org/?version=nightly&mode=release&edition=2024&gist=7f80d89e3c6ee9817b3f161b82276a51).
Contributor guide
Assessment
This issue has not been assessed yet.