Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 28bd22c

Browse files
committedMar 10, 2025
rustdoc: Gate unstable doc(cfg()) predicates
1 parent 3ea711f commit 28bd22c

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed
 

‎src/librustdoc/clean/cfg.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::{mem, ops};
88

99
use rustc_ast::{LitKind, MetaItem, MetaItemInner, MetaItemKind, MetaItemLit};
1010
use rustc_data_structures::fx::FxHashSet;
11-
use rustc_feature::Features;
1211
use rustc_session::parse::ParseSess;
1312
use rustc_span::Span;
1413
use rustc_span::symbol::{Symbol, sym};
@@ -132,18 +131,13 @@ impl Cfg {
132131
/// Checks whether the given configuration can be matched in the current session.
133132
///
134133
/// Equivalent to `attr::cfg_matches`.
135-
// FIXME: Actually make use of `features`.
136-
pub(crate) fn matches(&self, psess: &ParseSess, features: Option<&Features>) -> bool {
134+
pub(crate) fn matches(&self, psess: &ParseSess) -> bool {
137135
match *self {
138136
Cfg::False => false,
139137
Cfg::True => true,
140-
Cfg::Not(ref child) => !child.matches(psess, features),
141-
Cfg::All(ref sub_cfgs) => {
142-
sub_cfgs.iter().all(|sub_cfg| sub_cfg.matches(psess, features))
143-
}
144-
Cfg::Any(ref sub_cfgs) => {
145-
sub_cfgs.iter().any(|sub_cfg| sub_cfg.matches(psess, features))
146-
}
138+
Cfg::Not(ref child) => !child.matches(psess),
139+
Cfg::All(ref sub_cfgs) => sub_cfgs.iter().all(|sub_cfg| sub_cfg.matches(psess)),
140+
Cfg::Any(ref sub_cfgs) => sub_cfgs.iter().any(|sub_cfg| sub_cfg.matches(psess)),
147141
Cfg::Cfg(name, value) => psess.config.contains(&(name, value)),
148142
}
149143
}

‎src/librustdoc/clean/types.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,13 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
10561056
.meta_item()
10571057
.and_then(|item| rustc_expand::config::parse_cfg(item, sess))
10581058
{
1059+
// The result is unused here but we can gate unstable predicates
1060+
rustc_attr_parsing::cfg_matches(
1061+
cfg_mi,
1062+
tcx.sess,
1063+
rustc_ast::CRATE_NODE_ID,
1064+
Some(tcx.features()),
1065+
);
10591066
match Cfg::parse(cfg_mi) {
10601067
Ok(new_cfg) => cfg &= new_cfg,
10611068
Err(e) => {

‎src/librustdoc/doctest/rust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl HirCollector<'_> {
9898
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
9999
if let Some(ref cfg) =
100100
extract_cfg_from_attrs(ast_attrs.iter(), self.tcx, &FxHashSet::default())
101-
&& !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features()))
101+
&& !cfg.matches(&self.tcx.sess.psess)
102102
{
103103
return;
104104
}

‎tests/rustdoc-ui/doc-cfg-unstable.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// #138113: rustdoc didn't gate unstable predicates inside `doc(cfg(..))`
2+
#![feature(doc_cfg)]
3+
4+
// `cfg_boolean_literals`
5+
#[doc(cfg(false))] //~ ERROR `cfg(false)` is experimental and subject to change
6+
pub fn cfg_boolean_literals() {}
7+
8+
// `cfg_version`
9+
#[doc(cfg(sanitize = "thread"))] //~ ERROR `cfg(sanitize)` is experimental and subject to change
10+
pub fn cfg_sanitize() {}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0658]: `cfg(false)` is experimental and subject to change
2+
--> $DIR/doc-cfg-unstable.rs:5:11
3+
|
4+
LL | #[doc(cfg(false))]
5+
| ^^^^^
6+
|
7+
= note: see issue #131204 <https://github.com/rust-lang/rust/issues/131204> for more information
8+
= help: add `#![feature(cfg_boolean_literals)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: `cfg(sanitize)` is experimental and subject to change
12+
--> $DIR/doc-cfg-unstable.rs:9:11
13+
|
14+
LL | #[doc(cfg(sanitize = "thread"))]
15+
| ^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #39699 <https://github.com/rust-lang/rust/issues/39699> for more information
18+
= help: add `#![feature(cfg_sanitize)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)
Failed to load comments.