77use crate :: { errors, fluent_generated as fluent} ;
88use rustc_ast:: { ast, AttrStyle , Attribute , LitKind , MetaItemKind , MetaItemLit , NestedMetaItem } ;
99use rustc_data_structures:: fx:: FxHashMap ;
10- use rustc_errors:: { Applicability , IntoDiagnosticArg , MultiSpan } ;
10+ use rustc_errors:: { Applicability , DecorateLint , IntoDiagnostic , IntoDiagnosticArg , MultiSpan } ;
1111use rustc_feature:: { AttributeDuplicates , AttributeType , BuiltinAttribute , BUILTIN_ATTRIBUTE_MAP } ;
1212use rustc_hir as hir;
1313use rustc_hir:: def_id:: LocalModDefId ;
@@ -871,6 +871,19 @@ impl CheckAttrVisitor<'_> {
871871 true
872872 }
873873
874+ fn emit_invalid_doc_attribute (
875+ & self ,
876+ span : Span ,
877+ hir_id : HirId ,
878+ decorator : impl for < ' a > DecorateLint < ' a , ( ) > + for <' a > IntoDiagnostic < ' a > ,
879+ ) {
880+ if self . tcx . sess . at_least_rust_2024 ( ) {
881+ self . tcx . sess . create_err ( decorator) . set_span ( span) . emit ( ) ;
882+ } else {
883+ self . tcx . emit_spanned_lint ( INVALID_DOC_ATTRIBUTES , hir_id, span, decorator) ;
884+ }
885+ }
886+
874887 /// Checks `#[doc(inline)]`/`#[doc(no_inline)]` attributes. Returns `true` if valid.
875888 ///
876889 /// A doc inlining attribute is invalid if it is applied to a non-`use` item, or
@@ -909,10 +922,9 @@ impl CheckAttrVisitor<'_> {
909922 }
910923 }
911924 _ => {
912- self . tcx . emit_spanned_lint (
913- INVALID_DOC_ATTRIBUTES ,
914- hir_id,
925+ self . emit_invalid_doc_attribute (
915926 meta. span ( ) ,
927+ hir_id,
916928 errors:: DocInlineOnlyUse {
917929 attr_span : meta. span ( ) ,
918930 item_span : ( attr. style == AttrStyle :: Outer )
@@ -932,10 +944,9 @@ impl CheckAttrVisitor<'_> {
932944 target : Target ,
933945 ) -> bool {
934946 if target != Target :: ExternCrate {
935- self . tcx . emit_spanned_lint (
936- INVALID_DOC_ATTRIBUTES ,
937- hir_id,
947+ self . emit_invalid_doc_attribute (
938948 meta. span ( ) ,
949+ hir_id,
939950 errors:: DocMaskedOnlyExternCrate {
940951 attr_span : meta. span ( ) ,
941952 item_span : ( attr. style == AttrStyle :: Outer )
@@ -946,10 +957,9 @@ impl CheckAttrVisitor<'_> {
946957 }
947958
948959 if self . tcx . extern_mod_stmt_cnum ( hir_id. owner ) . is_none ( ) {
949- self . tcx . emit_spanned_lint (
950- INVALID_DOC_ATTRIBUTES ,
951- hir_id,
960+ self . emit_invalid_doc_attribute (
952961 meta. span ( ) ,
962+ hir_id,
953963 errors:: DocMaskedNotExternCrateSelf {
954964 attr_span : meta. span ( ) ,
955965 item_span : ( attr. style == AttrStyle :: Outer )
@@ -991,10 +1001,9 @@ impl CheckAttrVisitor<'_> {
9911001 . then_some ( errors:: AttrCrateLevelOnlySugg {
9921002 attr : attr. span . with_lo ( bang_span) . with_hi ( bang_span) ,
9931003 } ) ;
994- self . tcx . emit_spanned_lint (
995- INVALID_DOC_ATTRIBUTES ,
996- hir_id,
1004+ self . emit_invalid_doc_attribute (
9971005 meta. span ( ) ,
1006+ hir_id,
9981007 errors:: AttrCrateLevelOnly { sugg } ,
9991008 ) ;
10001009 return false ;
@@ -1011,34 +1020,27 @@ impl CheckAttrVisitor<'_> {
10111020 match ( i_meta. name_or_empty ( ) , i_meta. meta_item ( ) ) {
10121021 ( sym:: attr | sym:: no_crate_inject, _) => { }
10131022 ( _, Some ( m) ) => {
1014- self . tcx . emit_spanned_lint (
1015- INVALID_DOC_ATTRIBUTES ,
1016- hir_id,
1023+ self . emit_invalid_doc_attribute (
10171024 i_meta. span ( ) ,
1025+ hir_id,
10181026 errors:: DocTestUnknown {
10191027 path : rustc_ast_pretty:: pprust:: path_to_string ( & m. path ) ,
10201028 } ,
10211029 ) ;
10221030 is_valid = false ;
10231031 }
10241032 ( _, None ) => {
1025- self . tcx . emit_spanned_lint (
1026- INVALID_DOC_ATTRIBUTES ,
1027- hir_id,
1033+ self . emit_invalid_doc_attribute (
10281034 i_meta. span ( ) ,
1035+ hir_id,
10291036 errors:: DocTestLiteral ,
10301037 ) ;
10311038 is_valid = false ;
10321039 }
10331040 }
10341041 }
10351042 } else {
1036- self . tcx . emit_spanned_lint (
1037- INVALID_DOC_ATTRIBUTES ,
1038- hir_id,
1039- meta. span ( ) ,
1040- errors:: DocTestTakesList ,
1041- ) ;
1043+ self . emit_invalid_doc_attribute ( meta. span ( ) , hir_id, errors:: DocTestTakesList ) ;
10421044 is_valid = false ;
10431045 }
10441046 is_valid
@@ -1050,12 +1052,7 @@ impl CheckAttrVisitor<'_> {
10501052 if meta. meta_item_list ( ) . is_some ( ) {
10511053 true
10521054 } else {
1053- self . tcx . emit_spanned_lint (
1054- INVALID_DOC_ATTRIBUTES ,
1055- hir_id,
1056- meta. span ( ) ,
1057- errors:: DocCfgHideTakesList ,
1058- ) ;
1055+ self . emit_invalid_doc_attribute ( meta. span ( ) , hir_id, errors:: DocCfgHideTakesList ) ;
10591056 false
10601057 }
10611058 }
@@ -1183,11 +1180,11 @@ impl CheckAttrVisitor<'_> {
11831180 _ => {
11841181 let path = rustc_ast_pretty:: pprust:: path_to_string ( & i_meta. path ) ;
11851182 if i_meta. has_name ( sym:: spotlight) {
1186- self . tcx . emit_spanned_lint (
1187- INVALID_DOC_ATTRIBUTES ,
1188- hir_id ,
1189- i_meta. span ,
1190- errors :: DocTestUnknownSpotlight { path , span : i_meta . span } ,
1183+ self . emit_invalid_doc_attribute ( i_meta . span , hir_id ,
1184+ errors :: DocTestUnknownSpotlight {
1185+ path ,
1186+ span : i_meta. span
1187+ }
11911188 ) ;
11921189 } else if i_meta. has_name ( sym:: include)
11931190 && let Some ( value) = i_meta. value_str ( )
@@ -1199,10 +1196,7 @@ impl CheckAttrVisitor<'_> {
11991196 } ;
12001197 // If there are multiple attributes, the suggestion would suggest
12011198 // deleting all of them, which is incorrect.
1202- self . tcx . emit_spanned_lint (
1203- INVALID_DOC_ATTRIBUTES ,
1204- hir_id,
1205- i_meta. span ,
1199+ self . emit_invalid_doc_attribute ( i_meta. span , hir_id,
12061200 errors:: DocTestUnknownInclude {
12071201 path,
12081202 value : value. to_string ( ) ,
@@ -1214,21 +1208,15 @@ impl CheckAttrVisitor<'_> {
12141208 } ,
12151209 ) ;
12161210 } else {
1217- self . tcx . emit_spanned_lint (
1218- INVALID_DOC_ATTRIBUTES ,
1219- hir_id,
1220- i_meta. span ,
1221- errors:: DocTestUnknownAny { path } ,
1211+ self . emit_invalid_doc_attribute ( i_meta. span , hir_id,
1212+ errors:: DocTestUnknownAny { path }
12221213 ) ;
12231214 }
12241215 is_valid = false ;
12251216 }
12261217 }
12271218 } else {
1228- self . tcx . emit_spanned_lint (
1229- INVALID_DOC_ATTRIBUTES ,
1230- hir_id,
1231- meta. span ( ) ,
1219+ self . emit_invalid_doc_attribute ( meta. span ( ) , hir_id,
12321220 errors:: DocInvalid ,
12331221 ) ;
12341222 is_valid = false ;
0 commit comments