Extra cfg_hide a bit to handle inner cfgs

This commit is contained in:
Guillaume Gomez 2022-01-16 21:03:16 +01:00
parent fd005f53c2
commit caec4a23f2
4 changed files with 11 additions and 16 deletions

View File

@ -8,6 +8,7 @@
use std::ops; use std::ops;
use rustc_ast::{LitKind, MetaItem, MetaItemKind, NestedMetaItem}; use rustc_ast::{LitKind, MetaItem, MetaItemKind, NestedMetaItem};
use rustc_data_structures::fx::FxHashSet;
use rustc_feature::Features; use rustc_feature::Features;
use rustc_session::parse::ParseSess; use rustc_session::parse::ParseSess;
use rustc_span::symbol::{sym, Symbol}; use rustc_span::symbol::{sym, Symbol};
@ -45,7 +46,7 @@ impl Cfg {
/// Parses a `NestedMetaItem` into a `Cfg`. /// Parses a `NestedMetaItem` into a `Cfg`.
fn parse_nested( fn parse_nested(
nested_cfg: &NestedMetaItem, nested_cfg: &NestedMetaItem,
exclude: &[Symbol], exclude: &FxHashSet<Cfg>,
) -> Result<Option<Cfg>, InvalidCfgError> { ) -> Result<Option<Cfg>, InvalidCfgError> {
match nested_cfg { match nested_cfg {
NestedMetaItem::MetaItem(ref cfg) => Cfg::parse_without(cfg, exclude), NestedMetaItem::MetaItem(ref cfg) => Cfg::parse_without(cfg, exclude),
@ -57,7 +58,7 @@ fn parse_nested(
crate fn parse_without( crate fn parse_without(
cfg: &MetaItem, cfg: &MetaItem,
exclude: &[Symbol], exclude: &FxHashSet<Cfg>,
) -> Result<Option<Cfg>, InvalidCfgError> { ) -> Result<Option<Cfg>, InvalidCfgError> {
let name = match cfg.ident() { let name = match cfg.ident() {
Some(ident) => ident.name, Some(ident) => ident.name,
@ -70,19 +71,13 @@ fn parse_nested(
}; };
match cfg.kind { match cfg.kind {
MetaItemKind::Word => { MetaItemKind::Word => {
if exclude.contains(&name) { let cfg = Cfg::Cfg(name, None);
Ok(None) if exclude.contains(&cfg) { Ok(None) } else { Ok(Some(cfg)) }
} else {
Ok(Some(Cfg::Cfg(name, None)))
}
} }
MetaItemKind::NameValue(ref lit) => match lit.kind { MetaItemKind::NameValue(ref lit) => match lit.kind {
LitKind::Str(value, _) => { LitKind::Str(value, _) => {
if exclude.contains(&name) { let cfg = Cfg::Cfg(name, Some(value));
Ok(None) if exclude.contains(&cfg) { Ok(None) } else { Ok(Some(cfg)) }
} else {
Ok(Some(Cfg::Cfg(name, Some(value))))
}
} }
_ => Err(InvalidCfgError { _ => Err(InvalidCfgError {
// FIXME: if the main #[cfg] syntax decided to support non-string literals, // FIXME: if the main #[cfg] syntax decided to support non-string literals,
@ -126,7 +121,7 @@ fn parse_nested(
/// If the content is not properly formatted, it will return an error indicating what and where /// If the content is not properly formatted, it will return an error indicating what and where
/// the error is. /// the error is.
crate fn parse(cfg: &MetaItem) -> Result<Cfg, InvalidCfgError> { crate fn parse(cfg: &MetaItem) -> Result<Cfg, InvalidCfgError> {
Self::parse_without(cfg, &[]).map(|ret| ret.unwrap()) Self::parse_without(cfg, &FxHashSet::default()).map(|ret| ret.unwrap())
} }
/// Checks whether the given configuration can be matched in the current session. /// Checks whether the given configuration can be matched in the current session.

View File

@ -831,11 +831,10 @@ fn single<T: IntoIterator>(it: T) -> Option<T::Item> {
self.iter() self.iter()
.filter(|attr| attr.has_name(sym::cfg)) .filter(|attr| attr.has_name(sym::cfg))
.filter_map(|attr| single(attr.meta_item_list()?)) .filter_map(|attr| single(attr.meta_item_list()?))
.filter_map(|attr| match Cfg::parse_without(attr.meta_item()?, &[sym::test]) { .filter_map(|attr| match Cfg::parse_without(attr.meta_item()?, hidden_cfg) {
Ok(Some(c)) => Some(c), Ok(Some(c)) => Some(c),
_ => None, _ => None,
}) })
.filter(|cfg| !hidden_cfg.contains(cfg))
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg) .fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
} else { } else {
Cfg::True Cfg::True

View File

@ -141,6 +141,7 @@ fn store_path(&mut self, did: DefId) {
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
}) })
.chain([Cfg::Cfg(sym::test, None)].into_iter())
.collect(); .collect();
self.cx.cache.exact_paths = self.exact_paths; self.cx.cache.exact_paths = self.exact_paths;

View File

@ -26,7 +26,7 @@
// @has 'oud/struct.Oystercatcher.html' // @has 'oud/struct.Oystercatcher.html'
// @count - '//*[@class="stab portability"]' 1 // @count - '//*[@class="stab portability"]' 1
// @matches - '//*[@class="stab portability"]' 'crate features solecism and oystercatcher' // @matches - '//*[@class="stab portability"]' 'crate feature oystercatcher only'
// compile-flags:--cfg feature="oystercatcher" // compile-flags:--cfg feature="oystercatcher"
#[cfg(all(feature = "solecism", feature = "oystercatcher"))] #[cfg(all(feature = "solecism", feature = "oystercatcher"))]
pub struct Oystercatcher; pub struct Oystercatcher;