1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-09 01:16:12 +00:00

Analyzer: E0203 for bare return in typed function

A \`return\` with no value inside a function that has a declared
return type is an error — the caller expects a value. Previously
it compiled silently and the callee returned whatever was in A
from the last computation.

https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
Claude 2026-04-12 18:01:07 +00:00
parent 2fd3a6e545
commit c10251338d
No known key found for this signature in database
2 changed files with 31 additions and 1 deletions

View file

@ -1030,8 +1030,18 @@ impl Analyzer {
));
}
}
Statement::Return(None, span) => {
// Bare `return` in a function with a declared return
// type is an error — the caller expects a value.
if self.in_function_body && self.current_return_type.is_some() {
self.diagnostics.push(Diagnostic::error(
ErrorCode::E0203,
"missing return value in function with declared return type",
*span,
));
}
}
Statement::WaitFrame(_)
| Statement::Return(None, _)
| Statement::LoadBackground(_, _)
| Statement::SetPalette(_, _) => {}
Statement::DebugLog(args, _) => {

View file

@ -601,6 +601,26 @@ fn analyze_loop_with_break_ok() {
);
}
#[test]
fn analyze_bare_return_from_typed_fn_errors() {
// A `return` with no value inside a function that has a declared
// return type should produce E0203.
let errors = analyze_errors(
r#"
game "Test" { mapper: NROM }
fun get_five() -> u8 {
return
}
on frame { wait_frame }
start Main
"#,
);
assert!(
errors.contains(&ErrorCode::E0203),
"bare return from typed fn should produce E0203, got: {errors:?}"
);
}
#[test]
fn analyze_return_value_from_void_fn() {
let errors = analyze_errors(