Skip to content

Commit

Permalink
Rollup merge of rust-lang#24458 - michaelsproul:extended-errors, r=huonw
Browse files Browse the repository at this point in the history
 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
  • Loading branch information
Manishearth committed Apr 17, 2015
2 parents 96d54e3 + c17e78c commit 6ab3837
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
76 changes: 26 additions & 50 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,46 +112,6 @@ reference when using guards or refactor the entire expression, perhaps by
putting the condition inside the body of the arm.
"##,

E0152: r##"
Lang items are already implemented in the standard library. Unless you are
writing a free-standing application (e.g. a kernel), you do not need to provide
them yourself.
You can build a free-standing crate by adding `#![no_std]` to the crate
attributes:
#![feature(no_std)]
#![no_std]
See also https://doc.rust-lang.org/book/no-stdlib.html
"##,

E0158: r##"
`const` and `static` mean different things. A `const` is a compile-time
constant, an alias for a literal value. This property means you can match it
directly within a pattern.
The `static` keyword, on the other hand, guarantees a fixed location in memory.
This does not always mean that the value is constant. For example, a global
mutex can be declared `static` as well.
If you want to match against a `static`, consider using a guard instead:
static FORTY_TWO: i32 = 42;
match Some(42) {
Some(x) if x == FORTY_TWO => ...
...
}
"##,

E0161: r##"
In Rust, you can only move a value when its size is known at compile time.
To work around this restriction, consider "hiding" the value behind a reference:
either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
it around as usual.
"##,

E0162: r##"
An if-let pattern attempts to match the pattern, and enters the body if the
match was succesful. If the match is irrefutable (when it cannot fail to match),
Expand Down Expand Up @@ -217,6 +177,26 @@ use Method::*;
enum Method { GET, POST }
"##,

E0267: r##"
This error indicates the use of loop keyword (break or continue) inside a
closure but outside of any loop. Break and continue can be used as normal
inside closures as long as they are also contained within a loop. To halt the
execution of a closure you should instead use a return statement.
"##,

E0268: r##"
This error indicates the use of loop keyword (break or continue) outside of a
loop. Without a loop to break out of or continue in, no sensible action can be
taken.
"##,

E0296: r##"
This error indicates that the given recursion limit could not be parsed. Ensure
that the value provided is a positive integer between quotes, like so:
#![recursion_limit="1000"]
"##,

E0297: r##"
Patterns used to bind names must be irrefutable. That is, they must guarantee
that a name will be extracted in all cases. Instead of pattern matching the
Expand Down Expand Up @@ -293,16 +273,6 @@ match Some(5) {
}
See also https://github.com/rust-lang/rust/issues/14587
"##,

E0306: r##"
In an array literal `[x; N]`, `N` is the number of elements in the array. This
number cannot be negative.
"##,

E0307: r##"
The length of an array is part of its type. For this reason, this length must be
a compile-time constant.
"##

}
Expand Down Expand Up @@ -332,6 +302,10 @@ register_diagnostics! {
E0137,
E0138,
E0139,
E0152,
E0158,
E0161,
E0170,
E0261, // use of undeclared lifetime name
E0262, // illegal lifetime parameter name
E0263, // lifetime name declared twice in same scope
Expand Down Expand Up @@ -363,6 +337,8 @@ register_diagnostics! {
E0300, // unexpanded macro
E0304, // expected signed integer constant
E0305, // expected constant
E0306, // expected positive integer for repeat count
E0307, // expected constant integer for repeat count
E0308,
E0309, // thing may not live long enough
E0310, // thing may not live long enough
Expand Down
21 changes: 21 additions & 0 deletions src/libsyntax/diagnostics/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use parse::token;
use ptr::P;
use util::small_vector::SmallVector;

// Maximum width of any line in an extended error description (inclusive).
const MAX_DESCRIPTION_WIDTH: usize = 80;

thread_local! {
static REGISTERED_DIAGNOSTICS: RefCell<BTreeMap<Name, Option<Name>>> = {
RefCell::new(BTreeMap::new())
Expand Down Expand Up @@ -92,6 +95,24 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
}
_ => unreachable!()
};
// Check that the description starts and ends with a newline and doesn't
// overflow the maximum line width.
description.map(|raw_msg| {
let msg = raw_msg.as_str();
if !msg.starts_with("\n") || !msg.ends_with("\n") {
ecx.span_err(span, &format!(
"description for error code {} doesn't start and end with a newline",
token::get_ident(*code)
));
}
if msg.lines().any(|line| line.len() > MAX_DESCRIPTION_WIDTH) {
ecx.span_err(span, &format!(
"description for error code {} contains a line longer than {} characters",
token::get_ident(*code), MAX_DESCRIPTION_WIDTH
));
}
raw_msg
});
with_registered_diagnostics(|diagnostics| {
if diagnostics.insert(code.name, description).is_some() {
ecx.span_err(span, &format!(
Expand Down

0 comments on commit 6ab3837

Please sign in to comment.