Skip to content

Commit cede35e

Browse files
committed
Merge guidelines from RFC 1567 into UX Guidelines.
This is a partial fix for issue #34808. Most of the wording was copied verbatim from the RFC. Main difference is that I moved the actual template to the top of the section. It also makes the error explanations the longest section in the guidelines doc for now.
1 parent 34f35ed commit cede35e

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

Diff for: src/doc/rustc-ux-guidelines.md

+120
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Error explanations are long form descriptions of error messages provided with
5656
the compiler. They are accessible via the `--explain` flag. Each explanation
5757
comes with an example of how to trigger it and advice on how to fix it.
5858

59+
Long error codes explanations are a very important part of Rust. Having an
60+
explanation of what failed helps to understand the error and is appreciated by
61+
Rust developers of all skill levels.
62+
5963
* All of them are accessible [online](http://doc.rust-lang.org/error-index.html),
6064
which are auto-generated from rustc source code in different places:
6165
[librustc](https://github.com/rust-lang/rust/blob/master/src/librustc/diagnostics.rs),
@@ -74,6 +78,122 @@ code with backticks.
7478
* When talking about the compiler, call it `the compiler`, not `Rust` or
7579
`rustc`.
7680

81+
Note: The following sections are mostly a repaste of [RFC 1567](https://github.com/rust-lang/rfcs/blob/master/text/1567-long-error-codes-explanation-normalization.md).
82+
83+
### Template
84+
85+
Long error descriptions should match the following template. The next few
86+
sections of this document describe what each section is about.
87+
88+
E000: r##"
89+
[Error description]
90+
91+
Example of erroneous code:
92+
93+
```compile_fail
94+
[Minimal example]
95+
```
96+
97+
[Error explanation]
98+
99+
```
100+
[How to fix the problem]
101+
```
102+
103+
[Optional Additional information]
104+
105+
"##,
106+
107+
### Error description
108+
109+
Provide a more detailed error message. For example:
110+
111+
```ignore
112+
extern crate a;
113+
extern crate b as a;
114+
```
115+
116+
We get the `E0259` error code which says "an extern crate named `a` has already been imported in this module" and the error explanation says: "The name chosen for an external crate conflicts with another external crate that has been imported into the current module.".
117+
118+
### Minimal example
119+
120+
Provide an erroneous code example which directly follows `Error description`. The erroneous example will be helpful for the `How to fix the problem`. Making it as simple as possible is really important in order to help readers to understand what the error is about. A comment should be added with the error on the same line where the errors occur. Example:
121+
122+
```ignore
123+
type X = u32<i32>; // error: type parameters are not allowed on this type
124+
```
125+
126+
If the error comments is too long to fit 80 columns, split it up like this, so the next line start at the same column of the previous line:
127+
128+
```ignore
129+
type X = u32<'static>; // error: lifetime parameters are not allowed on
130+
// this type
131+
```
132+
133+
And if the sample code is too long to write an effective comment, place your comment on the line before the sample code:
134+
135+
```ignore
136+
// error: lifetime parameters are not allowed on this type
137+
fn super_long_function_name_and_thats_problematic() {}
138+
```
139+
140+
Of course, it the comment is too long, the split rules still applies.
141+
142+
### Error explanation
143+
144+
Provide a full explanation about "__why__ you get the error" and some leads on __how__ to fix it. If needed, use additional code snippets to improve your explanations.
145+
146+
### How to fix the problem
147+
148+
This part will show how to fix the error that we saw previously in the `Minimal example`, with comments explaining how it was fixed.
149+
150+
### Additional information
151+
152+
Some details which might be useful for the users, let's take back `E0109` example. At the end, the supplementary explanation is the following: "Note that type parameters for enum-variant constructors go after the variant, not after the enum (`Option::None::<u32>`, not `Option::<u32>::None`).". It provides more information, not directly linked to the error, but it might help user to avoid doing another error.
153+
154+
### Full Example
155+
156+
E0409: r##"
157+
An "or" pattern was used where the variable bindings are not consistently bound
158+
across patterns.
159+
160+
Example of erroneous code:
161+
162+
```compile_fail,E0409
163+
let x = (0, 2);
164+
match x {
165+
(0, ref y) | (y, 0) ={ /* use y */} // error: variable `y` is bound with
166+
// different mode in pattern #2
167+
// than in pattern #1
168+
_ =()
169+
}
170+
```
171+
172+
Here, `y` is bound by-value in one case and by-reference in the other.
173+
174+
To fix this error, just use the same mode in both cases.
175+
Generally using `ref` or `ref mut` where not already used will fix this:
176+
177+
```rust
178+
let x = (0, 2);
179+
match x {
180+
(0, ref y) | (ref y, 0) ={ /* use y */}
181+
_ =()
182+
}
183+
```
184+
185+
Alternatively, split the pattern:
186+
187+
```rust
188+
let x = (0, 2);
189+
match x {
190+
(y, 0) ={ /* use y */ }
191+
(0, ref y) ={ /* use y */}
192+
_ =()
193+
}
194+
```
195+
"##,
196+
77197
## Compiler Flags
78198

79199
* Flags should be orthogonal to each other. For example, if we'd have a

0 commit comments

Comments
 (0)