Skip to content

Commit 746bbbd

Browse files
Move remaning doc attribute parsing errors to warnings
1 parent 40da66c commit 746bbbd

14 files changed

Lines changed: 258 additions & 117 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,42 @@ fn check_attr_crate_level<S: Stage>(cx: &mut AcceptContext<'_, '_, S>, span: Spa
7070
true
7171
}
7272

73+
// FIXME: To be removed once merged and replace with `cx.expected_name_value(span, _name)`.
74+
fn expected_name_value<S: Stage>(
75+
cx: &mut AcceptContext<'_, '_, S>,
76+
span: Span,
77+
_name: Option<Symbol>,
78+
) {
79+
cx.emit_lint(
80+
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
81+
AttributeLintKind::ExpectedNameValue,
82+
span,
83+
);
84+
}
85+
86+
// FIXME: remove this method once merged and use `cx.expected_no_args(span)` instead.
87+
fn expected_no_args<S: Stage>(cx: &mut AcceptContext<'_, '_, S>, span: Span) {
88+
cx.emit_lint(
89+
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
90+
AttributeLintKind::ExpectedNoArgs,
91+
span,
92+
);
93+
}
94+
95+
// FIXME: remove this method once merged and use `cx.expected_no_args(span)` instead.
96+
// cx.expected_string_literal(span, _actual_literal);
97+
fn expected_string_literal<S: Stage>(
98+
cx: &mut AcceptContext<'_, '_, S>,
99+
span: Span,
100+
_actual_literal: Option<&MetaItemLit>,
101+
) {
102+
cx.emit_lint(
103+
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
104+
AttributeLintKind::MalformedDoc,
105+
span,
106+
);
107+
}
108+
73109
fn parse_keyword_and_attribute<S: Stage>(
74110
cx: &mut AcceptContext<'_, '_, S>,
75111
path: &OwnedPathParser,
@@ -78,12 +114,12 @@ fn parse_keyword_and_attribute<S: Stage>(
78114
attr_name: Symbol,
79115
) {
80116
let Some(nv) = args.name_value() else {
81-
cx.expected_name_value(args.span().unwrap_or(path.span()), path.word_sym());
117+
expected_name_value(cx, args.span().unwrap_or(path.span()), path.word_sym());
82118
return;
83119
};
84120

85121
let Some(value) = nv.value_as_str() else {
86-
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
122+
expected_string_literal(cx, nv.value_span, Some(nv.value_as_lit()));
87123
return;
88124
};
89125

@@ -127,7 +163,7 @@ impl DocParser {
127163
match path.word_sym() {
128164
Some(sym::no_crate_inject) => {
129165
if let Err(span) = args.no_args() {
130-
cx.expected_no_args(span);
166+
expected_no_args(cx, span);
131167
return;
132168
}
133169

@@ -153,7 +189,14 @@ impl DocParser {
153189
}
154190
Some(sym::attr) => {
155191
let Some(list) = args.list() else {
156-
cx.expected_list(cx.attr_span, args);
192+
// FIXME: remove this method once merged and uncomment the line below instead.
193+
// cx.expected_list(cx.attr_span, args);
194+
let span = cx.attr_span;
195+
cx.emit_lint(
196+
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
197+
AttributeLintKind::MalformedDoc,
198+
span,
199+
);
157200
return;
158201
};
159202

@@ -255,7 +298,7 @@ impl DocParser {
255298
inline: DocInline,
256299
) {
257300
if let Err(span) = args.no_args() {
258-
cx.expected_no_args(span);
301+
expected_no_args(cx, span);
259302
return;
260303
}
261304

@@ -337,7 +380,14 @@ impl DocParser {
337380
match sub_item.args() {
338381
a @ (ArgParser::NoArgs | ArgParser::NameValue(_)) => {
339382
let Some(name) = sub_item.path().word_sym() else {
340-
cx.expected_identifier(sub_item.path().span());
383+
// FIXME: remove this method once merged and uncomment the line
384+
// below instead.
385+
// cx.expected_identifier(sub_item.path().span());
386+
cx.emit_lint(
387+
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
388+
AttributeLintKind::MalformedDoc,
389+
sub_item.path().span(),
390+
);
341391
continue;
342392
};
343393
if let Ok(CfgEntry::NameValue { name, value, .. }) =
@@ -400,7 +450,7 @@ impl DocParser {
400450
macro_rules! no_args {
401451
($ident: ident) => {{
402452
if let Err(span) = args.no_args() {
403-
cx.expected_no_args(span);
453+
expected_no_args(cx, span);
404454
return;
405455
}
406456

@@ -419,7 +469,7 @@ impl DocParser {
419469
macro_rules! no_args_and_not_crate_level {
420470
($ident: ident) => {{
421471
if let Err(span) = args.no_args() {
422-
cx.expected_no_args(span);
472+
expected_no_args(cx, span);
423473
return;
424474
}
425475
let span = path.span();
@@ -432,7 +482,7 @@ impl DocParser {
432482
macro_rules! no_args_and_crate_level {
433483
($ident: ident) => {{
434484
if let Err(span) = args.no_args() {
435-
cx.expected_no_args(span);
485+
expected_no_args(cx, span);
436486
return;
437487
}
438488
let span = path.span();
@@ -445,12 +495,12 @@ impl DocParser {
445495
macro_rules! string_arg_and_crate_level {
446496
($ident: ident) => {{
447497
let Some(nv) = args.name_value() else {
448-
cx.expected_name_value(args.span().unwrap_or(path.span()), path.word_sym());
498+
expected_name_value(cx, args.span().unwrap_or(path.span()), path.word_sym());
449499
return;
450500
};
451501

452502
let Some(s) = nv.value_as_str() else {
453-
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
503+
expected_string_literal(cx, nv.value_span, Some(nv.value_as_lit()));
454504
return;
455505
};
456506

@@ -521,7 +571,14 @@ impl DocParser {
521571
self.parse_single_test_doc_attr_item(cx, mip);
522572
}
523573
MetaItemOrLitParser::Lit(lit) => {
524-
cx.unexpected_literal(lit.span);
574+
// FIXME: remove this method once merged and uncomment the line
575+
// below instead.
576+
// cx.unexpected_literal(lit.span);
577+
cx.emit_lint(
578+
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
579+
AttributeLintKind::MalformedDoc,
580+
lit.span,
581+
);
525582
}
526583
}
527584
}
@@ -604,27 +661,14 @@ impl DocParser {
604661
self.parse_single_doc_attr_item(cx, mip);
605662
}
606663
MetaItemOrLitParser::Lit(lit) => {
607-
// FIXME: Remove the lint and uncomment line after beta backport is
608-
// done.
609-
// cx.expected_name_value(lit.span, None);
610-
cx.emit_lint(
611-
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
612-
AttributeLintKind::MalformedDoc,
613-
lit.span,
614-
);
664+
expected_name_value(cx, lit.span, None);
615665
}
616666
}
617667
}
618668
}
619669
ArgParser::NameValue(nv) => {
620670
if nv.value_as_str().is_none() {
621-
// FIXME: Remove the lint and uncomment line after beta backport is done.
622-
// cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
623-
cx.emit_lint(
624-
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
625-
AttributeLintKind::MalformedDoc,
626-
nv.value_span,
627-
);
671+
expected_string_literal(cx, nv.value_span, Some(nv.value_as_lit()));
628672
} else {
629673
unreachable!(
630674
"Should have been handled at the same time as sugar-syntaxed doc comments"

compiler/rustc_lint/messages.ftl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ lint_expectation = this lint expectation is unfulfilled
326326
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
327327
.rationale = {$rationale}
328328
329+
lint_expected_name_value =
330+
expected this to be of the form `... = "..."`
331+
.warn = {-lint_previously_accepted}
332+
333+
lint_expected_no_args =
334+
didn't expect any arguments here
335+
.warn = {-lint_previously_accepted}
336+
329337
lint_for_loops_over_fallibles =
330338
for loop over {$article} `{$ref_prefix}{$ty}`. This is more readably written as an `if let` statement
331339
.suggestion = consider using `if let` to clear intent

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,5 +430,9 @@ pub fn decorate_attribute_lint(
430430
.decorate_lint(diag),
431431

432432
&AttributeLintKind::MalformedDoc => lints::MalformedDoc.decorate_lint(diag),
433+
434+
&AttributeLintKind::ExpectedNoArgs => lints::ExpectedNoArgs.decorate_lint(diag),
435+
436+
&AttributeLintKind::ExpectedNameValue => lints::ExpectedNameValue.decorate_lint(diag),
433437
}
434438
}

compiler/rustc_lint/src/lints.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,6 +3190,16 @@ pub(crate) struct UnusedDuplicate {
31903190
#[warning]
31913191
pub(crate) struct MalformedDoc;
31923192

3193+
#[derive(LintDiagnostic)]
3194+
#[diag(lint_expected_no_args)]
3195+
#[warning]
3196+
pub(crate) struct ExpectedNoArgs;
3197+
3198+
#[derive(LintDiagnostic)]
3199+
#[diag(lint_expected_name_value)]
3200+
#[warning]
3201+
pub(crate) struct ExpectedNameValue;
3202+
31933203
#[derive(LintDiagnostic)]
31943204
#[diag(lint_unsafe_attr_outside_unsafe)]
31953205
pub(crate) struct UnsafeAttrOutsideUnsafeLint {

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,8 @@ pub enum AttributeLintKind {
827827
suggested: Option<Symbol>,
828828
},
829829
MalformedDoc,
830+
ExpectedNoArgs,
831+
ExpectedNameValue,
830832
}
831833

832834
pub type RegisteredTools = FxIndexSet<Ident>;
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
// regression test for https://github.com/rust-lang/rust/issues/149187
2+
#![deny(invalid_doc_attributes)]
23

34
#![doc(html_favicon_url)]
4-
//~^ ERROR: malformed `doc` attribute
5-
//~| NOTE expected this to be of the form `html_favicon_url = "..."`
5+
//~^ ERROR
6+
//~| WARN
67
#![doc(html_logo_url)]
7-
//~^ ERROR: malformed `doc` attribute
8-
//~| NOTE expected this to be of the form `html_logo_url = "..."`
8+
//~^ ERROR
9+
//~| WARN
910
#![doc(html_playground_url)]
10-
//~^ ERROR: malformed `doc` attribute
11-
//~| NOTE expected this to be of the form `html_playground_url = "..."`
11+
//~^ ERROR
12+
//~| WARN
1213
#![doc(issue_tracker_base_url)]
13-
//~^ ERROR: malformed `doc` attribute
14-
//~| NOTE expected this to be of the form `issue_tracker_base_url = "..."`
14+
//~^ ERROR
15+
//~| WARN
1516
#![doc(html_favicon_url = 1)]
16-
//~^ ERROR malformed `doc` attribute
17-
//~| NOTE expected a string literal
17+
//~^ ERROR
18+
//~| WARN
1819
#![doc(html_logo_url = 2)]
19-
//~^ ERROR malformed `doc` attribute
20-
//~| NOTE expected a string literal
20+
//~^ ERROR
21+
//~| WARN
2122
#![doc(html_playground_url = 3)]
22-
//~^ ERROR malformed `doc` attribute
23-
//~| NOTE expected a string literal
23+
//~^ ERROR
24+
//~| WARN
2425
#![doc(issue_tracker_base_url = 4)]
25-
//~^ ERROR malformed `doc` attribute
26-
//~| NOTE expected a string literal
26+
//~^ ERROR
27+
//~| WARN
2728
#![doc(html_no_source = "asdf")]
28-
//~^ ERROR malformed `doc` attribute
29-
//~| NOTE didn't expect any arguments here
29+
//~^ ERROR
30+
//~| WARN

0 commit comments

Comments
 (0)