Skip to content

Commit 9d120c6

Browse files
Add code documentation, improve code and improve error message
1 parent 9c94fb7 commit 9d120c6

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ passes_doc_auto_cfg_expects_hide_or_show =
168168
`only "hide" or "show" are allowed in "#[doc(auto_cfg(...))]"`
169169
170170
passes_doc_auto_cfg_hide_show_expects_list =
171-
`#![doc(auto_cfg({$attr_name}(...)))]` only expects a list of items
171+
`#![doc(auto_cfg({$attr_name}(...)))]` expects a list of items
172172
173173
passes_doc_auto_cfg_hide_show_unexpected_item =
174174
`#![doc(auto_cfg({$attr_name}(...)))]` only accepts identifiers or key/values items

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::cell::Cell;
99
use std::collections::hash_map::Entry;
1010

1111
use rustc_abi::{Align, ExternAbi, Size};
12-
use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, ast};
12+
use rustc_ast::{AttrStyle, LitKind, MetaItem, MetaItemInner, MetaItemKind, ast};
1313
use rustc_attr_data_structures::{AttributeKind, InlineAttr, ReprAttr, find_attr};
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
@@ -1268,10 +1268,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12681268
}
12691269

12701270
/// Check that the `#![doc(auto_cfg(..))]` attribute has expected input.
1271-
fn check_doc_auto_cfg(&self, meta: &MetaItemInner, hir_id: HirId) {
1272-
let MetaItemInner::MetaItem(meta) = meta else {
1273-
unreachable!();
1274-
};
1271+
fn check_doc_auto_cfg(&self, meta: &MetaItem, hir_id: HirId) {
12751272
match &meta.kind {
12761273
MetaItemKind::Word => {}
12771274
MetaItemKind::NameValue(lit) => {
@@ -1379,7 +1376,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13791376
}
13801377

13811378
Some(sym::auto_cfg) => {
1382-
self.check_doc_auto_cfg(meta, hir_id);
1379+
self.check_doc_auto_cfg(i_meta, hir_id);
13831380
}
13841381

13851382
Some(sym::inline | sym::no_inline) => {

src/librustdoc/clean/types.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,11 +1005,19 @@ pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
10051005
.flatten()
10061006
}
10071007

1008+
/// This type keeps track of (doc) cfg information as we go down the item tree.
10081009
#[derive(Clone, Debug)]
10091010
pub(crate) struct CfgInfo {
1011+
/// List of `doc(auto_cfg(hide(...)))` cfgs.
10101012
hidden_cfg: FxHashSet<Cfg>,
1013+
/// Current computed `cfg`. Each time we enter a new item, this field is updated as well while
1014+
/// taking into account the `hidden_cfg` information.
10111015
current_cfg: Cfg,
1016+
/// Whether the `doc(auto_cfg())` feature is enabled or not at this point.
10121017
auto_cfg_active: bool,
1018+
/// If the parent item used `doc(cfg(...))`, then we don't want to overwrite `current_cfg`,
1019+
/// instead we will concatenate with it. However, if it's not the case, we need to overwrite
1020+
/// `current_cfg`.
10131021
parent_is_doc_cfg: bool,
10141022
}
10151023

@@ -1045,6 +1053,11 @@ fn show_hide_show_conflict_error(
10451053
diag.emit();
10461054
}
10471055

1056+
/// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1057+
/// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1058+
///
1059+
/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
1060+
/// and in `new_hide_attrs` arguments.
10481061
fn handle_auto_cfg_hide_show(
10491062
tcx: TyCtxt<'_>,
10501063
cfg_info: &mut CfgInfo,

src/librustdoc/passes/propagate_doc_cfg.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ struct CfgPropagator<'a, 'tcx> {
3030
cfg_info: CfgInfo,
3131
}
3232

33-
fn should_retain(token: &TokenTree) -> bool {
33+
/// Returns true if the provided `token` is a `cfg` ident.
34+
fn is_cfg_token(token: &TokenTree) -> bool {
3435
// We only keep `doc(cfg)` items.
3536
matches!(
3637
token,
@@ -47,7 +48,9 @@ fn should_retain(token: &TokenTree) -> bool {
4748
)
4849
}
4950

50-
fn filter_tokens_from_list(args_tokens: &TokenStream) -> Vec<TokenTree> {
51+
/// We only want to keep `#[cfg()]` and `#[doc(cfg())]` attributes so we rebuild a vec of
52+
/// `TokenTree` with only the tokens we're interested into.
53+
fn filter_non_cfg_tokens_from_list(args_tokens: &TokenStream) -> Vec<TokenTree> {
5154
let mut tokens = Vec::with_capacity(args_tokens.len());
5255
let mut skip_next_delimited = false;
5356
for token in args_tokens.iter() {
@@ -58,7 +61,7 @@ fn filter_tokens_from_list(args_tokens: &TokenStream) -> Vec<TokenTree> {
5861
}
5962
skip_next_delimited = false;
6063
}
61-
token if should_retain(token) => {
64+
token if is_cfg_token(token) => {
6265
skip_next_delimited = false;
6366
tokens.push(token.clone());
6467
}
@@ -70,7 +73,8 @@ fn filter_tokens_from_list(args_tokens: &TokenStream) -> Vec<TokenTree> {
7073
tokens
7174
}
7275

73-
// We only care about `#[cfg()]` and `#[doc(cfg())]`, we discard everything else.
76+
/// This function goes through the attributes list (`new_attrs`) and extract the `cfg` tokens from
77+
/// it and put them into `attrs`.
7478
fn add_only_cfg_attributes(attrs: &mut Vec<Attribute>, new_attrs: &[Attribute]) {
7579
for attr in new_attrs {
7680
if attr.is_doc_comment() {
@@ -84,7 +88,7 @@ fn add_only_cfg_attributes(attrs: &mut Vec<Attribute>, new_attrs: &[Attribute])
8488
if ident == sym::doc
8589
&& let AttrArgs::Delimited(args) = &mut normal.args
8690
{
87-
let tokens = filter_tokens_from_list(&args.tokens);
91+
let tokens = filter_non_cfg_tokens_from_list(&args.tokens);
8892
args.tokens = TokenStream::new(tokens);
8993
attrs.push(attr);
9094
} else if ident == sym::cfg_trace {

tests/rustdoc-ui/lints/doc_cfg_hide.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error: `#![doc(auto_cfg(hide(...)))]` only expects a list of items
1+
error: `#![doc(auto_cfg(hide(...)))]` expects a list of items
22
--> $DIR/doc_cfg_hide.rs:2:8
33
|
44
LL | #![doc(auto_cfg(hide = "test"))]
55
| ^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `#[deny(invalid_doc_attributes)]` on by default
88

9-
error: `#![doc(auto_cfg(hide(...)))]` only expects a list of items
9+
error: `#![doc(auto_cfg(hide(...)))]` expects a list of items
1010
--> $DIR/doc_cfg_hide.rs:3:8
1111
|
1212
LL | #![doc(auto_cfg(hide))]

0 commit comments

Comments
 (0)