auto merge of #11482 : fhahn/rust/issue-8005-better-error-msg-semi-last-stmt, r=alexcrichton

This is a patch for #8005, thanks @lfairy for the hint.

It seems like `block.expr` is None, if the last line of a function has a semi colon (= it ends with a statement).

@kmcallister does this error message cover the intended use cases? 
I'm not sure about the message, the wording and the span could probably be improved.
This commit is contained in:
bors 2014-01-13 11:06:41 -08:00
commit b8c60f906b
2 changed files with 55 additions and 3 deletions

View file

@ -439,7 +439,7 @@ fn visit_fn(v: &mut LivenessVisitor,
// check for various error conditions
lsets.visit_block(body, ());
lsets.check_ret(id, sp, fk, entry_ln);
lsets.check_ret(id, sp, fk, entry_ln, body);
lsets.warn_about_unused_args(decl, entry_ln);
}
@ -1575,7 +1575,8 @@ impl Liveness {
id: NodeId,
sp: Span,
_fk: &FnKind,
entry_ln: LiveNode) {
entry_ln: LiveNode,
body: &Block) {
if self.live_on_entry(entry_ln, self.s.no_ret_var).is_some() {
// if no_ret_var is live, then we fall off the end of the
// function without any kind of return expression:
@ -1588,9 +1589,30 @@ impl Liveness {
self.tcx.sess.span_err(
sp, "some control paths may return");
} else {
let ends_with_stmt = match body.expr {
None if body.stmts.len() > 0 =>
match body.stmts.last().node {
StmtSemi(e, _) => {
let t_stmt = ty::expr_ty(self.tcx, e);
ty::get(t_stmt).sty == ty::get(t_ret).sty
},
_ => false
},
_ => false
};
if ends_with_stmt {
let last_stmt = body.stmts.last();
let span_semicolon = Span {
lo: last_stmt.span.hi,
hi: last_stmt.span.hi,
expn_info: last_stmt.span.expn_info
};
self.tcx.sess.span_note(
span_semicolon, "consider removing this semicolon:");
}
self.tcx.sess.span_err(
sp, "not all control paths return a value");
}
}
}
}

View file

@ -0,0 +1,30 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// regression test for #8005
#[feature(macro_rules)];
macro_rules! test ( () => { fn foo() -> int { 1i; } } ) //~ ERROR not all control paths return a value
//~^ NOTE consider removing this semicolon
fn no_return() -> int {} //~ ERROR not all control paths return a value
fn bar(x: u32) -> u32 { //~ ERROR not all control paths return a value
x * 2; //~ NOTE consider removing this semicolon
}
fn baz(x: u64) -> u32 { //~ ERROR not all control paths return a value
x * 2;
}
fn main() {
test!();
}