Skip to content

Commit 6cab02c

Browse files
committed
simplify gated cfgs logic
1 parent 8ad4d15 commit 6cab02c

File tree

4 files changed

+32
-51
lines changed

4 files changed

+32
-51
lines changed

src/librustc_driver/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ use std::time::Instant;
6262

6363
use syntax::ast;
6464
use syntax::source_map::FileLoader;
65-
use syntax::feature_gate::{GatedCfg, UnstableFeatures};
66-
use syntax::symbol::sym;
67-
use syntax_pos::{DUMMY_SP, FileName};
65+
use syntax::feature_gate::{find_gated_cfg, UnstableFeatures};
66+
use syntax_pos::symbol::sym;
67+
use syntax_pos::FileName;
6868

6969
pub mod pretty;
7070
mod args;
@@ -677,12 +677,6 @@ impl RustcDefaultCalls {
677677
.is_nightly_build();
678678

679679
let mut cfgs = sess.parse_sess.config.iter().filter_map(|&(name, ref value)| {
680-
let gated_cfg = GatedCfg::gate(&ast::MetaItem {
681-
path: ast::Path::from_ident(ast::Ident::with_dummy_span(name)),
682-
kind: ast::MetaItemKind::Word,
683-
span: DUMMY_SP,
684-
});
685-
686680
// Note that crt-static is a specially recognized cfg
687681
// directive that's printed out here as part of
688682
// rust-lang/rust#37406, but in general the
@@ -693,10 +687,11 @@ impl RustcDefaultCalls {
693687
// through to build scripts.
694688
let value = value.as_ref().map(|s| s.as_str());
695689
let value = value.as_ref().map(|s| s.as_ref());
696-
if name != sym::target_feature || value != Some("crt-static") {
697-
if !allow_unstable_cfg && gated_cfg.is_some() {
698-
return None
699-
}
690+
if (name != sym::target_feature || value != Some("crt-static"))
691+
&& !allow_unstable_cfg
692+
&& find_gated_cfg(|cfg_sym| cfg_sym == name).is_some()
693+
{
694+
return None;
700695
}
701696

702697
if let Some(value) = value {

src/libsyntax/attr/builtin.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Parsing and validation of builtin attributes
22
33
use crate::ast::{self, Attribute, MetaItem, NestedMetaItem};
4+
use crate::feature_gate::{find_gated_cfg, emit_feature_err, GatedCfg, GateIssue};
45
use crate::print::pprust;
5-
use crate::feature_gate::GatedCfg;
66
use crate::sess::ParseSess;
77

88
use errors::{Applicability, Handler};
@@ -531,8 +531,9 @@ pub fn find_crate_name(attrs: &[Attribute]) -> Option<Symbol> {
531531
/// Tests if a cfg-pattern matches the cfg set
532532
pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool {
533533
eval_condition(cfg, sess, &mut |cfg| {
534-
if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
535-
gated_cfg.check_and_emit(sess, feats);
534+
let gate = find_gated_cfg(|sym| cfg.check_name(sym));
535+
if let (Some(feats), Some(gated_cfg)) = (features, gate) {
536+
gate_cfg(&gated_cfg, cfg.span, sess, feats);
536537
}
537538
let error = |span, msg| { sess.span_diagnostic.span_err(span, msg); true };
538539
if cfg.path.segments.len() != 1 {
@@ -561,12 +562,21 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
561562
})
562563
}
563564

565+
fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &Features) {
566+
let (cfg, feature, has_feature) = gated_cfg;
567+
if !has_feature(features) && !cfg_span.allows_unstable(*feature) {
568+
let explain = format!("`cfg({})` is experimental and subject to change", cfg);
569+
emit_feature_err(sess, *feature, cfg_span, GateIssue::Language, &explain);
570+
}
571+
}
572+
564573
/// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
565574
/// evaluate individual items.
566-
pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
567-
-> bool
568-
where F: FnMut(&ast::MetaItem) -> bool
569-
{
575+
pub fn eval_condition(
576+
cfg: &ast::MetaItem,
577+
sess: &ParseSess,
578+
eval: &mut impl FnMut(&ast::MetaItem) -> bool,
579+
) -> bool {
570580
match cfg.kind {
571581
ast::MetaItemKind::List(ref mis) => {
572582
for mi in mis.iter() {

src/libsyntax/feature_gate/builtin_attrs.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
use AttributeType::*;
44
use AttributeGate::*;
55

6-
use super::check::{emit_feature_err, GateIssue};
76
use super::check::{EXPLAIN_ALLOW_INTERNAL_UNSAFE, EXPLAIN_ALLOW_INTERNAL_UNSTABLE};
87
use rustc_feature::{Features, Stability};
98

109
use crate::ast;
11-
use crate::sess::ParseSess;
1210

1311
use syntax_pos::symbol::{Symbol, sym};
14-
use syntax_pos::Span;
1512
use rustc_data_structures::fx::FxHashMap;
1613
use lazy_static::lazy_static;
1714

18-
1915
type GateFn = fn(&Features) -> bool;
2016

2117
macro_rules! cfg_fn {
@@ -24,39 +20,19 @@ macro_rules! cfg_fn {
2420
}
2521
}
2622

23+
pub type GatedCfg = (Symbol, Symbol, GateFn);
24+
2725
/// `cfg(...)`'s that are feature gated.
28-
const GATED_CFGS: &[(Symbol, Symbol, GateFn)] = &[
26+
const GATED_CFGS: &[GatedCfg] = &[
2927
// (name in cfg, feature, function to check if the feature is enabled)
3028
(sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
3129
(sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
3230
(sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
3331
];
3432

35-
#[derive(Debug)]
36-
pub struct GatedCfg {
37-
span: Span,
38-
index: usize,
39-
}
40-
41-
impl GatedCfg {
42-
pub fn gate(cfg: &ast::MetaItem) -> Option<GatedCfg> {
43-
GATED_CFGS.iter()
44-
.position(|info| cfg.check_name(info.0))
45-
.map(|idx| {
46-
GatedCfg {
47-
span: cfg.span,
48-
index: idx
49-
}
50-
})
51-
}
52-
53-
pub fn check_and_emit(&self, sess: &ParseSess, features: &Features) {
54-
let (cfg, feature, has_feature) = GATED_CFGS[self.index];
55-
if !has_feature(features) && !self.span.allows_unstable(feature) {
56-
let explain = format!("`cfg({})` is experimental and subject to change", cfg);
57-
emit_feature_err(sess, feature, self.span, GateIssue::Language, &explain);
58-
}
59-
}
33+
/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.
34+
pub fn find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg> {
35+
GATED_CFGS.iter().find(|(cfg_sym, ..)| pred(*cfg_sym))
6036
}
6137

6238
// If you change this, please modify `src/doc/unstable-book` as well. You must

src/libsyntax/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub mod feature_gate {
101101
};
102102
mod builtin_attrs;
103103
pub use builtin_attrs::{
104-
AttributeGate, AttributeTemplate, AttributeType, GatedCfg,
104+
AttributeGate, AttributeTemplate, AttributeType, find_gated_cfg, GatedCfg,
105105
BuiltinAttribute, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
106106
deprecated_attributes, is_builtin_attr, is_builtin_attr_name,
107107
};

0 commit comments

Comments
 (0)