Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7c00bcc
Implement RFC 3631
GuillaumeGomez Mar 28, 2025
6537278
Update rustdoc tests
GuillaumeGomez Mar 28, 2025
2d82c99
Add "global" rustdoc test for RFC 3631
GuillaumeGomez Apr 1, 2025
63aefe0
Strenghten checks for `doc(auto_cfg(show/hide))` attributes
GuillaumeGomez Apr 1, 2025
4847eaf
Remove useless code in `propagate_doc_cfg.rs`
GuillaumeGomez Apr 1, 2025
18df897
Remove ui test for doc_cfg feature gate
GuillaumeGomez Apr 7, 2025
d6ba93a
Remove `doc_cfg` related content from rustdoc book unstable features …
GuillaumeGomez Apr 7, 2025
bb34332
Update book for `doc_cfg` feature
GuillaumeGomez Apr 11, 2025
ed05315
Rename `CfgInfo::doc_auto_cfg_active` into `auto_cfg_active`
GuillaumeGomez Apr 11, 2025
c06a076
Put back the `doc_cfg` code behind a nightly feature
GuillaumeGomez Apr 11, 2025
1561efe
Add code documentation, improve code and improve error message
GuillaumeGomez May 27, 2025
553308b
Improve code and better check `doc(cfg(...))` attributes
GuillaumeGomez May 29, 2025
fccba2c
Remove `doc_cfg_hide` feature
GuillaumeGomez May 29, 2025
a7ed9bf
fmt
GuillaumeGomez Jun 2, 2025
ec00723
Fix autodiff feature activation
GuillaumeGomez Jun 4, 2025
254a213
Remove `cfg(bootstrap)` for `doc_cfg` feature following #141925
GuillaumeGomez Jun 10, 2025
6fecff4
Fix `tests/rustdoc/target-feature.rs` test by adding missing `#![feat…
GuillaumeGomez Jul 1, 2025
ef8b2a2
Correctly handle target_feature in rustdoc cfg
GuillaumeGomez Jul 3, 2025
77885fe
Improve code comments and extend tests for `doc_cfg` feature
GuillaumeGomez Aug 26, 2025
653e103
Apply first review round suggestions
GuillaumeGomez Sep 22, 2025
9362ab5
Improve code and fix typo
GuillaumeGomez Sep 25, 2025
6cccea8
Remove `doc_auto_cfg` feature as well
GuillaumeGomez Sep 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
gate_doc!(
"experimental" {
cfg => doc_cfg
cfg_hide => doc_cfg_hide
auto_cfg => doc_cfg
masked => doc_masked
notable_trait => doc_notable_trait
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ declare_features! (
Some("never properly implemented; requires significant design work"), 127655),
/// Allows deriving traits as per `SmartPointer` specification
(removed, derive_smart_pointer, "1.84.0", Some(123430), Some("replaced by `CoercePointee`"), 131284),
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.
(removed, doc_auto_cfg, "1.58.0", Some(43781), Some("merged into `doc_cfg`"), 138907),
/// Allows `#[doc(cfg_hide(...))]`.
(removed, doc_cfg_hide, "1.57.0", Some(43781), Some("merged into `doc_cfg`"), 138907),
/// Allows using `#[doc(keyword = "...")]`.
(removed, doc_keyword, "1.58.0", Some(51315),
Some("merged into `#![feature(rustdoc_internals)]`"), 90420),
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,8 @@ declare_features! (
(incomplete, deref_patterns, "1.79.0", Some(87121)),
/// Allows deriving the From trait on single-field structs.
(unstable, derive_from, "1.91.0", Some(144889)),
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.
(unstable, doc_auto_cfg, "1.58.0", Some(43781)),
/// Allows `#[doc(cfg(...))]`.
(unstable, doc_cfg, "1.21.0", Some(43781)),
/// Allows `#[doc(cfg_hide(...))]`.
(unstable, doc_cfg_hide, "1.57.0", Some(43781)),
/// Allows `#[doc(masked)]`.
(unstable, doc_masked, "1.21.0", Some(44027)),
/// Allows features to allow target_feature to better interact with traits.
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,17 @@ passes_doc_attribute_not_attribute =
nonexistent builtin attribute `{$attribute}` used in `#[doc(attribute = "...")]`
.help = only existing builtin attributes are allowed in core/std

passes_doc_cfg_hide_takes_list =
`#[doc(cfg_hide(...))]` takes a list of attributes
passes_doc_auto_cfg_expects_hide_or_show =
only `hide` or `show` are allowed in `#[doc(auto_cfg(...))]`

passes_doc_auto_cfg_hide_show_expects_list =
`#![doc(auto_cfg({$attr_name}(...)))]` expects a list of items

passes_doc_auto_cfg_hide_show_unexpected_item =
`#![doc(auto_cfg({$attr_name}(...)))]` only accepts identifiers or key/value items

passes_doc_auto_cfg_wrong_literal =
expected boolean for `#[doc(auto_cfg = ...)]`

passes_doc_expect_str =
doc {$attr_name} attribute expects a string: #[doc({$attr_name} = "a")]
Expand Down
71 changes: 56 additions & 15 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::hash_map::Entry;
use std::slice;

use rustc_abi::{Align, ExternAbi, Size};
use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, ast};
use rustc_ast::{AttrStyle, LitKind, MetaItem, MetaItemInner, MetaItemKind, ast};
use rustc_attr_parsing::{AttributeParser, Late};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
Expand Down Expand Up @@ -1160,16 +1160,59 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}

/// Check that the `#![doc(cfg_hide(...))]` attribute only contains a list of attributes.
///
fn check_doc_cfg_hide(&self, meta: &MetaItemInner, hir_id: HirId) {
if meta.meta_item_list().is_none() {
self.tcx.emit_node_span_lint(
INVALID_DOC_ATTRIBUTES,
hir_id,
meta.span(),
errors::DocCfgHideTakesList,
);
/// Check that the `#![doc(auto_cfg)]` attribute has the expected input.
fn check_doc_auto_cfg(&self, meta: &MetaItem, hir_id: HirId) {
match &meta.kind {
MetaItemKind::Word => {}
MetaItemKind::NameValue(lit) => {
if !matches!(lit.kind, LitKind::Bool(_)) {
self.tcx.emit_node_span_lint(
INVALID_DOC_ATTRIBUTES,
hir_id,
meta.span,
errors::DocAutoCfgWrongLiteral,
);
}
}
MetaItemKind::List(list) => {
for item in list {
let Some(attr_name @ (sym::hide | sym::show)) = item.name() else {
self.tcx.emit_node_span_lint(
INVALID_DOC_ATTRIBUTES,
hir_id,
meta.span,
errors::DocAutoCfgExpectsHideOrShow,
);
continue;
};
if let Some(list) = item.meta_item_list() {
for item in list {
let valid = item.meta_item().is_some_and(|meta| {
meta.path.segments.len() == 1
&& matches!(
&meta.kind,
MetaItemKind::Word | MetaItemKind::NameValue(_)
)
});
if !valid {
self.tcx.emit_node_span_lint(
INVALID_DOC_ATTRIBUTES,
hir_id,
item.span(),
errors::DocAutoCfgHideShowUnexpectedItem { attr_name },
);
}
}
} else {
self.tcx.emit_node_span_lint(
INVALID_DOC_ATTRIBUTES,
hir_id,
meta.span,
errors::DocAutoCfgHideShowExpectsList { attr_name },
);
}
}
}
}
}

Expand Down Expand Up @@ -1245,10 +1288,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
self.check_attr_crate_level(attr, style, meta, hir_id);
}

Some(sym::cfg_hide) => {
if self.check_attr_crate_level(attr, style, meta, hir_id) {
self.check_doc_cfg_hide(meta, hir_id);
}
Some(sym::auto_cfg) => {
self.check_doc_auto_cfg(i_meta, hir_id);
}

Some(sym::inline | sym::no_inline) => {
Expand Down
20 changes: 18 additions & 2 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,24 @@ pub(crate) struct DocTestLiteral;
pub(crate) struct DocTestTakesList;

#[derive(LintDiagnostic)]
#[diag(passes_doc_cfg_hide_takes_list)]
pub(crate) struct DocCfgHideTakesList;
#[diag(passes_doc_auto_cfg_wrong_literal)]
pub(crate) struct DocAutoCfgWrongLiteral;

#[derive(LintDiagnostic)]
#[diag(passes_doc_auto_cfg_expects_hide_or_show)]
pub(crate) struct DocAutoCfgExpectsHideOrShow;

#[derive(LintDiagnostic)]
#[diag(passes_doc_auto_cfg_hide_show_expects_list)]
pub(crate) struct DocAutoCfgHideShowExpectsList {
pub attr_name: Symbol,
}

#[derive(LintDiagnostic)]
#[diag(passes_doc_auto_cfg_hide_show_unexpected_item)]
pub(crate) struct DocAutoCfgHideShowUnexpectedItem {
pub attr_name: Symbol,
}

#[derive(LintDiagnostic)]
#[diag(passes_doc_test_unknown_any)]
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ symbols! {
attributes,
audit_that,
augmented_assignments,
auto_cfg,
auto_traits,
autodiff,
autodiff_forward,
Expand Down Expand Up @@ -628,7 +629,6 @@ symbols! {
cfg_emscripten_wasm_eh,
cfg_eval,
cfg_fmt_debug,
cfg_hide,
cfg_overflow_checks,
cfg_panic,
cfg_relocation_model,
Expand Down Expand Up @@ -1149,6 +1149,7 @@ symbols! {
hashset_iter_ty,
hexagon_target_feature,
hidden,
hide,
hint,
homogeneous_aggregate,
host,
Expand Down Expand Up @@ -1987,6 +1988,7 @@ symbols! {
shl_assign,
shorter_tail_lifetimes,
should_panic,
show,
shr,
shr_assign,
sig_dfl,
Expand Down
10 changes: 1 addition & 9 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,7 @@
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
test(no_crate_inject, attr(allow(unused_variables), deny(warnings)))
)]
#![doc(cfg_hide(
not(test),
no_global_oom_handling,
not(no_global_oom_handling),
not(no_rc),
not(no_sync),
target_has_atomic = "ptr"
))]
#![doc(auto_cfg(hide(no_global_oom_handling, no_rc, no_sync, target_has_atomic = "ptr")))]
#![doc(rust_logo)]
#![feature(rustdoc_internals)]
#![no_std]
Expand Down Expand Up @@ -195,7 +188,6 @@
//
// Rustdoc features:
#![feature(doc_cfg)]
#![feature(doc_cfg_hide)]
// Technically, this is a bug in rustdoc: rustdoc sees the documentation on `#[lang = slice_alloc]`
// blocks is for `&[T]`, which also has documentation using this feature in `core`, and gets mad
// that the feature-gate isn't enabled. Ideally, it wouldn't check for the feature gate for docs
Expand Down
5 changes: 2 additions & 3 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
)]
#![doc(rust_logo)]
#![doc(cfg_hide(
#![doc(auto_cfg(hide(
no_fp_fmt_parse,
target_pointer_width = "16",
target_pointer_width = "32",
Expand All @@ -71,7 +71,7 @@
target_has_atomic_load_store = "32",
target_has_atomic_load_store = "64",
target_has_atomic_load_store = "ptr",
))]
)))]
#![no_core]
#![rustc_coherence_is_core]
#![rustc_preserve_ub_checks]
Expand Down Expand Up @@ -149,7 +149,6 @@
#![feature(deprecated_suggestion)]
#![feature(derive_const)]
#![feature(doc_cfg)]
#![feature(doc_cfg_hide)]
#![feature(doc_notable_trait)]
#![feature(extern_types)]
#![feature(f16)]
Expand Down
3 changes: 1 addition & 2 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
)]
#![doc(rust_logo)]
#![doc(cfg_hide(not(test), no_global_oom_handling, not(no_global_oom_handling)))]
#![doc(auto_cfg(hide(no_global_oom_handling)))]
// Don't link to std. We are std.
#![no_std]
// Tell the compiler to link to either panic_abort or panic_unwind
Expand Down Expand Up @@ -285,7 +285,6 @@
#![feature(decl_macro)]
#![feature(deprecated_suggestion)]
#![feature(doc_cfg)]
#![feature(doc_cfg_hide)]
#![feature(doc_masked)]
#![feature(doc_notable_trait)]
#![feature(dropck_eyepatch)]
Expand Down
Loading
Loading