Skip to content

Commit 734166f

Browse files
committed
Print out return type correctly in typestate error message
In the "not all paths return" error message, typestate was printing the AST type from the fn decl, not the ty::t type. This ended in tears when the AST return type was "ty_infer". Now it looks up the function node ID's type and uses util::ppaux::ty_to_str instead. Closes #2163.
1 parent 43061f3 commit 734166f

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/rustc/middle/tstate/ck.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import middle::ty;
77
import tstate::ann::{precond, prestate,
88
implies, ann_precond, ann_prestate};
99
import aux::*;
10-
import syntax::print::pprust::ty_to_str;
10+
11+
import util::ppaux::ty_to_str;
1112
import bitvectors::*;
1213
import annotate::annotate_crate;
1314
import collect_locals::mk_f_to_fn_info;
@@ -116,13 +117,14 @@ fn check_states_against_conditions(fcx: fn_ctxt,
116117
!ty::type_is_nil(ty::ty_fn_ret(ty::node_id_to_type(
117118
fcx.ccx.tcx, id))) &&
118119
f_decl.cf == return_val {
120+
let fn_ty = ty::node_id_to_type(fcx.ccx.tcx, id);
119121
fcx.ccx.tcx.sess.span_err(f_body.span,
120-
"in function " + fcx.name +
121-
", not all control paths \
122-
return a value");
122+
#fmt("in function `%s`, not all control paths \
123+
return a value", fcx.name));
123124
fcx.ccx.tcx.sess.span_fatal(f_decl.output.span,
124-
"see declared return type of '" +
125-
ty_to_str(f_decl.output) + "'");
125+
#fmt("see function return type of `%s`",
126+
ty_to_str(fcx.ccx.tcx,
127+
ty::ty_fn_ret(fn_ty))));
126128
} else if f_decl.cf == noreturn {
127129

128130
// check that this really always fails

src/test/compile-fail/issue-2163.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main(s: [str]) {
2+
let a = [];
3+
vec::each(a) { |x| //! ERROR in function `anon`, not all control paths
4+
} //! ERROR see function return type of `bool`
5+
}

0 commit comments

Comments
 (0)