"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 let me check the visitor for anonymous function expressions:
- Assistant
Now I'll implement the fix. First check the imports in code_action.rs:
- Assistant
Now add a regression test. Let me find the end of the extract-function test section:
- Assistant
The fix works — the extracted function now returns
Stringinstead ofNil. Let me view the full pending snapshot to verify: - Assistant
The snapshot is exactly right. Accept it:
- Assistant
Now run the full extract-function test suite to check for regressions:
- Assistant
All 1515 language server tests pass. Now update the changelog:
- Assistant
Finally, verify formatting and lints:
- Assistant
Done. The fix is complete:
Root cause
ExtractFunctiondecides whether an extracted statement is in "tail position" (and therefore whether the extracted function should return the statement's value orNil) by comparing againstlast_statement_location. The visitor updated that tracking field when entering a block (visit_typed_expr_block), but not when entering an anonymous function. So for a statement at the end of an anonymous function's body, the comparison was made against the enclosing named function's last statement, never matched, and the statement was treated asNotTail— producing an extracted function that returnedNil.Changes
-
language-server/src/code_action.rs— added avisit_typed_expr_fnoverride to theExtractFunctionvisitor that saves, sets (to the fn body's last statement), and restoreslast_statement_location, exactly mirroring the existing handling (code_action.rs:11280).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.