"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
Tests use snapshot assertions. Let me add a test for the reported bug near the extract function tests:
- Assistant
Now run the new tests (snapshots will be created) and the full extract-function test suite to check for regressions:
- Assistant
The new snapshots were generated (I can see the correct output:
fn function() -> String { echo "wobble" }). Let me review the pending snapshots before accepting: - Assistant
cargo-instaisn't installed — I'll accept the snapshots by renaming: - Assistant
Only my new snapshots have it — removing to match the repo convention:
- Assistant
Now let me add a changelog entry:
- Assistant
All 1516 language server tests pass. Here's a summary of the fix:
Root cause
The
ExtractFunctioncode action tracks the "tail position" statement vialast_statement_location, which was updated when entering a top-level function (visit_typed_function) and blocks (visit_typed_expr_block) — but not when descending into an anonymous function's body. So when extracting the last statement of anfn() { ... }literal, it was compared against the enclosing function's last statement, classified asStatementPosition::NotTail, and the extracted function fell into the "purely side effects" path that returnsNil— silently changing the anonymous function's return value.Changes
language-server/src/code_action.rs— added a override to 's visitor (mirroring the existing handling) that saves, sets, and restores around visiting the anonymous function's body, since its last statement is what the anonymous function returns. This also fixes the same issue for callbacks, which are expressions too.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.