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:
parent
28e1627953
commit
6f0e4a0a74
2 changed files with 74 additions and 3 deletions
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue