about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-10-17 14:06:46 -0700
committerGitHub <noreply@github.com>2016-10-17 14:06:46 -0700
commite0111758eb4f215db4ec26f809ef3edf5dfb66f5 (patch)
treefa8590b5ab17783327b773286e6e044a68916224
parentce316268014ed7bc3599324b379be1331997d3ab (diff)
parentd07602b573d210775dd183d414c1ac36a2751144 (diff)
downloadrust-e0111758eb4f215db4ec26f809ef3edf5dfb66f5.tar.gz
rust-e0111758eb4f215db4ec26f809ef3edf5dfb66f5.zip
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
-rw-r--r--src/librustc/diagnostics.rs17
-rw-r--r--src/librustc/infer/error_reporting.rs15
-rw-r--r--src/test/compile-fail/if-without-else-result.rs2
3 files changed, 28 insertions, 6 deletions
diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs
index 12a1a425524..465a09505e4 100644
--- a/src/librustc/diagnostics.rs
+++ b/src/librustc/diagnostics.rs
@@ -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
diff --git a/src/librustc/infer/error_reporting.rs b/src/librustc/infer/error_reporting.rs
index 3f216d69168..373ea4aac57 100644
--- a/src/librustc/infer/error_reporting.rs
+++ b/src/librustc/infer/error_reporting.rs
@@ -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
     }
diff --git a/src/test/compile-fail/if-without-else-result.rs b/src/test/compile-fail/if-without-else-result.rs
index e8aa1f70ea1..95bcce5a847 100644
--- a/src/test/compile-fail/if-without-else-result.rs
+++ b/src/test/compile-fail/if-without-else-result.rs
@@ -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