Skip to content

proc_macro::quote does not work with interpolated constant inside repetition #146603

@dtolnay

Description

@dtolnay

A constant N can be interpolated as $N inside quote, but not inside a repetition. I believe this should be supported.

#![feature(proc_macro_quote)]

use proc_macro::{quote, TokenStream};

const N: usize = 8;

#[proc_macro]
pub fn repro(_input: TokenStream) -> TokenStream {
    let ok = '?';
    let array = ['0', '1', '2'];
    let tokens = quote! {
        // Okay
        ... $ok $N ...

        // Okay
        $($ok $array)*

        // Fail
        $($N $array)*
    };
    eprintln!("{}", tokens);
    TokenStream::new()
}
error[E0530]: let bindings cannot shadow constants
  --> src/lib.rs:19:12
   |
 5 | const N: usize = 8;
   | ------------------- the constant `N` is defined here
...
19 |         $($N $array)*
   |            ^ cannot be named the same as a constant

Expected output:

... '?' 8usize ... '?' '0' '?' '1' '?' '2' 8usize '0' 8usize '1' 8usize '2'

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.F-proc_macro_quote`#![feature(proc_macro_quote)]`T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions