Skip to content

Commit

Permalink
Factor write_ty out of more checking functions
Browse files Browse the repository at this point in the history
  • Loading branch information
canndrew committed Sep 5, 2016
1 parent 06ad8fe commit 91b39b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/librustc_typeck/check/_match.rs
Expand Up @@ -473,8 +473,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
}

self.write_ty(expr.id, result_ty)
result_ty
}
}

Expand Down Expand Up @@ -545,6 +545,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

// Type check the path.
let pat_ty = self.instantiate_value_path(segments, opt_ty, def, pat.span, pat.id);
self.write_ty(pat.id, pat_ty);
self.demand_suptype(pat.span, expected, pat_ty);
}

Expand Down Expand Up @@ -603,6 +604,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

// Type check the path.
let pat_ty = self.instantiate_value_path(segments, opt_ty, def, pat.span, pat.id);
self.write_ty(pat.id, pat_ty);

let pat_ty = if pat_ty.is_fn() {
// Replace constructor type with constructed type for tuple struct patterns.
Expand Down
27 changes: 11 additions & 16 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -2857,7 +2857,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
cond_expr: &'gcx hir::Expr,
then_blk: &'gcx hir::Block,
opt_else_expr: Option<&'gcx hir::Expr>,
id: ast::NodeId,
sp: Span,
expected: Expectation<'tcx>) -> Ty<'tcx> {
let cond_ty = self.check_expr_has_type(cond_expr, self.tcx.types.bool);
Expand Down Expand Up @@ -2909,7 +2908,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}))
};

let if_ty = match result {
match result {
Ok(ty) => {
if cond_ty.references_error() {
self.tcx.types.err
Expand All @@ -2921,9 +2920,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self.report_mismatched_types(origin, expected, found, e);
self.tcx.types.err
}
};

self.write_ty(id, if_ty)
}
}

// Check field access expressions
Expand Down Expand Up @@ -3447,15 +3444,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self.instantiate_value_path(segments, opt_ty, def, expr.span, id)
} else {
self.set_tainted_by_errors();
self.write_error(id)
tcx.types.err
};

// We always require that the type provided as the value for
// a type parameter outlives the moment of instantiation.
self.opt_node_ty_substs(expr.id, |item_substs| {
self.add_wf_bounds(&item_substs.substs, expr);
});
ty

self.write_ty(id, ty)
}
hir::ExprInlineAsm(_, ref outputs, ref inputs) => {
for output in outputs {
Expand Down Expand Up @@ -3512,8 +3510,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}
hir::ExprIf(ref cond, ref then_blk, ref opt_else_expr) => {
self.check_then_else(&cond, &then_blk, opt_else_expr.as_ref().map(|e| &**e),
id, expr.span, expected)
let if_ty = self.check_then_else(&cond, &then_blk, opt_else_expr.as_ref().map(|e| &**e),
expr.span, expected);
self.write_ty(id, if_ty)
}
hir::ExprWhile(ref cond, ref body, _) => {
let cond_ty = self.check_expr_has_type(&cond, tcx.types.bool);
Expand All @@ -3535,7 +3534,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}
hir::ExprMatch(ref discrim, ref arms, match_src) => {
self.check_match(expr, &discrim, arms, expected, match_src)
let result_ty = self.check_match(expr, &discrim, arms, expected, match_src);
self.write_ty(expr.id, result_ty)
}
hir::ExprClosure(capture, ref decl, ref body, _) => {
self.check_expr_closure(expr, capture, &decl, &body, expected)
Expand Down Expand Up @@ -3571,16 +3571,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if t_expr.references_error() || t_cast.references_error() {
self.write_error(id)
} else {
// Write a type for the whole expression, assuming everything is going
// to work out Ok.
let ty = 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();
match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
Ok(cast_check) => {
deferred_cast_checks.push(cast_check);
ty
self.write_ty(id, t_cast)
}
Err(ErrorReported) => {
self.write_error(id)
Expand Down Expand Up @@ -4328,7 +4324,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
debug!("instantiate_value_path: type of {:?} is {:?}",
node_id,
ty_substituted);
self.write_ty(node_id, ty_substituted);
self.write_substs(node_id, ty::ItemSubsts {
substs: substs
});
Expand Down

0 comments on commit 91b39b5

Please sign in to comment.