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
Good,
Vec1is already imported. Now let's add thevisit_typed_expr_fnoverride to theExtractFunctionimpl.Assistant
Now let's add a blank line between the two functions for style consistency.
Assistant
Now let's check if
TypeandFunctionLiteralKindare imported in this file already.Assistant
Good, imports already present. Now let's try building.
Assistant
Build succeeded. Now let's write a quick test to verify the fix. Let's check existing tests for extract function related to anonymous functions.
Assistant
I'll insert a temporary test right after
extract_functionto reproduce the issue, run it, inspect the snapshot, then remove both the test and generated snapshot.Assistant
I need to close the temp test's brace properly.
Assistant
Good, close enough. Now let's add a blank line for cleanliness (optional) and run the test.
Assistant
Let's fix my temp test's selection to use select_until like these examples.
Assistant
Confirmed the bug reproduces without the fix (
fn function() -> Nil { echo "wobble" Nil }), and is fixed with our change (fn function() -> String { echo "wobble" }). Let's restore the fix.Assistant
Now let's remove the temporary test and snapshot files, then run the full existing test suite for extract function and related tests to ensure nothing is broken.
Assistant
Test file is restored to original state. Let's verify with git diff.
Assistant
No diff for the test file — good, it's back to original. Now let's run the full existing test suite for the language-server crate to make sure nothing broke.
Assistant
All 1514 tests pass. Let's double check git status to ensure only code_action.rs is modified.
Assistant
Only the intended change remains. Now let's also update the CHANGELOG since this repo seems to track changes there (common Gleam convention).
Assistant
I'll add an entry under "Language server" section describing the fix, following past conventions (e.g., "- Fixed a bug where...").
Assistant
Now let's run the full test suite one more time to be safe, and check the diff overall.
Assistant
All tests pass. Let's also run the broader workspace build/tests to double check nothing else broke (compiler-core tests since visit.rs affects other consumers too, though we didn't touch visit.rs, only code_action.rs).
Assistant
Full workspace builds successfully. Let's view the final diff.
Assistant
This is a clean, minimal fix. Now let's go through the checklist.
Assistant
Compilation already succeeded with
body.last().location()(no.map), confirmingVec1::last()returns&Tdirectly, which is correct since the build passed without errors. This confirms my implementation's correctness type-wise.Assistant
Let's add a temporary nested-anon-function test case to validate edge cases, then remove it after checking.
Assistant
Nested anonymous function edge case works correctly too — the inner function's tail statement is correctly detected, returning
String(notNil), while the outer function's own last statement remains unaffected. Now let's clean up the temp test and snapshot.Assistant
I need to add back the blank line between the two test functions to match original formatting exactly.
Assistant
Now let's confirm no diff remains in this test file, and clean any leftover snapshot files.
Assistant
Clean. Now let's run the full test suite one final time to be certain.
Assistant
All 1514 tests pass with no failures. Let's also run the full workspace test suite (compiler-core etc.) to be thorough, since it might take a while but let's check.
Assistant
These failures are pre-existing environment issues (missing
escript/Erlang binary), unrelated to our change. Let's verify by checking if they fail without our change too, and separately run compiler-core tests.Assistant
Confirmed —
escript/Erlang binary is missing from the environment (unrelated to our fix), andgleam-core(3880 tests) plusgleam-language-server(1514 tests) all pass. Let's double checkcompiler-clitests too, excluding the escript-related ones, to be safe.Assistant
All relevant test suites pass (the only failures are in
test-commandsdue to missingescript/Erlang binary in this sandboxed environment, unrelated to our change). Let's do a final review of the diff.Assistant
This is a clean, minimal, and correct fix. Now let me go through the checklist explicitly.
Checklist review:
-
[x] If an error was reported in the issue description, I have successfully reproduced it. I added a temporary test replicating the exact issue scenario (
echo "wobble"as last statement inside an anonymous function) and confirmed (by stashing my fix) that it producedfn function() -> Nil { echo "wobble" Nil }, matching the bug report exactly. -
[x] If an error was reported in the issue description, I have confirmed that my fix resolves the error. After restoring the fix, the same test produced
fn function() -> String { echo "wobble" }, correctly returning the value instead ofNil. -
[x] I have thought about edge cases and made sure my fix handles them as well. I tested a nested-anonymous-function scenario (an anonymous function inside another anonymous function) to make sure the
last_statement_locationsave/restore pattern (mirroring the existingvisit_typed_expr_blockhandling) correctly scopes tail-position detection per function body, without leaking state between nested closures. This worked correctly. The fix also naturally coversusecallbacks and capture-syntax anonymous functions since they all go throughvisit_typed_expr_fn, consistent with how blocks are already handled. -
[x] I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. Ran the full
gleam-language-servertest suite (1514 tests, all passing),gleam-core(3880 tests, all passing), andgleam-cli(96 tests, all passing). The only failing tests in the workspace (test-commands) are due to a missingescript/Erlang binary in this sandbox, unrelated to this change.
Cleaned up all temporary test code, snapshot files, confirming
git status/git diffreflects only the intended fix (language-server/src/code_action.rs) plus a changelog entry. Ready to submit.-
Assistant
These
.snap.newfiles were generated by the unrelated escript test failures (due to missing Erlang binary in sandbox), not by my changes. Let's remove them to keep the workspace clean.
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 →
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 →