diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2021-03-06 21:33:02 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2021-03-06 23:03:19 +0300 |
| commit | 5dad6c25751f577c53bd36d09f033e1c245d0806 (patch) | |
| tree | 5a5269785f864cdd6f1742f9cfcd5e0444e6427e /compiler/rustc_builtin_macros/src | |
| parent | 069e612e73f3fabb1184d9df009ea064118fffd8 (diff) | |
| download | rust-5dad6c25751f577c53bd36d09f033e1c245d0806.tar.gz rust-5dad6c25751f577c53bd36d09f033e1c245d0806.zip | |
Implement built-in attribute macro `#[cfg_eval]`
Diffstat (limited to 'compiler/rustc_builtin_macros/src')
| -rw-r--r-- | compiler/rustc_builtin_macros/src/cfg_eval.rs | 29 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/lib.rs | 2 |
2 files changed, 31 insertions, 0 deletions
diff --git a/compiler/rustc_builtin_macros/src/cfg_eval.rs b/compiler/rustc_builtin_macros/src/cfg_eval.rs new file mode 100644 index 00000000000..805f9d0634c --- /dev/null +++ b/compiler/rustc_builtin_macros/src/cfg_eval.rs @@ -0,0 +1,29 @@ +use crate::util::check_builtin_macro_attribute; + +use rustc_ast::{self as ast, AstLike}; +use rustc_expand::base::{Annotatable, ExtCtxt}; +use rustc_expand::config::StripUnconfigured; +use rustc_span::symbol::sym; +use rustc_span::Span; + +pub fn expand( + ecx: &mut ExtCtxt<'_>, + _span: Span, + meta_item: &ast::MetaItem, + item: Annotatable, +) -> Vec<Annotatable> { + check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval); + + let mut visitor = + StripUnconfigured { sess: ecx.sess, features: ecx.ecfg.features, modified: false }; + let mut item = visitor.fully_configure(item); + if visitor.modified { + // Erase the tokens if cfg-stripping modified the item + // This will cause us to synthesize fake tokens + // when `nt_to_tokenstream` is called on this item. + if let Some(tokens) = item.tokens_mut() { + *tokens = None; + } + } + vec![item] +} diff --git a/compiler/rustc_builtin_macros/src/lib.rs b/compiler/rustc_builtin_macros/src/lib.rs index 9a3c914337c..1017b23e567 100644 --- a/compiler/rustc_builtin_macros/src/lib.rs +++ b/compiler/rustc_builtin_macros/src/lib.rs @@ -24,6 +24,7 @@ mod asm; mod assert; mod cfg; mod cfg_accessible; +mod cfg_eval; mod compile_error; mod concat; mod concat_idents; @@ -89,6 +90,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) { register_attr! { bench: test::expand_bench, cfg_accessible: cfg_accessible::Expander, + cfg_eval: cfg_eval::expand, derive: derive::Expander, global_allocator: global_allocator::expand, test: test::expand_test, |
