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.
Interface
No new interfaces are introduced.
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
I'll start by exploring the repository structure to understand the codebase and find the relevant code for the "extract function" feature.
Assistant
Let me explore the codebase structure, especially the language-server directory since "extract function" is likely a language server feature.
Assistant
Let me look at the ExtractFunction implementation. Let me start by looking at the struct definition and the relevant parts.
Assistant
Let me look at the relevant code, specifically around the code_actions function for ExtractFunction.
Assistant
Now let me look at how the
StatementPosition(tail vs not-tail) is determined in the visit code.Assistant
Let me look at the ast::visit module to understand what visit methods are available for functions.
Assistant
Let me look for the visit trait/module in compiler-core.
Assistant
So there's a
visit_typed_expr_fnmethod available. TheExtractFunctionvisitor does NOT overridevisit_typed_expr_fn, so the default is used, which callsvisit_typed_expr_fn(self, ...). Let me look at that function to see what it does.Assistant
So
visit_typed_expr_fnvisits each statement in the body viav.visit_typed_statement(statement). It does NOT updatelast_statement_location. This is the bug. Thevisit_typed_expr_blockoverride inExtractFunctionupdateslast_statement_location, but there's no equivalent for anonymous functions. Let me confirm by looking at the existing overrides.Assistant
Now I understand the bug clearly. The fix is to add a
visit_typed_expr_fnoverride inExtractFunctionthat updateslast_statement_locationto the last statement of the function body, similar tovisit_typed_expr_block. Let me first write a test to reproduce. Let me look at an existing test for extract_function.Assistant
Let me look at the assert_code_action macro to understand the expected output format.
Assistant
The tests use insta snapshots which auto-generate. Let me first create a temporary reproduction test to confirm the bug. Let me check the imports in code_action.rs.
Assistant
The imports are available. Now let me add the
visit_typed_expr_fnoverride right aftervisit_typed_expr_block.Assistant
Now let me add the
visit_typed_expr_fnoverride right aftervisit_typed_expr_block:Assistant
The edit removed the closing brace of
visit_typed_expr_block. Let me fix that:Assistant
Now let me check how
visit_typed_exprdispatches tovisit_typed_expr_fnin the visit module.Assistant
Now let me build to check that the code compiles.
Assistant
The
Vec1::last()method seems to resolve differently for a reference. Let me check the Vec1 API.Assistant
Vec1doesn't have alast()method itself (only in tests). Sobody.last()on a&Vec1resolves to... Let me check howvisit_typed_functionusesfunction.body.last().function.bodyisVec1<TypedStatement>owned..last()- Vec1 might deref to Vec via Deref. Let me check Deref.Assistant
Let me look more carefully at how Vec1 implements
last.Assistant
Let me see the full error output.
Assistant
Two errors. First,
TypeAstnot found in scope. Let me check how it's referenced elsewhere in the file.Assistant
TypeAstneeds to beast::TypeAst. Let me also check the type offunction.bodyto understand the.last()issue.Assistant
function.bodyis a regularVec, butbodyinvisit_typed_expr_fnis aVec1. Let me see how other code handlesVec1::last().Assistant
Line 3117 shows
body.last().location()- soVec1::last()returns&Tdirectly (notOption), since Vec1 always has at least one element. Let me verify and fix both issues.Assistant
Now let me fix both issues - use
ast::TypeAstandSome(body.last().location())sinceVec1::last()returns&Tdirectly:Assistant
It compiles. Now let me write a temporary test to reproduce the bug and verify the fix. Let me first look at how snapshots are stored.
Assistant
Let me look at the existing anonymous function extraction tests to make sure my change doesn't break them.
Assistant
Let me write a temporary test to reproduce the issue. Let me add it to the test file temporarily.
Assistant
Let me add a temporary test right after the anonymous function tests (at line 13522):
Assistant
I'm missing the closing
);and}for the test. Let me fix it:Assistant
Now let me run the temporary test to see the output. Since there's no snapshot yet, insta will create a
.snap.newfile.Assistant
"No action with the given title" - the code action wasn't offered. Let me check
can_extract_statementto understand selection requirements.Assistant
Let me check the
completely_withinandwithinhelper functions.Assistant
completely_withinrequires strict inequality, so selecting exactly2should work. Let me check the test helpersunder_last_charandfind_position_ofto understand the selection.Assistant
The issue is
under_last_char()on a single-char value"2"gives offset 0, making an empty selection. Let me rewrite my test to select a multi-character expression. Let me update the test:Assistant
echois usable in tests. Let me rewrite my test to use the exact issue example withecho:Assistant
Now let me run the test with my fix applied to see the output:
Assistant
With my fix, the output is correct:
fn function() -> String { echo "wobble" }The extracted function returns
Stringwith no extraNil. Now let me confirm the bug existed WITHOUT my fix by temporarily reverting code_action.rs.Assistant
Confirmed the bug. Without my fix:
fn function() -> Nil { echo "wobble" Nil }This matches the issue exactly. Now let me restore my fix and clean up the snapshot file.
Assistant
My fix compiles. Now let me think about edge cases. Let me verify that extracting a NON-tail statement (the first one) still correctly returns
Nil. Let me also run the full set of extraction-related tests to ensure nothing breaks.Assistant
177 passed, only my temp test failed (no snapshot yet). All existing extract tests pass. Now let me verify the non-tail case (extracting the first statement) still correctly returns
Nil. Let me add a second temp test:Assistant
I need to close the not_tail test properly. Let me fix:
Assistant
The non-tail case correctly returns
Nil(side-effect only, which is correct). Both cases work properly now. Let me verify my edit is within the correctExtractFunctionVisit impl block (not another visitor).Assistant
My
visit_typed_expr_fnis correctly inside theExtractFunctionVisit impl. Now let me check the use-related extraction tests to ensure my change doesn't affectusecallback bodies unexpectedly.Assistant
These use tests select the whole
usestatement, not statements inside the callback body, so my change shouldn't affect them. Let me run the use-specific extraction tests to confirm:Assistant
All use extraction tests pass. Now let me clean up my temporary tests and any leftover snapshot files.
Assistant
Now let me remove the two temporary tests from the test file:
Assistant
There's now a double blank line. Let me clean it up:
Assistant
Now let me verify the test file has no remaining changes and confirm the only change is in code_action.rs:
Assistant
Only code_action.rs is modified. Let me view the final diff to confirm the change is clean:
Assistant
The diff is clean. Let me check the changelog convention to see if I should add an entry.
Assistant
Let me add a changelog entry under the Language server section:
Assistant
Now let me run the broader extraction test suite once more to confirm everything passes after cleanup:
Assistant
All tests pass. Let me run the full action test module to be thorough, since code_action.rs is shared by all code actions:
Assistant
All 871 action tests pass. Let me confirm no leftover snapshot files and do a final review of the diff:
Assistant
No leftover snapshot files. Let me do a final build to confirm everything compiles cleanly:
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
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →