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 a3e5157

Browse files
authoredMar 24, 2025
Unrolled build for rust-lang#138293
Rollup merge of rust-lang#138293 - clubby789:doc-cfg-gate, r=GuillaumeGomez rustdoc: Gate unstable `doc(cfg())` predicates Fixes rust-lang#138113 Since the extraction process treats `cfg(true)` as having no cfg attribute, we have to do the gating during parsing; so we remove the unused `features` arg from `Cfg::matches`
2 parents aa8f0fd + 85b1116 commit a3e5157

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-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
@@ -1068,6 +1068,13 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
10681068
.meta_item()
10691069
.and_then(|item| rustc_expand::config::parse_cfg(item, sess))
10701070
{
1071+
// The result is unused here but we can gate unstable predicates
1072+
rustc_attr_parsing::cfg_matches(
1073+
cfg_mi,
1074+
tcx.sess,
1075+
rustc_ast::CRATE_NODE_ID,
1076+
Some(tcx.features()),
1077+
);
10711078
match Cfg::parse(cfg_mi) {
10721079
Ok(new_cfg) => cfg &= new_cfg,
10731080
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-check-cfg.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Ensure that `doc(cfg())` respects `check-cfg`
2+
// Currently not properly working
3+
#![feature(doc_cfg)]
4+
#![deny(unexpected_cfgs)]
5+
6+
//@revisions: no_check cfg_empty cfg_foo
7+
//@[cfg_empty] compile-flags: --check-cfg cfg()
8+
//@[cfg_foo] compile-flags: --check-cfg cfg(foo)
9+
10+
//@[no_check] check-pass
11+
//@[cfg_empty] check-pass
12+
//@[cfg_empty] known-bug: #138358
13+
//@[cfg_foo] check-pass
14+
15+
#[doc(cfg(foo))]
16+
pub fn foo() {}

‎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.