Lookup node type in map rather than using write_ty_expr

This commit is contained in:
Andrew Cann 2016-08-03 00:43:19 +08:00
parent 3639341685
commit a6d6fff068
3 changed files with 37 additions and 38 deletions

View File

@ -473,7 +473,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
};
}
self.write_ty_expr(expr.id, result_ty);
self.write_ty(expr.id, result_ty);
}
}
@ -550,7 +550,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let scheme = tcx.lookup_item_type(def.def_id());
let predicates = tcx.lookup_predicates(def.def_id());
let pat_ty = self.instantiate_value_path(segments, scheme, &predicates,
opt_ty, def, pat.span, pat.id, false);
opt_ty, def, pat.span, pat.id);
self.demand_suptype(pat.span, expected, pat_ty);
}
@ -618,7 +618,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
};
let predicates = tcx.lookup_predicates(def.def_id());
let pat_ty = self.instantiate_value_path(segments, scheme, &predicates,
opt_ty, def, pat.span, pat.id, false);
opt_ty, def, pat.span, pat.id);
self.demand_eqtype(pat.span, expected, pat_ty);
// Type check subpatterns.

View File

@ -1551,13 +1551,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
debug!("write_ty({}, {:?}) in fcx {}",
node_id, ty, self.tag());
self.tables.borrow_mut().node_types.insert(node_id, ty);
}
#[inline]
pub fn write_ty_expr(&self, node_id: ast::NodeId, ty: Ty<'tcx>) {
self.write_ty(node_id, ty);
// Add adjustments to !-expressions
if ty.is_never() {
self.write_adjustment(node_id, adjustment::AdjustNeverToAny(self.next_diverging_ty_var()));
if let Some(hir::map::NodeExpr(_)) = self.tcx.map.find(node_id) {
let adj = adjustment::AdjustNeverToAny(self.next_diverging_ty_var());
self.write_adjustment(node_id, adj);
}
}
}
@ -1733,7 +1733,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
pub fn write_never(&self, node_id: ast::NodeId) {
self.write_ty_expr(node_id, self.tcx.types.never);
self.write_ty(node_id, self.tcx.types.never);
}
pub fn write_error(&self, node_id: ast::NodeId) {
@ -1977,7 +1977,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if self.type_var_diverges(resolved) {
debug!("default_type_parameters: defaulting `{:?}` to `!` because it diverges",
resolved);
self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.mk_diverging_default());
self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
self.tcx.mk_diverging_default());
} else {
match self.type_is_unconstrained_numeric(resolved) {
UnconstrainedInt => {
@ -2051,7 +2052,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
for ty in &unsolved_variables {
let resolved = self.resolve_type_vars_if_possible(ty);
if self.type_var_diverges(resolved) {
self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.mk_diverging_default());
self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
self.tcx.mk_diverging_default());
} else {
match self.type_is_unconstrained_numeric(resolved) {
UnconstrainedInt | UnconstrainedFloat => {
@ -2109,7 +2111,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let _ = self.commit_if_ok(|_: &infer::CombinedSnapshot| {
for ty in &unbound_tyvars {
if self.type_var_diverges(ty) {
self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.mk_diverging_default());
self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
self.tcx.mk_diverging_default());
} else {
match self.type_is_unconstrained_numeric(ty) {
UnconstrainedInt => {
@ -2205,7 +2208,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// reporting for more then one conflict.
for ty in &unbound_tyvars {
if self.type_var_diverges(ty) {
self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.mk_diverging_default());
self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
self.tcx.mk_diverging_default());
} else {
match self.type_is_unconstrained_numeric(ty) {
UnconstrainedInt => {
@ -2682,7 +2686,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
fn write_call(&self,
call_expr: &hir::Expr,
output: Ty<'tcx>) {
self.write_ty_expr(call_expr.id, output);
self.write_ty(call_expr.id, output);
}
// AST fragment checking
@ -2915,7 +2919,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// the type of the block, because old trans still uses it.
let adj = self.tables.borrow().adjustments.get(&then.id).cloned();
if res.is_ok() && adj.is_some() {
self.write_ty_expr(then_blk.id, self.adjust_expr_ty(then, adj.as_ref()));
self.write_ty(then_blk.id, self.adjust_expr_ty(then, adj.as_ref()));
}
res
@ -2956,7 +2960,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
self.write_ty_expr(id, if_ty);
self.write_ty(id, if_ty);
}
// Check field access expressions
@ -2977,7 +2981,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let field_ty = self.field_ty(expr.span, field, substs);
if field.vis.is_accessible_from(self.body_id, &self.tcx().map) {
autoderef.finalize(lvalue_pref, Some(base));
self.write_ty_expr(expr.id, field_ty);
self.write_ty(expr.id, field_ty);
self.write_autoderef_adjustment(base.id, autoderefs);
return;
}
@ -2989,7 +2993,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some((did, field_ty)) = private_candidate {
let struct_path = self.tcx().item_path_str(did);
self.write_ty_expr(expr.id, field_ty);
self.write_ty(expr.id, field_ty);
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
let mut err = self.tcx().sess.struct_span_err(expr.span, &msg);
// Also check if an accessible method exists, which is often what is meant.
@ -3084,7 +3088,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some(field_ty) = field {
autoderef.finalize(lvalue_pref, Some(base));
self.write_ty_expr(expr.id, field_ty);
self.write_ty(expr.id, field_ty);
self.write_autoderef_adjustment(base.id, autoderefs);
return;
}
@ -3095,7 +3099,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let struct_path = self.tcx().item_path_str(did);
let msg = format!("field `{}` of struct `{}` is private", idx.node, struct_path);
self.tcx().sess.span_err(expr.span, &msg);
self.write_ty_expr(expr.id, field_ty);
self.write_ty(expr.id, field_ty);
return;
}
@ -3348,7 +3352,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
hir::ExprLit(ref lit) => {
let typ = self.check_lit(&lit, expected);
self.write_ty_expr(id, typ);
self.write_ty(id, typ);
}
hir::ExprBinary(op, ref lhs, ref rhs) => {
self.check_binop(expr, op, lhs, rhs);
@ -3414,7 +3418,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}
}
self.write_ty_expr(id, oprnd_t);
self.write_ty(id, oprnd_t);
}
hir::ExprAddrOf(mutbl, ref oprnd) => {
let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
@ -3465,7 +3469,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let (scheme, predicates) = self.type_scheme_and_predicates_for_def(expr.span,
def);
self.instantiate_value_path(segments, scheme, &predicates,
opt_ty, def, expr.span, id, true);
opt_ty, def, expr.span, id);
} else {
self.set_tainted_by_errors();
self.write_error(id);
@ -3560,7 +3564,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
hir::ExprBlock(ref b) => {
self.check_block_with_expected(&b, expected);
self.write_ty_expr(id, self.node_ty(b.id));
self.write_ty(id, self.node_ty(b.id));
}
hir::ExprCall(ref callee, ref args) => {
self.check_call(expr, &callee, &args[..], expected);
@ -3596,7 +3600,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
} else {
// Write a type for the whole expression, assuming everything is going
// to work out Ok.
self.write_ty_expr(id, t_cast);
self.write_ty(id, t_cast);
// Defer other checks until we're done type checking.
let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
@ -3613,7 +3617,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
hir::ExprType(ref e, ref t) => {
let typ = self.to_ty(&t);
self.check_expr_eq_type(&e, typ);
self.write_ty_expr(id, typ);
self.write_ty(id, typ);
}
hir::ExprVec(ref args) => {
let uty = expected.to_option(self).and_then(|uty| {
@ -3747,7 +3751,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
Some((index_ty, element_ty)) => {
let idx_expr_ty = self.expr_ty(idx);
self.demand_eqtype(expr.span, index_ty, idx_expr_ty);
self.write_ty_expr(id, element_ty);
self.write_ty(id, element_ty);
}
None => {
self.check_expr_has_type(&idx, self.tcx.types.err);
@ -4042,7 +4046,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
} else if any_diverges {
self.write_ty(blk.id, self.next_diverging_ty_var());
} else {
self.write_ty_expr(blk.id, ety);
self.write_ty(blk.id, ety);
}
}
};
@ -4091,8 +4095,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
opt_self_ty: Option<Ty<'tcx>>,
def: Def,
span: Span,
node_id: ast::NodeId,
node_is_expr: bool)
node_id: ast::NodeId)
-> Ty<'tcx> {
debug!("instantiate_value_path(path={:?}, def={:?}, node_id={}, type_scheme={:?})",
segments,
@ -4356,11 +4359,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
debug!("instantiate_value_path: type of {:?} is {:?}",
node_id,
ty_substituted);
if node_is_expr {
self.write_ty_expr(node_id, ty_substituted);
} else {
self.write_ty(node_id, ty_substituted);
}
self.write_ty(node_id, ty_substituted);
self.write_substs(node_id, ty::ItemSubsts {
substs: substs
});

View File

@ -36,7 +36,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self.enforce_builtin_binop_types(lhs_expr, lhs_ty, rhs_expr, rhs_ty, op);
self.write_nil(expr.id);
} else {
self.write_ty_expr(expr.id, return_ty);
self.write_ty(expr.id, return_ty);
}
let tcx = self.tcx;
@ -69,7 +69,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// && and || are a simple case.
self.demand_suptype(lhs_expr.span, tcx.mk_bool(), lhs_ty);
self.check_expr_coercable_to_type(rhs_expr, tcx.mk_bool());
self.write_ty_expr(expr.id, tcx.mk_bool());
self.write_ty(expr.id, tcx.mk_bool());
}
_ => {
// Otherwise, we always treat operators as if they are
@ -101,7 +101,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self.demand_suptype(expr.span, builtin_return_ty, return_ty);
}
self.write_ty_expr(expr.id, return_ty);
self.write_ty(expr.id, return_ty);
}
}
}