Skip to content
TheChatArchiveThe Chat Archive
x-ai/grok-4.5· 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. Assistant

    I'll start by exploring the repository structure and understanding the issue about macro path completion inside macros.

  3. Assistant

    Let me search for existing tests and code related to macro path completion.

  4. Assistant

    rg is not available, let me use grep.

  5. Assistant

    There's a macro_def.rs - but the issue is about completing macro paths inside macro invocations. Let me look at path completions and how macros work with tt (token trees).

  6. Assistant

    Let me look at how path completion works and how macros in token trees are handled. The issue mentions completing a path that's an argument to a macro where the metavar is $i:ident and it's used as ...

  7. Assistant

    Let me look at how completion context is determined for macros and look at similar issues or tests related to completing inside macros.

  8. Assistant

    There's a test complete_missing_macro_arg and related special tests. Let me look at those and also understand how the path kind is determined when inside a macro.

  9. Assistant

    The issue is specifically about completing a macro path when it's used inside another macro that uses it with !(). Let me look at how completion works inside macros and how the expected type/kind is dete...

  10. Assistant

    Let me look at how completion gets the expected context when inside macro args. There's likely some logic that looks at what fragment kinds are expected.

  11. Assistant

    Let me look at the analysis of what happens for macro arguments and search for related issues or PR discussions. Also look at git history for similar fixes.

  12. Assistant

    So the expansion happens via speculative expansion. When the user writes foo!(bar), and the macro is $i:ident => $i!(), the expansion would try to expand to bar!(). Let me try ...

  13. Assistant

    Let me look at special.rs tests related to macros and also how expression completions handle macros.

  14. Assistant

    There's in_macro_expr_frag test. Let me look at that.

  15. Assistant

    I'll write a quick test to reproduce the issue and see what currently happens with completion.

  16. Assistant

    Let me add a temporary test case to reproduce the issue.

  17. Assistant

    Let me add a temporary test and run it. Also I should look more carefully at what analysis does when expansion succeeds for a incomplete macro path.

  18. Assistant

    Let me add a temporary test at the end of the file.

  19. Assistant

    I'll add a temporary test at the end.

  20. Assistant

    There's a problem - I replaced the last closing brace but I may have broken the previous test. Looking at the file, line 1769 shows } which is correct and I added after it. Good.

    But wait - the original last line was } at line 1769, and my edit said to replace 1769:1769, so it should have replaced the last } of fn_genericعمله_params_const_param_snippet - oh no, looking at the output:

    1768:    );
    1769:}
    1770:
    1771:#[test]
    

    Actually it looks correct - the } of fn_generic_params is there. Wait, looking at my edit:

    edit 1769:1769 << 'EOF'
    }
    
    #[test]
    ...
    EOF
    

    So I replaced line 1769 } with }\n\n#[test]... which is correct.

    Let me run the test.

  21. Assistant

    Need to set up rustup.

  22. Assistant

    There's a rust-version of 1.95. Let me try running the test.

  23. Assistant

    So completion returns nothing. This is the bug. When the macro expands to $i!(), the path should be in a MacroCall path position. Let me dig into why completion is empty.

  24. Assistant

    When we have:

    macro_rules! foo { ($i:ident) => { $i!() }; }
    foo!(bar$array);
    

    The expansion would be bar!(). But wait - the completion marker replaces theidentity - when the original expands successfully and the path is a real macro, the path is consumed and disappears from the expansion.

  25. Assistant

    So the recursion:

    1. Expand foo!(bar) -> actual expands to bar!() which then becomes the new file, but wait - expand_macro_call expands one level?

    2. Looking at expand - if expand succeeds for foo, it recursively calls expand_maybe_stop on the expansion which is bar!() with completion marker somehow.

    But bar!() as a MacroCall would attempt to expand further. When bar!() expands, the resulting tokens wouldn't contain the completion marker (since bar is completed to a real macro that expands to ()), so...

    Looking at what happens:

    // After expanding foo!(bar):
    // actual: bar!()
    // speculative: bar__ra_completion_marker__()  or something
    

    When trying to expand the MacroCall bar!(), actual expands successfully to () (no marker). Speculative might expand if bar__ra_completion_marker__!() is also somehow valid, or fail.

    Actually the path identity is different - fake path might be barra_complete or whatever the COMPLETION_MARKER is.

    Let me look more carefully.

  26. Assistant

    So COMPLETION_MARKER is "raCompletionMarker". The speculative expansion has barraCompletionMarker as the identifier path used as $i!() so it becomes barraCompletionMarker!(). When original bar!()...

  27. Assistant

    Looking at kind_macro_call, it looks at the parent of MacroCall. When MacroCall occurs... at item position of expansion? Looking at the expansion of foo!(bar) - $i!() expands somewhere?

  28. Assistant

    So is_inside_macro_call checks if any ancestor is MacroCall. For the token bar in expansion bar!(), the ancestors include Path, MacroCall - so yes, MacroCall is an ancestor! So is_inside_macro_call returns true for the path of a MacroCall itself!

    That means:

    1. Expand foo! -> get bar!()
    2. Trying to expand bar!() - actual expands to (), speculative fails (no barraCompletionMarker macro)
    3. expand returns None
    4. expand_maybe_stop checks is_inside_macro_call which is true for MacroCall path token
    5. Returns None!
    6. Completion fails entirely

    That's the bug! When we're positioned on a macro path itself (because the identifier was used as $i!()), we're considered "inside a macro call" and the base case returns None.

  29. Assistant

    Looking at the expand flow more carefully when we're on MacroCall path after first expansion:

    After expand foos successfully:

    • original_file is expansion of bar!() which is... wait, expand_macro_call returns the full expansion of foo which is bar!() as tokens, but are those re-parsed as MacroCall?

    When expand tries again on bar!():

    let orig_tt = ancestors_at_offset(&original_file.value, original_offset)
        .map_while(Either::<ast::TokenTree, ast::Meta>::cast)
        .last()?;
    

    Is the path token inside a TokenTree? In bar!(), the path is outside the token tree. The packed MacroCall structure is:

    MacroCall
      Path (bar)
      bang (!)
      TokenTree (empty)
    

    So the cursor is on Path, and ancestors wouldn't include TokenTree. So orig_tt would be None, expand returns None via ?.

    Then expand_maybe_stop:

    if token_at_offset_ignore_whitespace(...).is_some_and(|original_token| {
        !sema.is_inside_macro_call(...)
    })
    

    The original_token's ancestors include MacroCall, so is_inside_macro_call is true, so we return None!

    ── That's confirmed as the bug.

    The fix: In expand_maybe_stop, we should also stop (accept the expansion result) when the token is ont the path of a MacroCall (not inside its arguments). Or more generally: when the token is not inside the input/token tree of a macro.

  30. Assistant

    Looking at search.rs - if we fix is_inside_macro_call to not consider the macro path itself as "inside", that matches the docs which say "inside the input of a macro call". The path is not input; the TokenTree is. Let me verify and fix it.

  31. Assistant

    Let me look at MacroCall definition:

  32. Assistant

    I'll fix is_inside_macro_call so paths of macro calls aren't treated as inside the macro input.

  33. Assistant

    The edit went to the wrong file — I need to revert that and apply the fix to semantics.rs instead.

  34. Assistant

    Good, now I can edit the right file.

  35. Assistant

    There's an extra blank line before the next method but that's fine. Let me also check if special.rs change is still there and run the test.

  36. Assistant

    We're making progress! Now we get completions. The macros are rendered without !() because has_macro_bang is true from the expansion — which is correct for source since the bang comes from $i!(). Let me check filtering and refine the test.

  37. Assistant

    So when has_macro_bang is true, we still get all expr completions. Let me check how normal completion foo$0!() looks:

  38. Assistant

    There's a transfer issue with has_macro_bang - when the bang is only in the expanded form but not in the original source, we shouldn't set has_macro_bang, or rather has_macro_bang meaning "bang already written in source" is wrong when bang only exists in expansion.

    Wait - currently for our case: actual expansion shows has_macro_bang=true because we complete on স্ bar!() path But when editing original foo!(bar$0), inserting just bar is correct! Inserting bar! would be wrong for source.

    So the current behavior of labeling as ma_bar without bang and inserting just the name is CORRECT for the issueiled case. Let me verify normal completion and update the test.

  39. Assistant

    Need to open the file and go边上 that line first.

  40. Assistant

    Let me replace the test.

  41. Assistant

    When the prefix는 is ba (not a complete match for an existing macro), does the actual expansion of foo!(ba) succeed? Our analysis suggests yes — let's run the tests.

  42. Assistant

    The test passes. Now let me run related tests to make sure nothing is broken - especially search tests and completion tests involving macros.

  43. Assistant

    All ide-completion tests pass. Let me also run ide-db tests (search) and hir tests.

  44. Assistant

    Let me also check if there are tests that specifically test is_inside_macro_call or search for references of macros. And also run ide tests related to macros.

  45. Assistant

    All tests pass. Let me also think about more edge cases and verify searchbehavior when on tenga macro path remains correct.

  46. Assistant

    For macro references - the Find All References test already passed. Let me review the final diff and clean up.

  47. Assistant

    The instructions say not to add new tests — I need to remove the test I added before submitting.

  48. Assistant

    I'll remove thetest I added and keep only the fix.

  49. Assistant

    Good. Let me verify the fix still works with a temporary test script rather than adding a test to the repo.

  50. Assistant

    Good, verified. Remove the test again.

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