Skip to content

Commit 9f7cc5f

Browse files
committed
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
1 parent 791fb77 commit 9f7cc5f

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/librustc/diagnostics.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,21 @@ fn make_child<'elve>(x: &mut &'elve isize, y: &mut &'elve isize) {
14311431
```
14321432
"##,
14331433

1434+
E0317: r##"
1435+
An `if` expression without an `else` block is required to have the type `()`.
1436+
This error occurs when the `if` block has a type other than `()`. For example:
1437+
1438+
```compile_fail,E0317
1439+
fn main() {
1440+
let x = 5;
1441+
let a = if x == 5 { 1 };
1442+
}
1443+
```
1444+
1445+
To resolve this error, either add an `else` block having the same type as the
1446+
`if` block, or adjust the `if` block so that it has the type `()`.
1447+
"##,
1448+
14341449
E0398: r##"
14351450
In Rust 1.3, the default object lifetime bounds are expected to change, as
14361451
described in RFC #1156 [1]. You are getting a warning because the compiler

src/librustc/infer/error_reporting.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
577577
terr: &TypeError<'tcx>)
578578
-> DiagnosticBuilder<'tcx>
579579
{
580-
// FIXME: do we want to use a different error code for each origin?
581-
let mut diag = struct_span_err!(
582-
self.tcx.sess, trace.origin.span(), E0308,
583-
"{}", trace.origin.as_failure_str()
580+
let mut diag = self.tcx.sess.struct_span_err_with_code(
581+
trace.origin.span(),
582+
trace.origin.as_failure_str(),
583+
trace.origin.as_error_code()
584584
);
585585
self.note_type_err(&mut diag, trace.origin, None, Some(trace.values), terr);
586586
diag

src/librustc/infer/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,25 @@ pub enum TypeOrigin {
219219
}
220220

221221
impl TypeOrigin {
222+
fn as_error_code(&self) -> &'static str {
223+
match self {
224+
// FIXME: use distinct codes for each case
225+
&TypeOrigin::Misc(_) => "E0308",
226+
&TypeOrigin::RelateOutputImplTypes(_) => "E0308",
227+
&TypeOrigin::ExprAssignable(_) => "E0308",
228+
&TypeOrigin::MethodCompatCheck(_) => "E0308",
229+
&TypeOrigin::MatchExpressionArm(..) => "E0308",
230+
&TypeOrigin::IfExpression(_) => "E0308",
231+
&TypeOrigin::IfExpressionWithNoElse(_) => "E0317",
232+
&TypeOrigin::RangeExpression(_) => "E0308",
233+
&TypeOrigin::EquatePredicate(_) => "E0308",
234+
&TypeOrigin::MainFunctionType(_) => "E0308",
235+
&TypeOrigin::StartFunctionType(_) => "E0308",
236+
&TypeOrigin::IntrinsicType(_) => "E0308",
237+
&TypeOrigin::MethodReceiver(_) => "E0308",
238+
}
239+
}
240+
222241
fn as_failure_str(&self) -> &'static str {
223242
match self {
224243
&TypeOrigin::Misc(_) |

src/test/compile-fail/if-without-else-result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
fn main() {
1212
let a = if true { true };
13-
//~^ ERROR if may be missing an else clause
13+
//~^ ERROR if may be missing an else clause [E0317]
1414
//~| expected type `()`
1515
//~| found type `bool`
1616
//~| expected (), found bool

0 commit comments

Comments
 (0)