Skip to content

Commit 3db7704

Browse files
committed
Adjust rustdoc for literal boolean support
1 parent 8ce7146 commit 3db7704

File tree

5 files changed

+64
-23
lines changed

5 files changed

+64
-23
lines changed

src/librustdoc/clean/cfg.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use std::fmt::{self, Write};
77
use std::{mem, ops};
88

9-
use rustc_ast::{LitKind, MetaItem, MetaItemKind, NestedMetaItem};
9+
use rustc_ast::{LitKind, MetaItem, MetaItemKind, MetaItemLit, NestedMetaItem};
1010
use rustc_data_structures::fx::FxHashSet;
1111
use rustc_feature::Features;
1212
use rustc_session::parse::ParseSess;
@@ -48,6 +48,10 @@ impl Cfg {
4848
) -> Result<Option<Cfg>, InvalidCfgError> {
4949
match nested_cfg {
5050
NestedMetaItem::MetaItem(ref cfg) => Cfg::parse_without(cfg, exclude),
51+
NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => match *b {
52+
true => Ok(Some(Cfg::True)),
53+
false => Ok(Some(Cfg::False)),
54+
},
5155
NestedMetaItem::Lit(ref lit) => {
5256
Err(InvalidCfgError { msg: "unexpected literal", span: lit.span })
5357
}
@@ -120,8 +124,8 @@ impl Cfg {
120124
///
121125
/// If the content is not properly formatted, it will return an error indicating what and where
122126
/// the error is.
123-
pub(crate) fn parse(cfg: &MetaItem) -> Result<Cfg, InvalidCfgError> {
124-
Self::parse_without(cfg, &FxHashSet::default()).map(|ret| ret.unwrap())
127+
pub(crate) fn parse(cfg: &NestedMetaItem) -> Result<Cfg, InvalidCfgError> {
128+
Self::parse_nested(cfg, &FxHashSet::default()).map(|ret| ret.unwrap())
125129
}
126130

127131
/// Checks whether the given configuration can be matched in the current session.

src/librustdoc/clean/cfg/tests.rs

+34-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use rustc_ast::{MetaItemLit, Path, Safety, StrStyle};
1+
use rustc_ast::ast::LitIntType;
2+
use rustc_ast::{MetaItemLit, NestedMetaItem, Path, Safety, StrStyle};
23
use rustc_span::symbol::{Ident, kw};
34
use rustc_span::{DUMMY_SP, create_default_session_globals_then};
45
use thin_vec::thin_vec;
@@ -13,52 +14,52 @@ fn name_value_cfg(name: &str, value: &str) -> Cfg {
1314
Cfg::Cfg(Symbol::intern(name), Some(Symbol::intern(value)))
1415
}
1516

16-
fn dummy_meta_item_word(name: &str) -> MetaItem {
17-
MetaItem {
17+
fn dummy_lit(symbol: Symbol, kind: LitKind) -> NestedMetaItem {
18+
NestedMetaItem::Lit(MetaItemLit { symbol, suffix: None, kind, span: DUMMY_SP })
19+
}
20+
21+
fn dummy_meta_item_word(name: &str) -> NestedMetaItem {
22+
NestedMetaItem::MetaItem(MetaItem {
1823
unsafety: Safety::Default,
1924
path: Path::from_ident(Ident::from_str(name)),
2025
kind: MetaItemKind::Word,
2126
span: DUMMY_SP,
22-
}
27+
})
2328
}
2429

25-
fn dummy_meta_item_name_value(name: &str, symbol: Symbol, kind: LitKind) -> MetaItem {
30+
fn dummy_meta_item_name_value(name: &str, symbol: Symbol, kind: LitKind) -> NestedMetaItem {
2631
let lit = MetaItemLit { symbol, suffix: None, kind, span: DUMMY_SP };
27-
MetaItem {
32+
NestedMetaItem::MetaItem(MetaItem {
2833
unsafety: Safety::Default,
2934
path: Path::from_ident(Ident::from_str(name)),
3035
kind: MetaItemKind::NameValue(lit),
3136
span: DUMMY_SP,
32-
}
37+
})
3338
}
3439

3540
macro_rules! dummy_meta_item_list {
3641
($name:ident, [$($list:ident),* $(,)?]) => {
37-
MetaItem {
42+
NestedMetaItem::MetaItem(MetaItem {
3843
unsafety: Safety::Default,
3944
path: Path::from_ident(Ident::from_str(stringify!($name))),
4045
kind: MetaItemKind::List(thin_vec![
4146
$(
42-
NestedMetaItem::MetaItem(
43-
dummy_meta_item_word(stringify!($list)),
44-
),
47+
dummy_meta_item_word(stringify!($list)),
4548
)*
4649
]),
4750
span: DUMMY_SP,
48-
}
51+
})
4952
};
5053

5154
($name:ident, [$($list:expr),* $(,)?]) => {
52-
MetaItem {
55+
NestedMetaItem::MetaItem(MetaItem {
5356
unsafety: Safety::Default,
5457
path: Path::from_ident(Ident::from_str(stringify!($name))),
5558
kind: MetaItemKind::List(thin_vec![
56-
$(
57-
NestedMetaItem::MetaItem($list),
58-
)*
59+
$($list,)*
5960
]),
6061
span: DUMMY_SP,
61-
}
62+
})
6263
};
6364
}
6465

@@ -251,6 +252,14 @@ fn test_cfg_or() {
251252
#[test]
252253
fn test_parse_ok() {
253254
create_default_session_globals_then(|| {
255+
let r#true = Symbol::intern("true");
256+
let mi = dummy_lit(r#true, LitKind::Bool(true));
257+
assert_eq!(Cfg::parse(&mi), Ok(Cfg::True));
258+
259+
let r#false = Symbol::intern("false");
260+
let mi = dummy_lit(r#false, LitKind::Bool(false));
261+
assert_eq!(Cfg::parse(&mi), Ok(Cfg::False));
262+
254263
let mi = dummy_meta_item_word("all");
255264
assert_eq!(Cfg::parse(&mi), Ok(word_cfg("all")));
256265

@@ -309,6 +318,14 @@ fn test_parse_err() {
309318

310319
let mi = dummy_meta_item_list!(not, [dummy_meta_item_list!(foo, []),]);
311320
assert!(Cfg::parse(&mi).is_err());
321+
322+
let c = Symbol::intern("e");
323+
let mi = dummy_lit(c, LitKind::Char('e'));
324+
assert!(Cfg::parse(&mi).is_err());
325+
326+
let five = Symbol::intern("5");
327+
let mi = dummy_lit(five, LitKind::Int(5.into(), LitIntType::Unsuffixed));
328+
assert!(Cfg::parse(&mi).is_err());
312329
})
313330
}
314331

src/librustdoc/clean/types.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::{Arc, OnceLock as OnceCell};
55
use std::{fmt, iter};
66

77
use arrayvec::ArrayVec;
8+
use rustc_ast::NestedMetaItem;
89
use rustc_ast_pretty::pprust;
910
use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel, StableSince};
1011
use rustc_const_eval::const_eval::is_unstable_const_fn;
@@ -1016,7 +1017,7 @@ pub(crate) trait AttributesExt {
10161017
.peekable();
10171018
if doc_cfg.peek().is_some() && doc_cfg_active {
10181019
doc_cfg
1019-
.filter_map(|attr| Cfg::parse(attr.meta_item()?).ok())
1020+
.filter_map(|attr| Cfg::parse(&attr).ok())
10201021
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
10211022
} else if doc_auto_cfg_active {
10221023
// If there is no `doc(cfg())`, then we retrieve the `cfg()` attributes (because
@@ -1072,7 +1073,7 @@ pub(crate) trait AttributesExt {
10721073
let mut meta = attr.meta_item().unwrap().clone();
10731074
meta.path = ast::Path::from_ident(Ident::with_dummy_span(sym::target_feature));
10741075

1075-
if let Ok(feat_cfg) = Cfg::parse(&meta) {
1076+
if let Ok(feat_cfg) = Cfg::parse(&NestedMetaItem::MetaItem(meta)) {
10761077
cfg &= feat_cfg;
10771078
}
10781079
}

src/librustdoc/visit_ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
164164
.unwrap_or(&[])
165165
.iter()
166166
.filter_map(|attr| {
167-
Cfg::parse(attr.meta_item()?)
167+
Cfg::parse(attr)
168168
.map_err(|e| self.cx.sess().dcx().span_err(e.span, e.msg))
169169
.ok()
170170
})
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ check-pass
2+
3+
#![feature(cfg_boolean_literals)]
4+
#![feature(doc_cfg)]
5+
6+
#[doc(cfg(false))]
7+
pub fn foo() {}
8+
9+
#[doc(cfg(true))]
10+
pub fn bar() {}
11+
12+
#[doc(cfg(any(true)))]
13+
pub fn zoo() {}
14+
15+
#[doc(cfg(all(true)))]
16+
pub fn toy() {}
17+
18+
#[doc(cfg(not(true)))]
19+
pub fn nay() {}

0 commit comments

Comments
 (0)