Skip to content

Commit 6ab3837

Browse files
committed
Rollup merge of rust-lang#24458 - michaelsproul:extended-errors, r=huonw
I've updated the diagnostic registration plugin so that it validates error descriptions. An error description is only valid if it starts and ends with a newline, and contains no more than 80 characters per line. The plugin forced me to fix E0005 and E0006 which had escaped manual attention! I've also added errors for E0267, E0268, E0296, whilst updating E0303 as per discussion in rust-lang#24143. cc rust-lang#24407
2 parents 96d54e3 + c17e78c commit 6ab3837

File tree

2 files changed

+47
-50
lines changed

2 files changed

+47
-50
lines changed

src/librustc/diagnostics.rs

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -112,46 +112,6 @@ reference when using guards or refactor the entire expression, perhaps by
112112
putting the condition inside the body of the arm.
113113
"##,
114114

115-
E0152: r##"
116-
Lang items are already implemented in the standard library. Unless you are
117-
writing a free-standing application (e.g. a kernel), you do not need to provide
118-
them yourself.
119-
120-
You can build a free-standing crate by adding `#![no_std]` to the crate
121-
attributes:
122-
123-
#![feature(no_std)]
124-
#![no_std]
125-
126-
See also https://doc.rust-lang.org/book/no-stdlib.html
127-
"##,
128-
129-
E0158: r##"
130-
`const` and `static` mean different things. A `const` is a compile-time
131-
constant, an alias for a literal value. This property means you can match it
132-
directly within a pattern.
133-
134-
The `static` keyword, on the other hand, guarantees a fixed location in memory.
135-
This does not always mean that the value is constant. For example, a global
136-
mutex can be declared `static` as well.
137-
138-
If you want to match against a `static`, consider using a guard instead:
139-
140-
static FORTY_TWO: i32 = 42;
141-
match Some(42) {
142-
Some(x) if x == FORTY_TWO => ...
143-
...
144-
}
145-
"##,
146-
147-
E0161: r##"
148-
In Rust, you can only move a value when its size is known at compile time.
149-
150-
To work around this restriction, consider "hiding" the value behind a reference:
151-
either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
152-
it around as usual.
153-
"##,
154-
155115
E0162: r##"
156116
An if-let pattern attempts to match the pattern, and enters the body if the
157117
match was succesful. If the match is irrefutable (when it cannot fail to match),
@@ -217,6 +177,26 @@ use Method::*;
217177
enum Method { GET, POST }
218178
"##,
219179

180+
E0267: r##"
181+
This error indicates the use of loop keyword (break or continue) inside a
182+
closure but outside of any loop. Break and continue can be used as normal
183+
inside closures as long as they are also contained within a loop. To halt the
184+
execution of a closure you should instead use a return statement.
185+
"##,
186+
187+
E0268: r##"
188+
This error indicates the use of loop keyword (break or continue) outside of a
189+
loop. Without a loop to break out of or continue in, no sensible action can be
190+
taken.
191+
"##,
192+
193+
E0296: r##"
194+
This error indicates that the given recursion limit could not be parsed. Ensure
195+
that the value provided is a positive integer between quotes, like so:
196+
197+
#![recursion_limit="1000"]
198+
"##,
199+
220200
E0297: r##"
221201
Patterns used to bind names must be irrefutable. That is, they must guarantee
222202
that a name will be extracted in all cases. Instead of pattern matching the
@@ -293,16 +273,6 @@ match Some(5) {
293273
}
294274
295275
See also https://github.com/rust-lang/rust/issues/14587
296-
"##,
297-
298-
E0306: r##"
299-
In an array literal `[x; N]`, `N` is the number of elements in the array. This
300-
number cannot be negative.
301-
"##,
302-
303-
E0307: r##"
304-
The length of an array is part of its type. For this reason, this length must be
305-
a compile-time constant.
306276
"##
307277

308278
}
@@ -332,6 +302,10 @@ register_diagnostics! {
332302
E0137,
333303
E0138,
334304
E0139,
305+
E0152,
306+
E0158,
307+
E0161,
308+
E0170,
335309
E0261, // use of undeclared lifetime name
336310
E0262, // illegal lifetime parameter name
337311
E0263, // lifetime name declared twice in same scope
@@ -363,6 +337,8 @@ register_diagnostics! {
363337
E0300, // unexpanded macro
364338
E0304, // expected signed integer constant
365339
E0305, // expected constant
340+
E0306, // expected positive integer for repeat count
341+
E0307, // expected constant integer for repeat count
366342
E0308,
367343
E0309, // thing may not live long enough
368344
E0310, // thing may not live long enough

src/libsyntax/diagnostics/plugin.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ use parse::token;
2020
use ptr::P;
2121
use util::small_vector::SmallVector;
2222

23+
// Maximum width of any line in an extended error description (inclusive).
24+
const MAX_DESCRIPTION_WIDTH: usize = 80;
25+
2326
thread_local! {
2427
static REGISTERED_DIAGNOSTICS: RefCell<BTreeMap<Name, Option<Name>>> = {
2528
RefCell::new(BTreeMap::new())
@@ -92,6 +95,24 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
9295
}
9396
_ => unreachable!()
9497
};
98+
// Check that the description starts and ends with a newline and doesn't
99+
// overflow the maximum line width.
100+
description.map(|raw_msg| {
101+
let msg = raw_msg.as_str();
102+
if !msg.starts_with("\n") || !msg.ends_with("\n") {
103+
ecx.span_err(span, &format!(
104+
"description for error code {} doesn't start and end with a newline",
105+
token::get_ident(*code)
106+
));
107+
}
108+
if msg.lines().any(|line| line.len() > MAX_DESCRIPTION_WIDTH) {
109+
ecx.span_err(span, &format!(
110+
"description for error code {} contains a line longer than {} characters",
111+
token::get_ident(*code), MAX_DESCRIPTION_WIDTH
112+
));
113+
}
114+
raw_msg
115+
});
95116
with_registered_diagnostics(|diagnostics| {
96117
if diagnostics.insert(code.name, description).is_some() {
97118
ecx.span_err(span, &format!(

0 commit comments

Comments
 (0)