-
Notifications
You must be signed in to change notification settings - Fork 547
update error code docs to reflect recent changes #1625
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
Merged
JohnTitor
merged 2 commits into
rust-lang:master
from
Ezrashaw:update-error-explanations
Mar 3, 2023
+108
−93
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Error codes | ||
We generally try to assign each error message a unique code like `E0123`. These | ||
codes are defined in the compiler in the `diagnostics.rs` files found in each | ||
crate, which basically consist of macros. All error codes have an associated | ||
explanation: new error codes must include them. Note that not all _historical_ | ||
(no longer emitted) error codes have explanations. | ||
|
||
## Error explanations | ||
|
||
The explanations are written in Markdown (see the [CommonMark Spec] for | ||
specifics around syntax), and all of them are linked in the [`rustc_error_codes`] | ||
crate. Please read [RFC 1567] for details on how to format and write long error | ||
codes. As of <!-- date-check --> February 2023, there is an | ||
effort[^new-explanations] to replace this largely outdated RFC with a new more | ||
flexible standard. | ||
|
||
Error explanations should expand on the error message and provide details about | ||
_why_ the error occurs. It is not helpful for users to copy-paste a quick fix; | ||
explanations should help users understand why their code cannot be accepted by | ||
the compiler. Rust prides itself on helpful error messages and long-form | ||
explanations are no exception. However, before error explanations are | ||
overhauled[^new-explanations] it is a bit open as to how exactly they should be | ||
written, as always: ask your reviewer or ask around on the Rust Discord or Zulip. | ||
|
||
[^new-explanations]: See the draft RFC [here][new-explanations-rfc]. | ||
|
||
[`rustc_error_codes`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_error_codes/error_codes/index.html | ||
[CommonMark Spec]: https://spec.commonmark.org/current/ | ||
[RFC 1567]: https://github.com/rust-lang/rfcs/blob/master/text/1567-long-error-codes-explanation-normalization.md | ||
[new-explanations-rfc]: https://github.com/rust-lang/rfcs/pull/3370 | ||
|
||
## Allocating a fresh code | ||
|
||
Error codes are stored in `compiler/rustc_error_codes`. | ||
|
||
To create a new error, you first need to find the next available | ||
code. You can find it with `tidy`: | ||
|
||
``` | ||
./x.py test tidy | ||
``` | ||
|
||
This will invoke the tidy script, which generally checks that your code obeys | ||
our coding conventions. Some of these jobs check error codes and ensure that | ||
there aren't duplicates, etc (the tidy check is defined in | ||
`src/tools/tidy/src/error_codes.rs`). Once it is finished with that, tidy will | ||
print out the highest used error code: | ||
|
||
``` | ||
... | ||
tidy check | ||
Found 505 error codes | ||
Highest error code: `E0591` | ||
... | ||
``` | ||
|
||
Here we see the highest error code in use is `E0591`, so we _probably_ want | ||
`E0592`. To be sure, run `rg E0592` and check, you should see no references. | ||
|
||
You will have to write an extended description for your error, | ||
which will go in `rustc_error_codes/src/error_codes/E0592.md`. | ||
To register the error, open `rustc_error_codes/src/error_codes.rs` and add the | ||
code (in its proper numerical order) into` register_diagnostics!` macro, like | ||
this: | ||
|
||
```rust | ||
register_diagnostics! { | ||
... | ||
E0592: include_str!("./error_codes/E0592.md"), | ||
} | ||
``` | ||
|
||
To actually issue the error, you can use the `struct_span_err!` macro: | ||
|
||
```rust | ||
struct_span_err!(self.tcx.sess, // some path to the session here | ||
span, // whatever span in the source you want | ||
E0592, // your new error code | ||
fluent::example::an_error_message) | ||
.emit() // actually issue the error | ||
``` | ||
|
||
If you want to add notes or other snippets, you can invoke methods before you | ||
call `.emit()`: | ||
|
||
```rust | ||
struct_span_err!(...) | ||
.span_label(another_span, fluent::example::example_label) | ||
.span_note(another_span, fluent::example::separate_note) | ||
.emit() | ||
``` | ||
|
||
For an example of a PR adding an error code, see [#76143]. | ||
|
||
[#76143]: https://github.com/rust-lang/rust/pull/76143 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.