Skip to content
TheChatArchiveThe Chat Archive
QwenQwen/Qwen3.6-27B· alibabaHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION "Extract function" incorrectly returns Nil when extracting from…

  1. User

    ISSUE DESCRIPTION

    "Extract function" incorrectly returns Nil when extracting from anonymous function

    When 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 using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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-$

  2. Qwen 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 Nil even when the original expression has a different return type.

    Let me start by exploring the repository structure to understand the codebase.

  3. Qwen Assistant

    This looks like a Gleam compiler repository written in Rust. Let me search for "extract function" related code:

  4. Qwen Assistant

    Let me search more specifically for "extract function" (not "extract variable"):

  5. Qwen Assistant

    Let me look at the code_action.rs file which likely contains the extract function logic:

  6. Qwen Assistant

    Let me search for the "Extract function" code action more specifically:

  7. Qwen Assistant

    Let me continue reading the code to understand the extract function logic:

  8. Qwen Assistant

    Let me continue reading more, especially focusing on the part about extracting from anonymous functions and how the return type is handled:

  9. Qwen Assistant

    Now I need to understand the issue better. Let me look at where the extract function handles anonymous function body extraction and the Nil return issue. Let me look at the tests to understand the problem more:

  10. Qwen Assistant

    Let me search for tests related to the specific issue - extracting from inside an anonymous function where a value is returned:

  11. Qwen Assistant

    Let me look at the snapshots to see the expected outputs:

  12. Qwen Assistant

    Now let me search for snapshots related to extracting from inside an anonymous function that doesn't capture variables:

  13. Qwen 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:

  14. Qwen Assistant

    Now I need to understand what happens when extracting statements from inside an anonymous function. Let me look at the visit_typed_expr_fn method:

  15. Qwen Assistant

    Let me look at the visit_typed_expr_fn in the ExtractFunction visitor (around line 12360):

  16. Qwen Assistant

    Let me look at the register_function method and continue exploring the code:

  17. Qwen Assistant

    That's a different visitor (UnwrapAnonymousFunction). Let me find where register_function is defined for ExtractFunction:

  18. Qwen Assistant

    I see there's only one register_function and it's for UnwrapAnonymousFunction. 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:

  19. Qwen Assistant

    Let me look at the extract_statements function more closely to see where the Nil return happens:

  20. Qwen Assistant

    Now I understand the structure better. The extract_statements function handles non-tail position statements and returns Nil when 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 returns Nil.

    But the problem described in the issue is different - it says when extracting echo "wobble" from an anonymous function, it returns Nil even though echo expressions have a return value that should be preserved. Let me create a test script to reproduce the issue:

  21. Qwen Assistant

    Good, the build succeeded. Now let me run the existing tests to see if there are any failing tests related to the issue:

  22. Qwen 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:

  23. Qwen Assistant

    Let me look for the assert_code_action macro to understand how tests work:

  24. Qwen Assistant

    Now let me search for the EXTRACT_FUNCTION constant:

  25. Qwen 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:

  26. Qwen 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 how visit_typed_expr_fn handles 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:

  27. Qwen Assistant

    I see the issue now. Let me look at how visit_typed_expr_fn is implemented for ExtractFunction:

  28. Qwen Assistant

    Wait, this visit_typed_expr_fn is for a different visitor (UnwrapAnonymousFunction). Let me check which visitor ExtractFunction belongs to:

  29. Qwen Assistant

    I see! The ExtractFunction visitor has visit_typed_function for top-level functions and visit_typed_expr_block for blocks. But there's no visit_typed_expr_fn override for anonymous functions!

    When visiting an anonymous function (fn() { ... }), the body is a Vec1<TypedStatement>, but there's no handler that updates last_statement_location when entering an anonymous function body. Let me verify this:

  30. Qwen Assistant

    Now I see the problem! When ExtractFunction visits a top-level function (visit_typed_function), it sets last_statement_location to the location of the last statement in the function body. Similarly, when it visits a block (visit_typed_expr_block), it updates last_statement_location to 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_location tracking might not be correct. Let me trace what happens:

    1. When visiting the module, we encounter fn wibble() { ... } - this is a TypedFunction and visit_typed_function is called, setting last_statement_location to the last statement of wibble.
    2. Inside wibble, we encounter let wobble = fn() { ... } - this is a statement containing an anonymous function.
    3. When visiting the anonymous function body, the statements are visited through visit_typed_statement -> ast::visit::visit_typed_statement which eventually visits each expression/statement in the anonymous function body.

    But wait, let me check what ast::visit::visit_typed_expr_fn does in the standard visitor:

  31. Qwen Assistant

    Now I understand! The visit_typed_expr_fn in the standard visitor just calls visit_typed_statement for each statement in the body. It does NOT call visit_typed_expr_block.

    This is the bug! When statements are inside an anonymous function, they're visited through visit_typed_statement, not through visit_typed_expr_block. But last_statement_location is only updated in visit_typed_expr_block, not when visiting statements in an anonymous function body.

    So when extracting from inside an anonymous function, the last_statement_location still 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_fn override to ExtractFunction that updates last_statement_location when entering an anonymous function body, similar to how visit_typed_expr_block does it.

    Let me also check how visit_typed_expr_block works:

  32. Qwen Assistant

    Now I need to add a visit_typed_expr_fn method to ExtractFunction that tracks the last_statement_location similar to how it's done for blocks. Let me see where the other methods end:

  33. Qwen Assistant

    Now I understand the fix. I need to add a visit_typed_expr_fn method that saves and restores last_statement_location, setting it to the last statement of the anonymous function body. Let me add this method right after visit_typed_expr_block:

  34. Qwen Assistant

    Now let me add the fix. I'll add a visit_typed_expr_fn method after visit_typed_expr_block:

  35. Qwen Assistant

    Now I'll add the visit_typed_expr_fn method right after visit_typed_expr_block. The fix needs to save and restore last_statement_location when visiting an anonymous function body:

  36. Qwen Assistant

    I need to use the --file option with the edit command.

  37. Qwen Assistant

    The code has been added. Now let me verify that the code compiles:

  38. Qwen Assistant

    The code compiles. Now let me run the existing tests related to extract_function:

  39. Qwen Assistant

    All 20 tests pass. Now let me also run the tests related to anonymous functions:

  40. Qwen Assistant

    All tests pass. Let me also run all the action tests to make sure nothing is broken:

  41. Qwen 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:

  42. Qwen Assistant

    Let me write a temporary test script to verify the fix works correctly for the reported issue:

  43. Qwen 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:

  44. Qwen Assistant

    Let me look at the extract_statements_in_tail_position test:

  45. Qwen Assistant

    Let me check the snapshot for this test to see what it produces:

  46. Qwen 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 returns Nil with an explicit Nil at the end.

    But wait, echo returns its value, so if I'm extracting echo "wobble" and it's in the tail position of the anonymous function, the extracted function should return String (the type of what echo returns), not Nil.

    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 -> Nil return type AND adds an explicit Nil at the end. But if the expression being extracted (echo "wobble") has a different return type (String), then this is incorrect.

    Wait, but echo returns the value it echoes, so echo "wobble" returns "wobble" which is a String. So the extracted function should return String, not Nil.

    Let me check what echo returns in Gleam:

  47. Qwen Assistant

    Good, so echo returns the same type as the expression it echoes. So echo "wobble" returns String.

    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 because last_statement_location wasn't updated when entering the anonymous function body.

    Let me verify this fix is correct by running all tests:

  48. Qwen Assistant

    The test failures are unrelated to my changes - they're about a missing escript program in the environment. Let me run only the language-server tests:

  49. Qwen Assistant

    All 1514 language-server tests pass. Let me also run the compiler-core tests to make sure nothing is broken there:

  50. Qwen Assistant

    All 3880 compiler-core tests pass. Now let me look at the git diff to confirm what changes I made:

  51. Qwen 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>

  52. Qwen Assistant

    Let me run the command again properly.

  53. Qwen 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.

  54. Qwen Assistant

    Let me run more comprehensive tests to make sure my fix handles edge cases correctly:

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.