Skip to content

Wrong macro expansion #20480

@stefnotch

Description

@stefnotch

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 (b2a58b8 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 (2e2642e 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 {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macromacro expansionC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions