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:
rustmacro_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 inspect the repository layout and locate macro/path completion code and nearby tests, then reproduce the missing completion through the project’s existing test conventions.
- Assistant
rgis unavailable, so I’ll usefind/grepto identify the completion implementation and macro-related fixtures. - Assistant
The likely failure is in constructing or classifying the completion context after speculative expansion of
foo!(bar…). I’ll read the pipeline and the macro-input token adjustment logic before adding a focused reproduction to the appropriate existing test module. - Assistant
The expansion code already maps the fake identifier into
$i!(), so the defect may be that the cursor is analyzed as the macro name (where only macros should be offered) but the completion dispatch does not handle that path context correctly. I’ll inspectexpand_maybe_stop, path classification, and existing tests for completion inside macro arguments. - Assistant
To reproduce without altering repository tests, I’ll inspect the test helper/API and build a temporary standalone harness if practical. I’ll also check repository history around this expansion code, since this may be a regression with a narrowly identifiable token-mapping cause.
- Assistant
There is already a no-assertion regression test for identifier forwarding, but not for forwarding into macro-call position. The newest “Less conjuration magic” change touched this exact file, so I’ll compare its parent to identify changed invariants, then use the rust-analyzer LSP binary (or a temporary out-of-tree harness) to observe the actual completion response.
- Assistant
The failure mechanism is now clear: after expanding
foo!, the real tree containsbar!()while the speculative tree containsbar__ra_completion!(). Resolving/expanding those macro calls cannot proceed, and the recursion base case rejects stopping because the token is “inside a macro call.” That rejection is correct for macro arguments but wrong for the macro call’s own path—the current level is exactly where path completion must run. I’ll adjust the base case to stop when the completion token lies in a macro-call path, while retaining recursive expansion for tokens in arguments. - Assistant
The source edit is rustfmt-compatible by inspection and
git diff --checkpassed, but no Rust toolchain is configured. I’ll inspect the pinned version and installed toolchains, then configure/install the repository-required toolchain so I can compile and test the fix. - Assistant
The repository pins a rustc commit rather than a channel and has no installed toolchain. I’ll install a minimal nightly toolchain; if the latest nightly is incompatible, I’ll derive the matching date from repository metadata or use the issue’s nightly date.
- Assistant
Nightly installed successfully, but the repository’s
rust-versionfile causes rustup to request the pinned commit as a custom toolchain. I’ll explicitly invokecargo +nightly/rustc +nightly, format, and compile the focused completion crate. - Assistant
The minimal profile omitted rustfmt. I’ll add it, then rerun formatting and the focused test.
- Assistant
The focused existing macro-expansion regression passes. To directly verify the reported fixture without touching repository tests, I’ll build an out-of-tree temporary Rust harness that uses the public completion API and fixture database setup, modeled on the project’s own helper.
- Assistant
Although a standalone completion harness is possible, it would require duplicating a large configuration surface. Since most dependencies are now built, I’ll build the actual server and send it an LSP completion request against a temporary Cargo project. This validates the full user-visible behavior without modifying or adding repository tests.
- Assistant
The server builds successfully. I’ll now create a disposable Cargo project and use a framed JSON-RPC script to initialize rust-analyzer, open the exact fixture, request completion immediately after
bar, and print whether thebarmacro appears. - Assistant
The full LSP request still returned no items, so either the fix’s path detection is not reached/matching or the request raced workspace loading. I’ll first eliminate the LSP timing possibility by waiting while servicing server requests/notifications and issue a control completion outside the macro; then I’ll instrument the local build if needed.
- Assistant
With workspace loading allowed to finish, the exact reported case now returns 134 completions including
bar. To conclusively reproduce the original defect, I’ll temporarily rebuild the server from the unmodified source, run the same LSP script expecting nobar, and then immediately restore/rebuild the fixed source. This changes no tests and will leave only the intended source diff. - Assistant
The same exact LSP request against baseline produced zero completions, confirming reproduction; the fixed binary includes
bar. I’ll add a concise rationale comment for this non-obvious base-case exception, then run the completeide-completiontest suite and a final exact LSP verification. - Assistant
All 732 completion tests pass. I’ll perform the final end-to-end request (the rebuilt fixed binary differs only by the subsequently added comment), inspect the final diff/status for accidental files, and then submit.
- Assistant
Checklist review:
- Reported error reproduced: Yes. Using the exact macro fixture through an LSP completion request, the baseline server returned 0 completion items and no
bar. - Fix confirmed: Yes. The same end-to-end request against the fixed server returned 134 items, including the
barmacro. - Edge cases considered: Yes. The exception is restricted to tokens contained by a macro call’s path. Completion inside macro arguments still follows the existing recursive expansion behavior, and ancestor macro calls do not match because their path range does not contain an argument token.
- Affected existing tests run: Yes.
cargo +nightly test -p ide-completion --no-fail-fastpassed all 732 tests. The server and completion crate also compiled successfully; only a pre-existing unrelated unused-import warning appeared.
The final diff contains one source-file change, is formatted, and passes
git diff --check. I’ll submit it now. - Reported error reproduced: Yes. Using the exact macro fixture through an LSP completion request, the baseline server returned 0 completion items and no
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.