"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:
fn wibble() { let wobble = fn() { echo "wibble" echo "wobble" // extracting this line } }is turned into
fn 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
I'll investigate the "Extract function" logic in the codebase to find why anonymous-function extractions get an incorrect
Nilreturn. - Assistant
The bug is that
last_statement_locationisn't updated for anonymous function bodies, so tail statements are misclassified. I'll add avisit_typed_expr_fnoverride and a test. - Assistant
Implementing the fix and adding a test.
- Assistant
The test selectors were zero-width; I'll fix them to cover the full expression.
- Assistant
The fix works. Accepting the snapshot files.
- Assistant
Fixed the bug. Extracting a statement from inside an anonymous function body was misclassified as non-tail, so the refactor always appended an explicit
Nilreturn.Root cause
ExtractFunctiontrackslast_statement_locationto tell tail from non-tail statements. That was updated invisit_typed_expr_blockfor block expressions, but not invisit_typed_expr_fnfor anonymous function bodies.When extracting the last statement in an anonymous function, it was treated as
StatementPosition::NotTail, which routes throughextract_statementsand always addsNilat the end.Fix
Added a
visit_typed_expr_fnoverride that sets from the anonymous function body before visiting its statements, mirroring :
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.