diff options
| author | Urgau <urgau@numericable.fr> | 2025-05-10 15:15:54 +0200 |
|---|---|---|
| committer | Urgau <urgau@numericable.fr> | 2025-05-26 21:52:21 +0200 |
| commit | 3fd0265fbb236c8309f2f913cf9946e64818f643 (patch) | |
| tree | 087850b011c4f037f751da64b1cfae64d5e2c709 /src/librustdoc/clean | |
| parent | 93f3db25c0e554d3143bed945e2671a14aa5288d (diff) | |
| download | rust-3fd0265fbb236c8309f2f913cf9946e64818f643.tar.gz rust-3fd0265fbb236c8309f2f913cf9946e64818f643.zip | |
rustdoc: use custom `CfgMatchesLintEmitter` to make check-cfg work
Diffstat (limited to 'src/librustdoc/clean')
| -rw-r--r-- | src/librustdoc/clean/inline.rs | 4 | ||||
| -rw-r--r-- | src/librustdoc/clean/mod.rs | 1 | ||||
| -rw-r--r-- | src/librustdoc/clean/types.rs | 39 |
3 files changed, 39 insertions, 5 deletions
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 55a116a018a..f25cf606812 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -409,12 +409,12 @@ pub(crate) fn merge_attrs( } else { Attributes::from_hir(&both) }, - extract_cfg_from_attrs(both.iter(), cx.tcx, &cx.cache.hidden_cfg), + extract_cfg_from_attrs(both.iter(), cx.tcx, None, &cx.cache.hidden_cfg), ) } else { ( Attributes::from_hir(old_attrs), - extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg), + extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, None, &cx.cache.hidden_cfg), ) } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 28dfa01534e..bacc7b50018 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -210,6 +210,7 @@ fn generate_item_with_correct_attrs( Cow::Owned(attr) => attr, }), cx.tcx, + def_id.as_local().map(|did| cx.tcx.local_def_id_to_hir_id(did)), &cx.cache.hidden_cfg, ); let attrs = Attributes::from_hir_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false); diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index bb3469867d5..0f92aab5abe 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -12,8 +12,9 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; use rustc_hir::def::{CtorKind, DefKind, Res}; use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId}; use rustc_hir::lang_items::LangItem; -use rustc_hir::{BodyId, Mutability}; +use rustc_hir::{BodyId, HirId, Mutability}; use rustc_index::IndexVec; +use rustc_lint_defs::{BuiltinLintDiag, Lint}; use rustc_metadata::rendered_const; use rustc_middle::span_bug; use rustc_middle::ty::fast_reject::SimplifiedType; @@ -477,7 +478,12 @@ impl Item { name, kind, Attributes::from_hir(hir_attrs), - extract_cfg_from_attrs(hir_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg), + extract_cfg_from_attrs( + hir_attrs.iter(), + cx.tcx, + def_id.as_local().map(|did| cx.tcx.local_def_id_to_hir_id(did)), + &cx.cache.hidden_cfg, + ), ) } @@ -1033,6 +1039,7 @@ pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>( pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> + Clone>( attrs: I, tcx: TyCtxt<'_>, + hir_id: Option<HirId>, hidden_cfg: &FxHashSet<Cfg>, ) -> Option<Arc<Cfg>> { let doc_cfg_active = tcx.features().doc_cfg(); @@ -1056,6 +1063,32 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> .peekable(); if doc_cfg.peek().is_some() && doc_cfg_active { let sess = tcx.sess; + + struct RustdocCfgMatchesLintEmitter<'a>(TyCtxt<'a>, Option<HirId>); + + impl<'a> rustc_attr_parsing::CfgMatchesLintEmitter for RustdocCfgMatchesLintEmitter<'a> { + fn emit_span_lint( + &self, + sess: &Session, + lint: &'static Lint, + sp: rustc_span::Span, + builtin_diag: BuiltinLintDiag, + ) { + if let Some(hir_id) = self.1 { + self.0.node_span_lint(lint, hir_id, sp, |diag| { + rustc_lint::decorate_builtin_lint( + sess, + Some(self.0), + builtin_diag, + diag, + ) + }); + } else { + // No HIR id. Probably in another crate. Don't lint. + } + } + } + doc_cfg.fold(Cfg::True, |mut cfg, item| { if let Some(cfg_mi) = item.meta_item().and_then(|item| rustc_expand::config::parse_cfg(item, sess)) @@ -1064,7 +1097,7 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> rustc_attr_parsing::cfg_matches( cfg_mi, tcx.sess, - rustc_ast::CRATE_NODE_ID, + RustdocCfgMatchesLintEmitter(tcx, hir_id), Some(tcx.features()), ); match Cfg::parse(cfg_mi) { |
