HaxeFoundation / HaxeFoundation/haxe

improve uninitialized var tracking?

Open
#8,054 1 comment 1 reaction 0 assignees View on GitHub
priority-netherlands
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

```haxe
class Test {
static function main() {
final foo = Math.random() > 0.5;
var bar;
if(foo) bar = 'bar';
for(i in 0...5) {
if(foo) trace(bar.length);
}
}
}
```
This causes `Test.hx:6: characters 16-19 : Local variable bar used without being initialized`. Could compiler figure out it's actually initialized in cases like this?
Alternatively, would some kind of metadata override make sense?

My use case is initializing the variable using inlined constructor, so initializing to null breaks the inline.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Test.hx example and its uninitialized-variable diagnostic. Investigate the compiler's tracking of bar across the if statement and the implications of the proposed metadata override, including the inline-constructor use case. Done should mean the intended initialization is accepted without requiring a null initializer, with the behavior covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.