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

#[derive(...)] doesn't invalidate tokens for nested items #81099

Open
petrochenkov opened this issue Jan 16, 2021 · 4 comments
Open

#[derive(...)] doesn't invalidate tokens for nested items #81099

petrochenkov opened this issue Jan 16, 2021 · 4 comments
Assignees
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug.

Comments

@petrochenkov
Copy link
Contributor

Reproduction:

// check-pass
// aux-build:test-macros.rs

#![feature(stmt_expr_attributes)]
#![feature(proc_macro_hygiene)]

#[macro_use]
extern crate test_macros;

#[derive(PartialEq, Eq)]
struct S {
    field: [u8; #[print_attr] {
        #[cfg(FALSE)] { 10 }
        #[cfg(not(FALSE))]  { 11 }
    }],
}

fn main() {}

#[derive] will fully configure the item S and remove #[cfg(FALSE)] { 10 } before outputting the item or passing it to PartialEq and Eq.
#[print_attr] however will print tokens for both cfg cases because its input tokens are not invalidated and are stale.

Fix:

  • #[derive] should walk the whole input item and invalidate tokens for all nested nodes, not only for the item itself.
  • Attribute macros should be ready to face input with no tokens and use CanSynthesizeMissingTokens::Yes.

cc @Aaron1011

@petrochenkov petrochenkov added the C-bug Category: This is a bug. label Jan 16, 2021
@Aaron1011 Aaron1011 self-assigned this Jan 16, 2021
@petrochenkov
Copy link
Contributor Author

The part about CanSynthesizeMissingTokens::Yes also becomes relevant with #79078, because once that PR lands #[my_attr] in

#[derive(MyTrait)]
#[my_attr]
struct S { ... }

should be ready to receive an item with invalidated tokens as an input.

@jonas-schievink jonas-schievink added the A-proc-macros Area: Procedural macros label Jan 16, 2021
@petrochenkov
Copy link
Contributor Author

I'm going to feature-gate macro attributes on nested nodes in #79078 because they are very similar to attribute macros after derive in the sense that they allow macros to observe derive's output, and the lang team's position is that it shouldn't be allowed on stable yet.
(It's unlikely to be used in practice, but beta crater runs will us is something breaks.)

@Aaron1011
Copy link
Member

#80689 will automatically fix the #[derive(MyTrait)] #[my_attr] case, since the AST node for the derive target will have its tokens cfg-stripped, and then passed to #[my_attr]. It will not directly fix the 'nested attribute' case, but will give us the infrastructure to do so (we'll want to run cfg-stripping for the tokens of all AST nodes, not just the top-level derive target).

@petrochenkov
Copy link
Contributor Author

#[cfg_eval] implemented in #82682 is also affected by this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants