Skip to content

Procedural macros stringify inputs with macro invocations too aggressively #50840

Closed
@alexcrichton

Description

@alexcrichton

Given this procedural macro:

#![feature(proc_macro)]

extern crate proc_macro;
use proc_macro::*;

#[proc_macro_attribute]
pub fn doit(_: TokenStream, input: TokenStream) -> TokenStream {
    input.into_iter().collect()
}

and this invocation:

#![feature(proc_macro)]

extern crate bar;
use bar::doit;

macro_rules! id {
    ($($t:tt)*) => ($($t)*)
}

#[doit]
id![static X: u32 = 'a';];


fn main() {}

It generates:

$ rustc +nightly bar.rs --crate-type proc-macro && rustc +nightly foo.rs -L .
error[E0308]: mismatched types

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

Not a great error message!

If the delimiters on the macro are changed from [] to () you get:

error[E0308]: mismatched types
  --> foo.rs:11:21
   |
11 | id!(static X: u32 = 'a';);
   |                     ^^^ expected u32, found char

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

A much better error message!

This issue is forked off #50700

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-1-2Area: Declarative macros 1.2A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions