Skip to content

Commit 4923d4b

Browse files
committed
rustdoc: use custom CfgMatchesLintEmitter to make check-cfg work
1 parent 3df3290 commit 4923d4b

File tree

8 files changed

+95
-19
lines changed

8 files changed

+95
-19
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,12 @@ pub(crate) fn merge_attrs(
409409
} else {
410410
Attributes::from_hir(&both)
411411
},
412-
extract_cfg_from_attrs(both.iter(), cx.tcx, &cx.cache.hidden_cfg),
412+
extract_cfg_from_attrs(both.iter(), cx.tcx, None, &cx.cache.hidden_cfg),
413413
)
414414
} else {
415415
(
416416
Attributes::from_hir(old_attrs),
417-
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
417+
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, None, &cx.cache.hidden_cfg),
418418
)
419419
}
420420
}

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ fn generate_item_with_correct_attrs(
210210
Cow::Owned(attr) => attr,
211211
}),
212212
cx.tcx,
213+
def_id.as_local().map(|did| cx.tcx.local_def_id_to_hir_id(did)),
213214
&cx.cache.hidden_cfg,
214215
);
215216
let attrs = Attributes::from_hir_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);

src/librustdoc/clean/types.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
1010
use rustc_hir::def::{CtorKind, DefKind, Res};
1111
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId};
1212
use rustc_hir::lang_items::LangItem;
13-
use rustc_hir::{BodyId, Mutability};
13+
use rustc_hir::{BodyId, HirId, Mutability};
1414
use rustc_index::IndexVec;
15+
use rustc_lint_defs::{BuiltinLintDiag, Lint};
1516
use rustc_metadata::rendered_const;
1617
use rustc_middle::span_bug;
1718
use rustc_middle::ty::fast_reject::SimplifiedType;
@@ -475,7 +476,12 @@ impl Item {
475476
name,
476477
kind,
477478
Attributes::from_hir(hir_attrs),
478-
extract_cfg_from_attrs(hir_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
479+
extract_cfg_from_attrs(
480+
hir_attrs.iter(),
481+
cx.tcx,
482+
def_id.as_local().map(|did| cx.tcx.local_def_id_to_hir_id(did)),
483+
&cx.cache.hidden_cfg,
484+
),
479485
)
480486
}
481487

