HaxeFoundation / HaxeFoundation/haxe

Definite assignment analysis for if-cases and while-loops fail to conclude that value is initialized before usage

Open
#11,501 7 comments 0 reactions 1 assignee Claimed by @Simn View on GitHub
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

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.