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

Implement built-in attribute macro #[cfg_eval] + some refactoring #82682

Merged
merged 5 commits into from
Mar 8, 2021

Conversation

petrochenkov
Copy link
Contributor

@petrochenkov petrochenkov commented Mar 1, 2021

This PR implements a built-in attribute macro #[cfg_eval] as it was suggested in #79078 to avoid #[derive()] without arguments being abused as a way to configure input for other attributes.

The macro is used for eagerly expanding all #[cfg] and #[cfg_attr] attributes in its input ("fully configuring" the input).
The effect is identical to effect of #[derive(Foo, Bar)] which also fully configures its input before passing it to macros Foo and Bar, but unlike #[derive] #[cfg_eval] can be applied to any syntax nodes supporting macro attributes, not only certain items.

cfg_eval was the first name suggested in #79078, but other alternatives are also possible, e.g. cfg_expand.

#[cfg_eval]
#[my_attr] // Receives `struct S {}` as input, the field is configured away by `#[cfg_eval]`
struct S {
    #[cfg(FALSE)]
    field: u8,
}

Tracking issue: #82679

@rust-highfive
Copy link
Collaborator

r? @varkor

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 1, 2021
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 1, 2021
@petrochenkov petrochenkov changed the title [WIP] Implement built-in attribute macro #[cfg_eval] Implement built-in attribute macro #[cfg_eval] + some refactoring Mar 6, 2021
@petrochenkov
Copy link
Contributor Author

petrochenkov commented Mar 6, 2021

This should be ready for review.

  • The first commit makes resetting tokens in cfg_eval/derive simpler.
  • The second commit implements #[cfg_eval] itself.
  • The last three commits are refactorings of the cfg eval logic.

r? @Aaron1011

@rust-highfive rust-highfive assigned Aaron1011 and unassigned varkor Mar 6, 2021
@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 6, 2021
@@ -1452,6 +1452,18 @@ pub(crate) mod builtin {
/* compiler built-in */
}

/// Expands all `#[cfg]` and `#[cfg_attr]` attributes in the code fragment it's applied to.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add an example here prior to stabilization, but it doesn't need to block this PR.

@Aaron1011
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Mar 7, 2021

📌 Commit 5d27728 has been approved by Aaron1011

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 7, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 8, 2021
Implement built-in attribute macro `#[cfg_eval]` + some refactoring

This PR implements a built-in attribute macro `#[cfg_eval]` as it was suggested in rust-lang#79078 to avoid `#[derive()]` without arguments being abused as a way to configure input for other attributes.

The macro is used for eagerly expanding all `#[cfg]` and `#[cfg_attr]` attributes in its input ("fully configuring" the input).
The effect is identical to effect of `#[derive(Foo, Bar)]` which also fully configures its input before passing it to macros `Foo` and `Bar`, but unlike `#[derive]` `#[cfg_eval]` can be applied to any syntax nodes supporting macro attributes, not only certain items.

`cfg_eval` was the first name suggested in rust-lang#79078, but other alternatives are also possible, e.g. `cfg_expand`.

```rust
#[cfg_eval]
#[my_attr] // Receives `struct S {}` as input, the field is configured away by `#[cfg_eval]`
struct S {
    #[cfg(FALSE)]
    field: u8,
}
```

Tracking issue: rust-lang#82679
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 8, 2021
Implement built-in attribute macro `#[cfg_eval]` + some refactoring

This PR implements a built-in attribute macro `#[cfg_eval]` as it was suggested in rust-lang#79078 to avoid `#[derive()]` without arguments being abused as a way to configure input for other attributes.

The macro is used for eagerly expanding all `#[cfg]` and `#[cfg_attr]` attributes in its input ("fully configuring" the input).
The effect is identical to effect of `#[derive(Foo, Bar)]` which also fully configures its input before passing it to macros `Foo` and `Bar`, but unlike `#[derive]` `#[cfg_eval]` can be applied to any syntax nodes supporting macro attributes, not only certain items.

`cfg_eval` was the first name suggested in rust-lang#79078, but other alternatives are also possible, e.g. `cfg_expand`.

```rust
#[cfg_eval]
#[my_attr] // Receives `struct S {}` as input, the field is configured away by `#[cfg_eval]`
struct S {
    #[cfg(FALSE)]
    field: u8,
}
```

Tracking issue: rust-lang#82679
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 8, 2021
Implement built-in attribute macro `#[cfg_eval]` + some refactoring

This PR implements a built-in attribute macro `#[cfg_eval]` as it was suggested in rust-lang#79078 to avoid `#[derive()]` without arguments being abused as a way to configure input for other attributes.

The macro is used for eagerly expanding all `#[cfg]` and `#[cfg_attr]` attributes in its input ("fully configuring" the input).
The effect is identical to effect of `#[derive(Foo, Bar)]` which also fully configures its input before passing it to macros `Foo` and `Bar`, but unlike `#[derive]` `#[cfg_eval]` can be applied to any syntax nodes supporting macro attributes, not only certain items.

`cfg_eval` was the first name suggested in rust-lang#79078, but other alternatives are also possible, e.g. `cfg_expand`.

```rust
#[cfg_eval]
#[my_attr] // Receives `struct S {}` as input, the field is configured away by `#[cfg_eval]`
struct S {
    #[cfg(FALSE)]
    field: u8,
}
```

Tracking issue: rust-lang#82679
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 8, 2021
Implement built-in attribute macro `#[cfg_eval]` + some refactoring

This PR implements a built-in attribute macro `#[cfg_eval]` as it was suggested in rust-lang#79078 to avoid `#[derive()]` without arguments being abused as a way to configure input for other attributes.

The macro is used for eagerly expanding all `#[cfg]` and `#[cfg_attr]` attributes in its input ("fully configuring" the input).
The effect is identical to effect of `#[derive(Foo, Bar)]` which also fully configures its input before passing it to macros `Foo` and `Bar`, but unlike `#[derive]` `#[cfg_eval]` can be applied to any syntax nodes supporting macro attributes, not only certain items.

`cfg_eval` was the first name suggested in rust-lang#79078, but other alternatives are also possible, e.g. `cfg_expand`.

```rust
#[cfg_eval]
#[my_attr] // Receives `struct S {}` as input, the field is configured away by `#[cfg_eval]`
struct S {
    #[cfg(FALSE)]
    field: u8,
}
```

Tracking issue: rust-lang#82679
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 8, 2021
Rollup of 10 pull requests

Successful merges:

 - rust-lang#82047 (bypass auto_da_alloc for metadata files)
 - rust-lang#82415 (expand: Refactor module loading)
 - rust-lang#82557 (Add natvis for Result, NonNull, CString, CStr, and Cow)
 - rust-lang#82613 (Remove Item::kind, use tagged enum. Rename variants to match)
 - rust-lang#82642 (Fix jemalloc usage on OSX)
 - rust-lang#82682 (Implement built-in attribute macro `#[cfg_eval]` + some refactoring)
 - rust-lang#82684 (Disable destination propagation on all mir-opt-levels)
 - rust-lang#82755 (Refactor confirm_builtin_call, remove partial if)
 - rust-lang#82857 (Edit ructc_ast_lowering docs)
 - rust-lang#82862 (Generalize Write impl for Vec<u8> to Vec<u8, A>)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9c31057 into rust-lang:master Mar 8, 2021
@cuviper cuviper added this to the 1.52.0 milestone Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants