ISSUE DESCRIPTION "Extract function" incorrectly returns Nil when extracting from…
- User
ISSUE DESCRIPTION
"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.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /gleam, current file: none) bash-$
Assistant
Let me analyze the issue. The problem is about the "extract function" feature in Gleam. When extracting a function from within an anonymous function, the extracted function incorrectly returns
Nileven when the original expression has a different return type.Let me start by exploring the repository structure to understand the codebase.
Assistant
This looks like a Gleam compiler repository written in Rust. Let me search for "extract function" related code:
Assistant
Let me search more specifically for "extract function" (not "extract variable"):
Assistant
Let me look at the code_action.rs file which likely contains the extract function logic:
Assistant
Let me search for the "Extract function" code action more specifically:
Assistant
Let me continue reading the code to understand the extract function logic:
Assistant
Let me continue reading more, especially focusing on the part about extracting from anonymous functions and how the return type is handled:
Assistant
Now I need to understand the issue better. Let me look at where the extract function handles anonymous function body extraction and the
Nilreturn issue. Let me look at the tests to understand the problem more:Assistant
Let me search for tests related to the specific issue - extracting from inside an anonymous function where a value is returned:
Assistant
Let me look at the snapshots to see the expected outputs:
Assistant
Now let me search for snapshots related to extracting from inside an anonymous function that doesn't capture variables:
Assistant
Now I need to understand the issue better. The issue says when extracting a function from within an anonymous function (not extracting the whole anonymous function), the extracted function incorrectly returns
Nil. Let me look at the visitor implementation to understand how statements inside an anonymous function are handled:Assistant
Now I need to understand what happens when extracting statements from inside an anonymous function. Let me look at the
visit_typed_expr_fnmethod:Assistant
Let me look at the visit_typed_expr_fn in the ExtractFunction visitor (around line 12360):
Assistant
Let me look at the
register_functionmethod and continue exploring the code:Assistant
That's a different visitor (
UnwrapAnonymousFunction). Let me find whereregister_functionis defined forExtractFunction:Assistant
I see there's only one
register_functionand it's forUnwrapAnonymousFunction. Let me look at the ExtractFunction visitor implementation to see where it tracks when we're inside an anonymous function. Let me look more closely at the issue - the problem is about extracting code FROM INSIDE an anonymous function.Let me search for the line_numbers field that is used in ExtractFunction:
Assistant
Let me look at the extract_statements function more closely to see where the Nil return happens:
Assistant
Now I understand the structure better. The
extract_statementsfunction handles non-tail position statements and returnsNilwhen there are no returned variables. Let me now understand the issue - when extracting from within an anonymous function body (not the whole anonymous function), and the selected statements are in a non-tail position, the code returnsNil.But the problem described in the issue is different - it says when extracting
echo "wobble"from an anonymous function, it returnsNileven thoughechoexpressions have a return value that should be preserved. Let me create a test script to reproduce the issue:Assistant
Good, the build succeeded. Now let me run the existing tests to see if there are any failing tests related to the issue:
Assistant
Now let me look at the issue more closely. The issue is about extracting a statement from within an anonymous function body (not extracting the entire anonymous function). Let me write a test case to reproduce the issue:
Assistant
Let me look for the
assert_code_actionmacro to understand how tests work:Assistant
Now let me search for the
EXTRACT_FUNCTIONconstant:Assistant
Now I understand the testing pattern. Let me create a simple test to reproduce the issue. The issue is about extracting a statement from inside an anonymous function that is NOT in the tail position:
Assistant
Let me understand the issue better. Looking at the example again:
fn wibble() { let wobble = fn() { echo "wibble" echo "wobble" // extracting this line } }When extracting
echo "wobble"(which is the last statement in the anonymous function body), it should be treated as tail position. Let me look more closely at howvisit_typed_expr_fnhandles this case.The problem is that when we're inside an anonymous function and extract a statement, we need to determine if it's in tail position. Let me trace the code more carefully:
Assistant
I see the issue now. Let me look at how
visit_typed_expr_fnis implemented forExtractFunction:Assistant
Wait, this
visit_typed_expr_fnis for a different visitor (UnwrapAnonymousFunction). Let me check which visitorExtractFunctionbelongs to:Assistant
I see! The
ExtractFunctionvisitor hasvisit_typed_functionfor top-level functions andvisit_typed_expr_blockfor blocks. But there's novisit_typed_expr_fnoverride for anonymous functions!When visiting an anonymous function (
fn() { ... }), the body is aVec1<TypedStatement>, but there's no handler that updateslast_statement_locationwhen entering an anonymous function body. Let me verify this:Assistant
Now I see the problem! When
ExtractFunctionvisits a top-level function (visit_typed_function), it setslast_statement_locationto the location of the last statement in the function body. Similarly, when it visits a block (visit_typed_expr_block), it updateslast_statement_locationto the last statement in the block.However, when visiting an anonymous function (via the default visitor), the body of the anonymous function is visited as a block, but the
last_statement_locationtracking might not be correct. Let me trace what happens:- When visiting the module, we encounter
fn wibble() { ... }- this is aTypedFunctionandvisit_typed_functionis called, settinglast_statement_locationto the last statement ofwibble. - Inside
wibble, we encounterlet wobble = fn() { ... }- this is a statement containing an anonymous function. - When visiting the anonymous function body, the statements are visited through
visit_typed_statement->ast::visit::visit_typed_statementwhich eventually visits each expression/statement in the anonymous function body.
But wait, let me check what
ast::visit::visit_typed_expr_fndoes in the standard visitor:- When visiting the module, we encounter
Assistant
Now I understand! The
visit_typed_expr_fnin the standard visitor just callsvisit_typed_statementfor each statement in the body. It does NOT callvisit_typed_expr_block.This is the bug! When statements are inside an anonymous function, they're visited through
visit_typed_statement, not throughvisit_typed_expr_block. Butlast_statement_locationis only updated invisit_typed_expr_block, not when visiting statements in an anonymous function body.So when extracting from inside an anonymous function, the
last_statement_locationstill points to the last statement of the enclosing scope (e.g., the outer function), not the anonymous function body. This means the last statement in the anonymous function body is incorrectly treated as NOT in tail position.The fix should be to add a
visit_typed_expr_fnoverride toExtractFunctionthat updateslast_statement_locationwhen entering an anonymous function body, similar to howvisit_typed_expr_blockdoes it.Let me also check how
visit_typed_expr_blockworks:Assistant
Now I need to add a
visit_typed_expr_fnmethod toExtractFunctionthat tracks thelast_statement_locationsimilar to how it's done for blocks. Let me see where the other methods end:Assistant
Now I understand the fix. I need to add a
visit_typed_expr_fnmethod that saves and restoreslast_statement_location, setting it to the last statement of the anonymous function body. Let me add this method right aftervisit_typed_expr_block:Assistant
Now let me add the fix. I'll add a
visit_typed_expr_fnmethod aftervisit_typed_expr_block:Assistant
Now I'll add the
visit_typed_expr_fnmethod right aftervisit_typed_expr_block. The fix needs to save and restorelast_statement_locationwhen visiting an anonymous function body:Assistant
I need to use the --file option with the edit command.
Assistant
The code has been added. Now let me verify that the code compiles:
Assistant
The code compiles. Now let me run the existing tests related to extract_function:
Assistant
All 20 tests pass. Now let me also run the tests related to anonymous functions:
Assistant
All tests pass. Let me also run all the action tests to make sure nothing is broken:
Assistant
All 871 action tests pass. Now let me verify the fix by looking at the test snapshots for similar scenarios and checking if the issue described is now handled correctly.
Let me create a test case to verify the fix works for the exact issue described:
Assistant
Let me write a temporary test script to verify the fix works correctly for the reported issue:
Assistant
Let me look for a test case that would have failed before the fix. Let me check what happens when extracting the last statement from inside an anonymous function:
Assistant
Let me look at the
extract_statements_in_tail_positiontest:Assistant
Let me check the snapshot for this test to see what it produces:
Assistant
Good. Now let me create a simple test case to verify the fix. First, let me understand what the issue describes more clearly.
Looking at the issue description again:
fn wibble() { let wobble = fn() { echo "wibble" echo "wobble" // extracting this line } }When extracting
echo "wobble"(the last line in the anonymous function), the extracted function incorrectly returnsNilwith an explicitNilat the end.But wait,
echoreturns its value, so if I'm extractingecho "wobble"and it's in the tail position of the anonymous function, the extracted function should returnString(the type of what echo returns), notNil.Actually, let me re-read the issue. The issue says "the extracted function always returns
Nil, even when incorrect". Looking at the example:fn function() -> Nil { echo "wobble" Nil // this is incorrect! }The problem is that the extracted function has
-> Nilreturn type AND adds an explicitNilat the end. But if the expression being extracted (echo "wobble") has a different return type (String), then this is incorrect.Wait, but
echoreturns the value it echoes, soecho "wobble"returns"wobble"which is aString. So the extracted function should returnString, notNil.Let me check what
echoreturns in Gleam:Assistant
Good, so
echoreturns the same type as the expression it echoes. Soecho "wobble"returnsString.Now let me look at the issue more carefully. When extracting a statement in a non-tail position (not the last statement in its scope), the code returns
Nil. But when extracting from inside an anonymous function, the bug was that the last statement was being treated as non-tail position becauselast_statement_locationwasn't updated when entering the anonymous function body.Let me verify this fix is correct by running all tests:
Assistant
The test failures are unrelated to my changes - they're about a missing
escriptprogram in the environment. Let me run only the language-server tests:Assistant
All 1514 language-server tests pass. Let me also run the compiler-core tests to make sure nothing is broken there:
Assistant
All 3880 compiler-core tests pass. Now let me look at the git diff to confirm what changes I made:
Assistant
ed I've verified my change. Now let me verify by checking the issue description more carefully and looking at if there's any existing test that tests extracting from inside an anonymous function:
<tool_call> <function=bash_terminal> <parameter=command> grep -A 30 "extract_variable_in_nested_anonymous_function" /gleam/language-server/src/tests/action.rs | head -35 </tool_call>
Assistant
Let me run the command again properly.
Assistant
Alright, let me verify that my fix is working correctly by looking at what tests exist for extracting functions (not variables) from inside anonymous functions. Let me also make sure there isn't already a test case that would have failed before the fix.
Let me also check if there's a test that extracts a function (not a variable) from inside an anonymous function.
Assistant
Let me run more comprehensive tests to make sure my fix handles edge cases correctly:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →