typeck: cleanup/refactoring

This commit is contained in:
Lindsey Kuper 2012-05-25 11:03:52 -07:00
parent f7cb7b06d5
commit 123c5c4315
8 changed files with 35 additions and 44 deletions

View File

@ -53,7 +53,7 @@ import middle::ty;
import middle::ty::{arg, field, node_type_table, mk_nil,
ty_param_bounds_and_ty, lookup_public_fields};
import middle::ty::{ty_vid, region_vid, vid};
import middle::typeck::infer::{ty_and_region_var_methods};
import middle::typeck::infer::methods;
import util::ppaux::{ty_to_str, tys_to_str, region_to_str,
bound_region_to_str, vstore_to_str};
import std::smallintmap;

View File

@ -445,9 +445,6 @@ impl of region_scope for @fn_ctxt {
impl methods for @fn_ctxt {
fn tag() -> str { #fmt["%x", ptr::addr_of(*self) as uint] }
fn ty_to_str(t: ty::t) -> str {
ty_to_str(self.ccx.tcx, resolve_type_vars_if_possible(self, t))
}
fn block_region() -> result<ty::region, str> {
alt vec::last_opt(self.blocks) {
some(bid) { result::ok(ty::re_scope(bid)) }
@ -521,8 +518,8 @@ impl methods for @fn_ctxt {
self.ccx.tcx.sess.span_err(
sp,
#fmt["mismatched types: expected `%s` but found `%s` (%s)",
self.ty_to_str(e),
self.ty_to_str(a),
self.infcx.ty_to_str(e),
self.infcx.ty_to_str(a),
ty::type_err_to_str(self.ccx.tcx, err)]);
}
@ -589,13 +586,6 @@ fn do_autoderef(fcx: @fn_ctxt, sp: span, t: ty::t) -> ty::t {
};
}
fn resolve_type_vars_if_possible(fcx: @fn_ctxt, typ: ty::t) -> ty::t {
alt infer::resolve_deep(fcx.infcx, typ, false) {
result::ok(new_type) { ret new_type; }
result::err(_) { ret typ; }
}
}
// Returns true if the two types unify and false if they don't.
fn are_compatible(fcx: @fn_ctxt, expected: ty::t, actual: ty::t) -> bool {
alt fcx.mk_eqty(expected, actual) {
@ -723,7 +713,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
// type with fresh region variables.
#debug["check_call_or_bind: before universal quant., fty=%s",
fcx.ty_to_str(fty)];
fcx.infcx.ty_to_str(fty)];
// This is subtle: we expect `fty` to be a function type, which
// normally introduce a level of binding. In this case, we want to
@ -745,7 +735,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
};
#debug["check_call_or_bind: after universal quant., fty=%s",
fcx.ty_to_str(fty)];
fcx.infcx.ty_to_str(fty)];
let supplied_arg_count = vec::len(args);
@ -780,7 +770,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
fcx.ccx.tcx.sess.span_fatal(sp, "mismatched types: \
expected function or native \
function but found "
+ fcx.ty_to_str(fty));
+ fcx.infcx.ty_to_str(fty));
}
};
@ -1002,7 +992,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
tcx.sess.span_err(
ex.span, "binary operation " + ast_util::binop_to_str(op) +
" cannot be applied to type `" +
fcx.ty_to_str(lhs_resolved_t) +
fcx.infcx.ty_to_str(lhs_resolved_t) +
"`");
(lhs_resolved_t, false)
}
@ -1013,7 +1003,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
_ {
fcx.ccx.tcx.sess.span_err(
ex.span, #fmt["cannot apply unary operator `%s` to type `%s`",
op_str, fcx.ty_to_str(rhs_t)]);
op_str, fcx.infcx.ty_to_str(rhs_t)]);
rhs_t
}
}
@ -1060,7 +1050,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
expected_tys));
#debug("check_expr_fn_with_unifier %s fty=%s",
expr_to_str(expr), fcx.ty_to_str(fty));
expr_to_str(expr), fcx.infcx.ty_to_str(fty));
fcx.write_ty(expr.id, fty);
@ -1185,7 +1175,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
tcx.sess.span_fatal(
expr.span,
#fmt["type %s cannot be dereferenced",
fcx.ty_to_str(oper_t)]);
fcx.infcx.ty_to_str(oper_t)]);
}
}
}
@ -1428,8 +1418,8 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
let t_1 = fcx.to_ty(t);
let t_e = fcx.expr_ty(e);
#debug["t_1=%s", fcx.ty_to_str(t_1)];
#debug["t_e=%s", fcx.ty_to_str(t_e)];
#debug["t_1=%s", fcx.infcx.ty_to_str(t_1)];
#debug["t_e=%s", fcx.infcx.ty_to_str(t_e)];
alt ty::get(t_1).struct {
// This will be looked up later on
@ -1600,7 +1590,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
fcx.ccx.method_map.insert(id, origin);
}
none {
let t_err = resolve_type_vars_if_possible(fcx, expr_t);
let t_err = fcx.infcx.resolve_type_vars_if_possible(expr_t);
let msg = #fmt["attempted access of field %s on type %s, but \
no public field or method with that name was found",
field, ty_to_str(tcx, t_err)];
@ -1674,7 +1664,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
}
none {
let t_err = resolve_type_vars_if_possible(fcx, p_ty);
let t_err = fcx.infcx.resolve_type_vars_if_possible(p_ty);
let msg = #fmt["no `alloc()` method found for type `%s`",
ty_to_str(tcx, t_err)];
tcx.sess.span_err(expr.span, msg);
@ -1710,7 +1700,7 @@ fn require_integral(fcx: @fn_ctxt, sp: span, t: ty::t) {
if !type_is_integral(fcx, sp, t) {
fcx.ccx.tcx.sess.span_err(sp, "mismatched types: expected \
integral type but found `"
+ fcx.ty_to_str(t) + "`");
+ fcx.infcx.ty_to_str(t) + "`");
}
}

