mirror of
https://github.com/imjasonh/nescript
synced 2026-07-18 14:45:58 +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:
parent
2fd3a6e545
commit
c10251338d
2 changed files with 31 additions and 1 deletions
|
|
@ -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::WaitFrame(_)
|
||||||
| Statement::Return(None, _)
|
|
||||||
| Statement::LoadBackground(_, _)
|
| Statement::LoadBackground(_, _)
|
||||||
| Statement::SetPalette(_, _) => {}
|
| Statement::SetPalette(_, _) => {}
|
||||||
Statement::DebugLog(args, _) => {
|
Statement::DebugLog(args, _) => {
|
||||||
|
|
|
||||||
|
|
@ -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]
|
#[test]
|
||||||
fn analyze_return_value_from_void_fn() {
|
fn analyze_return_value_from_void_fn() {
|
||||||
let errors = analyze_errors(
|
let errors = analyze_errors(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue