luau
luau copied to clipboard
Analysis can't tell an uninitialized variable may not be initialized
The following code passes luau-analyze, because it thinks that foo
is a string
, even though foo
never gets initialized when ran, and causes an error trying to concatenate with nil
local foo
if false then
foo = "World!"
end
print("Hello " .. foo)
The equivalent code in TypeScript or Rust's analysis does not pass:
Two notes:
- We currently don't track conditions of known value during analysis for the purpose of uninitialized variable detection or other uses. So
if false
above behaves as if it wasif math.random() < 0.5
. I'm not sure this is useful to change? - We have attempted to implement a rigorous detection of possibly-uninitialized values (unlike the current analysis that detects definitely-uninitialized values). Unfortunately, this found no bugs in real-world code based on our corpus, and produced a fair number of false positives associated with complex logic. Thus we ended up not shipping this, as our threshold for new analysis passes is such that the true reports must outweigh false positives significantly to be included.
I'm not sure we should act on this given the above.
The equivalent TypeScript example to the type of code I was referring to n pt2 is this:
let foo, bar;
if (Math.random() < 0.5) {
foo = "bar";
bar = true;
}
if (bar) {
console.log(foo.length);
}
which produces a false positive. This is a simpler example and we had some that have more involved logic in real-world code.