提交 d23e2052 编写于 作者: M Marijn Haverbeke

Support interface casting in the typechecker

Issue #1437
上级 d214e3df
......@@ -1892,7 +1892,7 @@ fn visit_mod_with_impl_scope(e: @env, m: ast::_mod, s: span, sc: iscopes,
fn resolve_impl_in_expr(e: @env, x: @ast::expr, sc: iscopes, v: vt<iscopes>) {
alt x.node {
ast::expr_field(_, _, _) | ast::expr_path(_) {
ast::expr_field(_, _, _) | ast::expr_path(_) | ast::expr_cast(_, _) {
e.impl_map.insert(x.id, sc);
}
_ {}
......
......@@ -25,6 +25,7 @@
method_static(ast::def_id);
// iface id, method num, param num, bound num
method_param(ast::def_id, uint, uint, uint);
method_iface;
}
type method_map = hashmap<ast::node_id, method_origin>;
......@@ -1572,6 +1573,16 @@ fn lookup_method(fcx: @fn_ctxt, isc: resolve::iscopes,
}
ret none;
}
ty::ty_iface(did, tps) {
for m in *ty::iface_methods(tcx, did) {
if m.ident == name {
ret some({method_ty: ty::mk_fn(tcx, m.fty),
n_tps: vec::len(*m.tps),
substs: tps,
origin: method_iface});
}
}
}
_ {}
}
......@@ -2192,13 +2203,18 @@ fn lower_bound_proto(proto: ast::proto) -> ast::proto {
ty_to_str(tcx, t_1));
}
// FIXME there are more forms of cast to support, eventually.
if !( type_is_scalar(fcx, expr.span, t_e)
&& type_is_scalar(fcx, expr.span, t_1)) {
tcx.sess.span_err(expr.span,
"non-scalar cast: " +
alt ty::struct(tcx, t_1) {
// This will be looked up later on
ty::ty_iface(_, _) {}
_ {
// FIXME there are more forms of cast to support, eventually.
if !( type_is_scalar(fcx, expr.span, t_e)
&& type_is_scalar(fcx, expr.span, t_1)) {
tcx.sess.span_err(expr.span, "non-scalar cast: " +
ty_to_str(tcx, t_e) + " as " +
ty_to_str(tcx, t_1));
}
}
}
write::ty_only_fixup(fcx, id, t_1);
}
......@@ -3019,6 +3035,18 @@ fn resolve_expr(ex: @ast::expr, &&fcx: @fn_ctxt, v: visit::vt<@fn_ctxt>) {
_ {}
}
}
ast::expr_cast(src, _) {
let target_ty = expr_ty(cx.tcx, ex);
alt ty::struct(cx.tcx, target_ty) {
ty::ty_iface(_, _) {
let impls = cx.impl_map.get(ex.id);
let dict = lookup_dict(fcx, impls, ex.span,
expr_ty(cx.tcx, src), target_ty);
cx.dict_map.insert(ex.id, @[dict]);
}
_ {}
}
}
ast::expr_fn(ast::proto_block., _, _, _) {}
ast::expr_fn(_, _, _, _) { ret; }
_ {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册