Skip to content
TheChatArchiveThe Chat Archive
GLMz-ai/glm-5.2· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION Cannot complete macro path in macro <!-- Troubleshooting guide:…

  1. User

    ISSUE DESCRIPTION

    Cannot complete macro path in macro

    rust-analyzer version: rust-analyzer 0.4.2897-standalone

    rustc version: rustc 1.97.0-nightly (36ba2c771 2026-04-23)

    editor or extension: Vim

    code snippet to reproduce:

    macro_rules! bar { () => (); }
    macro_rules! foo { ($i:ident) => { $i!() }; }
    fn main() {
        foo!(bar$0);
    }
    

    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: /rust-analyzer, current file: none) bash-$

  2. GLM Assistant

    Let me explore the repository structure to understand what we're working with.

  3. GLM Assistant

    This is rust-analyzer. The issue is about macro path completion inside macros. When you have foo!(bar$0) where foo! expands to $i!(), the completion at the cursor position should suggest bar as a macro path completion.

    Let me search for the completion code related to macros.

  4. GLM Assistant

    Let me look at the directory structure.

  5. GLM Assistant

    Let me understand the issue better. The problem is that when you're inside a macro call foo!(bar$0) where foo! expands to $i!(), the completion should suggest bar as a macro. But the cursor is in a position where the $i is being used as a macro path ($i!()).

    Let me look at how macro completion paths work. Let me look at the context analysis to understand how the path context is determined.

  6. GLM Assistant

    Let me look at the key part of the expansion logic - around line 379-420 where fn-like macro calls are expanded.

  7. GLM Assistant

    Let me look at the full expand function to understand how it picks the best expansion.

  8. GLM Assistant

    Let me look at the analyze function more carefully, specifically how it handles the macro call path context.

  9. GLM Assistant

    Let me look at existing completion tests for macros.

  10. GLM Assistant

    Let me look at the completes_in_macro_body test which seems more relevant.

  11. GLM Assistant

    Let me create a test to reproduce the issue. Let me write a quick test using the test infrastructure.

  12. GLM Assistant

    Let me create a temporary test to reproduce the issue.

  13. GLM Assistant

    I need to set up a toolchain. Let me try installing one.

  14. GLM Assistant

    The repo needs nightly. Let me check what's needed. The rust-version is 1.95. Let me try building with stable.

  15. GLM Assistant

    The test passes with the empty expect, which confirms the issue: completion returns nothing. Now let me understand the issue deeper. Let me check what the COMPLETION_MARKER is and look at the expansion logic.

  16. GLM Assistant

    So the fake ident raCompletionMarker is inserted at the cursor position. In our case, foo!(bar$0) becomes foo!(barraCompletionMarker). Then the macro foo is called with $i:ident. The $i would be barraCompletionMarker as a single ident. Then the expansion would be barraCompletionMarker!().

    So the speculative expansion would map the barraCompletionMarker token to the barraCompletionMarker token in the expansion barraCompletionMarker!(). The fake_mapped_token text contains COMPLETION_MARKER.

    Then expand_maybe_stop is called recursively. In the recursion, the offset would be at barraCompletionMarker in the expansion. The expansion barraCompletionMarker!() is a macro call. The cursor is on the path barraCompletionMarker. Now we try to expand further - is barraCompletionMarker a macro call? No, barraCompletionMarker!() is a macro call but the macro barraCompletionMarker doesn't exist (it's not bar). So sema.expand_macro_call would fail. So the recursive expand returns None.

    Then we check the base case - is the token inside a macro call? The token barraCompletionMarker in the expansion IS inside a macro call (the barraCompletionMarker!() macro call itself - well, the path is the macro being called, but the token is the path of the macro call). Hmm.

    Actually, is_inside_macro_call - let me check what that does. The token is part of the macro call barraCompletionMarker!(). Is it "inside" the macro call? The path of the macro call might not be considered "inside" the macro call. Let me check.

  17. GLM Assistant

    So is_inside_macro_call checks if any parent ancestor is a MacroCall. In our case, the token barraCompletionMarker is the path of the macro call barraCompletionMarker!(). The path's parent is the MacroCall node. So is_inside_macro_call returns true.

    So in the base case check, the original token (in the actual expansion bar!()) is the path bar of the macro call bar!(). is_inside_macro_call returns true because the parent is a MacroCall. So the base case fails, and expand_maybe_stop returns None.

    So the whole expansion returns None, and we fall back to the non-expanded context. Let me look at the part after expansion that determines what to do.

  18. GLM Assistant

    So the issue is clear. The expansion of foo!(bar) -> bar!() succeeds, but then when we try to recurse/stop, the base case check fails because bar (in the expansion bar!()) is considered "inside a macro call" (because it's the path of the bar!() macro call).

    The problem is the is_inside_macro_call check. When the cursor is on the path of a macro call (i.e., we're completing the macro name itself), we shouldn't consider that as "inside a macro call" - we should stop and provide completions.

    Let me check the usages of is_inside_macro_call.

  19. GLM Assistant

    Let me look at the search usage to see if it would be affected.

  20. GLM Assistant

    In search, is_inside_macro_call is used to determine whether to descend into macros. Let me understand the structure of MacroCall better.

  21. GLM Assistant

    Now I understand the structure. The MacroCall has a path() (the macro name) and token_tree() (the input). The path is the macro being invoked, NOT the input. The is_inside_macro_call semantics should be: a token is inside a macro call if it's part of the input (token tree) that gets expanded, NOT if it's the path (macro name).

    Let me look at the is_inside_macro_call implementation more carefully and modify it.

  22. GLM Assistant

    The doc comment explicitly says: "we strictly check if it lies inside the input of a macro call." The path (macro name) is NOT the input. So the current implementation has a bug - it returns true for the path of a macro call, contradicting the documented intent. Let me fix is_inside_macro_call.

  23. GLM Assistant

    Now let me make the fix to is_inside_macro_call:

  24. GLM Assistant

    Now let me build and test the reproduction.

  25. GLM Assistant

    The fix works - now bar is completed. Let me see the full output.

  26. GLM Assistant

    The fix works - ma bar macro_rules! bar now appears. Let me remove the temporary test and run the relevant test suites to check for regressions.

  27. GLM Assistant

    All ide-completion tests pass. Now let me run the search and references tests since is_inside_macro_call is also used there.

  28. GLM Assistant

    All ide tests pass. Let me also run the hir tests to be safe.

  29. GLM Assistant

    Now let me add a proper regression test. Let me look at the existing completion test for macros to add one in the right place.

  30. GLM Assistant

    Let me add a regression test after completes_in_simple_macro_call.

  31. GLM Assistant

    The test passes. Let me verify a few edge cases with temporary tests.

  32. GLM Assistant

    Both passed but with empty expect - let me check if they actually produce completions. The qualified case foo!(self::bar$0) won't expand since $i:ident only matches a single identifier. Let me investigate the nested case more.

  33. GLM Assistant

    Let me investigate the nested case more - I expected it to produce completions. Let me check the actual output.

  34. GLM Assistant

    Let me change the expect to a non-empty value to see the actual output for edge2.

  35. GLM Assistant

    Wait, it passed even with expect![[r#"X"#]]? That means the actual was also "X"? No, that doesn't make sense. Oh wait, maybe the sed didn't replace correctly. Let me check.

  36. GLM Assistant

    The sed produced a duplicate. Let me clean this up - let me remove all the temp tests.

  37. GLM Assistant

    I reverted everything. Let me investigate the nested case first with a clean temporary test before adding the final regression test.

  38. GLM Assistant

    Let me test the single-level outer!(bar$0) case (outer expands $i!() = bar!()) which is a cleaner test.

  39. GLM Assistant

    outer!(bar$0) works. Now let me re-test the nested case cleanly.

  40. GLM Assistant

    Let me check if the nested case was already returning empty before my fix (i.e., it's a pre-existing limitation, not a regression).

  41. GLM Assistant

    The stash removed the test too. Let me do this differently - keep the test but revert only the semantics.rs change.

  42. GLM Assistant

    The nested case returns empty even WITHOUT my fix - it's a pre-existing limitation, not a regression. Let me confirm outer!(bar$0) was also empty before my fix (to confirm my fix enables it).

  43. GLM Assistant

    outer!(bar$0) was empty before my fix, returns completions after. My fix enables both. The nested case is a pre-existing separate limitation. Let me clean up and add the proper regression test.

  44. GLM Assistant

    The regression test passes. Now let me run the full ide-completion, ide-db, and ide test suites once more to confirm no regressions.

  45. GLM Assistant

    All tests pass. Let me run a few more relevant crates that involve macro semantics to be thorough.

  46. GLM Assistant

    All pass. Let me review the final diff.

  47. GLM Assistant

    Let me remove the specific issue number reference since I don't actually know it.

  48. GLM Assistant

    Let me remove the unnecessary .clone() since cast consumes the owned node and we return immediately.

  49. GLM Assistant

    The clone is necessary because ancestor is used later when cast returns None. Let me restore it.

  50. GLM Assistant

    The clippy error is in the syntax crate, unrelated to my change. Let me verify it's pre-existing.

  51. GLM Assistant

    The clippy error is in crates/syntax/src/fuzz.rs - a pre-existing issue with the newer clippy version (1.97.0), completely unrelated to my change. My change is fine.

    Let me do a final review of the complete diff.

  52. GLM Assistant

    The fix is complete. Let me verify there are no leftover temp files and run one final confirmation of the relevant test suites.

  53. GLM Assistant

    Let me run the final confirmation of the full test suites.

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