diff options
| author | Takayuki Maeda <takoyaki0316@gmail.com> | 2025-03-25 15:36:33 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-25 15:36:33 +0900 |
| commit | 07d0963c5f8566e9440b39e2a203efd5288a85d0 (patch) | |
| tree | c1bbaaf3e71e3c62bd3b940a17d6eaf5c8aa6a7f | |
| parent | e9a528c9b57f3babf10b09c0d789e7a5c8c30146 (diff) | |
| parent | d9f250af21dbadcde2113a961132a263392bf091 (diff) | |
| download | rust-07d0963c5f8566e9440b39e2a203efd5288a85d0.tar.gz rust-07d0963c5f8566e9440b39e2a203efd5288a85d0.zip | |
Rollup merge of #138755 - GuillaumeGomez:rm-duplicated-loop, r=camelid
[rustdoc] Remove duplicated loop when computing doc cfgs Working on implementing https://github.com/rust-lang/rfcs/blob/master/text/3631-rustdoc-cfgs-handling.md and found this weird case where the first loop was actually not doing anything since we were passing `cfg(...)` to `Cfg::parse` instead of `cfg(...)` items. Well, that should be a first nice cleanup before the rest comes in. cc ```@notriddle``` r? ```@camelid```
| -rw-r--r-- | src/librustdoc/clean/types.rs | 59 |
1 files changed, 21 insertions, 38 deletions
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 5f80aded9d0..27eb56a9858 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1013,7 +1013,6 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet<Cfg>, ) -> Option<Arc<Cfg>> { - let sess = tcx.sess; let doc_cfg_active = tcx.features().doc_cfg(); let doc_auto_cfg_active = tcx.features().doc_auto_cfg(); @@ -1034,9 +1033,27 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> .filter(|attr| attr.has_name(sym::cfg)) .peekable(); if doc_cfg.peek().is_some() && doc_cfg_active { - doc_cfg - .filter_map(|attr| Cfg::parse(&attr).ok()) - .fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg) + let sess = tcx.sess; + doc_cfg.fold(Cfg::True, |mut cfg, item| { + if let Some(cfg_mi) = + item.meta_item().and_then(|item| rustc_expand::config::parse_cfg(item, sess)) + { + // The result is unused here but we can gate unstable predicates + rustc_attr_parsing::cfg_matches( + cfg_mi, + tcx.sess, + rustc_ast::CRATE_NODE_ID, + Some(tcx.features()), + ); + match Cfg::parse(cfg_mi) { + Ok(new_cfg) => cfg &= new_cfg, + Err(e) => { + sess.dcx().span_err(e.span, e.msg); + } + } + } + cfg + }) } else if doc_auto_cfg_active { // If there is no `doc(cfg())`, then we retrieve the `cfg()` attributes (because // `doc(cfg())` overrides `cfg()`). @@ -1053,40 +1070,6 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> Cfg::True }; - for attr in attrs.clone() { - // #[doc] - if attr.doc_str().is_none() && attr.has_name(sym::doc) { - // #[doc(...)] - if let Some(list) = attr.meta_item_list() { - for item in list { - // #[doc(hidden)] - if !item.has_name(sym::cfg) { - continue; - } - // #[doc(cfg(...))] - if let Some(cfg_mi) = item - .meta_item() - .and_then(|item| rustc_expand::config::parse_cfg(item, sess)) - { - // The result is unused here but we can gate unstable predicates - rustc_attr_parsing::cfg_matches( - cfg_mi, - tcx.sess, - rustc_ast::CRATE_NODE_ID, - Some(tcx.features()), - ); - match Cfg::parse(cfg_mi) { - Ok(new_cfg) => cfg &= new_cfg, - Err(e) => { - sess.dcx().span_err(e.span, e.msg); - } - } - } - } - } - } - } - // treat #[target_feature(enable = "feat")] attributes as if they were // #[doc(cfg(target_feature = "feat"))] attributes as well for attr in hir_attr_lists(attrs, sym::target_feature) { |
