Skip to content

Inconsistent tokens between the same struct defined inside vs outside of function #49604

Closed
@dtolnay

Description

@dtolnay

The following script reproduces the issue as of rustc 1.26.0-nightly (06fa27d 2018-04-01).

The repro crate defines two identical structs, S1 at the top level outside of a function and S2 inside of a function. The repro_derive crate prints the Debug representation of the first four input token trees. For both structs the first four token trees are #, [doc = "..."], #, [doc = "..."].

Pay attention to the tokens corresponding to the = signs that come after "doc". For S1 (defined outside of function), the first and second = tokens both say kind: Tree.

TokenStream {
    kind: Tree(
        Token(
            Span {/* ... */},
            Eq
        )
    )
},

But in S2 (defined within a function), the first = token is kind: Tree while the second = token is kind: JointTree. It would be good to understand what is causing the inconsistency between the tokenization of S1 vs S2.

TokenStream {
    kind: JointTree(
        Token(
            Span {/* ... */},
            Eq
        )
    )
},

Repro script

#!/bin/sh

cargo new --lib repro_derive
cargo new --lib repro

echo >repro_derive/src/lib.rs '
#![feature(proc_macro)]

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_derive(Repro, attributes(repro))]
pub fn derive_repro(input: TokenStream) -> TokenStream {
    for tt in input.into_iter().take(4) {
        println!("{:#?}", tt);
    }
    TokenStream::empty()
}
'

echo >>repro_derive/Cargo.toml '
[lib]
proc-macro = true
'

echo >repro/src/lib.rs '
#![allow(dead_code)]

#[macro_use]
extern crate repro_derive;

/// X1
/// Y1
#[repro]
#[derive(Repro)]
struct S1;

fn f() {
    /// X2
    /// Y2
    #[repro]
    #[derive(Repro)]
    struct S2;
}
'

echo >>repro/Cargo.toml '
repro_derive = { path = "../repro_derive" }
'

cargo build --manifest-path repro/Cargo.toml

@alexcrichton

Metadata

Metadata

Assignees

Labels

A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions