"Extract function" incorrectly returns Nil when extracting from anonymous function <!--…
- User
"Extract function" incorrectly returns
Nilwhen extracting from anonymous functionWhen trying to extract a function from within an anonymous function, the extracted function always returns
Nil, even when incorrect.For example:
gleamfn wibble() { let wobble = fn() { echo "wibble" echo "wobble" // extracting this line } }is turned into
gleamfn wibble() { let wobble = fn() { echo "wibble" function() } } fn function() -> Nil { echo "wobble" Nil // this is incorrect! }I have a fix for it.
Interface
No new interfaces are introduced.
- Assistant
Now add a regression test:
- Assistant
These are insta snapshot tests — new snapshots need review. Let me check the generated output is correct:
- Assistant
All 1516 tests pass. Now update the changelog:
- Assistant
Fixed. Here's a summary:
Root cause
The
ExtractFunctioncode action tracks the location of the last statement in scope (last_statement_location) to decide whether extracted code is in tail position — if it is, the extracted function must return the last statement's value; otherwise it returnsNil.The visitor updated this tracking for named function bodies (
visit_typed_function) and blocks (visit_typed_expr_block), but not for anonymous function bodies. So statements insidefn() { ... }were compared against the enclosing function's last statement, never matched, and were always treated as non-tail — producing a spuriousNilreturn.Changes
language-server/src/code_action.rs— added avisit_typed_expr_fnoverride on 's visitor that saves, sets, and restores around visiting the anonymous function body, mirroring the existing block handling.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.