Skip to content

Commit d1277cc

Browse files
Update check_doc_cfg pass in rustdoc, remove old rustc_attr_parsing::cfg_matches API
1 parent 9c8c67b commit d1277cc

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
@@ -4,8 +4,6 @@ use rustc_feature::{Features, GatedCfg, find_gated_cfg};
44
use rustc_hir::RustcVersion;
55
use rustc_hir::lints::AttributeLintKind;
66
use rustc_session::Session;
7-
use rustc_session::config::ExpectedValues;
8-
use rustc_session::lint::builtin::UNEXPECTED_CFGS;
97
use rustc_session::lint::{BuiltinLintDiag, Lint};
108
use rustc_session::parse::feature_err;
119
use rustc_span::{Span, Symbol, sym};
@@ -37,44 +35,6 @@ pub struct Condition {
3735
pub span: Span,
3836
}
3937

40-
/// Tests if a cfg-pattern matches the cfg set
41-
pub fn cfg_matches(
42-
cfg: &MetaItemInner,
43-
sess: &Session,
44-
lint_emitter: impl CfgMatchesLintEmitter,
45-
features: Option<&Features>,
46-
) -> bool {
47-
eval_condition(cfg, sess, features, &mut |cfg| {
48-
try_gate_cfg(cfg.name, cfg.span, sess, features);
49-
match sess.psess.check_config.expecteds.get(&cfg.name) {
50-
Some(ExpectedValues::Some(values)) if !values.contains(&cfg.value) => {
51-
lint_emitter.emit_span_lint(
52-
sess,
53-
UNEXPECTED_CFGS,
54-
cfg.span,
55-
BuiltinLintDiag::AttributeLint(AttributeLintKind::UnexpectedCfgValue(
56-
(cfg.name, cfg.name_span),
57-
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
58-
)),
59-
);
60-
}
61-
None if sess.psess.check_config.exhaustive_names => {
62-
lint_emitter.emit_span_lint(
63-
sess,
64-
UNEXPECTED_CFGS,
65-
cfg.span,
66-
BuiltinLintDiag::AttributeLint(AttributeLintKind::UnexpectedCfgName(
67-
(cfg.name, cfg.name_span),
68-
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
69-
)),
70-
);
71-
}
72-
_ => { /* not unexpected */ }
73-
}
74-
sess.psess.config.contains(&(cfg.name, cfg.value))
75-
})
76-
}
77-
7838
pub fn try_gate_cfg(name: Symbol, span: Span, sess: &Session, features: Option<&Features>) {
7939
let gate = find_gated_cfg(|sym| sym == name);
8040
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)