Skip to content

Commit 4f60324

Browse files
Add code documentation, improve code and improve error message
1 parent b12ebd6 commit 4f60324

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};
@@ -1272,10 +1272,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12721272
}
12731273

12741274
/// Check that the `#![doc(auto_cfg(..))]` attribute has expected input.
1275-
fn check_doc_auto_cfg(&self, meta: &MetaItemInner, hir_id: HirId) {
1276-
let MetaItemInner::MetaItem(meta) = meta else {
1277-
unreachable!();
1278-
};
1275+
fn check_doc_auto_cfg(&self, meta: &MetaItem, hir_id: HirId) {
12791276
match &meta.kind {
12801277
MetaItemKind::Word => {}
12811278
MetaItemKind::NameValue(lit) => {
@@ -1383,7 +1380,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13831380
}
13841381

13851382
Some(sym::auto_cfg) => {
1386-
self.check_doc_auto_cfg(meta, hir_id);
1383+
self.check_doc_auto_cfg(i_meta, hir_id);
13871384
}
13881385

13891386
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
@@ -994,11 +994,19 @@ pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
994994
.flatten()
995995
}
996996

997+
/// This type keeps track of (doc) cfg information as we go down the item tree.
997998
#[derive(Clone, Debug)]
998999
pub(crate) struct CfgInfo {
1000+
/// List of `doc(auto_cfg(hide(...)))` cfgs.
9991001
hidden_cfg: FxHashSet<Cfg>,
1002+
/// Current computed `cfg`. Each time we enter a new item, this field is updated as well while
1003+
/// taking into account the `hidden_cfg` information.
10001004
current_cfg: Cfg,
1005+
/// Whether the `doc(auto_cfg())` feature is enabled or not at this point.
10011006
auto_cfg_active: bool,
1007+
/// If the parent item used `doc(cfg(...))`, then we don't want to overwrite `current_cfg`,
1008+
/// instead we will concatenate with it. However, if it's not the case, we need to overwrite
1009+
/// `current_cfg`.
10021010
parent_is_doc_cfg: bool,
10031011
}
10041012

@@ -1034,6 +1042,11 @@ fn show_hide_show_conflict_error(
10341042
diag.emit();
10351043
}
10361044

1045+
/// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1046+
/// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1047+
///
1048+
/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
1049+
/// and in `new_hide_attrs` arguments.
10371050
fn handle_auto_cfg_hide_show(
10381051
tcx: TyCtxt<'_>,
10391052
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)