mirror of
https://github.com/imjasonh/nescript
synced 2026-07-19 07:05:58 +00:00
Analyzer: E0502 on assignment to undefined variable
Previously an assignment to an undeclared name was silently accepted because the IR lowering would synthesize a fresh VarId for it. Now the analyzer emits E0502 with the same "did you mean" suggestion path as read-side undefined variable errors. https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
parent
1778ae0ec8
commit
59970a4a45
2 changed files with 24 additions and 0 deletions
|
|
@ -797,6 +797,11 @@ impl Analyzer {
|
||||||
*span,
|
*span,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Assigning to an undeclared name is an
|
||||||
|
// error — the lowering would otherwise
|
||||||
|
// silently synthesize a VarId for it.
|
||||||
|
self.emit_undefined_var(name, *span);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LValue::ArrayIndex(name, idx) => {
|
LValue::ArrayIndex(name, idx) => {
|
||||||
|
|
@ -808,6 +813,8 @@ impl Analyzer {
|
||||||
*span,
|
*span,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
self.emit_undefined_var(name, *span);
|
||||||
}
|
}
|
||||||
// Indexing an array counts as a read of the array,
|
// Indexing an array counts as a read of the array,
|
||||||
// and the index expression itself may contain reads.
|
// and the index expression itself may contain reads.
|
||||||
|
|
|
||||||
|
|
@ -360,6 +360,23 @@ fn analyze_unknown_struct_type_errors() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn analyze_assign_to_undefined_var_errors() {
|
||||||
|
// Assigning to an undeclared variable must produce E0502
|
||||||
|
// rather than silently creating the variable.
|
||||||
|
let errors = analyze_errors(
|
||||||
|
r#"
|
||||||
|
game "Test" { mapper: NROM }
|
||||||
|
on frame { nope = 5 }
|
||||||
|
start Main
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
errors.contains(&ErrorCode::E0502),
|
||||||
|
"assignment to undefined var should produce E0502, got: {errors:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn analyze_enum_variants_as_constants() {
|
fn analyze_enum_variants_as_constants() {
|
||||||
let result = analyze_ok(
|
let result = analyze_ok(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue