"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 I'll add a
visit_typed_expr_fnoverride mirroring the block handling: - Assistant
Now check that
Vec1andTypeAstare imported in this file: - Assistant
TypeAstis referenced asast::TypeAstin this file: - Assistant
Compiles. Now let me add a regression test. Let me look at existing extract function tests:
- Assistant
The output is now correct —
fn function() -> String { echo "wobble" }instead of returningNil. Accept the new snapshot: - Assistant
All 21 extract_function tests pass. Now run the full language-server test suite since many EXTRACT_FUNCTION tests have other names:
- Assistant
Now verify formatting and clippy on the changed crate:
- Assistant
Fixed. The problem was that the "Extract function" code action tracks
last_statement_locationto decide whether a statement being extracted is in tail position (in which case the extracted function must return its value rather thanNil) — but it only updated that tracking when entering named functions and blocks, never anonymous function bodies. So the last statement of an anonymous fn was misclassified asNotTail, producing a spuriousNilreturn and changing the anonymous function's type.Changes:
-
language-server/src/code_action.rs— Added avisit_typed_expr_fnoverride toExtractFunction'sVisitimpl, mirroring the existingvisit_typed_expr_blockhandling: it saves the outerlast_statement_location, sets it to the anonymous fn body's last statement, visits the body, then restores it. This also correctly coversusecallback bodies, which areTypedExpr::Fnnodes too. -
language-server/src/tests/action.rs+ snapshot — Regression test reproducing the issue exactly. The action now produces:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.