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.
This commit is contained in:
Tim Chevalier 2011-06-09 14:49:45 -07:00
parent efcf8570f0
commit 1cc3fe567c
2 changed files with 25 additions and 4 deletions

View File

@ -234,6 +234,7 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
}
fn instantiate(&ty::ctxt tcx,
&span sp,
&ty_getter getter,
&ast::def_id id,
&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 {
// The typedef is type-parametric. Do the type substitution.
//
// TODO: Make sure the number of supplied bindings matches the number
// of type parameters in the typedef. Emit a friendly error otherwise.
let vec[ty::t] param_bindings = [];
for (@ast::ty ast_ty in args) {
param_bindings += [ast_ty_to_ty(tcx, getter, ast_ty)];
}
if (vec::len(param_bindings) !=
ty::count_ty_params(tcx, params_opt_and_ty._1)) {
tcx.sess.span_err(sp, "Wrong number of type arguments for a"
+ " polymorphic tag");
}
auto typ = ty::substitute_type_params(tcx, param_bindings,
params_opt_and_ty._1);
ret typ;
@ -315,11 +321,13 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
case (ast::ty_path(?path, ?ann)) {
alt (tcx.def_map.get(ann.id)) {
case (ast::def_ty(?id)) {
typ = instantiate(tcx, getter, id, path.node.types);
typ = instantiate(tcx, ast_ty.span, getter, id,
path.node.types);
}
case (ast::def_native_ty(?id)) { typ = getter(id)._1; }
case (ast::def_obj(?id)) {
typ = instantiate(tcx, getter, id, path.node.types);
typ = instantiate(tcx, ast_ty.span, getter, id,
path.node.types);
}
case (ast::def_ty_arg(?id)) { typ = ty::mk_param(tcx, id); }
case (_) {

View File

@ -0,0 +1,13 @@
// xfail-stage0
// error-pattern: Wrong number of type arguments
tag quux[T] {
}
fn foo(quux c) -> () {
assert false;
}
fn main() {
fail;
}