From 9aa1ccdae85b9d5b1575f7a2e48cf31196462e0f Mon Sep 17 00:00:00 2001 From: Sprite Date: Wed, 15 Jun 2022 03:06:28 +0800 Subject: [PATCH 1/2] Fix `cfg(not)` and `cfg(all)` causing Rustdoc stab to disappear --- src/librustdoc/clean/cfg.rs | 14 +++++++++----- src/librustdoc/clean/cfg/tests.rs | 2 +- src/test/rustdoc/doc-auto-cfg.rs | 18 +++++++++++++++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/librustdoc/clean/cfg.rs b/src/librustdoc/clean/cfg.rs index deac1723b26..f33f5d27d1a 100644 --- a/src/librustdoc/clean/cfg.rs +++ b/src/librustdoc/clean/cfg.rs @@ -87,15 +87,20 @@ impl Cfg { }), }, MetaItemKind::List(ref items) => { + let orig_len = items.len(); let sub_cfgs = items.iter().filter_map(|i| Cfg::parse_nested(i, exclude).transpose()); 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::>(); - if sub_cfgs.len() == 1 { - Ok(!sub_cfgs.pop().unwrap()?) + if orig_len == 1 { + let mut sub_cfgs = sub_cfgs.collect::>(); + if sub_cfgs.len() == 1 { + Ok(!sub_cfgs.pop().unwrap()?) + } else { + return Ok(None); + } } else { Err(InvalidCfgError { msg: "expected 1 cfg-pattern", span: cfg.span }) } @@ -304,8 +309,7 @@ impl ops::BitAnd for Cfg { impl ops::BitOrAssign for Cfg { fn bitor_assign(&mut self, other: Cfg) { match (self, other) { - (&mut Cfg::True, _) | (_, Cfg::False) => {} - (s, Cfg::True) => *s = Cfg::True, + (Cfg::True, _) | (_, Cfg::False) | (_, Cfg::True) => {} (s @ &mut Cfg::False, b) => *s = b, (&mut Cfg::Any(ref mut a), Cfg::Any(ref mut b)) => { for c in b.drain(..) { diff --git a/src/librustdoc/clean/cfg/tests.rs b/src/librustdoc/clean/cfg/tests.rs index ece3fcb18b6..7f72d5d39a7 100644 --- a/src/librustdoc/clean/cfg/tests.rs +++ b/src/librustdoc/clean/cfg/tests.rs @@ -161,7 +161,7 @@ fn test_cfg_or() { x = word_cfg("test"); x |= Cfg::True; - assert_eq!(x, Cfg::True); + assert_eq!(x, word_cfg("test")); x = word_cfg("test2"); x |= Cfg::False; diff --git a/src/test/rustdoc/doc-auto-cfg.rs b/src/test/rustdoc/doc-auto-cfg.rs index 57dd0529535..d73718762a7 100644 --- a/src/test/rustdoc/doc-auto-cfg.rs +++ b/src/test/rustdoc/doc-auto-cfg.rs @@ -1,5 +1,4 @@ #![feature(doc_auto_cfg)] - #![crate_name = "foo"] // @has foo/fn.foo.html @@ -12,3 +11,20 @@ pub fn foo() {} // @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'test' #[cfg(any(test, doc))] pub fn bar() {} + +// @has foo/fn.appear_1.html +// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' +// @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'non-test' +#[cfg(any(doc, not(test)))] +pub fn appear_1() {} // issue #98065 + +// @has foo/fn.appear_2.html +// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' +// @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'test' +#[cfg(any(doc, all(test)))] +pub fn appear_2() {} // issue #98065 + +// @has foo/fn.appear_3.html +// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' +#[cfg(any(doc, all()))] +pub fn appear_3() {} // issue #98065 From 713578b82791523afa62803f02a9fc7eef2c3368 Mon Sep 17 00:00:00 2001 From: Sprite Date: Wed, 15 Jun 2022 03:34:00 +0800 Subject: [PATCH 2/2] Exclude cfg "doc" and "doctest" from feature `doc_auto_cfg` --- src/librustdoc/visit_ast.rs | 5 ++++- src/test/rustdoc/doc-auto-cfg.rs | 25 +++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 00553d3f007..ac934f6925d 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -141,7 +141,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { }) .collect::>() }) - .chain([Cfg::Cfg(sym::test, None)].into_iter()) + .chain( + [Cfg::Cfg(sym::test, None), Cfg::Cfg(sym::doc, None), Cfg::Cfg(sym::doctest, None)] + .into_iter(), + ) .collect(); self.cx.cache.exact_paths = self.exact_paths; diff --git a/src/test/rustdoc/doc-auto-cfg.rs b/src/test/rustdoc/doc-auto-cfg.rs index d73718762a7..7842ee69c9f 100644 --- a/src/test/rustdoc/doc-auto-cfg.rs +++ b/src/test/rustdoc/doc-auto-cfg.rs @@ -2,29 +2,34 @@ #![crate_name = "foo"] // @has foo/fn.foo.html -// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'non-doctest' -#[cfg(not(doctest))] +// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'non-meowmeow' +#[cfg(not(meowmeow))] pub fn foo() {} // @has foo/fn.bar.html -// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' +// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'meowmeow' // @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'test' -#[cfg(any(test, doc))] +// @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' +// @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doctest' +#[cfg(any(meowmeow, test, doc, doctest))] pub fn bar() {} // @has foo/fn.appear_1.html -// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' +// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'meowmeow' +// @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' // @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'non-test' -#[cfg(any(doc, not(test)))] +#[cfg(any(meowmeow, doc, not(test)))] pub fn appear_1() {} // issue #98065 // @has foo/fn.appear_2.html -// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' +// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'meowmeow' +// @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' // @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'test' -#[cfg(any(doc, all(test)))] +#[cfg(any(meowmeow, doc, all(test)))] pub fn appear_2() {} // issue #98065 // @has foo/fn.appear_3.html -// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' -#[cfg(any(doc, all()))] +// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'meowmeow' +// @!has - '//*[@class="item-info"]/*[@class="stab portability"]' 'doc' +#[cfg(any(meowmeow, doc, all()))] pub fn appear_3() {} // issue #98065