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 scheme = tcx.lookup_item_type(def.def_id());
let predicates = tcx.lookup_predicates(def.def_id()); let predicates = tcx.lookup_predicates(def.def_id());
let pat_ty = self.instantiate_value_path(segments, scheme, &predicates, 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); 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 predicates = tcx.lookup_predicates(def.def_id());
let pat_ty = self.instantiate_value_path(segments, scheme, &predicates, 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); self.demand_eqtype(pat.span, expected, pat_ty);
// Type check subpatterns. // Type check subpatterns.

View File

@ -1551,13 +1551,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
debug!("write_ty({}, {:?}) in fcx {}", debug!("write_ty({}, {:?}) in fcx {}",
node_id, ty, self.tag()); node_id, ty, self.tag());
self.tables.borrow_mut().node_types.insert(node_id, ty); self.tables.borrow_mut().node_types.insert(node_id, ty);
}
#[inline] // Add adjustments to !-expressions
pub fn write_ty_expr(&self, node_id: ast::NodeId, ty: Ty<'tcx>) {
self.write_ty(node_id, ty);
if ty.is_never() { 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) { 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) { 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) { if self.type_var_diverges(resolved) {
debug!("default_type_parameters: defaulting `{:?}` to `!` because it diverges", debug!("default_type_parameters: defaulting `{:?}` to `!` because it diverges",
resolved); 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 { } else {
match self.type_is_unconstrained_numeric(resolved) { match self.type_is_unconstrained_numeric(resolved) {
UnconstrainedInt => { UnconstrainedInt => {
@ -2051,7 +2052,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
for ty in &unsolved_variables { for ty in &unsolved_variables {
let resolved = self.resolve_type_vars_if_possible(ty); let resolved = self.resolve_type_vars_if_possible(ty);
if self.type_var_diverges(resolved) { 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 { } else {
match self.type_is_unconstrained_numeric(resolved) { match self.type_is_unconstrained_numeric(resolved) {
UnconstrainedInt | UnconstrainedFloat => { UnconstrainedInt | UnconstrainedFloat => {
@ -2109,7 +2111,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let _ = self.commit_if_ok(|_: &infer::CombinedSnapshot| { let _ = self.commit_if_ok(|_: &infer::CombinedSnapshot| {
for ty in &unbound_tyvars { for ty in &unbound_tyvars {
if self.type_var_diverges(ty) { 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 { } else {
match self.type_is_unconstrained_numeric(ty) { match self.type_is_unconstrained_numeric(ty) {
UnconstrainedInt => { UnconstrainedInt => {
@ -2205,7 +2208,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// reporting for more then one conflict. // reporting for more then one conflict.
for ty in &unbound_tyvars { for ty in &unbound_tyvars {
if self.type_var_diverges(ty) { 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 { } else {
match self.type_is_unconstrained_numeric(ty) { match self.type_is_unconstrained_numeric(ty) {
UnconstrainedInt => { UnconstrainedInt => {
@ -2682,7 +2686,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
fn write_call(&self, fn write_call(&self,
call_expr: &hir::Expr, call_expr: &hir::Expr,
output: Ty<'tcx>) { output: Ty<'tcx>) {
self.write_ty_expr(call_expr.id, output); self.write_ty(call_expr.id, output);
} }
// AST fragment checking // 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. // the type of the block, because old trans still uses it.
let adj = self.tables.borrow().adjustments.get(&then.id).cloned(); let adj = self.tables.borrow().adjustments.get(&then.id).cloned();
if res.is_ok() && adj.is_some() { 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 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 // 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); let field_ty = self.field_ty(expr.span, field, substs);
if field.vis.is_accessible_from(self.body_id, &self.tcx().map) { if field.vis.is_accessible_from(self.body_id, &self.tcx().map) {
autoderef.finalize(lvalue_pref, Some(base)); 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); self.write_autoderef_adjustment(base.id, autoderefs);
return; return;
} }
@ -2989,7 +2993,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some((did, field_ty)) = private_candidate { if let Some((did, field_ty)) = private_candidate {
let struct_path = self.tcx().item_path_str(did); 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 msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
let mut err = self.tcx().sess.struct_span_err(expr.span, &msg); 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. // 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 { if let Some(field_ty) = field {
autoderef.finalize(lvalue_pref, Some(base)); 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); self.write_autoderef_adjustment(base.id, autoderefs);
return; return;
} }
@ -3095,7 +3099,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let struct_path = self.tcx().item_path_str(did); let struct_path = self.tcx().item_path_str(did);
let msg = format!("field `{}` of struct `{}` is private", idx.node, struct_path); let msg = format!("field `{}` of struct `{}` is private", idx.node, struct_path);
self.tcx().sess.span_err(expr.span, &msg); self.tcx().sess.span_err(expr.span, &msg);
self.write_ty_expr(expr.id, field_ty); self.write_ty(expr.id, field_ty);
return; return;
} }
@ -3348,7 +3352,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
hir::ExprLit(ref lit) => { hir::ExprLit(ref lit) => {
let typ = self.check_lit(&lit, expected); 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) => { hir::ExprBinary(op, ref lhs, ref rhs) => {
self.check_binop(expr, op, lhs, 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) => { hir::ExprAddrOf(mutbl, ref oprnd) => {
let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| { 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, let (scheme, predicates) = self.type_scheme_and_predicates_for_def(expr.span,
def); def);
self.instantiate_value_path(segments, scheme, &predicates, self.instantiate_value_path(segments, scheme, &predicates,
opt_ty, def, expr.span, id, true); opt_ty, def, expr.span, id);
} else { } else {
self.set_tainted_by_errors(); self.set_tainted_by_errors();
self.write_error(id); self.write_error(id);
@ -3560,7 +3564,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
} }
hir::ExprBlock(ref b) => { hir::ExprBlock(ref b) => {
self.check_block_with_expected(&b, expected); 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) => { hir::ExprCall(ref callee, ref args) => {
self.check_call(expr, &callee, &args[..], expected); self.check_call(expr, &callee, &args[..], expected);
@ -3596,7 +3600,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
} else { } else {
// Write a type for the whole expression, assuming everything is going // Write a type for the whole expression, assuming everything is going
// to work out Ok. // 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. // Defer other checks until we're done type checking.
let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut(); 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) => { hir::ExprType(ref e, ref t) => {
let typ = self.to_ty(&t); let typ = self.to_ty(&t);
self.check_expr_eq_type(&e, typ); self.check_expr_eq_type(&e, typ);
self.write_ty_expr(id, typ); self.write_ty(id, typ);
} }
hir::ExprVec(ref args) => { hir::ExprVec(ref args) => {
let uty = expected.to_option(self).and_then(|uty| { 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)) => { Some((index_ty, element_ty)) => {
let idx_expr_ty = self.expr_ty(idx); let idx_expr_ty = self.expr_ty(idx);
self.demand_eqtype(expr.span, index_ty, idx_expr_ty); self.demand_eqtype(expr.span, index_ty, idx_expr_ty);
self.write_ty_expr(id, element_ty); self.write_ty(id, element_ty);
} }
None => { None => {
self.check_expr_has_type(&idx, self.tcx.types.err); 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 { } else if any_diverges {
self.write_ty(blk.id, self.next_diverging_ty_var()); self.write_ty(blk.id, self.next_diverging_ty_var());
} else { } 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>>, opt_self_ty: Option<Ty<'tcx>>,
def: Def, def: Def,
span: Span, span: Span,
node_id: ast::NodeId, node_id: ast::NodeId)
node_is_expr: bool)
-> Ty<'tcx> { -> Ty<'tcx> {
debug!("instantiate_value_path(path={:?}, def={:?}, node_id={}, type_scheme={:?})", debug!("instantiate_value_path(path={:?}, def={:?}, node_id={}, type_scheme={:?})",
segments, segments,
@ -4356,11 +4359,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
debug!("instantiate_value_path: type of {:?} is {:?}", debug!("instantiate_value_path: type of {:?} is {:?}",
node_id, node_id,
ty_substituted); ty_substituted);
if node_is_expr { self.write_ty(node_id, ty_substituted);
self.write_ty_expr(node_id, ty_substituted);
} else {
self.write_ty(node_id, ty_substituted);
}
self.write_substs(node_id, ty::ItemSubsts { self.write_substs(node_id, ty::ItemSubsts {
substs: substs 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.enforce_builtin_binop_types(lhs_expr, lhs_ty, rhs_expr, rhs_ty, op);
self.write_nil(expr.id); self.write_nil(expr.id);
} else { } else {
self.write_ty_expr(expr.id, return_ty); self.write_ty(expr.id, return_ty);
} }
let tcx = self.tcx; let tcx = self.tcx;
@ -69,7 +69,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// && and || are a simple case. // && and || are a simple case.
self.demand_suptype(lhs_expr.span, tcx.mk_bool(), lhs_ty); self.demand_suptype(lhs_expr.span, tcx.mk_bool(), lhs_ty);
self.check_expr_coercable_to_type(rhs_expr, tcx.mk_bool()); 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 // 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.demand_suptype(expr.span, builtin_return_ty, return_ty);
} }
self.write_ty_expr(expr.id, return_ty); self.write_ty(expr.id, return_ty);
} }
} }
} }