Rollup merge of #82682 - petrochenkov:cfgeval, r=Aaron1011
Implement built-in attribute macro `#[cfg_eval]` + some refactoring This PR implements a built-in attribute macro `#[cfg_eval]` as it was suggested in https://github.com/rust-lang/rust/pull/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 https://github.com/rust-lang/rust/pull/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: https://github.com/rust-lang/rust/issues/82679
This commit is contained in:
@@ -81,8 +81,22 @@ impl AstLike for Annotatable {
|
||||
}
|
||||
}
|
||||
|
||||
fn finalize_tokens(&mut self, tokens: LazyTokenStream) {
|
||||
panic!("Called finalize_tokens on an Annotatable: {:?}", tokens);
|
||||
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
|
||||
match self {
|
||||
Annotatable::Item(item) => item.tokens_mut(),
|
||||
Annotatable::TraitItem(trait_item) => trait_item.tokens_mut(),
|
||||
Annotatable::ImplItem(impl_item) => impl_item.tokens_mut(),
|
||||
Annotatable::ForeignItem(foreign_item) => foreign_item.tokens_mut(),
|
||||
Annotatable::Stmt(stmt) => stmt.tokens_mut(),
|
||||
Annotatable::Expr(expr) => expr.tokens_mut(),
|
||||
Annotatable::Arm(arm) => arm.tokens_mut(),
|
||||
Annotatable::Field(field) => field.tokens_mut(),
|
||||
Annotatable::FieldPat(fp) => fp.tokens_mut(),
|
||||
Annotatable::GenericParam(gp) => gp.tokens_mut(),
|
||||
Annotatable::Param(p) => p.tokens_mut(),
|
||||
Annotatable::StructField(sf) => sf.tokens_mut(),
|
||||
Annotatable::Variant(v) => v.tokens_mut(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user