rust-lang / rust-lang/rust-analyzer

Wrong macro expansion

Open
#20,480 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-macro C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

I wrote a macro that compiles flawlessly. Then I tested it with the new r-a trait solver, and was surprised to see it failing.

After some investigation, I found out that the macro expansion seems to cut off early.

rust-analyzer version: rust-analyzer version: 0.4.2579-standalone (b2a58b8c6e 2025-08-12) [c:\Users\Stefnotch.vscode\extensions\rust-lang.rust-analyzer-0.4.2579-win32-x64\server\rust-analyzer.exe]

rustc version: rustc 1.91.0-nightly (2e2642e64 2025-08-16)

editor or extension: VSCode, current nightly version of r-a

relevant settings:

"rust-analyzer.diagnostics.experimental.enable": true,

code snippet to reproduce:

The following code compiles. However, r-a complains expected Box<dyn DemoTrait, Global>, found Box<ThisDoesNot, Global>rust-analyzer E0308

pub trait DemoTrait {}

pub struct ThisWorks;
pub struct ThisDoesNot;

macro_rules! mod_items {
    ( $( Foobar $( $ast:ty)? ),+ ) => {
        $(
        $(impl DemoTrait for $ast {})+
        )?
    };
}

fn testo() {
    let a: Box<dyn DemoTrait> = Box::new(ThisWorks);
    let a: Box<dyn DemoTrait> = Box::new(ThisDoesNot);
}

mod_items! {
  Foobar ThisWorks,
  Foobar ThisDoesNot
}

When looking at the macro expansion (F1 expand macro recursively), one only gets

// Recursive expansion of mod_items! macro
// ========================================

impl DemoTrait for ThisWorks {}

what expansion was expected

// Recursive expansion of mod_items! macro
// ========================================

impl DemoTrait for ThisWorks {}
impl DemoTrait for ThisDoesNot {}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the supplied macro and rust-analyzer nightly, then use the F1 “expand macro recursively” entry point to inspect why only the first impl appears. Compare the expansion with the two expected impls and verify that both generated items are shown while the diagnostic no longer reports ThisDoesNot as missing the trait.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.