Skip to content

Commit 123a308

Browse files
Improve code and fix typo
1 parent 605b60b commit 123a308

File tree

7 files changed

+43
-65
lines changed

7 files changed

+43
-65
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,6 @@ passes_doc_attribute_not_attribute =
149149
nonexistent builtin attribute `{$attribute}` used in `#[doc(attribute = "...")]`
150150
.help = only existing builtin attributes are allowed in core/std
151151
152-
passes_doc_cfg_hide_takes_list =
153-
`#[doc(cfg_hide(...))]` takes a list of attributes
154-
155152
passes_doc_auto_cfg_expects_hide_or_show =
156153
only `hide` or `show` are allowed in `#[doc(auto_cfg(...))]`
157154

compiler/rustc_passes/src/check_attr.rs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11591159
}
11601160
}
11611161

1162-
/// Check that the `#![doc(auto_cfg(..))]` attribute has expected input.
1162+
/// Check that the `#![doc(auto_cfg)]` attribute has the expected input.
11631163
fn check_doc_auto_cfg(&self, meta: &MetaItem, hir_id: HirId) {
11641164
match &meta.kind {
11651165
MetaItemKind::Word => {}
@@ -1175,7 +1175,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11751175
}
11761176
MetaItemKind::List(list) => {
11771177
for item in list {
1178-
let Some(attr_name) = item.name() else {
1178+
let Some(attr_name @ (sym::hide | sym::show)) = item.name() else {
11791179
self.tcx.emit_node_span_lint(
11801180
INVALID_DOC_ATTRIBUTES,
11811181
hir_id,
@@ -1184,36 +1184,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11841184
);
11851185
continue;
11861186
};
1187-
if attr_name != sym::hide && attr_name != sym::show {
1188-
self.tcx.emit_node_span_lint(
1189-
INVALID_DOC_ATTRIBUTES,
1190-
hir_id,
1191-
meta.span,
1192-
errors::DocAutoCfgExpectsHideOrShow,
1193-
);
1194-
} else if let Some(list) = item.meta_item_list() {
1187+
if let Some(list) = item.meta_item_list() {
11951188
for item in list {
1196-
if item.meta_item_list().is_some() {
1197-
self.tcx.emit_node_span_lint(
1198-
INVALID_DOC_ATTRIBUTES,
1199-
hir_id,
1200-
item.span(),
1201-
errors::DocAutoCfgHideShowUnexpectedItem {
1202-
attr_name: attr_name.as_str(),
1203-
},
1204-
);
1205-
} else if match item {
1206-
MetaItemInner::Lit(_) => true,
1207-
// We already checked above that it's not a list.
1208-
MetaItemInner::MetaItem(meta) => meta.path.segments.len() != 1,
1209-
} {
1189+
let valid = item.meta_item().is_some_and(|meta| {
1190+
meta.path.segments.len() == 1
1191+
&& matches!(
1192+
&meta.kind,
1193+
MetaItemKind::Word | MetaItemKind::NameValue(_)
1194+
)
1195+
});
1196+
if !valid {
12101197
self.tcx.emit_node_span_lint(
12111198
INVALID_DOC_ATTRIBUTES,
12121199
hir_id,
12131200
item.span(),
1214-
errors::DocAutoCfgHideShowUnexpectedItem {
1215-
attr_name: attr_name.as_str(),
1216-
},
1201+
errors::DocAutoCfgHideShowUnexpectedItem { attr_name },
12171202
);
12181203
}
12191204
}
@@ -1222,7 +1207,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12221207
INVALID_DOC_ATTRIBUTES,
12231208
hir_id,
12241209
meta.span,
1225-
errors::DocAutoCfgHideShowExpectsList { attr_name: attr_name.as_str() },
1210+
errors::DocAutoCfgHideShowExpectsList { attr_name },
12261211
);
12271212
}
12281213
}

compiler/rustc_passes/src/errors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,14 @@ pub(crate) struct DocAutoCfgExpectsHideOrShow;
318318

319319
#[derive(LintDiagnostic)]
320320
#[diag(passes_doc_auto_cfg_hide_show_expects_list)]
321-
pub(crate) struct DocAutoCfgHideShowExpectsList<'a> {
322-
pub attr_name: &'a str,
321+
pub(crate) struct DocAutoCfgHideShowExpectsList {
322+
pub attr_name: Symbol,
323323
}
324324

325325
#[derive(LintDiagnostic)]
326326
#[diag(passes_doc_auto_cfg_hide_show_unexpected_item)]
327-
pub(crate) struct DocAutoCfgHideShowUnexpectedItem<'a> {
328-
pub attr_name: &'a str,
327+
pub(crate) struct DocAutoCfgHideShowUnexpectedItem {
328+
pub attr_name: Symbol,
329329
}
330330

331331
#[derive(LintDiagnostic)]

compiler/rustc_span/src/symbol.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ symbols! {
629629
cfg_emscripten_wasm_eh,
630630
cfg_eval,
631631
cfg_fmt_debug,
632-
cfg_hide,
633632
cfg_overflow_checks,
634633
cfg_panic,
635634
cfg_relocation_model,

src/doc/rustdoc/src/unstable-features.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,9 @@ will be split as follows:
736736

737737
This flag enables the generation of toggles to expand macros in the HTML source code pages.
738738

739-
## `#[doc(cfg)]`
739+
## `#[doc(cfg)]` and `#[doc(auto_cfg)]`
740740

741-
This feature aims at providing rustdoc users the possibility to add visual markers to the rendered documentation to know under which conditions an item is available (currently possible through the following unstable features: `doc_cfg`, `doc_auto_cfg` and `doc_cfg_hide`).
741+
This feature aims at providing rustdoc users the possibility to add visual markers to the rendered documentation to know under which conditions an item is available (currently possible through the following unstable feature: `doc_cfg`).
742742

743743
It does not aim to allow having a same item with different `cfg`s to appear more than once in the generated documentation.
744744

@@ -752,25 +752,6 @@ This features adds the following attributes:
752752

753753
All of these attributes can be added to a module or to the crate root, and they will be inherited by the child items unless another attribute overrides it. This is why "opposite" attributes like `auto_cfg(hide(...))` and `auto_cfg(show(...))` are provided: they allow a child item to override its parent.
754754

755-
### `#[doc(auto_cfg)`/`#[doc(auto_cfg = true)]`/`#[doc(auto_cfg = false)]`
756-
757-
By default, `#[doc(auto_cfg)]` is enabled at the crate-level. When it's enabled, Rustdoc will automatically display `cfg(...)` compatibility information as-if the same `#[doc(cfg(...))]` had been specified.
758-
759-
This attribute impacts the item on which it is used and its descendants.
760-
761-
So if we take back the previous example:
762-
763-
```rust
764-
#[cfg(feature = "futures-io")]
765-
pub mod futures {}
766-
```
767-
768-
There's no need to "duplicate" the `cfg` into a `doc(cfg())` to make Rustdoc display it.
769-
770-
In some situations, the detailed conditional compilation rules used to implement the feature might not serve as good documentation (for example, the list of supported platforms might be very long, and it might be better to document them in one place). To turn it off, add the `#[doc(auto_cfg = false)]` attribute on the item.
771-
772-
If no argument is specified (ie `#[doc(auto_cfg)]`), it's the same as writing `#[doc(auto_cfg = true)]`.
773-
774755
### `#[doc(cfg(...))]`
775756

776757
This attribute provides a standardized format to override `#[cfg()]` attributes to document conditionally available items. Example:
@@ -943,6 +924,25 @@ Using this attribute will re-enable `auto_cfg` if it was disabled at this locati
943924
pub fn foo() {}
944925
```
945926

927+
### `#[doc(auto_cfg)`/`#[doc(auto_cfg = true)]`/`#[doc(auto_cfg = false)]`
928+
929+
By default, `#[doc(auto_cfg)]` is enabled at the crate-level. When it's enabled, Rustdoc will automatically display `cfg(...)` compatibility information as-if the same `#[doc(cfg(...))]` had been specified.
930+
931+
This attribute impacts the item on which it is used and its descendants.
932+
933+
So if we take back the previous example:
934+
935+
```rust
936+
#[cfg(feature = "futures-io")]
937+
pub mod futures {}
938+
```
939+
940+
There's no need to "duplicate" the `cfg` into a `doc(cfg())` to make Rustdoc display it.
941+
942+
In some situations, the detailed conditional compilation rules used to implement the feature might not serve as good documentation (for example, the list of supported platforms might be very long, and it might be better to document them in one place). To turn it off, add the `#[doc(auto_cfg = false)]` attribute on the item.
943+
944+
If no argument is specified (ie `#[doc(auto_cfg)]`), it's the same as writing `#[doc(auto_cfg = true)]`.
945+
946946
## Inheritance
947947

948948
Rustdoc merges `cfg` attributes from parent modules to its children. For example, in this case, the module `non_unix` will describe the entire compatibility matrix for the module, and not just its directly attached information:

src/librustdoc/clean/types.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
10371037
/// This type keeps track of (doc) cfg information as we go down the item tree.
10381038
#[derive(Clone, Debug)]
10391039
pub(crate) struct CfgInfo {
1040-
/// List of currently active `doc(auto_cfg(hide(...)))` cfgs,minus currently active
1040+
/// List of currently active `doc(auto_cfg(hide(...)))` cfgs, minus currently active
10411041
/// `doc(auto_cfg(show(...)))` cfgs.
10421042
hidden_cfg: FxHashSet<Cfg>,
10431043
/// Current computed `cfg`. Each time we enter a new item, this field is updated as well while
@@ -1054,13 +1054,11 @@ pub(crate) struct CfgInfo {
10541054
impl Default for CfgInfo {
10551055
fn default() -> Self {
10561056
Self {
1057-
hidden_cfg: [
1057+
hidden_cfg: FxHashSet::from_iter([
10581058
Cfg::Cfg(sym::test, None),
10591059
Cfg::Cfg(sym::doc, None),
10601060
Cfg::Cfg(sym::doctest, None),
1061-
]
1062-
.into_iter()
1063-
.collect(),
1061+
]),
10641062
current_cfg: Cfg::True,
10651063
auto_cfg_active: true,
10661064
parent_is_doc_cfg: false,
@@ -1102,7 +1100,7 @@ fn handle_auto_cfg_hide_show(
11021100
&& let MetaItemKind::List(items) = &item.kind
11031101
{
11041102
for item in items {
1105-
// Cfg parsing errors should already have been reported in `rustc_passes::check_attr`.
1103+
// FIXME: Report in case `Cfg::parse` reports an error?
11061104
if let Ok(Cfg::Cfg(key, value)) = Cfg::parse(item) {
11071105
if is_show {
11081106
if let Some(span) = new_hide_attrs.get(&(key, value)) {

src/librustdoc/passes/propagate_doc_cfg.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ impl CfgPropagator<'_, '_> {
9797
//
9898
// Otherwise, `cfg_info` already tracks everything we need so nothing else to do!
9999
if matches!(item.kind, ItemKind::ImplItem(_))
100-
&& let Some(def_id) = item.item_id.as_def_id().and_then(|def_id| def_id.as_local())
100+
&& let Some(mut next_def_id) = item.item_id.as_local_def_id()
101101
{
102-
let mut next_def_id = def_id;
103102
while let Some(parent_def_id) = self.cx.tcx.opt_local_parent(next_def_id) {
104103
let x = load_attrs(self.cx, parent_def_id.to_def_id());
105104
add_only_cfg_attributes(&mut attrs, x);

0 commit comments

Comments
 (0)