Adjust rustdoc for literal boolean support

This commit is contained in:
Urgau 2024-09-29 23:40:06 +02:00
parent 62ef411631
commit 781f1840cd
5 changed files with 64 additions and 23 deletions

View File

@ -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;
@ -48,6 +48,10 @@ fn parse_nested(
) -> 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 })
}
@ -120,8 +124,8 @@ pub(crate) fn parse_without(
///
/// 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.

View File

@ -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;
@ -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,
}
})
};
}
@ -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")));
@ -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());
})
}

View File

@ -5,6 +5,7 @@
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;
@ -1016,7 +1017,7 @@ fn single<T: IntoIterator>(it: T) -> Option<T::Item> {
.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
@ -1072,7 +1073,7 @@ fn single<T: IntoIterator>(it: T) -> Option<T::Item> {
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;
}
}

View File

@ -164,7 +164,7 @@ pub(crate) fn visit(mut self) -> Module<'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()
})

View File

@ -0,0 +1,19 @@
//@ check-pass
#![feature(cfg_boolean_literals)]
#![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() {}