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

Inconsistency between accessing field of braced struct vs tuple struct in proc macro #47312

Closed
dtolnay opened this issue Jan 10, 2018 · 1 comment · Fixed by #48083
Closed
Assignees
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Jan 10, 2018

If a procedural macro generates an access to a named struct field like self.x then the . may be either def_site or call_site and it works either way. But if accessing an unnamed tuple struct field like self.0 then it only works if . is call_site.

I believe it should work either way in either case.

#![feature(proc_macro)]

extern crate proc_macro;
use proc_macro::{TokenStream, TokenTree, TokenNode, Spacing, Span};

#[proc_macro]
pub fn field(input: TokenStream) -> TokenStream {
    let mut iter = input.into_iter();
    let struct_name = iter.next().unwrap();
    let field_name = iter.next().unwrap();
    assert!(iter.next().is_none());

    let dot = TokenTree {
        // Call_site works for both braced structs and tuple structs.
        // Def_site works for braced structs but not tuple structs.
        span: Span::def_site(),
        kind: TokenNode::Op('.', Spacing::Alone),
    };
    vec![
        struct_name,
        dot,
        field_name,
    ].into_iter().collect()
}
#![feature(proc_macro)]

extern crate mac;

struct S {
    a: u8,
}

struct T(u8);

fn main() {
    let s = S { a: 0 };
    println!("{:?}", mac::field!(s a));

    let t = T(0);
    println!("{:?}", mac::field!(t 0));
}
error[E0611]: field `0` of tuple-struct `T` is private
  --> src/main.rs:55:34
   |
55 |     println!("{:?}", mac::field!(t 0));
   |                                  ^^^

@jseyfried

@jseyfried jseyfried self-assigned this Jan 10, 2018
@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-macros-2.0 Area: Declarative macros 2.0 (#39412) labels Feb 6, 2018
@jseyfried
Copy link
Contributor

@dtolnay Sorry for the delay!
Fixed in #48083.

kennytm added a commit to kennytm/rust that referenced this issue Feb 13, 2018
…d_access_hygiene, r=petrochenkov

Improve tuple struct field access hygiene

Fixes rust-lang#47312 by fixing a span bug.
r? @nrc
Manishearth added a commit to Manishearth/rust that referenced this issue Feb 19, 2018
…d_access_hygiene, r=petrochenkov

Improve tuple struct field access hygiene

Fixes rust-lang#47312 by fixing a span bug.
r? @nrc
Manishearth added a commit to Manishearth/rust that referenced this issue Feb 23, 2018
…d_access_hygiene, r=petrochenkov

Improve tuple struct field access hygiene

Fixes rust-lang#47312 by fixing a span bug.
r? @nrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants