@@ -12,8 +12,9 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
12
12
use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
13
13
use rustc_hir:: def_id:: { CrateNum , DefId , LOCAL_CRATE , LocalDefId } ;
14
14
use rustc_hir:: lang_items:: LangItem ;
15
- use rustc_hir:: { BodyId , Mutability } ;
15
+ use rustc_hir:: { BodyId , HirId , Mutability } ;
16
16
use rustc_index:: IndexVec ;
17
+ use rustc_lint_defs:: { BuiltinLintDiag , Lint } ;
17
18
use rustc_metadata:: rendered_const;
18
19
use rustc_middle:: span_bug;
19
20
use rustc_middle:: ty:: fast_reject:: SimplifiedType ;
@@ -477,7 +478,12 @@ impl Item {
477
478
name,
478
479
kind,
479
480
Attributes :: from_hir ( hir_attrs) ,
480
- extract_cfg_from_attrs ( hir_attrs. iter ( ) , cx. tcx , & cx. cache . hidden_cfg ) ,
481
+ extract_cfg_from_attrs (
482
+ hir_attrs. iter ( ) ,
483
+ cx. tcx ,
484
+ def_id. as_local ( ) . map ( |did| cx. tcx . local_def_id_to_hir_id ( did) ) ,
485
+ & cx. cache . hidden_cfg ,
486
+ ) ,
481
487
)
482
488
}
483
489
@@ -1033,6 +1039,7 @@ pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
1033
1039
pub ( crate ) fn extract_cfg_from_attrs < ' a , I : Iterator < Item = & ' a hir:: Attribute > + Clone > (
1034
1040
attrs : I ,
1035
1041
tcx : TyCtxt < ' _ > ,
1042
+ hir_id : Option < HirId > ,
1036
1043
hidden_cfg : & FxHashSet < Cfg > ,
1037
1044
) -> Option < Arc < Cfg > > {
1038
1045
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>
1056
1063
. peekable ( ) ;
1057
1064
if doc_cfg. peek ( ) . is_some ( ) && doc_cfg_active {
1058
1065
let sess = tcx. sess ;
1066
+
1067
+ struct RustdocCfgMatchesLintEmitter < ' a > ( TyCtxt < ' a > , Option < HirId > ) ;
1068
+
1069
+ impl < ' a > rustc_attr_parsing:: CfgMatchesLintEmitter for RustdocCfgMatchesLintEmitter < ' a > {
1070
+ fn emit_span_lint (
1071
+ & self ,
1072
+ sess : & Session ,
1073
+ lint : & ' static Lint ,
1074
+ sp : rustc_span:: Span ,
1075
+ builtin_diag : BuiltinLintDiag ,
1076
+ ) {
1077
+ if let Some ( hir_id) = self . 1 {
1078
+ self . 0 . node_span_lint ( lint, hir_id, sp, |diag| {
1079
+ rustc_lint:: decorate_builtin_lint (
1080
+ sess,
1081
+ Some ( self . 0 ) ,
1082
+ builtin_diag,
1083
+ diag,
1084
+ )
1085
+ } ) ;
1086
+ } else {
1087
+ // No HIR id. Probably in another crate. Don't lint.
1088
+ }
1089
+ }
1090
+ }
1091
+
1059
1092
doc_cfg. fold ( Cfg :: True , |mut cfg, item| {
1060
1093
if let Some ( cfg_mi) =
1061
1094
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>
1064
1097
rustc_attr_parsing:: cfg_matches (
1065
1098
cfg_mi,
1066
1099
tcx. sess ,
1067
- rustc_ast :: CRATE_NODE_ID ,
1100
+ RustdocCfgMatchesLintEmitter ( tcx , hir_id ) ,
1068
1101
Some ( tcx. features ( ) ) ,
1069
1102
) ;
1070
1103
match Cfg :: parse ( cfg_mi) {
0 commit comments