Skip to content

Commit bcc6f65

Browse files
committed
Adjust rustdoc for literal boolean support
1 parent 5794918 commit bcc6f65

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
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/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_literal)]
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)