@@ -1014,6 +1020,7 @@ pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
10141020
pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> + Clone>(
10151021
attrs: I,
10161022
tcx: TyCtxt<'_>,
1023+
hir_id: Option<HirId>,
10171024
hidden_cfg: &FxHashSet<Cfg>,
10181025
) -> Option<Arc<Cfg>> {
10191026
let doc_cfg_active = tcx.features().doc_cfg();
@@ -1037,6 +1044,32 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
10371044
.peekable();
10381045
if doc_cfg.peek().is_some() && doc_cfg_active {
10391046
let sess = tcx.sess;
1047+
1048+
struct RustdocCfgMatchesLintEmitter<'a>(TyCtxt<'a>, Option<HirId>);
1049+
1050+
impl<'a> rustc_attr_parsing::CfgMatchesLintEmitter for RustdocCfgMatchesLintEmitter<'a> {
1051+
fn emit_span_lint(
1052+
&self,
1053+
sess: &Session,
1054+
lint: &'static Lint,
1055+
sp: rustc_span::Span,
1056+
builtin_diag: BuiltinLintDiag,
1057+
) {
1058+
if let Some(hir_id) = self.1 {
1059+
self.0.node_span_lint(lint, hir_id, sp, |diag| {
1060+
rustc_lint::decorate_builtin_lint(
1061+
sess,
1062+
Some(self.0),
1063+
builtin_diag,
1064+
diag,
1065+
)
1066+
});
1067+
} else {
1068+
// no HIR id, probably in another crate don't lint
1069+
}
1070+
}
1071+
}
1072+
10401073
doc_cfg.fold(Cfg::True, |mut cfg, item| {
10411074
if let Some(cfg_mi) =
10421075
item.meta_item().and_then(|item| rustc_expand::config::parse_cfg(item, sess))
@@ -1045,7 +1078,7 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
10451078
rustc_attr_parsing::cfg_matches(
10461079
cfg_mi,
10471080
tcx.sess,
1048-
rustc_ast::CRATE_NODE_ID,
1081+
RustdocCfgMatchesLintEmitter(tcx, hir_id),
10491082
Some(tcx.features()),
10501083
);
10511084
match Cfg::parse(cfg_mi) {

src/librustdoc/doctest/rust.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ impl HirCollector<'_> {
9595
nested: F,
9696
) {
9797
let ast_attrs = self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
98-
if let Some(ref cfg) =
99-
extract_cfg_from_attrs(ast_attrs.iter(), self.tcx, &FxHashSet::default())
100-
&& !cfg.matches(&self.tcx.sess.psess)
98+
if let Some(ref cfg) = extract_cfg_from_attrs(
99+
ast_attrs.iter(),
100+
self.tcx,
101+
Some(self.tcx.local_def_id_to_hir_id(def_id)),
102+
&FxHashSet::default(),
103+
) && !cfg.matches(&self.tcx.sess.psess)
101104
{
102105
return;
103106
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
warning: unexpected `cfg` condition name: `foo`
2+
--> $DIR/doc-cfg-check-cfg.rs:13:11
3+
|
4+
LL | #[doc(cfg(foo))]
5+
| ^^^
6+
|
7+
= help: to expect this configuration use `--check-cfg=cfg(foo)`
8+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
9+
= note: `#[warn(unexpected_cfgs)]` on by default
10+
11+
warning: 1 warning emitted
12+

tests/rustdoc-ui/doc-cfg-check-cfg.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
// Ensure that `doc(cfg())` respects `check-cfg`
22
// Currently not properly working
3-
#![feature(doc_cfg)]
4-
#![deny(unexpected_cfgs)]
53

6-
//@revisions: no_check cfg_empty cfg_foo
4+
//@ check-pass
5+
//@ no-auto-check-cfg
6+
7+
//@ revisions: no_check cfg_empty cfg_foo
78
//@[cfg_empty] compile-flags: --check-cfg cfg()
89
//@[cfg_foo] compile-flags: --check-cfg cfg(foo)
910

10-
//@[no_check] check-pass
11-
//@[cfg_empty] check-pass
12-
//@[cfg_empty] known-bug: #138358
13-
//@[cfg_foo] check-pass
11+
#![feature(doc_cfg)]
1412

1513
#[doc(cfg(foo))]
14+
//[cfg_empty]~^ WARN unexpected `cfg` condition name: `foo`
1615
pub fn foo() {}
16+
17+
pub mod module {
18+
#[allow(unexpected_cfgs)]
19+
#[doc(cfg(bar))]
20+
pub fn bar() {}
21+
}

tests/rustdoc-ui/doc-cfg.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#[doc(cfg(), cfg(foo, bar))]
44
//~^ ERROR
55
//~^^ ERROR
6-
#[doc(cfg(foo), cfg(bar))] // ok!
6+
#[doc(cfg(foo), cfg(bar))]
7+
//~^ WARN unexpected `cfg` condition name: `foo`
8+
//~^^ WARN unexpected `cfg` condition name: `bar`
79
#[doc(cfg())] //~ ERROR
810
#[doc(cfg(foo, bar))] //~ ERROR
911
pub fn foo() {}

tests/rustdoc-ui/doc-cfg.stderr

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,37 @@ error: multiple `cfg` predicates are specified
1010
LL | #[doc(cfg(), cfg(foo, bar))]
1111
| ^^^
1212

13+
warning: unexpected `cfg` condition name: `foo`
14+
--> $DIR/doc-cfg.rs:6:11
15+
|
16+
LL | #[doc(cfg(foo), cfg(bar))]
17+
| ^^^
18+
|
19+
= help: expected names are: `FALSE` and `test` and 31 more
20+
= help: to expect this configuration use `--check-cfg=cfg(foo)`
21+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
22+
= note: `#[warn(unexpected_cfgs)]` on by default
23+
24+
warning: unexpected `cfg` condition name: `bar`
25+
--> $DIR/doc-cfg.rs:6:21
26+
|
27+
LL | #[doc(cfg(foo), cfg(bar))]
28+
| ^^^
29+
|
30+
= help: to expect this configuration use `--check-cfg=cfg(bar)`
31+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
32+
1333
error: `cfg` predicate is not specified
14-
--> $DIR/doc-cfg.rs:7:7
34+
--> $DIR/doc-cfg.rs:9:7
1535
|
1636
LL | #[doc(cfg())]
1737
| ^^^^^ help: expected syntax is: `cfg(/* predicate */)`
1838

1939
error: multiple `cfg` predicates are specified
20-
--> $DIR/doc-cfg.rs:8:16
40+
--> $DIR/doc-cfg.rs:10:16
2141
|
2242
LL | #[doc(cfg(foo, bar))]
2343
| ^^^
2444

25-
error: aborting due to 4 previous errors
45+
error: aborting due to 4 previous errors; 2 warnings emitted
2646

0 commit comments

Comments
 (0)