Point at coercion reason for if exprs without else clause

```
error[E0317]: if may be missing an else clause
  --> $DIR/if-without-else-as-fn-expr.rs:2:5
   |
LL |   fn foo(bar: usize) -> usize {
   |                         ----- found `usize` because of this return type
LL | /     if bar % 5 == 0 {
LL | |         return 3;
LL | |     }
   | |_____^ expected (), found usize
   |
   = note: expected type `()`
              found type `usize`
   = note: `if` expressions without `else` must evaluate to `()`
```
This commit is contained in:
Esteban Küber 2019-03-06 17:54:35 -08:00
parent f22dca0a1b
commit 369058eacd
6 changed files with 62 additions and 1 deletions

View file

@ -3472,8 +3472,39 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// We won't diverge unless both branches do (or the condition does).
self.diverges.set(cond_diverges | then_diverges & else_diverges);
} else {
// If this `if` expr is the parent's function return expr, the cause of the type
// coercion is the return type, point at it. (#25228)
let mut ret_reason = None;
if let Node::Block(block) = self.tcx.hir().get_by_hir_id(
self.tcx.hir().get_parent_node_by_hir_id(
self.tcx.hir().get_parent_node_by_hir_id(then_expr.hir_id),
),
) {
// check that the body's parent is an fn
let parent = self.tcx.hir().get_by_hir_id(
self.tcx.hir().get_parent_node_by_hir_id(
self.tcx.hir().get_parent_node_by_hir_id(block.hir_id),
),
);
if let (Some(expr), Node::Item(hir::Item {
node: hir::ItemKind::Fn(..), ..
})) = (&block.expr, parent) {
// check that the `if` expr without `else` is the fn body's expr
if expr.span == sp {
ret_reason = self.get_fn_decl(then_expr.hir_id).map(|(fn_decl, _)| (
fn_decl.output.span(),
format!("found `{}` because of this return type", fn_decl.output),
));
}
}
}
let else_cause = self.cause(sp, ObligationCauseCode::IfExpressionWithNoElse);
coerce.coerce_forced_unit(self, &else_cause, &mut |_| (), true);
coerce.coerce_forced_unit(self, &else_cause, &mut |err| {
if let Some((sp, msg)) = &ret_reason {
err.span_label(*sp, msg.as_str());
}
err.note("`if` expressions without `else` must evaluate to `()`");
}, true);
// If the condition is false we can't diverge.
self.diverges.set(cond_diverges);

View file

@ -0,0 +1,10 @@
fn foo(bar: usize) -> usize {
if bar % 5 == 0 {
return 3;
}
//~^^^ ERROR if may be missing an else clause
}
fn main() {
let _ = foo(1);
}

View file

@ -0,0 +1,17 @@
error[E0317]: if may be missing an else clause
--> $DIR/if-without-else-as-fn-expr.rs:2:5
|
LL | fn foo(bar: usize) -> usize {
| ----- found `usize` because of this return type
LL | / if bar % 5 == 0 {
LL | | return 3;
LL | | }
| |_____^ expected (), found usize
|
= note: expected type `()`
found type `usize`
= note: `if` expressions without `else` must evaluate to `()`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0317`.

View file

@ -6,6 +6,7 @@ LL | let a = if true { true };
|
= note: expected type `()`
found type `bool`
= note: `if` expressions without `else` must evaluate to `()`
error: aborting due to previous error

View file

@ -13,6 +13,7 @@ LL | | };
|
= note: expected type `()`
found type `{integer}`
= note: `if` expressions without `else` must evaluate to `()`
error: aborting due to previous error

View file

@ -6,6 +6,7 @@ LL | Drop = assert_eq!(1, 1)
|
= note: expected type `()`
found type `isize`
= note: `if` expressions without `else` must evaluate to `()`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error: aborting due to previous error