Skip to content

Commit 9362ab5

Browse files
Improve code and fix typo
1 parent 653e103 commit 9362ab5

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
@@ -140,9 +140,6 @@ passes_doc_attribute_not_attribute =
140140
nonexistent builtin attribute `{$attribute}` used in `#[doc(attribute = "...")]`
141141
.help = only existing builtin attributes are allowed in core/std
142142
143-
passes_doc_cfg_hide_takes_list =
144-
`#[doc(cfg_hide(...))]` takes a list of attributes
145-
146143
passes_doc_auto_cfg_expects_hide_or_show =
147144
only `hide` or `show` are allowed in `#[doc(auto_cfg(...))]`
148145

compiler/rustc_passes/src/check_attr.rs

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

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

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
@@ -720,9 +720,9 @@ pass `--doctest-build-arg ARG` for each argument `ARG`.
720720

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

723-
## `#[doc(cfg)]`
723+
## `#[doc(cfg)]` and `#[doc(auto_cfg)]`
724724

725-
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`).
725+
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`).
726726

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

@@ -736,25 +736,6 @@ This features adds the following attributes:
736736

737737
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.
738738

739-
### `#[doc(auto_cfg)`/`#[doc(auto_cfg = true)]`/`#[doc(auto_cfg = false)]`
740-
741-
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.
742-
743-
This attribute impacts the item on which it is used and its descendants.
744-
745-
So if we take back the previous example:
746-
747-
```rust
748-
#[cfg(feature = "futures-io")]
749-
pub mod futures {}
750-
```
751-
752-
There's no need to "duplicate" the `cfg` into a `doc(cfg())` to make Rustdoc display it.
753-
754-
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.
755-
756-
If no argument is specified (ie `#[doc(auto_cfg)]`), it's the same as writing `#[doc(auto_cfg = true)]`.
757-
758739
### `#[doc(cfg(...))]`
759740

760741
This attribute provides a standardized format to override `#[cfg()]` attributes to document conditionally available items. Example:
@@ -927,6 +908,25 @@ Using this attribute will re-enable `auto_cfg` if it was disabled at this locati
927908
pub fn foo() {}
928909
```
929910

911+
### `#[doc(auto_cfg)`/`#[doc(auto_cfg = true)]`/`#[doc(auto_cfg = false)]`
912+
913+
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.
914+
915+
This attribute impacts the item on which it is used and its descendants.
916+
917+
So if we take back the previous example:
918+
919+
```rust
920+
#[cfg(feature = "futures-io")]
921+
pub mod futures {}
922+
```
923+
924+
There's no need to "duplicate" the `cfg` into a `doc(cfg())` to make Rustdoc display it.
925+
926+
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.
927+
928+
If no argument is specified (ie `#[doc(auto_cfg)]`), it's the same as writing `#[doc(auto_cfg = true)]`.
929+
930930
## Inheritance
931931

932932
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
@@ -925,7 +925,7 @@ pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
925925
/// This type keeps track of (doc) cfg information as we go down the item tree.
926926
#[derive(Clone, Debug)]
927927
pub(crate) struct CfgInfo {
928-
/// List of currently active `doc(auto_cfg(hide(...)))` cfgs,minus currently active
928+
/// List of currently active `doc(auto_cfg(hide(...)))` cfgs, minus currently active
929929
/// `doc(auto_cfg(show(...)))` cfgs.
930930
hidden_cfg: FxHashSet<Cfg>,
931931
/// Current computed `cfg`. Each time we enter a new item, this field is updated as well while
@@ -942,13 +942,11 @@ pub(crate) struct CfgInfo {
942942
impl Default for CfgInfo {
943943
fn default() -> Self {
944944
Self {
945-
hidden_cfg: [
945+
hidden_cfg: FxHashSet::from_iter([
946946
Cfg::Cfg(sym::test, None),
947947
Cfg::Cfg(sym::doc, None),
948948
Cfg::Cfg(sym::doctest, None),
949-
]
950-
.into_iter()
951-
.collect(),
949+
]),
952950
current_cfg: Cfg::True,
953951
auto_cfg_active: true,
954952
parent_is_doc_cfg: false,
@@ -990,7 +988,7 @@ fn handle_auto_cfg_hide_show(
990988
&& let MetaItemKind::List(items) = &item.kind
991989
{
992990
for item in items {
993-
// Cfg parsing errors should already have been reported in `rustc_passes::check_attr`.
991+
// FIXME: Report in case `Cfg::parse` reports an error?
994992
if let Ok(Cfg::Cfg(key, value)) = Cfg::parse(item) {
995993
if is_show {
996994
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)