Skip to content

Commit eaec783

Browse files
Turn INVALID_DOC_ATTRIBUTES into a hard error starting 2024 edition
1 parent adda05f commit eaec783

File tree

2 files changed

+49
-61
lines changed

2 files changed

+49
-61
lines changed

compiler/rustc_passes/src/check_attr.rs

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::{errors, fluent_generated as fluent};
88
use rustc_ast::{ast, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem};
99
use rustc_data_structures::fx::FxHashMap;
10-
use rustc_errors::{Applicability, IntoDiagnosticArg, MultiSpan};
10+
use rustc_errors::{Applicability, DecorateLint, IntoDiagnostic, IntoDiagnosticArg, MultiSpan};
1111
use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
1212
use rustc_hir as hir;
1313
use 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;

compiler/rustc_passes/src/errors.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pub struct DocKeywordConflict {
257257
pub spans: MultiSpan,
258258
}
259259

260-
#[derive(LintDiagnostic)]
260+
#[derive(LintDiagnostic, Diagnostic)]
261261
#[diag(passes_doc_inline_only_use)]
262262
#[note]
263263
pub struct DocInlineOnlyUse {
@@ -267,7 +267,7 @@ pub struct DocInlineOnlyUse {
267267
pub item_span: Option<Span>,
268268
}
269269

270-
#[derive(LintDiagnostic)]
270+
#[derive(LintDiagnostic, Diagnostic)]
271271
#[diag(passes_doc_masked_only_extern_crate)]
272272
#[note]
273273
pub struct DocMaskedOnlyExternCrate {
@@ -277,7 +277,7 @@ pub struct DocMaskedOnlyExternCrate {
277277
pub item_span: Option<Span>,
278278
}
279279

280-
#[derive(LintDiagnostic)]
280+
#[derive(LintDiagnostic, Diagnostic)]
281281
#[diag(passes_doc_masked_not_extern_crate_self)]
282282
pub struct DocMaskedNotExternCrateSelf {
283283
#[label]
@@ -294,31 +294,31 @@ pub struct DocAttrNotCrateLevel<'a> {
294294
pub attr_name: &'a str,
295295
}
296296

297-
#[derive(LintDiagnostic)]
297+
#[derive(LintDiagnostic, Diagnostic)]
298298
#[diag(passes_doc_test_unknown)]
299299
pub struct DocTestUnknown {
300300
pub path: String,
301301
}
302302

303-
#[derive(LintDiagnostic)]
303+
#[derive(LintDiagnostic, Diagnostic)]
304304
#[diag(passes_doc_test_literal)]
305305
pub struct DocTestLiteral;
306306

307-
#[derive(LintDiagnostic)]
307+
#[derive(LintDiagnostic, Diagnostic)]
308308
#[diag(passes_doc_test_takes_list)]
309309
pub struct DocTestTakesList;
310310

311-
#[derive(LintDiagnostic)]
311+
#[derive(LintDiagnostic, Diagnostic)]
312312
#[diag(passes_doc_cfg_hide_takes_list)]
313313
pub struct DocCfgHideTakesList;
314314

315-
#[derive(LintDiagnostic)]
315+
#[derive(LintDiagnostic, Diagnostic)]
316316
#[diag(passes_doc_test_unknown_any)]
317317
pub struct DocTestUnknownAny {
318318
pub path: String,
319319
}
320320

321-
#[derive(LintDiagnostic)]
321+
#[derive(LintDiagnostic, Diagnostic)]
322322
#[diag(passes_doc_test_unknown_spotlight)]
323323
#[note]
324324
#[note(passes_no_op_note)]
@@ -328,7 +328,7 @@ pub struct DocTestUnknownSpotlight {
328328
pub span: Span,
329329
}
330330

331-
#[derive(LintDiagnostic)]
331+
#[derive(LintDiagnostic, Diagnostic)]
332332
#[diag(passes_doc_test_unknown_include)]
333333
pub struct DocTestUnknownInclude {
334334
pub path: String,
@@ -338,7 +338,7 @@ pub struct DocTestUnknownInclude {
338338
pub sugg: (Span, Applicability),
339339
}
340340

341-
#[derive(LintDiagnostic)]
341+
#[derive(LintDiagnostic, Diagnostic)]
342342
#[diag(passes_doc_invalid)]
343343
pub struct DocInvalid;
344344

@@ -1812,7 +1812,7 @@ pub struct UnusedVarTryIgnoreSugg {
18121812
pub name: String,
18131813
}
18141814

1815-
#[derive(LintDiagnostic)]
1815+
#[derive(LintDiagnostic, Diagnostic)]
18161816
#[diag(passes_attr_crate_level)]
18171817
#[note]
18181818
pub struct AttrCrateLevelOnly {

0 commit comments

Comments
 (0)