Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Procedural macros stringify inputs with macro invocations too aggressively #50840

Closed
alexcrichton opened this issue May 17, 2018 · 3 comments
Closed
Labels
A-macros-1.2 Area: Declarative macros 1.2 A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug.

Comments

@alexcrichton
Copy link
Member

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

@alexcrichton alexcrichton added the A-macros-2.0 Area: Declarative macros 2.0 (#39412) label May 17, 2018
@alexcrichton
Copy link
Member Author

The cause of this bug is described here

@alexcrichton
Copy link
Member Author

This is also caused by #43081, but we can fix it without fixing all of #43081

@alexcrichton
Copy link
Member Author

A fix for this should be included with #50971

alexcrichton added a commit to alexcrichton/rust that referenced this issue May 22, 2018
This commit updates the `Mac_` AST structure to keep track of the delimiters
that it originally had for its invocation. This allows us to faithfully
pretty-print macro invocations not using parentheses (e.g. `vec![...]`). This in
turn helps procedural macros due to rust-lang#43081.

Closes rust-lang#50840
@alexcrichton alexcrichton added C-bug Category: This is a bug. A-macros-1.2 Area: Declarative macros 1.2 labels May 22, 2018
bors added a commit that referenced this issue May 24, 2018
rustc: Correctly pretty-print macro delimiters

This commit updates the `Mac_` AST structure to keep track of the delimiters
that it originally had for its invocation. This allows us to faithfully
pretty-print macro invocations not using parentheses (e.g. `vec![...]`). This in
turn helps procedural macros due to #43081.

Closes #50840
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros-1.2 Area: Declarative macros 1.2 A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

1 participant