-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make cfg imply doc(cfg) #79341
Make cfg imply doc(cfg) #79341
Changes from all commits
85ec783
74d0d3f
e0c438f
28500e7
0225d97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,7 +124,8 @@ impl Clean<Item> for doctree::Module<'_> { | |
|
||
impl Clean<Attributes> for [ast::Attribute] { | ||
fn clean(&self, cx: &mut DocContext<'_>) -> Attributes { | ||
Attributes::from_ast(cx.sess().diagnostic(), self, None) | ||
let doc_cfg_active = cx.tcx.features().doc_cfg; | ||
Attributes::from_ast(cx.sess().diagnostic(), self, None, doc_cfg_active, &cx.hidden_cfg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why put this on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found why - you use it directly in |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -811,29 +811,6 @@ pub struct RenderedLink { | |||||||||
} | ||||||||||
|
||||||||||
impl Attributes { | ||||||||||
/// Extracts the content from an attribute `#[doc(cfg(content))]`. | ||||||||||
crate fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> { | ||||||||||
use rustc_ast::NestedMetaItem::MetaItem; | ||||||||||
|
||||||||||
if let ast::MetaItemKind::List(ref nmis) = mi.kind { | ||||||||||
if nmis.len() == 1 { | ||||||||||
if let MetaItem(ref cfg_mi) = nmis[0] { | ||||||||||
if cfg_mi.has_name(sym::cfg) { | ||||||||||
if let ast::MetaItemKind::List(ref cfg_nmis) = cfg_mi.kind { | ||||||||||
if cfg_nmis.len() == 1 { | ||||||||||
if let MetaItem(ref content_mi) = cfg_nmis[0] { | ||||||||||
return Some(content_mi); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
None | ||||||||||
} | ||||||||||
|
||||||||||
/// Reads a `MetaItem` from within an attribute, looks for whether it is a | ||||||||||
/// `#[doc(include="file")]`, and returns the filename and contents of the file as loaded from | ||||||||||
/// its expansion. | ||||||||||
|
@@ -893,10 +870,11 @@ impl Attributes { | |||||||||
diagnostic: &::rustc_errors::Handler, | ||||||||||
attrs: &[ast::Attribute], | ||||||||||
additional_attrs: Option<(&[ast::Attribute], DefId)>, | ||||||||||
doc_cfg_active: bool, | ||||||||||
hidden_cfg: &FxHashSet<Cfg>, | ||||||||||
) -> Attributes { | ||||||||||
let mut doc_strings: Vec<DocFragment> = vec![]; | ||||||||||
let mut sp = None; | ||||||||||
let mut cfg = Cfg::True; | ||||||||||
let mut doc_line = 0; | ||||||||||
|
||||||||||
fn update_need_backline(doc_strings: &mut Vec<DocFragment>, frag: &DocFragment) { | ||||||||||
|
@@ -943,36 +921,27 @@ impl Attributes { | |||||||||
if sp.is_none() { | ||||||||||
sp = Some(attr.span); | ||||||||||
} | ||||||||||
None | ||||||||||
} else { | ||||||||||
if attr.has_name(sym::doc) { | ||||||||||
if let Some(mi) = attr.meta() { | ||||||||||
if let Some(cfg_mi) = Attributes::extract_cfg(&mi) { | ||||||||||
// Extracted #[doc(cfg(...))] | ||||||||||
match Cfg::parse(cfg_mi) { | ||||||||||
Ok(new_cfg) => cfg &= new_cfg, | ||||||||||
Err(e) => diagnostic.span_err(e.span, e.msg), | ||||||||||
} | ||||||||||
} else if let Some((filename, contents)) = Attributes::extract_include(&mi) | ||||||||||
{ | ||||||||||
let line = doc_line; | ||||||||||
doc_line += contents.as_str().lines().count(); | ||||||||||
let frag = DocFragment { | ||||||||||
line, | ||||||||||
span: attr.span, | ||||||||||
doc: contents, | ||||||||||
kind: DocFragmentKind::Include { filename }, | ||||||||||
parent_module, | ||||||||||
need_backline: false, | ||||||||||
indent: 0, | ||||||||||
}; | ||||||||||
update_need_backline(&mut doc_strings, &frag); | ||||||||||
doc_strings.push(frag); | ||||||||||
} | ||||||||||
return None; | ||||||||||
} else if attr.has_name(sym::doc) { | ||||||||||
if let Some(mi) = attr.meta() { | ||||||||||
if let Some((filename, contents)) = Attributes::extract_include(&mi) { | ||||||||||
let line = doc_line; | ||||||||||
doc_line += contents.as_str().lines().count(); | ||||||||||
let frag = DocFragment { | ||||||||||
line, | ||||||||||
span: attr.span, | ||||||||||
doc: contents, | ||||||||||
kind: DocFragmentKind::Include { filename }, | ||||||||||
parent_module, | ||||||||||
need_backline: false, | ||||||||||
indent: 0, | ||||||||||
}; | ||||||||||
update_need_backline(&mut doc_strings, &frag); | ||||||||||
doc_strings.push(frag); | ||||||||||
} | ||||||||||
} | ||||||||||
Some(attr.clone()) | ||||||||||
} | ||||||||||
Some(attr.clone()) | ||||||||||
}; | ||||||||||
|
||||||||||
// Additional documentation should be shown before the original documentation | ||||||||||
|
@@ -984,6 +953,50 @@ impl Attributes { | |||||||||
.filter_map(clean_attr) | ||||||||||
.collect(); | ||||||||||
|
||||||||||
trait SingleExt { | ||||||||||
type Item; | ||||||||||
fn single(self) -> Option<Self::Item>; | ||||||||||
} | ||||||||||
|
||||||||||
impl<T: IntoIterator> SingleExt for T { | ||||||||||
type Item = T::Item; | ||||||||||
fn single(self) -> Option<Self::Item> { | ||||||||||
let mut iter = self.into_iter(); | ||||||||||
let item = iter.next()?; | ||||||||||
iter.next().is_none().then_some(())?; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a little too cute :P
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'll likely scrap the whole |
||||||||||
Some(item) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
let mut cfg = if doc_cfg_active { | ||||||||||
let mut doc_cfg = attrs | ||||||||||
.iter() | ||||||||||
.filter(|attr| attr.has_name(sym::doc)) | ||||||||||
.filter_map(|attr| Some(attr.meta_item_list()?.single()?)) | ||||||||||
.filter(|attr| attr.has_name(sym::cfg)) | ||||||||||
.filter_map(|attr| Some(attr.meta_item_list()?.single()?.meta_item()?.clone())) | ||||||||||
.peekable(); | ||||||||||
if doc_cfg.peek().is_some() { | ||||||||||
doc_cfg | ||||||||||
.filter_map(|attr| { | ||||||||||
Cfg::parse(&attr).map_err(|e| diagnostic.span_err(e.span, e.msg)).ok() | ||||||||||
}) | ||||||||||
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg) | ||||||||||
} else { | ||||||||||
attrs | ||||||||||
.iter() | ||||||||||
.filter(|attr| attr.has_name(sym::cfg)) | ||||||||||
.filter_map(|attr| Some(attr.meta_item_list()?.single()?.meta_item()?.clone())) | ||||||||||
.filter_map(|attr| { | ||||||||||
Cfg::parse(&attr).map_err(|e| diagnostic.span_err(e.span, e.msg)).ok() | ||||||||||
}) | ||||||||||
.filter(|cfg| !hidden_cfg.contains(cfg)) | ||||||||||
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg) | ||||||||||
} | ||||||||||
} else { | ||||||||||
Cfg::True | ||||||||||
}; | ||||||||||
|
||||||||||
// treat #[target_feature(enable = "feat")] attributes as if they were | ||||||||||
// #[doc(cfg(target_feature = "feat"))] attributes as well | ||||||||||
for attr in attrs.lists(sym::target_feature) { | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why hide these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These, along with
target_has_atomic_ptr
, are unstablecfg
. While I think it would be really nice to have this all documented on the items, I don't think thedoc(cfg)
presentation of them is good enough. I'll attach a screenshot of what it looks like with them unhidden.I also think it might make sense to enhance
doc(cfg_hide)
to support specifying just the key of key-value cfg expressions so that these aren't duplicated for each size.