Skip to content
TheChatArchiveThe Chat Archive
GLMz-ai/glm-5.2· unknownHugging 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.

    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 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. GLM Assistant

    I'll start by exploring the repository structure to understand the codebase and find the relevant code for the "extract function" feature.

  3. GLM Assistant

    Let me explore the codebase structure, especially the language-server directory since "extract function" is likely a language server feature.

  4. GLM Assistant

    Let me look at the ExtractFunction implementation. Let me start by looking at the struct definition and the relevant parts.

  5. GLM Assistant

    Let me look at the relevant code, specifically around the code_actions function for ExtractFunction.

  6. GLM Assistant

    Now let me look at how the StatementPosition (tail vs not-tail) is determined in the visit code.

  7. GLM Assistant

    Let me look at the ast::visit module to understand what visit methods are available for functions.

  8. GLM Assistant

    Let me look for the visit trait/module in compiler-core.

  9. GLM Assistant

    So there's a visit_typed_expr_fn method available. The ExtractFunction visitor does NOT override visit_typed_expr_fn, so the default is used, which calls visit_typed_expr_fn(self, ...). Let me look at that function to see what it does.

  10. GLM Assistant

    So visit_typed_expr_fn visits each statement in the body via v.visit_typed_statement(statement). It does NOT update last_statement_location. This is the bug. The visit_typed_expr_block override in ExtractFunction updates last_statement_location, but there's no equivalent for anonymous functions. Let me confirm by looking at the existing overrides.

  11. GLM Assistant

    Now I understand the bug clearly. The fix is to add a visit_typed_expr_fn override in ExtractFunction that updates last_statement_location to the last statement of the function body, similar to visit_typed_expr_block. Let me first write a test to reproduce. Let me look at an existing test for extract_function.

  12. GLM Assistant

    Let me look at the assert_code_action macro to understand the expected output format.

  13. GLM 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.

  14. GLM Assistant

    The imports are available. Now let me add the visit_typed_expr_fn override right after visit_typed_expr_block.

  15. GLM Assistant

    Now let me add the visit_typed_expr_fn override right after visit_typed_expr_block:

  16. GLM Assistant

    The edit removed the closing brace of visit_typed_expr_block. Let me fix that:

  17. GLM Assistant

    Now let me check how visit_typed_expr dispatches to visit_typed_expr_fn in the visit module.

  18. GLM Assistant

    Now let me build to check that the code compiles.

  19. GLM Assistant

    The Vec1::last() method seems to resolve differently for a reference. Let me check the Vec1 API.

  20. GLM Assistant

    Vec1 doesn't have a last() method itself (only in tests). So body.last() on a &Vec1 resolves to... Let me check how visit_typed_function uses function.body.last(). function.body is Vec1<TypedStatement> owned. .last() - Vec1 might deref to Vec via Deref. Let me check Deref.

  21. GLM Assistant

    Let me look more carefully at how Vec1 implements last.

  22. GLM Assistant

    Let me see the full error output.

  23. GLM Assistant

    Two errors. First, TypeAst not found in scope. Let me check how it's referenced elsewhere in the file.

  24. GLM Assistant

    TypeAst needs to be ast::TypeAst. Let me also check the type of function.body to understand the .last() issue.

  25. GLM Assistant

    function.body is a regular Vec, but body in visit_typed_expr_fn is a Vec1. Let me see how other code handles Vec1::last().

  26. GLM Assistant

    Line 3117 shows body.last().location() - so Vec1::last() returns &T directly (not Option), since Vec1 always has at least one element. Let me verify and fix both issues.

  27. GLM Assistant

    Now let me fix both issues - use ast::TypeAst and Some(body.last().location()) since Vec1::last() returns &T directly:

  28. GLM 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.

  29. GLM Assistant

    Let me look at the existing anonymous function extraction tests to make sure my change doesn't break them.

  30. GLM Assistant

    Let me write a temporary test to reproduce the issue. Let me add it to the test file temporarily.

  31. GLM Assistant

    Let me add a temporary test right after the anonymous function tests (at line 13522):

  32. GLM Assistant

    I'm missing the closing ); and } for the test. Let me fix it:

  33. GLM Assistant

    Now let me run the temporary test to see the output. Since there's no snapshot yet, insta will create a .snap.new file.

  34. GLM Assistant

    "No action with the given title" - the code action wasn't offered. Let me check can_extract_statement to understand selection requirements.

  35. GLM Assistant

    Let me check the completely_within and within helper functions.

  36. GLM Assistant

    completely_within requires strict inequality, so selecting exactly 2 should work. Let me check the test helpers under_last_char and find_position_of to understand the selection.

  37. GLM 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:

  38. GLM Assistant

    echo is usable in tests. Let me rewrite my test to use the exact issue example with echo:

  39. GLM Assistant

    Now let me run the test with my fix applied to see the output:

  40. GLM Assistant

    With my fix, the output is correct:

    fn function() -> String {
      echo "wobble"
    }
    

    The extracted function returns String with no extra Nil. Now let me confirm the bug existed WITHOUT my fix by temporarily reverting code_action.rs.

  41. GLM 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.

  42. GLM 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.

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

  44. GLM Assistant

    I need to close the not_tail test properly. Let me fix:

  45. GLM 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 correct ExtractFunction Visit impl block (not another visitor).

  46. GLM Assistant

    My visit_typed_expr_fn is correctly inside the ExtractFunction Visit impl. Now let me check the use-related extraction tests to ensure my change doesn't affect use callback bodies unexpectedly.

  47. GLM Assistant

    These use tests select the whole use statement, not statements inside the callback body, so my change shouldn't affect them. Let me run the use-specific extraction tests to confirm:

  48. GLM Assistant

    All use extraction tests pass. Now let me clean up my temporary tests and any leftover snapshot files.

  49. GLM Assistant

    Now let me remove the two temporary tests from the test file:

  50. GLM Assistant

    There's now a double blank line. Let me clean it up:

  51. GLM Assistant

    Now let me verify the test file has no remaining changes and confirm the only change is in code_action.rs:

  52. GLM Assistant

    Only code_action.rs is modified. Let me view the final diff to confirm the change is clean:

  53. GLM Assistant

    The diff is clean. Let me check the changelog convention to see if I should add an entry.

  54. GLM Assistant

    Let me add a changelog entry under the Language server section:

  55. GLM Assistant

    Now let me run the broader extraction test suite once more to confirm everything passes after cleanup:

  56. GLM 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:

  57. GLM Assistant

    All 871 action tests pass. Let me confirm no leftover snapshot files and do a final review of the diff:

  58. GLM Assistant

    No leftover snapshot files. Let me do a final build to confirm everything compiles cleanly:

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