Skip to content

Inconsistency between Display and IntoIterator for a TokenStream containing a module #47627

Closed
@dtolnay

Description

@dtolnay

The following crate reproduces the issue. Given an attribute macro on a mod item like #[...] mod module;, iterating over the tokens of the TokenStream produces mod module; but invoking to_string() on the TokenStream produces mod module { /* the contents */ }. I don't know which one is correct but they should be consistent.

@jseyfried


Cargo.toml

[package]
name = "repro"
version = "0.0.0"
publish = false

[lib]
proc-macro = true

src/main.rs

#![feature(proc_macro)]

extern crate repro;

#[repro::print]
mod module;

fn main() {}

src/module.rs

pub struct S;

src/lib.rs

#![feature(proc_macro)]

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn print(_args: TokenStream, input: TokenStream) -> TokenStream {
    println!("{}", input);
    for tt in input {
        println!("{:?}", tt.kind);
    }
    TokenStream::empty()
}

Output

mod module {
    pub struct S;
}
Term(Term(mod))
Term(Term(module))
Op(';', Alone)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler 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