Skip to content

Commit

Permalink
Adjust rustdoc for literal boolean support
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Sep 30, 2024
1 parent 0964b9e commit c17dd7b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
10 changes: 7 additions & 3 deletions src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use std::fmt::{self, Write};
use std::{mem, ops};

use rustc_ast::{LitKind, MetaItem, MetaItemKind, NestedMetaItem};
use rustc_ast::{LitKind, MetaItem, MetaItemKind, MetaItemLit, NestedMetaItem};
use rustc_data_structures::fx::FxHashSet;
use rustc_feature::Features;
use rustc_session::parse::ParseSess;
Expand Down Expand Up @@ -48,6 +48,10 @@ impl Cfg {
) -> Result<Option<Cfg>, InvalidCfgError> {
match nested_cfg {
NestedMetaItem::MetaItem(ref cfg) => Cfg::parse_without(cfg, exclude),
NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => match *b {
true => Ok(Some(Cfg::True)),
false => Ok(Some(Cfg::False)),
},
NestedMetaItem::Lit(ref lit) => {
Err(InvalidCfgError { msg: "unexpected literal", span: lit.span })
}
Expand Down Expand Up @@ -120,8 +124,8 @@ impl Cfg {
///
/// If the content is not properly formatted, it will return an error indicating what and where
/// the error is.
pub(crate) fn parse(cfg: &MetaItem) -> Result<Cfg, InvalidCfgError> {
Self::parse_without(cfg, &FxHashSet::default()).map(|ret| ret.unwrap())
pub(crate) fn parse(cfg: &NestedMetaItem) -> Result<Cfg, InvalidCfgError> {
Self::parse_nested(cfg, &FxHashSet::default()).map(|ret| ret.unwrap())
}

/// Checks whether the given configuration can be matched in the current session.
Expand Down
51 changes: 34 additions & 17 deletions src/librustdoc/clean/cfg/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustc_ast::{MetaItemLit, Path, Safety, StrStyle};
use rustc_ast::ast::LitIntType;
use rustc_ast::{MetaItemLit, NestedMetaItem, Path, Safety, StrStyle};
use rustc_span::symbol::{Ident, kw};
use rustc_span::{DUMMY_SP, create_default_session_globals_then};
use thin_vec::thin_vec;
Expand All @@ -13,52 +14,52 @@ fn name_value_cfg(name: &str, value: &str) -> Cfg {
Cfg::Cfg(Symbol::intern(name), Some(Symbol::intern(value)))
}

fn dummy_meta_item_word(name: &str) -> MetaItem {
MetaItem {
fn dummy_lit(symbol: Symbol, kind: LitKind) -> NestedMetaItem {
NestedMetaItem::Lit(MetaItemLit { symbol, suffix: None, kind, span: DUMMY_SP })
}

fn dummy_meta_item_word(name: &str) -> NestedMetaItem {
NestedMetaItem::MetaItem(MetaItem {
unsafety: Safety::Default,
path: Path::from_ident(Ident::from_str(name)),
kind: MetaItemKind::Word,
span: DUMMY_SP,
}
})
}

fn dummy_meta_item_name_value(name: &str, symbol: Symbol, kind: LitKind) -> MetaItem {
fn dummy_meta_item_name_value(name: &str, symbol: Symbol, kind: LitKind) -> NestedMetaItem {
let lit = MetaItemLit { symbol, suffix: None, kind, span: DUMMY_SP };
MetaItem {
NestedMetaItem::MetaItem(MetaItem {
unsafety: Safety::Default,
path: Path::from_ident(Ident::from_str(name)),
kind: MetaItemKind::NameValue(lit),
span: DUMMY_SP,
}
})
}

macro_rules! dummy_meta_item_list {
($name:ident, [$($list:ident),* $(,)?]) => {
MetaItem {
NestedMetaItem::MetaItem(MetaItem {
unsafety: Safety::Default,
path: Path::from_ident(Ident::from_str(stringify!($name))),
kind: MetaItemKind::List(thin_vec![
$(
NestedMetaItem::MetaItem(
dummy_meta_item_word(stringify!($list)),
),
dummy_meta_item_word(stringify!($list)),
)*
]),
span: DUMMY_SP,
}
})
};

($name:ident, [$($list:expr),* $(,)?]) => {
MetaItem {
NestedMetaItem::MetaItem(MetaItem {
unsafety: Safety::Default,
path: Path::from_ident(Ident::from_str(stringify!($name))),
kind: MetaItemKind::List(thin_vec![
$(
NestedMetaItem::MetaItem($list),
)*
$($list,)*
]),
span: DUMMY_SP,
}
})
};
}

Expand Down Expand Up @@ -251,6 +252,14 @@ fn test_cfg_or() {
#[test]
fn test_parse_ok() {
create_default_session_globals_then(|| {
let r#true = Symbol::intern("true");
let mi = dummy_lit(r#true, LitKind::Bool(true));
assert_eq!(Cfg::parse(&mi), Ok(Cfg::True));

let r#false = Symbol::intern("false");
let mi = dummy_lit(r#false, LitKind::Bool(false));
assert_eq!(Cfg::parse(&mi), Ok(Cfg::False));

let mi = dummy_meta_item_word("all");
assert_eq!(Cfg::parse(&mi), Ok(word_cfg("all")));

Expand Down Expand Up @@ -309,6 +318,14 @@ fn test_parse_err() {

let mi = dummy_meta_item_list!(not, [dummy_meta_item_list!(foo, []),]);
assert!(Cfg::parse(&mi).is_err());

let c = Symbol::intern("e");
let mi = dummy_lit(c, LitKind::Char('e'));
assert!(Cfg::parse(&mi).is_err());

let five = Symbol::intern("5");
let mi = dummy_lit(five, LitKind::Int(5.into(), LitIntType::Unsuffixed));
assert!(Cfg::parse(&mi).is_err());
})
}

Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::{Arc, OnceLock as OnceCell};
use std::{fmt, iter};

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

if let Ok(feat_cfg) = Cfg::parse(&meta) {
if let Ok(feat_cfg) = Cfg::parse(&NestedMetaItem::MetaItem(meta)) {
cfg &= feat_cfg;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
.unwrap_or(&[])
.iter()
.filter_map(|attr| {
Cfg::parse(attr.meta_item()?)
Cfg::parse(attr)
.map_err(|e| self.cx.sess().dcx().span_err(e.span, e.msg))
.ok()
})
Expand Down
19 changes: 19 additions & 0 deletions tests/rustdoc-ui/cfg-boolean-literal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@ check-pass

#![feature(cfg_boolean_literal)]
#![feature(doc_cfg)]

#[doc(cfg(false))]
pub fn foo() {}

#[doc(cfg(true))]
pub fn bar() {}

#[doc(cfg(any(true)))]
pub fn zoo() {}

#[doc(cfg(all(true)))]
pub fn toy() {}

#[doc(cfg(not(true)))]
pub fn nay() {}

0 comments on commit c17dd7b

Please sign in to comment.