ISSUE DESCRIPTION Cannot complete macro path in macro <!-- Troubleshooting guide:…
- 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); }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: /rust-analyzer, current file: none) bash-$
- Assistant
I’ll first inspect the repository layout and search for macro/path completion code and relevant completion tests. Then I’ll reproduce the missing completion with the project’s test conventions before implementing the narrowest general fix.
- Assistant
rgis unavailable, so I’ll usefind/grepto locate the completion implementation and test fixtures. - Assistant
The issue aligns with the dedicated
macro_def::complete_macro_segmentpath and nearby tests inspecial.rs. I’ll read that implementation, its dispatch conditions, and existing macro-token completion cases to identify whether the failure is in context construction or candidate filtering. - Assistant
The existing simple macro-call test proves ordinary path completion survives macro expansion, but it only expects functions. The reported case passes an
identthrough expansion into a macro-call path ($i!()), so I’ll trace name-reference classification and expression/path completion filtering, especially how the expected namespace is inferred after expansion. - Assistant
Expression completion already admits function-like macros, so the likely problem is that a path which is syntactically the callee of
!is classified specially or discarded during speculative macro expansion. I’ll inspect that classification logic and the expansion mapping around macro calls. - Assistant
The classifier sets
has_macro_bangand maps a macro call to expression/item/pattern/type context, which should allow macros. I need the full parent-match sequence and, more importantly, the expansion algorithm’s success criteria: the speculative invocationfoo!(bar__ra_completion)likely no longer matches$i:ident, while the original does, causing expansion states to diverge. - Assistant
The failure mechanism is now clear: after expanding
foo!, the mapped cursor sits in the generatedbar!()path. Recursive expansion then refuses because the original path isbarwhile the speculative path contains the completion marker; the fallback also refuses because it sees the cursor “inside a macro call.” The correct recursion base is to stop at a macro-call path (which is itself the completion target), while still expanding when the cursor is in macro arguments. I’ll implement that syntactic distinction inexpand_maybe_stop. - Assistant
The source change is formatted by construction and passes
git diff --check, but this checkout has no rustup default, so Cargo could not run. I’ll inspect the pinned Rust version and installed toolchains, select/install the appropriate one if available, then compile and run the focused completion tests. - Assistant
No toolchain is installed. I’ll install the repository’s pinned compiler with the minimal profile; this is necessary to verify both API compatibility (
contains_range) and behavior through the existing completion suite. - Assistant
The pin is a raw compiler commit, which stock rustup does not accept as a channel name. I’ll install current nightly as the closest compatible compiler (the project likely uses unstable features), then compile the affected crate.
- Assistant
Nightly installed successfully, but the chained post-install invocation hit an environment/wrapper lookup issue (“rustup is not installed at
/root/.cargo”). I’ll inspect executable and rustup paths, then invoke the toolchain binaries directly if needed. - Assistant
The shell has an empty
HOME, while rustup installed under/root; I’ll explicitly setHOME=/rootfor tool invocations, add rustfmt, format, and run the focused completion tests. The existingspecialmodule includes macro-expansion completion coverage and should catch regressions in this recursion logic. - Assistant
The focused suite passes (54/54), including nested/recursive macro completion. To verify the exact report without altering any tests, I’ll use the public LSP completion endpoint against a temporary Cargo project. I’ll build both the baseline and fixed analyzer by temporarily reversing only the source patch, then compare whether the completion response contains
bar; the patch will be restored immediately afterward. - Assistant
Both binaries are built and the patch is restored cleanly. I’ll now run a small temporary JSON-RPC client against each binary, opening the exact snippet and requesting completion immediately after
bar. It will report the completion labels (and specifically whetherbaris present), then shut each server down. - Assistant
Both responses were empty, so the first LSP probe may be racing workspace loading or the fix may not reach the intended recursion base. I’ll distinguish those possibilities by probing a direct
barprefix completion and waiting for rust-analyzer’sexperimental/serverStatusto becomequiescentbefore requesting completion. I’ll also compare direct macro-path and expanded macro-path cases. - Assistant
This client capability set does not receive the server-status notification, so waiting for it timed out. I’ll replace that wait with a short startup delay; the response reader will still correctly skip intervening notifications.
- Assistant
The exact A/B reproduction now confirms the fix: baseline returns no completions, while the patched analyzer returns
bar(along with other valid names). A direct macro-path completion also remains functional. I’ll run the entireide-completioncrate test suite, then inspect the final diff/status for accidental files or formatting issues.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.