Skip to content

Commit 625143b

Browse files
committed
Add link to docs on malformed attributes
1 parent 189f264 commit 625143b

23 files changed

+344
-87
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 270 additions & 71 deletions
Large diffs are not rendered by default.

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ lint_ill_formed_attribute_input = {$num_suggestions ->
349349
[1] attribute must be of the form {$suggestions}
350350
*[other] valid forms for the attribute are {$suggestions}
351351
}
352+
.note = for more information, visit <{$docs}>
352353
353354
lint_impl_trait_overcaptures = `{$self_ty}` will capture more lifetimes than possibly intended in edition 2024
354355
.note = specifically, {$num_captured ->

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,14 @@ pub fn decorate_builtin_lint(
447447
BuiltinLintDiag::UnusedCrateDependency { extern_crate, local_crate } => {
448448
lints::UnusedCrateDependency { extern_crate, local_crate }.decorate_lint(diag)
449449
}
450-
BuiltinLintDiag::IllFormedAttributeInput { suggestions } => {
450+
BuiltinLintDiag::IllFormedAttributeInput { suggestions, docs } => {
451451
lints::IllFormedAttributeInput {
452452
num_suggestions: suggestions.len(),
453453
suggestions: DiagArgValue::StrListSepByAnd(
454454
suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(),
455455
),
456+
has_docs: docs.is_some(),
457+
docs: docs.unwrap_or(""),
456458
}
457459
.decorate_lint(diag)
458460
}

compiler/rustc_lint/src/lints.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,6 +2685,9 @@ pub(crate) struct UnusedCrateDependency {
26852685
pub(crate) struct IllFormedAttributeInput {
26862686
pub num_suggestions: usize,
26872687
pub suggestions: DiagArgValue,
2688+
#[note]
2689+
pub has_docs: bool,
2690+
pub docs: &'static str,
26882691
}
26892692

26902693
#[derive(LintDiagnostic)]

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ pub enum BuiltinLintDiag {
793793
},
794794
IllFormedAttributeInput {
795795
suggestions: Vec<String>,
796+
docs: Option<&'static str>,
796797
},
797798
InnerAttributeUnstable {
798799
is_macro: bool,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -313,24 +313,27 @@ fn emit_malformed_attribute(
313313
ILL_FORMED_ATTRIBUTE_INPUT,
314314
span,
315315
ast::CRATE_NODE_ID,
316-
BuiltinLintDiag::IllFormedAttributeInput { suggestions: suggestions.clone() },
316+
BuiltinLintDiag::IllFormedAttributeInput {
317+
suggestions: suggestions.clone(),
318+
docs: template.docs,
319+
},
317320
);
318321
} else {
319322
suggestions.sort();
320-
psess
321-
.dcx()
322-
.struct_span_err(span, error_msg)
323-
.with_span_suggestions(
324-
span,
325-
if suggestions.len() == 1 {
326-
"must be of the form"
327-
} else {
328-
"the following are the possible correct uses"
329-
},
330-
suggestions,
331-
Applicability::HasPlaceholders,
332-
)
333-
.emit();
323+
let mut err = psess.dcx().struct_span_err(span, error_msg).with_span_suggestions(
324+
span,
325+
if suggestions.len() == 1 {
326+
"must be of the form"
327+
} else {
328+
"the following are the possible correct uses"
329+
},
330+
suggestions,
331+
Applicability::HasPlaceholders,
332+
);
333+
if let Some(link) = template.docs {
334+
err.note(format!("for more information, visit <{link}>"));
335+
}
336+
err.emit();
334337
}
335338
}
336339

tests/ui/attributes/crate-name-macro-call.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: malformed `crate_name` attribute input
33
|
44
LL | #![crate_name = concat!("my", "crate")]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![crate_name = "name"]`
6+
|
7+
= note: for more information, visit <https://doc.rust-lang.org/reference/crates-and-source-files.html#the-crate_name-attribute>
68

79
error: aborting due to 1 previous error
810

tests/ui/attributes/crate-type-delimited.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: malformed `crate_type` attribute input
44
LL | #![crate_type(lib)]
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7+
= note: for more information, visit <https://doc.rust-lang.org/reference/linkage.html>
78
help: the following are the possible correct uses
89
|
910
LL - #![crate_type(lib)]

tests/ui/attributes/crate-type-empty.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: malformed `crate_type` attribute input
44
LL | #![crate_type]
55
| ^^^^^^^^^^^^^^
66
|
7+
= note: for more information, visit <https://doc.rust-lang.org/reference/linkage.html>
78
help: the following are the possible correct uses
89
|
910
LL | #![crate_type = "bin"]

tests/ui/attributes/crate-type-macro-call.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: malformed `crate_type` attribute input
44
LL | #![crate_type = foo!()]
55
| ^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= note: for more information, visit <https://doc.rust-lang.org/reference/linkage.html>
78
help: the following are the possible correct uses
89
|
910
LL - #![crate_type = foo!()]

0 commit comments

Comments
 (0)