Skip to content

Commit

Permalink
Auto merge of rust-lang#36915 - jfirebaugh:E0308-split, r=nikomatsakis
Browse files Browse the repository at this point in the history
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 rust-lang#36596
  • Loading branch information
bors authored Oct 17, 2016
2 parents ce31626 + d07602b commit e011175
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions src/librustc/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
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
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/if-without-else-result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e011175

Please sign in to comment.