Cannot complete macro path in macro <!-- Troubleshooting guide:…
- User
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); }Interface
No new interfaces are introduced.
- Assistant
Investigating how rust-analyzer handles macro-path completion inside macro invocations.
- Assistant
Reproducing the issue and searching for related tests.
- Assistant
Identifying the issue: inserting the completion marker merges into the ident (
bar→barraCompletionMarker), breaking macro expansion. Checking how to detect macro-call token-tree context and adjust marker insertion. - Assistant
Implementing the fix: skip merging the completion marker into identifiers inside macro-call token trees, and allow expansion token mapping without the marker.
- Assistant
The inner macro can't expand further, but we should still stop at the macro path. Updating
expand_maybe_stopto handle that case: - Assistant
The expanded macro uses
NAME_REF, notPathSegmentdirectly. Updating the detection logic. - Assistant
Removing debug code and running broader tests.
- Assistant
The
expand_maybe_stopchange is too broad. Restricting the macro-path early stop to when we skipped the completion marker. - Assistant
Switching to a fallback retry: always insert the marker first, then retry without it when analysis fails for a sole-ident macro argument.
- Assistant
Fixed macro-path completion inside macro invocations.
Root cause
When completing inside a macro argument like
foo!(bar$0), rust-analyzer inserts a fake identifier (raCompletionMarker) to drive speculative expansion. That marker was appended tobar, producingbarraCompletionMarker. The:identmetavar then expanded tobarraCompletionMarker!()instead ofbar!(), so completion context analysis failed and no completions were offered.Fix
Three changes in the completion context analysis:
-
Fallback retry — If analysis with the completion marker fails and the cursor is on the only identifier in a macro-call argument (e.g.
foo!(bar$0)), retry without inserting the marker so expansion staysbar!(). -
Macro-path stop condition — When expanding without a marker, allow stopping inside a macro call if the cursor is on the macro path (including
NameRefnodes in expanded code). -
Token mapping — When no marker is used, don’t require mapped tokens to contain .
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.