Auto merge of #36915 - jfirebaugh:E0308-split, r=nikomatsakis

Use a distinct error code for "if may be missing an else clause"

Introduce the possibility of assigning distinct error codes to the various origin types of E0308. Start by assigning E0317 for the "IfExpressionWithNoElse" case, and write a long diagnostic specific to this case.

Fixes #36596
This commit is contained in:
bors
2016-10-17 14:06:46 -07:00
committed by GitHub
3 changed files with 28 additions and 6 deletions
+17
View File
@@ -1407,6 +1407,23 @@ fn make_child<'elve>(x: &mut &'elve isize, y: &mut &'elve isize) {
```
"##,
E0317: r##"
This error occurs when an `if` expression without an `else` block is used in a
context where a type other than `()` is expected, for example a `let`
expression:
```compile_fail,E0317
fn main() {
let x = 5;
let a = if x == 5 { 1 };
}
```
An `if` expression without an `else` block has the type `()`, so this is a type
error. To resolve it, add an `else` block having the same type as the `if`
block.
"##,
E0398: r##"
In Rust 1.3, the default object lifetime bounds are expected to change, as
described in RFC #1156 [1]. You are getting a warning because the compiler
+10 -5
View File
@@ -577,11 +577,16 @@ pub fn report_and_explain_type_error(&self,
terr: &TypeError<'tcx>)
-> DiagnosticBuilder<'tcx>
{
// FIXME: do we want to use a different error code for each origin?
let mut diag = struct_span_err!(
self.tcx.sess, trace.origin.span(), E0308,
"{}", trace.origin.as_failure_str()
);
let span = trace.origin.span();
let failure_str = trace.origin.as_failure_str();
let mut diag = match trace.origin {
TypeOrigin::IfExpressionWithNoElse(_) => {
struct_span_err!(self.tcx.sess, span, E0317, "{}", failure_str)
},
_ => {
struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str)
},
};
self.note_type_err(&mut diag, trace.origin, None, Some(trace.values), terr);
diag
}
@@ -10,7 +10,7 @@
fn main() {
let a = if true { true };
//~^ ERROR if may be missing an else clause
//~^ ERROR if may be missing an else clause [E0317]
//~| expected type `()`
//~| found type `bool`
//~| expected (), found bool