-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make diagnostics emitting independent of the happy code path #61132
Comments
The vast majority of these could probably be handled by a custom derive: #[derive(AsError)]
#[error = "Cannot move {ty} out of borrow", span]
#[note = "First borrwed here", other_span]
#[code = E12345]
struct MoveOutOfBorrowError {
ty: Ty,
span: Span,
other_span: Span,
} (exact syntax is bikesheddable, of course) |
At first I thought about how the derive macro would be constraining for some of the more complex errors, but looking at it I can see how it'd be more than appropriate for the great majority of errors we emit:
|
Oh, tagging spans with this stuff is genius. |
Hmm, it needs a little tweak: #[derive(AsError)]
#[code = E12345]
struct MoveOutOfBorrowError {
ty: Ty,
#[error = "cannot move {ty} out of borrow"]
#[label = "cannot move out of borrow"]
span: Span,
#[label = "`{ty}` first borrowed here"]
other_span: Span,
#[suggestion(msg = "consider cloning here", code = "{1}.clone()")]
opt_sugg: Option<(Span, Ident)>,
} |
So here we'd have to special case |
This sounds like a great improvment in typeck maintainability. I think I can do this, It will just take time and trials. What do you think ? |
Go for it, I think. cc @estebank The derive will basically have to specially notice |
I think we can iterate and disregard the more complex parts of the feature (IE, implement them manually in the code for now) and later pull them into the proc macro. |
@Electron-libre Are you still working on this? If so please |
No sorry, my son is born ( 😍 ) few days after my last comment and then I forgot about this issue. |
I and @yaahc may end up working on this |
Starting to think about the design. Would this be implemented as a normal derive proc macro (using a syn dependency) or something more like the builtin libsyntax derives? I'm not sure if rustc having a syn dependency is a good idea. If it's going to be a libsyntax thing, what x.py commands would y'all recommend for testing it for minimal cycle time? Ideally only libsyntax needs to be built to play with this. It's been a while since i poked at this stuff and the build system has changed. cc @estebank |
I think it should be in |
@Manishearth @yaahc Did you end up / are you still intending on working on this? It's unassigned and I'd like to take this one, but I just want to make sure I'm not treading on any toes, first. |
Go for it, I want to eventually work on this but if someone else gets there first that's fine |
Alright, cheers. @rustbot claim |
I'm still actively working on this. Sorry I've been taking so long -- I'll hurry up and get a PR in soon. @rustbot claim |
…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 rust-lang#61132. r? @oli-obk
In our first @rust-lang/wg-diagnostics meeting on zulip we figured that a first start would be to create a new trait
that is implemented for structs containing all relevant info for a diagnostic, e.g.
and then the code that currently constructs the diagnostic and emits it would just become
This issue has been assigned to @jumbatm via this comment.
The text was updated successfully, but these errors were encountered: