Check kind bounds when calling methods

Closes #1915
This commit is contained in:
Marijn Haverbeke 2012-03-16 16:25:36 +01:00
parent 25c2be0ff4
commit 35fc4a4291
5 changed files with 43 additions and 13 deletions

View File

@ -330,7 +330,7 @@ fn uint_hash<V: copy>() -> hashmap<uint, V> {
#[doc = "
Convenience function for adding keys to a hashmap with nil type keys
"]
fn set_add<K>(set: set<K>, key: K) -> bool { ret set.insert(key, ()); }
fn set_add<K: copy>(set: set<K>, key: K) -> bool { ret set.insert(key, ()); }
#[cfg(test)]
mod tests {

View File

@ -93,7 +93,7 @@ fn emit_from_vec<S: serializer, T>(s: S, v: [T], f: fn(T)) {
}
}
fn read_to_vec<D: deserializer, T>(d: D, f: fn() -> T) -> [T] {
fn read_to_vec<D: deserializer, T: copy>(d: D, f: fn() -> T) -> [T] {
d.read_vec {|len|
vec::from_fn(len) {|i|
d.read_vec_elt(i) {|| f() }
@ -108,7 +108,7 @@ impl serializer_helpers<S: serializer> for S {
}
impl deserializer_helpers<D: deserializer> for D {
fn read_to_vec<T>(f: fn() -> T) -> [T] {
fn read_to_vec<T: copy>(f: fn() -> T) -> [T] {
read_to_vec(self, f)
}
}
@ -252,7 +252,8 @@ fn serialize_option<S: serializer,T>(s: S, v: option<T>, st: fn(T)) {
}
}
fn deserialize_option<D: deserializer,T>(d: D, st: fn() -> T) -> option<T> {
fn deserialize_option<D: deserializer,T: copy>(d: D, st: fn() -> T)
-> option<T> {
d.read_enum("option") {||
d.read_enum_variant {|i|
alt check i {

View File

@ -180,22 +180,38 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
i += 1u;
}
}
expr_path(_) {
expr_path(_) | expr_field(_, _, _) {
alt cx.tcx.node_type_substs.find(e.id) {
some(ts) {
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(e.id));
let bounds = ty::lookup_item_type(cx.tcx, did).bounds;
let i = 0u;
for ty in ts {
let bounds = alt check e.node {
expr_path(_) {
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(e.id));
ty::lookup_item_type(cx.tcx, did).bounds
}
expr_field(_, _, _) {
alt cx.method_map.get(e.id) {
typeck::method_static(did) {
ty::lookup_item_type(cx.tcx, did).bounds
}
typeck::method_param(ifce_id, n_mth, _, _) |
typeck::method_iface(ifce_id, n_mth) {
let ifce_bounds =
ty::lookup_item_type(cx.tcx, ifce_id).bounds;
let mth = ty::iface_methods(cx.tcx, ifce_id)[n_mth];
@(*ifce_bounds + *mth.tps)
}
}
}
};
vec::iter2(ts, *bounds) {|ty, bound|
let kind = ty::type_kind(cx.tcx, ty);
let p_kind = ty::param_bounds_to_kind(bounds[i]);
let p_kind = ty::param_bounds_to_kind(bound);
if !ty::kind_lteq(p_kind, kind) {
cx.tcx.sess.span_err(e.span, "instantiating a " +
kind_to_str(p_kind) +
" type parameter with a "
+ kind_to_str(kind) + " type");
}
i += 1u;
}
}
none {}

View File

@ -723,8 +723,10 @@ fn mk_deser_fn(cx: ext_ctxt, span: span, name: str, tps: [ast::ty_param],
let deser_tps: [ast::ty_param] =
[{ident: "__D",
id: cx.next_id(),
bounds: deser_bnds}] +
vec::map(tps) {|tp| cx.clone_ty_param(tp) };
bounds: deser_bnds}] + vec::map(tps) {|tp|
let cloned = cx.clone_ty_param(tp);
{bounds: @(*cloned.bounds + [ast::bound_copy]) with cloned}
};
let deser_blk = cx.expr_blk(f(cx, tps_map, #ast(expr){__d}));

View File

@ -0,0 +1,11 @@
// error-pattern:instantiating a copyable type parameter with a noncopyable
fn foo<T>() {
1u.bar::<T>();
}
impl methods for uint {
fn bar<T:copy>() {
}
}
fn main() {}