Skip to content

Leftover tokens error in valid macro defining macro #20936

@hayashi-stl

Description

@hayashi-stl

The code snippet for reproduction below contains a macro that rust-analyzer complains with leftover tokens about, specifically line 15, at [x, y].
The code compiles with cargo build, so the macro is valid. Using cargo expand results in:

#![feature(prelude_import)]
#[macro_use]
extern crate std;
#[prelude_import]
use std::prelude::rust_2024::*;
struct X {
    x: f64,
}
pub fn test() {
    let a = X { x: 0.0 };
    let u1 = 0.0;
    let u1 = 0.0;
}

as expected.

rust-analyzer doesn't complain if sep_let is called only with $($d:tt $var:ident),*s of the same length. So it doesn't complain for

sep_let!(($x, $y) => let u1: [x, y]);
sep_let!(($x, $y) => let u1: [x, y]);

rust-analyzer version: 0.3.2660-standalone

rustc version: rustc 1.90.0 (1159e78c4 2025-09-14)

editor or extension: VSCode rust-analyzer extension v0.3.2660

relevant settings: probably none

code snippet to reproduce:

macro_rules! sep_let {
    (($($d:tt $var:ident),*) => let $name:ident : [$($vals:tt)*] $(,)?) => {
        macro_rules! __mac {
            ($($d $var:ident),*) => { 0.0 };
        }

        let $name = __mac!($($vals)*);
    };
}

struct X { x: f64 }

pub fn test() {
    let a = X { x: 0.0 };
    sep_let!(($x, $y) => let u1: [x, y]);
    sep_let!(($x) => let u1: [x]);
}

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