diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-11-30 01:57:53 +0100 |
|---|---|---|
| committer | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-11-30 01:57:53 +0100 |
| commit | 6cab02cf149787db6c9e30ee0ceed97768bbe9aa (patch) | |
| tree | 69ca7363f283a7a6be0251062f76956dfd2bc4e5 /src/libsyntax/feature_gate | |
| parent | 8ad4d15f3888f1339d52632d40e0a47697dd2a24 (diff) | |
| download | rust-6cab02cf149787db6c9e30ee0ceed97768bbe9aa.tar.gz rust-6cab02cf149787db6c9e30ee0ceed97768bbe9aa.zip | |
simplify gated cfgs logic
Diffstat (limited to 'src/libsyntax/feature_gate')
| -rw-r--r-- | src/libsyntax/feature_gate/builtin_attrs.rs | 36 |
1 files changed, 6 insertions, 30 deletions
diff --git a/src/libsyntax/feature_gate/builtin_attrs.rs b/src/libsyntax/feature_gate/builtin_attrs.rs index 7435b2e056a..36916de57fc 100644 --- a/src/libsyntax/feature_gate/builtin_attrs.rs +++ b/src/libsyntax/feature_gate/builtin_attrs.rs @@ -3,19 +3,15 @@ use AttributeType::*; use AttributeGate::*; -use super::check::{emit_feature_err, GateIssue}; use super::check::{EXPLAIN_ALLOW_INTERNAL_UNSAFE, EXPLAIN_ALLOW_INTERNAL_UNSTABLE}; use rustc_feature::{Features, Stability}; use crate::ast; -use crate::sess::ParseSess; use syntax_pos::symbol::{Symbol, sym}; -use syntax_pos::Span; use rustc_data_structures::fx::FxHashMap; use lazy_static::lazy_static; - type GateFn = fn(&Features) -> bool; macro_rules! cfg_fn { @@ -24,39 +20,19 @@ macro_rules! cfg_fn { } } +pub type GatedCfg = (Symbol, Symbol, GateFn); + /// `cfg(...)`'s that are feature gated. -const GATED_CFGS: &[(Symbol, Symbol, GateFn)] = &[ +const GATED_CFGS: &[GatedCfg] = &[ // (name in cfg, feature, function to check if the feature is enabled) (sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)), (sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), (sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), ]; -#[derive(Debug)] -pub struct GatedCfg { - span: Span, - index: usize, -} - -impl GatedCfg { - pub fn gate(cfg: &ast::MetaItem) -> Option<GatedCfg> { - GATED_CFGS.iter() - .position(|info| cfg.check_name(info.0)) - .map(|idx| { - GatedCfg { - span: cfg.span, - index: idx - } - }) - } - - pub fn check_and_emit(&self, sess: &ParseSess, features: &Features) { - let (cfg, feature, has_feature) = GATED_CFGS[self.index]; - if !has_feature(features) && !self.span.allows_unstable(feature) { - let explain = format!("`cfg({})` is experimental and subject to change", cfg); - emit_feature_err(sess, feature, self.span, GateIssue::Language, &explain); - } - } +/// Find a gated cfg determined by the `pred`icate which is given the cfg's name. +pub fn find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg> { + GATED_CFGS.iter().find(|(cfg_sym, ..)| pred(*cfg_sym)) } // If you change this, please modify `src/doc/unstable-book` as well. You must |
