Skip to content

Commit 1cc3fe5

Browse files
committed
Improve error message for wrong number of type arguments
Improve error message in the case where a use of a polymorphic tag has insufficient type arguments given. Before, the typechecker was just crashing with a bounds check error.
1 parent efcf857 commit 1cc3fe5

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/comp/middle/typeck.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
234234
}
235235

236236
fn instantiate(&ty::ctxt tcx,
237+
&span sp,
237238
&ty_getter getter,
238239
&ast::def_id id,
239240
&vec[@ast::ty] args) -> ty::t {
@@ -246,13 +247,18 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
246247

247248
// The typedef is type-parametric. Do the type substitution.
248249
//
249-
// TODO: Make sure the number of supplied bindings matches the number
250-
// of type parameters in the typedef. Emit a friendly error otherwise.
251250
let vec[ty::t] param_bindings = [];
252251
for (@ast::ty ast_ty in args) {
253252
param_bindings += [ast_ty_to_ty(tcx, getter, ast_ty)];
254253
}
255254

255+
if (vec::len(param_bindings) !=
256+
ty::count_ty_params(tcx, params_opt_and_ty._1)) {
257+
tcx.sess.span_err(sp, "Wrong number of type arguments for a"
258+
+ " polymorphic tag");
259+
}
260+
261+
256262
auto typ = ty::substitute_type_params(tcx, param_bindings,
257263
params_opt_and_ty._1);
258264
ret typ;
@@ -315,11 +321,13 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
315321
case (ast::ty_path(?path, ?ann)) {
316322
alt (tcx.def_map.get(ann.id)) {
317323
case (ast::def_ty(?id)) {
318-
typ = instantiate(tcx, getter, id, path.node.types);
324+
typ = instantiate(tcx, ast_ty.span, getter, id,
325+
path.node.types);
319326
}
320327
case (ast::def_native_ty(?id)) { typ = getter(id)._1; }
321328
case (ast::def_obj(?id)) {
322-
typ = instantiate(tcx, getter, id, path.node.types);
329+
typ = instantiate(tcx, ast_ty.span, getter, id,
330+
path.node.types);
323331
}
324332
case (ast::def_ty_arg(?id)) { typ = ty::mk_param(tcx, id); }
325333
case (_) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// xfail-stage0
2+
// error-pattern: Wrong number of type arguments
3+
4+
tag quux[T] {
5+
}
6+
7+
fn foo(quux c) -> () {
8+
assert false;
9+
}
10+
11+
fn main() {
12+
fail;
13+
}

0 commit comments

Comments
 (0)