"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 test reproducing the bug. Let me check the position helpers:
- Assistant
Bug reproduced — the snapshot shows the incorrect
Nilreturn. Now the fix — add avisit_typed_expr_fnoverride mirroring the block one: - Assistant
All imports are present. Run the test:
- Assistant
The fix works — the extracted function now correctly returns
String(the value ofecho "wobble") instead ofNil. The failure is just the new snapshot needing acceptance: - Assistant
Now run the full language-server test suite to check for regressions:
- Assistant
All 1515 tests pass. Now update the changelog:
- Assistant
Final verification — run all extract function tests plus a build check:
- Assistant
Fixed. Summary of the changes:
Root cause
ExtractFunctiondecides whether extracted statements are in "tail position" by comparing againstself.last_statement_location. This was updated when entering a function definition and a block (visit_typed_expr_block), but not when entering an anonymous function body. So when extracting the last statement of an anonymous fn, it was compared against the enclosing function's last statement, classified asNotTail, and the extracted function fell into the side-effect path that returnsNil— silently discarding the statement's value.Changes
-
language-server/src/code_action.rs— added avisit_typed_expr_fnoverride toimpl Visit for ExtractFunction(next to the existing override) that saves , sets it to the anonymous fn body's last statement, visits, then restores it.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.