View File

@ -1,4 +1,5 @@
import middle::typeck::infer::{ty_and_region_var_methods};
import middle::typeck::infer::methods; // next_ty_var,
// resolve_type_vars_if_possible
fn check_alt(fcx: @fn_ctxt,
expr: @ast::expr,
@ -113,7 +114,7 @@ fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
tcx.sess.span_fatal
(pat.span,
#fmt["mismatched types: expected enum but found `%s`",
fcx.ty_to_str(expected)]);
fcx.infcx.ty_to_str(expected)]);
}
}
}
@ -134,12 +135,11 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
ast::pat_range(begin, end) {
check_expr_with(fcx, begin, expected);
check_expr_with(fcx, end, expected);
let b_ty = resolve_type_vars_if_possible(fcx,
fcx.expr_ty(begin));
let b_ty =
fcx.infcx.resolve_type_vars_if_possible(fcx.expr_ty(begin));
if !require_same_types(
tcx, pat.span, b_ty,
resolve_type_vars_if_possible(
fcx, fcx.expr_ty(end)),
fcx.infcx.resolve_type_vars_if_possible(fcx.expr_ty(end)),
{|| "mismatched types in range" }) {
// no-op
} else if !ty::type_is_numeric(b_ty) {
@ -179,7 +179,7 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
tcx.sess.span_fatal
(pat.span,
#fmt["mismatched types: expected `%s` but found record",
fcx.ty_to_str(expected)]);
fcx.infcx.ty_to_str(expected)]);
}
};
let f_count = vec::len(fields);
@ -216,7 +216,7 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
tcx.sess.span_fatal
(pat.span,
#fmt["mismatched types: expected `%s`, found tuple",
fcx.ty_to_str(expected)]);
fcx.infcx.ty_to_str(expected)]);
}
};
let e_count = vec::len(elts);
@ -244,7 +244,7 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
tcx.sess.span_fatal(
pat.span,
"mismatched types: expected `" +
fcx.ty_to_str(expected) +
fcx.infcx.ty_to_str(expected) +
"` found box");
}
}
@ -259,7 +259,7 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
tcx.sess.span_fatal(
pat.span,
"mismatched types: expected `" +
fcx.ty_to_str(expected) +
fcx.infcx.ty_to_str(expected) +
"` found uniq");
}
}

