diff options
| author | Guillaume Gomez <guillaume.gomez@huawei.com> | 2022-01-13 15:50:11 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume.gomez@huawei.com> | 2022-01-20 22:05:25 +0100 |
| commit | 682ef4db8060a0b26266cdcf5631c5776cd45e0a (patch) | |
| tree | 9f529b6b110b51a1adfd2e8d47c8e038c9bf23bd | |
| parent | 74fbbefea8d13683cca5eee62e4740706cb3144a (diff) | |
| download | rust-682ef4db8060a0b26266cdcf5631c5776cd45e0a.tar.gz rust-682ef4db8060a0b26266cdcf5631c5776cd45e0a.zip | |
Exclude "test" from doc_auto_cfg rendering
| -rw-r--r-- | src/librustdoc/clean/cfg.rs | 61 | ||||
| -rw-r--r-- | src/librustdoc/clean/types.rs | 5 |
2 files changed, 50 insertions, 16 deletions
diff --git a/src/librustdoc/clean/cfg.rs b/src/librustdoc/clean/cfg.rs index dfee2b702c1..2c1dcad1afc 100644 --- a/src/librustdoc/clean/cfg.rs +++ b/src/librustdoc/clean/cfg.rs @@ -43,23 +43,22 @@ crate struct InvalidCfgError { impl Cfg { /// Parses a `NestedMetaItem` into a `Cfg`. - fn parse_nested(nested_cfg: &NestedMetaItem) -> Result<Cfg, InvalidCfgError> { + fn parse_nested( + nested_cfg: &NestedMetaItem, + exclude: &[Symbol], + ) -> Result<Option<Cfg>, InvalidCfgError> { match nested_cfg { - NestedMetaItem::MetaItem(ref cfg) => Cfg::parse(cfg), + NestedMetaItem::MetaItem(ref cfg) => Cfg::parse_without(cfg, exclude), NestedMetaItem::Literal(ref lit) => { Err(InvalidCfgError { msg: "unexpected literal", span: lit.span }) } } } - /// Parses a `MetaItem` into a `Cfg`. - /// - /// The `MetaItem` should be the content of the `#[cfg(...)]`, e.g., `unix` or - /// `target_os = "redox"`. - /// - /// If the content is not properly formatted, it will return an error indicating what and where - /// the error is. - crate fn parse(cfg: &MetaItem) -> Result<Cfg, InvalidCfgError> { + crate fn parse_without( + cfg: &MetaItem, + exclude: &[Symbol], + ) -> Result<Option<Cfg>, InvalidCfgError> { let name = match cfg.ident() { Some(ident) => ident.name, None => { @@ -70,9 +69,21 @@ impl Cfg { } }; match cfg.kind { - MetaItemKind::Word => Ok(Cfg::Cfg(name, None)), + MetaItemKind::Word => { + if exclude.contains(&name) { + Ok(None) + } else { + Ok(Some(Cfg::Cfg(name, None))) + } + } MetaItemKind::NameValue(ref lit) => match lit.kind { - LitKind::Str(value, _) => Ok(Cfg::Cfg(name, Some(value))), + LitKind::Str(value, _) => { + if exclude.contains(&name) { + Ok(None) + } else { + Ok(Some(Cfg::Cfg(name, Some(value)))) + } + } _ => Err(InvalidCfgError { // FIXME: if the main #[cfg] syntax decided to support non-string literals, // this should be changed as well. @@ -81,23 +92,43 @@ impl Cfg { }), }, MetaItemKind::List(ref items) => { - let mut sub_cfgs = items.iter().map(Cfg::parse_nested); - match name { + let sub_cfgs = items.iter().filter_map(|i| match Cfg::parse_nested(i, exclude) { + Ok(Some(c)) => Some(Ok(c)), + Err(e) => Some(Err(e)), + _ => None, + }); + let ret = match name { sym::all => sub_cfgs.fold(Ok(Cfg::True), |x, y| Ok(x? & y?)), sym::any => sub_cfgs.fold(Ok(Cfg::False), |x, y| Ok(x? | y?)), sym::not => { + let mut sub_cfgs = sub_cfgs.collect::<Vec<_>>(); if sub_cfgs.len() == 1 { - Ok(!sub_cfgs.next().unwrap()?) + Ok(!sub_cfgs.pop().unwrap()?) } else { Err(InvalidCfgError { msg: "expected 1 cfg-pattern", span: cfg.span }) } } _ => Err(InvalidCfgError { msg: "invalid predicate", span: cfg.span }), + }; + match ret { + Ok(c) => Ok(Some(c)), + Err(e) => Err(e), } } } } + /// Parses a `MetaItem` into a `Cfg`. + /// + /// The `MetaItem` should be the content of the `#[cfg(...)]`, e.g., `unix` or + /// `target_os = "redox"`. + /// + /// If the content is not properly formatted, it will return an error indicating what and where + /// the error is. + crate fn parse(cfg: &MetaItem) -> Result<Cfg, InvalidCfgError> { + Self::parse_without(cfg, &[]).map(|ret| ret.unwrap()) + } + /// Checks whether the given configuration can be matched in the current session. /// /// Equivalent to `attr::cfg_matches`. diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index fac1a0817e0..347f9d0a47c 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -831,7 +831,10 @@ impl AttributesExt for [ast::Attribute] { self.iter() .filter(|attr| attr.has_name(sym::cfg)) .filter_map(|attr| single(attr.meta_item_list()?)) - .filter_map(|attr| Cfg::parse(attr.meta_item()?).ok()) + .filter_map(|attr| match Cfg::parse_without(attr.meta_item()?, &[sym::test]) { + Ok(Some(c)) => Some(c), + _ => None, + }) .filter(|cfg| !hidden_cfg.contains(cfg)) .fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg) } else { |
