1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-11 10:10:49 +00:00

Analyzer: validate return statement types

Tracks the current function's declared return type and checks that
`return value` statements match it. Returning from a void function
with a value produces E0203; returning a value with the wrong type
produces E0201. State handler `return value` is still silently
accepted (the value is discarded).

https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
Claude 2026-04-12 10:52:06 +00:00
parent 28e1627953
commit 6f0e4a0a74
No known key found for this signature in database
2 changed files with 74 additions and 3 deletions

View file

@ -270,6 +270,50 @@ fn analyze_call_arity_in_expr_context() {
);
}
#[test]
fn analyze_return_type_ok() {
analyze_ok(
r#"
game "Test" { mapper: NROM }
fun get_five() -> u8 { return 5 }
on frame { wait_frame }
start Main
"#,
);
}
#[test]
fn analyze_return_wrong_type() {
let errors = analyze_errors(
r#"
game "Test" { mapper: NROM }
fun is_ok() -> bool { return 5 }
on frame { wait_frame }
start Main
"#,
);
assert!(
errors.contains(&ErrorCode::E0201),
"returning wrong type should produce E0201, got: {errors:?}"
);
}
#[test]
fn analyze_return_value_from_void_fn() {
let errors = analyze_errors(
r#"
game "Test" { mapper: NROM }
fun do_nothing() { return 5 }
on frame { wait_frame }
start Main
"#,
);
assert!(
errors.contains(&ErrorCode::E0203),
"returning value from void function should produce E0203, got: {errors:?}"
);
}
#[test]
fn analyze_const_assignment_error() {
let errors = analyze_errors(