View File

@ -2,7 +2,7 @@
import syntax::ast_map;
import regionmanip::universally_quantify_from_sty;
import middle::typeck::infer::{ty_and_region_var_methods};
import middle::typeck::infer::methods; // next_ty_vars
enum lookup = {
fcx: @fn_ctxt,

View File

@ -12,7 +12,7 @@ fn universally_quantify_from_sty(fcx: @fn_ctxt,
sty: ty::sty) -> ty::t {
#debug["universally_quantify_from_sty(bound_tys=%?)",
bound_tys.map {|x| fcx.ty_to_str(x) }];
bound_tys.map {|x| fcx.infcx.ty_to_str(x) }];
indent {||
let tcx = fcx.tcx();
let isr = collect_bound_regions_in_tys(tcx, @nil, bound_tys) { |br|
@ -25,7 +25,8 @@ fn universally_quantify_from_sty(fcx: @fn_ctxt,
let t_res = ty::fold_sty_to_ty(fcx.ccx.tcx, sty) { |t|
replace_bound_regions(tcx, span, isr, t)
};
#debug["Result of universal quant. is %s", fcx.ty_to_str(t_res)];
#debug["Result of universal quant. is %s",
fcx.infcx.ty_to_str(t_res)];
t_res
}
}

View File

@ -54,7 +54,7 @@ fn lookup_vtable(fcx: @fn_ctxt, isc: resolve::iscopes, sp: span,
-> vtable_origin {
#debug["lookup_vtable(ty=%s, iface_ty=%s)",
fcx.ty_to_str(ty), fcx.ty_to_str(iface_ty)];
fcx.infcx.ty_to_str(ty), fcx.infcx.ty_to_str(iface_ty)];
let _i = indenter();
let tcx = fcx.ccx.tcx;

View File

@ -123,7 +123,7 @@ fn visit_pat(p: @ast::pat, wbcx: wb_ctxt, v: wb_vt) {
resolve_type_vars_for_node(wbcx, p.span, p.id);
#debug["Type for pattern binding %s (id %d) resolved to %s",
pat_to_str(p), p.id,
wbcx.fcx.ty_to_str(
wbcx.fcx.infcx.ty_to_str(
ty::node_id_to_type(wbcx.fcx.ccx.tcx,
p.id))];
visit::visit_pat(p, wbcx, v);
@ -135,7 +135,7 @@ fn visit_local(l: @ast::local, wbcx: wb_ctxt, v: wb_vt) {
result::ok(lty) {
#debug["Type for local %s (id %d) resolved to %s",
pat_to_str(l.node.pat), l.node.id,
wbcx.fcx.ty_to_str(lty)];
wbcx.fcx.infcx.ty_to_str(lty)];
write_ty_to_tcx(wbcx.fcx.ccx.tcx, l.node.id, lty);
}
result::err(e) {

View File

@ -167,7 +167,7 @@ export mk_assignty;
export resolve_shallow;
export resolve_deep;
export resolve_deep_var;
export ty_and_region_var_methods;
export methods; // for infer_ctxt
export compare_tys;
export fixup_err, fixup_err_to_str;
@ -388,7 +388,7 @@ fn uok() -> ures {
ok(())
}
impl methods for infer_ctxt {
impl transaction_methods for infer_ctxt {
fn commit<T,E>(f: fn() -> result<T,E>) -> result<T,E> {
assert self.vb.bindings.len() == 0u;
@ -431,7 +431,7 @@ impl methods for infer_ctxt {
}
}
impl ty_and_region_var_methods for infer_ctxt {
impl methods for infer_ctxt {
fn next_ty_var_id() -> ty_vid {
let id = *self.ty_var_counter;
*self.ty_var_counter += 1u;