Skip to content

Commit ac3cc6c

Browse files
committed
Rollup merge of #24143 - michaelsproul:extended-errors, r=pnkfelix
I've taken another look at extended errors - fixing up the printing and adding a few more for match expressions. With regards to printing, the previous behaviour was to just print the error message string directly, despite it containing indentation which caused it to overflow the standard terminal width of 80 columns (try `rustc --explain E0004`). The first approach I considered was to strip the leading whitespace from each line and lay out the text dynamically, inserting spaces in between. This approach became quite messy when taking multi-paragraph errors into account (and seemed overkill). The approach I settled on removes the indentation in the string itself and begins each message with a newline that is stripped before printing. I feel like complete extended errors would be nice to have for 1.0.0 and I'm happy to spearhead an effort to get them written. Brian got me onto writing them at an SF meetup and I think it shouldn't be too hard to get the remaining 80 or so written with the help of people who don't really work on compiler innards.
2 parents 926f38e + 039a553 commit ac3cc6c

File tree

2 files changed

+115
-32
lines changed

2 files changed

+115
-32
lines changed

src/librustc/diagnostics.rs

Lines changed: 113 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,134 @@
1010

1111
#![allow(non_snake_case)]
1212

13+
// Error messages for EXXXX errors.
14+
// Each message should start and end with a new line, and be wrapped to 80 characters.
15+
// In vim you can `:set tw=80` and use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
1316
register_long_diagnostics! {
14-
E0001: r##"
15-
This error suggests that the expression arm corresponding to the noted pattern
16-
will never be reached as for all possible values of the expression being matched,
17-
one of the preceding patterns will match.
1817

19-
This means that perhaps some of the preceding patterns are too general, this
20-
one is too specific or the ordering is incorrect.
18+
E0001: r##"
19+
This error suggests that the expression arm corresponding to the noted pattern
20+
will never be reached as for all possible values of the expression being
21+
matched, one of the preceding patterns will match.
22+
23+
This means that perhaps some of the preceding patterns are too general, this one
24+
is too specific or the ordering is incorrect.
25+
"##,
26+
27+
E0002: r##"
28+
This error indicates that an empty match expression is illegal because the type
29+
it is matching on is non-empty (there exist values of this type). In safe code
30+
it is impossible to create an instance of an empty type, so empty match
31+
expressions are almost never desired. This error is typically fixed by adding
32+
one or more cases to the match expression.
33+
34+
An example of an empty type is `enum Empty { }`.
2135
"##,
2236

23-
E0003: r##"
24-
Not-a-Number (NaN) values can not be compared for equality and hence can never match
25-
the input to a match expression. To match against NaN values, you should instead use
26-
the `is_nan` method in a guard, as in: x if x.is_nan() => ...
37+
E0003: r##"
38+
Not-a-Number (NaN) values cannot be compared for equality and hence can never
39+
match the input to a match expression. To match against NaN values, you should
40+
instead use the `is_nan` method in a guard, as in: x if x.is_nan() => ...
2741
"##,
2842

29-
E0004: r##"
30-
This error indicates that the compiler can not guarantee a matching pattern for one
31-
or more possible inputs to a match expression. Guaranteed matches are required in order
32-
to assign values to match expressions, or alternatively, determine the flow of execution.
43+
E0004: r##"
44+
This error indicates that the compiler cannot guarantee a matching pattern for
45+
one or more possible inputs to a match expression. Guaranteed matches are
46+
required in order to assign values to match expressions, or alternatively,
47+
determine the flow of execution.
3348
34-
If you encounter this error you must alter your patterns so that every possible value of
35-
the input type is matched. For types with a small number of variants (like enums) you
36-
should probably cover all cases explicitly. Alternatively, the underscore `_` wildcard
37-
pattern can be added after all other patterns to match "anything else".
49+
If you encounter this error you must alter your patterns so that every possible
50+
value of the input type is matched. For types with a small number of variants
51+
(like enums) you should probably cover all cases explicitly. Alternatively, the
52+
underscore `_` wildcard pattern can be added after all other patterns to match
53+
"anything else".
3854
"##,
3955

40-
// FIXME: Remove duplication here?
41-
E0005: r##"
42-
Patterns used to bind names must be irrefutable, that is, they must guarantee that a
43-
name will be extracted in all cases. If you encounter this error you probably need
44-
to use a `match` or `if let` to deal with the possibility of failure.
56+
// FIXME: Remove duplication here?
57+
E0005: r##"
58+
Patterns used to bind names must be irrefutable, that is, they must guarantee that a
59+
name will be extracted in all cases. If you encounter this error you probably need
60+
to use a `match` or `if let` to deal with the possibility of failure.
4561
"##,
4662

47-
E0006: r##"
48-
Patterns used to bind names must be irrefutable, that is, they must guarantee that a
49-
name will be extracted in all cases. If you encounter this error you probably need
50-
to use a `match` or `if let` to deal with the possibility of failure.
63+
E0006: r##"
64+
Patterns used to bind names must be irrefutable, that is, they must guarantee that a
65+
name will be extracted in all cases. If you encounter this error you probably need
66+
to use a `match` or `if let` to deal with the possibility of failure.
67+
"##,
68+
69+
E0007: r##"
70+
This error indicates that the bindings in a match arm would require a value to
71+
be moved into more than one location, thus violating unique ownership. Code like
72+
the following is invalid as it requires the entire Option<String> to be moved
73+
into a variable called `op_string` while simultaneously requiring the inner
74+
String to be moved into a variable called `s`.
75+
76+
let x = Some("s".to_string());
77+
match x {
78+
op_string @ Some(s) => ...
79+
None => ...
80+
}
81+
82+
See also Error 303.
83+
"##,
84+
85+
E0008: r##"
86+
Names bound in match arms retain their type in pattern guards. As such, if a
87+
name is bound by move in a pattern, it should also be moved to wherever it is
88+
referenced in the pattern guard code. Doing so however would prevent the name
89+
from being available in the body of the match arm. Consider the following:
90+
91+
match Some("hi".to_string()) {
92+
Some(s) if s.len() == 0 => // use s.
93+
...
94+
}
95+
96+
The variable `s` has type String, and its use in the guard is as a variable of
97+
type String. The guard code effectively executes in a separate scope to the body
98+
of the arm, so the value would be moved into this anonymous scope and therefore
99+
become unavailable in the body of the arm. Although this example seems
100+
innocuous, the problem is most clear when considering functions that take their
101+
argument by value.
102+
103+
match Some("hi".to_string()) {
104+
Some(s) if { drop(s); false } => (),
105+
Some(s) => // use s.
106+
...
107+
}
108+
109+
The value would be dropped in the guard then become unavailable not only in the
110+
body of that arm but also in all subsequent arms! The solution is to bind by
111+
reference when using guards or refactor the entire expression, perhaps by
112+
putting the condition inside the body of the arm.
113+
"##,
114+
115+
E0303: r##"
116+
In certain cases it is possible for sub-bindings to violate memory safety.
117+
Updates to the borrow checker in a future version of Rust may remove this
118+
restriction, but for now patterns must be rewritten without sub-bindings.
119+
120+
// Code like this...
121+
match Some(5) {
122+
ref op_num @ Some(num) => ...
123+
None => ...
124+
}
125+
126+
// ... should be updated to code like this.
127+
match Some(5) {
128+
Some(num) => {
129+
let op_num = &Some(num);
130+
...
131+
}
132+
None => ...
133+
}
134+
135+
See also https://github.com/rust-lang/rust/issues/14587
51136
"##
137+
52138
}
53139

54140
register_diagnostics! {
55-
E0002,
56-
E0007,
57-
E0008,
58141
E0009,
59142
E0010,
60143
E0011,
@@ -117,7 +200,6 @@ register_diagnostics! {
117200
E0300, // unexpanded macro
118201
E0301, // cannot mutable borrow in a pattern guard
119202
E0302, // cannot assign in a pattern guard
120-
E0303, // pattern bindings are not allowed after an `@`
121203
E0304, // expected signed integer constant
122204
E0305, // expected constant
123205
E0306, // expected positive integer for repeat count

src/librustc_driver/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
277277
Some(ref code) => {
278278
match descriptions.find_description(&code[..]) {
279279
Some(ref description) => {
280-
println!("{}", description);
280+
// Slice off the leading newline and print.
281+
print!("{}", &description[1..]);
281282
}
282283
None => {
283284
early_error(&format!("no extended information for {}", code));

0 commit comments

Comments
 (0)