Skip to content

Commit 2cb8057

Browse files
Update check_doc_cfg pass in rustdoc, remove old rustc_attr_parsing::cfg_matches API
1 parent 324fa7b commit 2cb8057

File tree

2 files changed

+49
-91
lines changed

2 files changed

+49
-91
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg_old.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use rustc_ast_pretty::pprust;
33
use rustc_feature::{Features, GatedCfg, find_gated_cfg};
44
use rustc_hir::RustcVersion;
55
use rustc_session::Session;
6-
use rustc_session::config::ExpectedValues;
7-
use rustc_session::lint::builtin::UNEXPECTED_CFGS;
86
use rustc_session::lint::{BuiltinLintDiag, Lint};
97
use rustc_session::parse::feature_err;
108
use rustc_span::{Span, Symbol, sym};
@@ -36,44 +34,6 @@ pub struct Condition {
3634
pub span: Span,
3735
}
3836

39-
/// Tests if a cfg-pattern matches the cfg set
40-
pub fn cfg_matches(
41-
cfg: &MetaItemInner,
42-
sess: &Session,
43-
lint_emitter: impl CfgMatchesLintEmitter,
44-
features: Option<&Features>,
45-
) -> bool {
46-
eval_condition(cfg, sess, features, &mut |cfg| {
47-
try_gate_cfg(cfg.name, cfg.span, sess, features);
48-
match sess.psess.check_config.expecteds.get(&cfg.name) {
49-
Some(ExpectedValues::Some(values)) if !values.contains(&cfg.value) => {
50-
lint_emitter.emit_span_lint(
51-
sess,
52-
UNEXPECTED_CFGS,
53-
cfg.span,
54-
BuiltinLintDiag::UnexpectedCfgValue(
55-
(cfg.name, cfg.name_span),
56-
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
57-
),
58-
);
59-
}
60-
None if sess.psess.check_config.exhaustive_names => {
61-
lint_emitter.emit_span_lint(
62-
sess,
63-
UNEXPECTED_CFGS,
64-
cfg.span,
65-
BuiltinLintDiag::UnexpectedCfgName(
66-
(cfg.name, cfg.name_span),
67-
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
68-
),
69-
);
70-
}
71-
_ => { /* not unexpected */ }
72-
}
73-
sess.psess.config.contains(&(cfg.name, cfg.value))
74-
})
75-
}
76-
7737
pub fn try_gate_cfg(name: Symbol, span: Span, sess: &Session, features: Option<&Features>) {
7838
let gate = find_gated_cfg(|sym| sym == name);
7939
if let (Some(feats), Some(gated_cfg)) = (features, gate) {
Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#![allow(dead_code, unused_imports)]
2-
3-
use rustc_hir::HirId;
1+
use rustc_attr_parsing::{ShouldEmit, eval_config_entry};
2+
use rustc_hir::attrs::AttributeKind;
43
use rustc_hir::def_id::LocalDefId;
4+
use rustc_hir::{Attribute, HirId};
55
use rustc_middle::ty::TyCtxt;
6-
use rustc_span::sym;
76

87
use super::Pass;
98
use crate::clean::{Attributes, Crate, Item};
@@ -16,59 +15,58 @@ pub(crate) const CHECK_DOC_CFG: Pass = Pass {
1615
description: "checks `#[doc(cfg(...))]` for stability feature and unexpected cfgs",
1716
};
1817

19-
pub(crate) fn check_doc_cfg(krate: Crate, _cx: &mut DocContext<'_>) -> Crate {
20-
// let mut checker = DocCfgChecker { cx };
21-
// checker.visit_crate(&krate);
18+
pub(crate) fn check_doc_cfg(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
19+
let mut checker = DocCfgChecker { cx };
20+
checker.visit_crate(&krate);
2221
krate
2322
}
2423

25-
// struct RustdocCfgMatchesLintEmitter<'a>(TyCtxt<'a>, HirId);
24+
struct RustdocCfgMatchesLintEmitter<'a>(TyCtxt<'a>, HirId);
2625

27-
// impl<'a> rustc_attr_parsing::CfgMatchesLintEmitter for RustdocCfgMatchesLintEmitter<'a> {
28-
// fn emit_span_lint(
29-
// &self,
30-
// sess: &rustc_session::Session,
31-
// lint: &'static rustc_lint::Lint,
32-
// sp: rustc_span::Span,
33-
// builtin_diag: rustc_lint_defs::BuiltinLintDiag,
34-
// ) {
35-
// self.0.node_span_lint(lint, self.1, sp, |diag| {
36-
// rustc_lint::decorate_builtin_lint(sess, Some(self.0), builtin_diag, diag)
37-
// });
38-
// }
39-
// }
26+
impl<'a> rustc_attr_parsing::CfgMatchesLintEmitter for RustdocCfgMatchesLintEmitter<'a> {
27+
fn emit_span_lint(
28+
&self,
29+
sess: &rustc_session::Session,
30+
lint: &'static rustc_lint::Lint,
31+
sp: rustc_span::Span,
32+
builtin_diag: rustc_lint_defs::BuiltinLintDiag,
33+
) {
34+
self.0.node_span_lint(lint, self.1, sp, |diag| {
35+
rustc_lint::decorate_builtin_lint(sess, Some(self.0), builtin_diag, diag)
36+
});
37+
}
38+
}
4039

41-
// struct DocCfgChecker<'a, 'tcx> {
42-
// cx: &'a mut DocContext<'tcx>,
43-
// }
40+
struct DocCfgChecker<'a, 'tcx> {
41+
cx: &'a mut DocContext<'tcx>,
42+
}
4443

45-
// impl DocCfgChecker<'_, '_> {
46-
// fn check_attrs(&mut self, attrs: &Attributes, did: LocalDefId) {
47-
// for attr in &attrs.other_attrs {
48-
// let Attribute::Parsed(AttributeKind::Doc(d)) = attr else { continue };
49-
// let Some(doc_cfg) = d.cfg else { continue };
44+
impl DocCfgChecker<'_, '_> {
45+
fn check_attrs(&mut self, attrs: &Attributes, did: LocalDefId) {
46+
for attr in &attrs.other_attrs {
47+
let Attribute::Parsed(AttributeKind::Doc(d)) = attr else { continue };
5048

51-
// if let Some([cfg_mi]) = doc_cfg.meta_item_list() {
52-
// let _ = rustc_attr_parsing::cfg_matches(
53-
// cfg_mi,
54-
// &self.cx.tcx.sess,
55-
// RustdocCfgMatchesLintEmitter(
56-
// self.cx.tcx,
57-
// self.cx.tcx.local_def_id_to_hir_id(did),
58-
// ),
59-
// Some(self.cx.tcx.features()),
60-
// );
61-
// }
62-
// }
63-
// }
64-
// }
49+
for doc_cfg in &d.cfg {
50+
let _ = eval_config_entry(
51+
&self.cx.tcx.sess,
52+
doc_cfg,
53+
&RustdocCfgMatchesLintEmitter(
54+
self.cx.tcx,
55+
self.cx.tcx.local_def_id_to_hir_id(did),
56+
),
57+
ShouldEmit::ErrorsAndLints,
58+
);
59+
}
60+
}
61+
}
62+
}
6563

66-
// impl DocVisitor<'_> for DocCfgChecker<'_, '_> {
67-
// fn visit_item(&mut self, item: &'_ Item) {
68-
// if let Some(Some(local_did)) = item.def_id().map(|did| did.as_local()) {
69-
// self.check_attrs(&item.attrs, local_did);
70-
// }
64+
impl DocVisitor<'_> for DocCfgChecker<'_, '_> {
65+
fn visit_item(&mut self, item: &'_ Item) {
66+
if let Some(Some(local_did)) = item.def_id().map(|did| did.as_local()) {
67+
self.check_attrs(&item.attrs, local_did);
68+
}
7169

72-
// self.visit_item_recur(item);
73-
// }
74-
// }
70+
self.visit_item_recur(item);
71+
}
72+
}

0 commit comments

Comments
 (0)