Skip to content

Commit 71569e4

Browse files
committed
Auto merge of #75138 - jumbatm:session-diagnostic-derive, r=oli-obk
Add derive macro for specifying diagnostics using attributes. Introduces `#[derive(SessionDiagnostic)]`, a derive macro for specifying structs that can be converted to Diagnostics using directions given by attributes on the struct and its fields. Currently, the following attributes have been implemented: - `#[code = "..."]` -- this sets the Diagnostic's error code, and must be provided on the struct iself (ie, not on a field). Equivalent to calling `code`. - `#[message = "..."]` -- this sets the Diagnostic's primary error message. - `#[label = "..."]` -- this must be applied to fields of type `Span`, and is equivalent to `span_label` - `#[suggestion(..)]` -- this allows a suggestion message to be supplied. This attribute must be applied to a field of type `Span` or `(Span, Applicability)`, and is equivalent to calling `span_suggestion`. Valid arguments are: - `message = "..."` -- this sets the suggestion message. - (Optional) `code = "..."` -- this suggests code for the suggestion. Defaults to empty. `suggestion`also comes with other variants: `#[suggestion_short(..)]`, `#[suggestion_hidden(..)]` and `#[suggestion_verbose(..)]` which all take the same keys. Within the strings passed to each attribute, fields can be referenced without needing to be passed explicitly into the format string -- eg, `#[error = "{ident} already declared"] ` will set the error message to `format!("{} already declared", &self.ident)`. Any fields on the struct can be referenced in this way. Additionally, for any of these attributes, Option fields can be used to only optionally apply the decoration -- for example: ```rust #[derive(SessionDiagnostic)] #[code = "E0123"] struct SomeKindOfError { ... #[suggestion(message = "informative error message")] opt_sugg: Option<(Span, Applicability)> ... } ``` will not emit a suggestion if `opt_sugg` is `None`. We plan on iterating on this macro further; this PR is a start. Closes #61132. r? `@oli-obk`
2 parents 0e2c128 + 5956254 commit 71569e4

File tree

19 files changed

+1361
-205
lines changed

19 files changed

+1361
-205
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4087,6 +4087,7 @@ dependencies = [
40874087
"rustc_hir_pretty",
40884088
"rustc_index",
40894089
"rustc_infer",
4090+
"rustc_macros",
40904091
"rustc_middle",
40914092
"rustc_session",
40924093
"rustc_span",

compiler/rustc_error_codes/src/error_codes/E0224.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
A trait object was declaired with no traits.
1+
A trait object was declared with no traits.
22

33
Erroneous code example:
44

@@ -8,7 +8,7 @@ type Foo = dyn 'static +;
88

99
Rust does not currently support this.
1010

11-
To solve ensure the the trait object has at least one trait:
11+
To solve, ensure that the trait object has at least one trait:
1212

1313
```
1414
type Foo = dyn 'static + Copy;

compiler/rustc_macros/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(proc_macro_diagnostic)]
12
#![allow(rustc::default_hash_types)]
23
#![recursion_limit = "128"]
34

@@ -9,6 +10,7 @@ mod hash_stable;
910
mod lift;
1011
mod query;
1112
mod serialize;
13+
mod session_diagnostic;
1214
mod symbols;
1315
mod type_foldable;
1416

@@ -36,3 +38,14 @@ decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
3638
decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
3739
decl_derive!([TypeFoldable, attributes(type_foldable)] => type_foldable::type_foldable_derive);
3840
decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
41+
decl_derive!(
42+
[SessionDiagnostic, attributes(
43+
message,
44+
lint,
45+
error,
46+
label,
47+
suggestion,
48+
suggestion_short,
49+
suggestion_hidden,
50+
suggestion_verbose)] => session_diagnostic::session_diagnostic_derive
51+
);

0 commit comments

Comments
 (0)