From 09c76884bc6ba1957dc6d71d79b797de2cc40b2d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 6 Oct 2021 20:06:33 +0200 Subject: [PATCH] Simplify AttributesExt::cfg function and remove error emissions since they are not useful --- src/librustdoc/clean/types.rs | 37 ++++++++---------------------- src/test/rustdoc-ui/doc-cfg.rs | 9 ++++++++ src/test/rustdoc-ui/doc-cfg.stderr | 26 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 src/test/rustdoc-ui/doc-cfg.rs create mode 100644 src/test/rustdoc-ui/doc-cfg.stderr diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 09cb99dbf22..5b722175f98 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -776,48 +776,31 @@ impl AttributesExt for [ast::Attribute] { let sess = tcx.sess; let doc_cfg_active = tcx.features().doc_cfg; - trait SingleExt { - type Item; - fn single(self) -> Option; - } - - impl SingleExt for T { - type Item = T::Item; - fn single(self) -> Option { - let mut iter = self.into_iter(); - let item = iter.next()?; - if iter.next().is_some() { - return None; - } - Some(item) + fn single(it: T) -> Option { + let mut iter = it.into_iter(); + let item = iter.next()?; + if iter.next().is_some() { + return None; } + Some(item) } let mut cfg = if doc_cfg_active { let mut doc_cfg = self .iter() .filter(|attr| attr.has_name(sym::doc)) - .filter_map(|attr| Some(attr.meta_item_list()?.single()?)) + .flat_map(|attr| attr.meta_item_list().unwrap_or_else(Vec::new)) .filter(|attr| attr.has_name(sym::cfg)) - .filter_map(|attr| Some(attr.meta_item_list()?.single()?.meta_item()?.clone())) .peekable(); if doc_cfg.peek().is_some() { doc_cfg - .filter_map(|attr| { - Cfg::parse(&attr) - .map_err(|e| sess.diagnostic().span_err(e.span, e.msg)) - .ok() - }) + .filter_map(|attr| Cfg::parse(attr.meta_item()?).ok()) .fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg) } else { self.iter() .filter(|attr| attr.has_name(sym::cfg)) - .filter_map(|attr| Some(attr.meta_item_list()?.single()?.meta_item()?.clone())) - .filter_map(|attr| { - Cfg::parse(&attr) - .map_err(|e| sess.diagnostic().span_err(e.span, e.msg)) - .ok() - }) + .filter_map(|attr| single(attr.meta_item_list()?)) + .filter_map(|attr| Cfg::parse(attr.meta_item()?).ok()) .filter(|cfg| !hidden_cfg.contains(cfg)) .fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg) } diff --git a/src/test/rustdoc-ui/doc-cfg.rs b/src/test/rustdoc-ui/doc-cfg.rs new file mode 100644 index 00000000000..354d76bc3c4 --- /dev/null +++ b/src/test/rustdoc-ui/doc-cfg.rs @@ -0,0 +1,9 @@ +#![feature(doc_cfg)] + +#[doc(cfg(), cfg(foo, bar))] +//~^ ERROR +//~^^ ERROR +#[doc(cfg(foo), cfg(bar))] // ok! +#[doc(cfg())] //~ ERROR +#[doc(cfg(foo, bar))] //~ ERROR +pub fn foo() {} diff --git a/src/test/rustdoc-ui/doc-cfg.stderr b/src/test/rustdoc-ui/doc-cfg.stderr new file mode 100644 index 00000000000..b379f6febe2 --- /dev/null +++ b/src/test/rustdoc-ui/doc-cfg.stderr @@ -0,0 +1,26 @@ +error: `cfg` predicate is not specified + --> $DIR/doc-cfg.rs:3:7 + | +LL | #[doc(cfg(), cfg(foo, bar))] + | ^^^^^ + +error: multiple `cfg` predicates are specified + --> $DIR/doc-cfg.rs:3:23 + | +LL | #[doc(cfg(), cfg(foo, bar))] + | ^^^ + +error: `cfg` predicate is not specified + --> $DIR/doc-cfg.rs:7:7 + | +LL | #[doc(cfg())] + | ^^^^^ + +error: multiple `cfg` predicates are specified + --> $DIR/doc-cfg.rs:8:16 + | +LL | #[doc(cfg(foo, bar))] + | ^^^ + +error: aborting due to 4 previous errors +