Allow passing expr metavariable as cfg predicate#146961
Allow passing expr metavariable as cfg predicate#146961rust-bors[bot] merged 2 commits intorust-lang:mainfrom
expr metavariable as cfg predicate#146961Conversation
|
Some changes occurred in compiler/rustc_attr_parsing |
|
r? @fee1-dead rustbot has assigned @fee1-dead. Use |
This comment has been minimized.
This comment has been minimized.
1cd49c1 to
10f76de
Compare
|
Just as an update, after #124141 we effectively no longer have nonterminal tokens in the grammar (*). I hoped that some time after #124141 either @nnethercote or me relax these rules in many cases, when there are no backward compatibility concerns at least, and pass the change through lang team, but it didn't happen yet. A number of current hacks like accepting @Jules-Bertholet You could very well start the same process, just from a different point and accept any token streams that look like cfg predicates as cfg predicates, from any matchers, not just (*) Or we technically have, but only for compatibility and to avoid extending the language without the lang team process. |
|
As for the current implementation, the new |
|
This seems reasonable to me. It allows all @rfcbot merge |
|
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
|
@rfcbot reviewed |
1 similar comment
|
@rfcbot reviewed |
This comment was marked as resolved.
This comment was marked as resolved.
10f76de to
856c691
Compare
|
The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease |
This comment has been minimized.
This comment has been minimized.
856c691 to
7f83c78
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready This should now be confined to affecting |
expr metavariable to cfgexpr metavariable as cfg predicate
|
r? @JonathanBrouwer (Why does it all converge to |
|
@rustbot ready |
…anBrouwer
Allow passing `expr` metavariable as `cfg` predicate
This PR allows expanding `expr` metavariables inside the configuration predicates of `cfg` and `cfg_attr` invocations.
For example, the following code will now compile:
```rust
macro_rules! mac {
($e:expr) => {
#[cfg_attr($e, inline)]
#[cfg($e)]
fn func() {}
#[cfg(not($e))]
fn func() {
panic!()
}
}
}
mac!(any(unix, feature = "foo"));
```
There is currently no `macro_rules` fragment specifier that can represent all valid `cfg` predicates. `meta` comes closest, but excludes `true` and `false`. By fixing that, this change makes it easier to write declarative macros that parse `cfg` or `cfg_attr` invocations, for example rust-lang#146281.
@rustbot label T-lang needs-fcp A-attributes A-cfg A-macros
…uwer Rollup of 6 pull requests Successful merges: - #154154 (Emit fewer errors for incorrect rtn and `=` -> `:` typos in bindings) - #154155 (tests/ui/async-await/drop-option-future.rs: New regression test) - #146961 (Allow passing `expr` metavariable as `cfg` predicate) - #154118 (don't suggest non-deriveable traits for unions) - #154120 (Start migrating `DecorateDiagCompat::Builtin` items to `DecorateDiagCompat::Dynamic`) - #154156 (Moving issue-52049 to borrowck)
…uwer Rollup of 6 pull requests Successful merges: - #154154 (Emit fewer errors for incorrect rtn and `=` -> `:` typos in bindings) - #154155 (tests/ui/async-await/drop-option-future.rs: New regression test) - #146961 (Allow passing `expr` metavariable as `cfg` predicate) - #154118 (don't suggest non-deriveable traits for unions) - #154120 (Start migrating `DecorateDiagCompat::Builtin` items to `DecorateDiagCompat::Dynamic`) - #154156 (Moving issue-52049 to borrowck)
View all comments
This PR allows expanding
exprmetavariables inside the configuration predicates ofcfgandcfg_attrinvocations.For example, the following code will now compile:
There is currently no
macro_rulesfragment specifier that can represent all validcfgpredicates.metacomes closest, but excludestrueandfalse. By fixing that, this change makes it easier to write declarative macros that parsecfgorcfg_attrinvocations, for example #146281.@rustbot label T-lang needs-fcp A-attributes A-cfg A-macros