check return expressions against the enclosing function's return type #78

Merged
jacksonopp merged 1 commit from us59/check-return-against-fn-return-type into main 2026-10-04 02:27:11 +00:00
Owner

Closes #59.

Extends #3's return-type checking to early returns, not just a function body's trailing expression.

  • Adds Scope::current_fn_return: Option<Ty<'db>>, set by check_fn before checking the body and restored to its previous value right after - a save/restore around the body check (not a stack), so Rust's own call stack naturally handles fn-nested-in-fn correctly and non-nested functions never see a leaked value.
  • New check_return handles Expr::Return's optional value: return x is checked against current_fn_return, bare return is checked against Ty::unit(db), reusing DiagnosticKind::ReturnTypeMismatch/NoTypeProduced rather than adding new diagnostic variants.
  • Mirrors check_fn's -> type carve-out (a function declared -> type accepts any concrete type a return produces).
  • return outside any function (current_fn_return unset) is deferred, same policy as break outside a loop.

New tests cover matching/mismatched value and bare-return cases, the outside-a-function case, the -> type carve-out, and a return value that itself fails to type-check.

Closes #59. Extends #3's return-type checking to early `return`s, not just a function body's trailing expression. - Adds `Scope::current_fn_return: Option<Ty<'db>>`, set by `check_fn` before checking the body and restored to its previous value right after - a save/restore around the body check (not a stack), so Rust's own call stack naturally handles `fn`-nested-in-`fn` correctly and non-nested functions never see a leaked value. - New `check_return` handles `Expr::Return`'s optional value: `return x` is checked against `current_fn_return`, bare `return` is checked against `Ty::unit(db)`, reusing `DiagnosticKind::ReturnTypeMismatch`/`NoTypeProduced` rather than adding new diagnostic variants. - Mirrors `check_fn`'s `-> type` carve-out (a function declared `-> type` accepts any concrete type a `return` produces). - `return` outside any function (`current_fn_return` unset) is deferred, same policy as `break` outside a loop. New tests cover matching/mismatched value and bare-return cases, the outside-a-function case, the `-> type` carve-out, and a return value that itself fails to type-check.
check return expressions against the enclosing function's declared return type
All checks were successful
CI / check (pull_request) Successful in 3m17s
302d6586db
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jacksonopp/twill!78
No description provided.