diff options
| author | John Firebaugh <john.firebaugh@gmail.com> | 2016-10-02 12:59:44 -0700 |
|---|---|---|
| committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-10-02 13:12:35 -0700 |
| commit | 9f7cc5faa0e9c9b865a95aa1fb0725528b95595e (patch) | |
| tree | a20855d0ca6d8c8a1ce97f7bd5691b36b24f2448 | |
| parent | 791fb778ccfb3dc831d99544093a9d4e3cf82a49 (diff) | |
| download | rust-9f7cc5faa0e9c9b865a95aa1fb0725528b95595e.tar.gz rust-9f7cc5faa0e9c9b865a95aa1fb0725528b95595e.zip | |
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.rs | 15 | ||||
| -rw-r--r-- | src/librustc/infer/error_reporting.rs | 8 | ||||
| -rw-r--r-- | src/librustc/infer/mod.rs | 19 | ||||
| -rw-r--r-- | src/test/compile-fail/if-without-else-result.rs | 2 |
4 files changed, 39 insertions, 5 deletions
diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index de68cc707ad..f43cbb2696d 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -1431,6 +1431,21 @@ fn make_child<'elve>(x: &mut &'elve isize, y: &mut &'elve isize) { ``` "##, +E0317: r##" +An `if` expression without an `else` block is required to have the type `()`. +This error occurs when the `if` block has a type other than `()`. For example: + +```compile_fail,E0317 +fn main() { + let x = 5; + let a = if x == 5 { 1 }; +} +``` + +To resolve this error, either add an `else` block having the same type as the +`if` block, or adjust the `if` block so that it has the type `()`. +"##, + 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..f157b02f14d 100644 --- a/src/librustc/infer/error_reporting.rs +++ b/src/librustc/infer/error_reporting.rs @@ -577,10 +577,10 @@ 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 mut diag = self.tcx.sess.struct_span_err_with_code( + trace.origin.span(), + trace.origin.as_failure_str(), + trace.origin.as_error_code() ); self.note_type_err(&mut diag, trace.origin, None, Some(trace.values), terr); diag diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index 39fc50666a8..036b3ae6284 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -219,6 +219,25 @@ pub enum TypeOrigin { } impl TypeOrigin { + fn as_error_code(&self) -> &'static str { + match self { + // FIXME: use distinct codes for each case + &TypeOrigin::Misc(_) => "E0308", + &TypeOrigin::RelateOutputImplTypes(_) => "E0308", + &TypeOrigin::ExprAssignable(_) => "E0308", + &TypeOrigin::MethodCompatCheck(_) => "E0308", + &TypeOrigin::MatchExpressionArm(..) => "E0308", + &TypeOrigin::IfExpression(_) => "E0308", + &TypeOrigin::IfExpressionWithNoElse(_) => "E0317", + &TypeOrigin::RangeExpression(_) => "E0308", + &TypeOrigin::EquatePredicate(_) => "E0308", + &TypeOrigin::MainFunctionType(_) => "E0308", + &TypeOrigin::StartFunctionType(_) => "E0308", + &TypeOrigin::IntrinsicType(_) => "E0308", + &TypeOrigin::MethodReceiver(_) => "E0308", + } + } + fn as_failure_str(&self) -> &'static str { match self { &TypeOrigin::Misc(_) | 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 |
