Skip to content

Commit 1a6d029

Browse files
committed
Emit a better error message for unbound type parameters in nested functions
This code was causing a bounds check failure: fn hd[U](&vec[U] v) -> U { fn hd1(&vec[U] w) -> U { ret w.(0); } ret hd1(v); } because in hd1, U was being treated as if it referred to a type parameter of hd1, rather than referring to the lexically enclosing binding for U that's part of hd. I'm actually not sure whether this is a legit program or not. But I wanted to get rid of the bounds check error, so I assumed that program shouldn't compile and made it a proper error message.
1 parent efd8ff4 commit 1a6d029

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/comp/middle/ty.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,18 +2580,23 @@ fn type_err_to_str(&ty::type_err err) -> str {
25802580

25812581
// Converts type parameters in a type to type variables and returns the
25822582
// resulting type along with a list of type variable IDs.
2583-
fn bind_params_in_type(&ctxt cx, fn() -> int next_ty_var, t typ,
2583+
fn bind_params_in_type(&span sp, &ctxt cx, fn() -> int next_ty_var, t typ,
25842584
uint ty_param_count) -> tup(vec[int], t) {
25852585
let vec[int] param_var_ids = [];
25862586
auto i = 0u;
25872587
while (i < ty_param_count) { param_var_ids += [next_ty_var()]; i += 1u; }
2588-
fn binder(ctxt cx, vec[int] param_var_ids, fn() -> int next_ty_var,
2589-
uint index) -> t {
2590-
ret mk_var(cx, param_var_ids.(index));
2588+
fn binder(span sp, ctxt cx, vec[int] param_var_ids,
2589+
fn() -> int next_ty_var, uint index) -> t {
2590+
if (index < vec::len(param_var_ids)) {
2591+
ret mk_var(cx, param_var_ids.(index));
2592+
}
2593+
else {
2594+
cx.sess.span_fatal(sp, "Unbound type parameter in callee's type");
2595+
}
25912596
}
25922597
auto new_typ =
2593-
fold_ty(cx, fm_param(bind binder(cx, param_var_ids, next_ty_var, _)),
2594-
typ);
2598+
fold_ty(cx, fm_param(bind binder(sp, cx, param_var_ids,
2599+
next_ty_var, _)), typ);
25952600
ret tup(param_var_ids, new_typ);
25962601
}
25972602

src/comp/middle/typeck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn instantiate_path(&@fn_ctxt fcx, &ast::path pth, &ty_param_count_and_ty tpt,
132132
&span sp) -> ty_param_substs_opt_and_ty {
133133
auto ty_param_count = tpt._0;
134134
auto bind_result =
135-
bind_params_in_type(fcx.ccx.tcx, bind next_ty_var_id(fcx), tpt._1,
135+
bind_params_in_type(sp, fcx.ccx.tcx, bind next_ty_var_id(fcx), tpt._1,
136136
ty_param_count);
137137
auto ty_param_vars = bind_result._0;
138138
auto t = bind_result._1;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// error-pattern:Unbound type parameter in callee
2+
/* I'm actually not sure whether this should compile.
3+
But having a nice error message seems better than
4+
a bounds check failure (which is what was happening
5+
before.) */
6+
fn hd[U](&vec[U] v) -> U {
7+
fn hd1(&vec[U] w) -> U {
8+
ret w.(0);
9+
}
10+
ret hd1(v);
11+
}

0 commit comments

Comments
 (0)