Skip to content

Commit 686558a

Browse files
Add code documentation, improve code and improve error message
1 parent 6f2991b commit 686558a

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
@@ -186,7 +186,7 @@ passes_doc_auto_cfg_expects_hide_or_show =
186186
`only "hide" or "show" are allowed in "#[doc(auto_cfg(...))]"`
187187
188188
passes_doc_auto_cfg_hide_show_expects_list =
189-
`#![doc(auto_cfg({$attr_name}(...)))]` only expects a list of items
189+
`#![doc(auto_cfg({$attr_name}(...)))]` expects a list of items
190190
191191
passes_doc_auto_cfg_hide_show_unexpected_item =
192192
`#![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, MetaItemLit, ast};
12+
use rustc_ast::{AttrStyle, LitKind, MetaItem, MetaItemInner, MetaItemKind, MetaItemLit, ast};
1313
use rustc_attr_data_structures::{AttributeKind, ReprAttr, find_attr};
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
@@ -1304,10 +1304,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13041304
}
13051305

13061306
/// Check that the `#![doc(auto_cfg(..))]` attribute has expected input.
1307-
fn check_doc_auto_cfg(&self, meta: &MetaItemInner, hir_id: HirId) {
1308-
let MetaItemInner::MetaItem(meta) = meta else {
1309-
unreachable!();
1310-
};
1307+
fn check_doc_auto_cfg(&self, meta: &MetaItem, hir_id: HirId) {
13111308
match &meta.kind {
13121309
MetaItemKind::Word => {}
13131310
MetaItemKind::NameValue(lit) => {
@@ -1416,7 +1413,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
14161413
}
14171414

14181415
Some(sym::auto_cfg) => {
1419-
self.check_doc_auto_cfg(meta, hir_id);
1416+
self.check_doc_auto_cfg(i_meta, hir_id);
14201417
}
14211418

14221419
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
@@ -1007,11 +1007,19 @@ pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
10071007
.flatten()
10081008
}
10091009

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

@@ -1047,6 +1055,11 @@ fn show_hide_show_conflict_error(
10471055
diag.emit();
10481056
}
10491057

1058+
/// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1059+
/// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1060+
///
1061+
/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
1062+
/// and in `new_hide_attrs` arguments.
10501063
fn handle_auto_cfg_hide_show(
10511064
tcx: TyCtxt<'_>,
10521065
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)