Lyricdev
GitHub

LYR-SEM0018

a variable read before it is surely assigned

error ยท spec 07-statements

A local declared without an initializer is read on a path where no assignment has reached it.

Explanation

A local may be declared without a value (var x: int;) and assigned later, but every read has to come after an assignment on every path that leads to it. An assignment in only one branch of an if, or inside a loop that may not run, does not count.

Assign the variable on every path before the read, or give it an initializer.

Wrong

fn main(): void {
    let flag = true;
    var n: int;
    if (flag) { n = 1; }
    let _ = n;
}
fn main(): void {
    let flag = true;
    var n: int;
    if (flag) { n = 1; } else { n = 2; }
    let _ = n;
}