Skip to content

Commit 16e4fef

Browse files
committed
auto merge of #20158 : nikomatsakis/rust/fn-inference-refactor, r=eddyb
Various refactorings simplifying the mem-categorization and regionck interface. This is working towards an improvement for closure-and-upvar-mode inference. r? @eddyb
2 parents 18842f8 + 4856456 commit 16e4fef

File tree

8 files changed

+202
-271
lines changed

8 files changed

+202
-271
lines changed

src/librustc/middle/expr_use_visitor.rs

Lines changed: 49 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -302,22 +302,6 @@ pub struct ExprUseVisitor<'d,'t,'tcx,TYPER:'t> {
302302
param_env: ParameterEnvironment<'tcx>,
303303
}
304304

305-
// If the TYPER results in an error, it's because the type check
306-
// failed (or will fail, when the error is uncovered and reported
307-
// during writeback). In this case, we just ignore this part of the
308-
// code.
309-
//
310-
// Note that this macro appears similar to try!(), but, unlike try!(),
311-
// it does not propagate the error.
312-
macro_rules! return_if_err {
313-
($inp: expr) => (
314-
match $inp {
315-
Ok(v) => v,
316-
Err(()) => return
317-
}
318-
)
319-
}
320-
321305
/// Whether the elements of an overloaded operation are passed by value or by reference
322306
enum PassArgs {
323307
ByValue,
@@ -348,7 +332,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
348332
decl: &ast::FnDecl,
349333
body: &ast::Block) {
350334
for arg in decl.inputs.iter() {
351-
let arg_ty = return_if_err!(self.typer.node_ty(arg.pat.id));
335+
let arg_ty = self.typer.node_ty(arg.pat.id);
352336

353337
let fn_body_scope = region::CodeExtent::from_node_id(body.id);
354338
let arg_cmt = self.mc.cat_rvalue(
@@ -385,7 +369,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
385369
pub fn consume_expr(&mut self, expr: &ast::Expr) {
386370
debug!("consume_expr(expr={})", expr.repr(self.tcx()));
387371

388-
let cmt = return_if_err!(self.mc.cat_expr(expr));
372+
let cmt = self.mc.cat_expr(expr);
389373
self.delegate_consume(expr.id, expr.span, cmt);
390374
self.walk_expr(expr);
391375
}
@@ -394,7 +378,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
394378
assignment_expr: &ast::Expr,
395379
expr: &ast::Expr,
396380
mode: MutateMode) {
397-
let cmt = return_if_err!(self.mc.cat_expr(expr));
381+
let cmt = self.mc.cat_expr(expr);
398382
self.delegate.mutate(assignment_expr.id, assignment_expr.span, cmt, mode);
399383
self.walk_expr(expr);
400384
}
@@ -407,7 +391,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
407391
debug!("borrow_expr(expr={}, r={}, bk={})",
408392
expr.repr(self.tcx()), r.repr(self.tcx()), bk.repr(self.tcx()));
409393

410-
let cmt = return_if_err!(self.mc.cat_expr(expr));
394+
let cmt = self.mc.cat_expr(expr);
411395
self.delegate.borrow(expr.id, expr.span, cmt, r, bk, cause);
412396

413397
// Note: Unlike consume, we can ignore ExprParen. cat_expr
@@ -500,7 +484,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
500484
}
501485

502486
ast::ExprMatch(ref discr, ref arms, _) => {
503-
let discr_cmt = return_if_err!(self.mc.cat_expr(&**discr));
487+
let discr_cmt = self.mc.cat_expr(&**discr);
504488
self.borrow_expr(&**discr, ty::ReEmpty, ty::ImmBorrow, MatchDiscriminant);
505489

506490
// treatment of the discriminant is handled while walking the arms.
@@ -559,7 +543,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
559543

560544
// Fetch the type of the value that the iteration yields to
561545
// produce the pattern's categorized mutable type.
562-
let pattern_type = return_if_err!(self.typer.node_ty(pat.id));
546+
let pattern_type = self.typer.node_ty(pat.id);
563547
let blk_scope = region::CodeExtent::from_node_id(blk.id);
564548
let pat_cmt = self.mc.cat_rvalue(pat.id,
565549
pat.span,
@@ -647,7 +631,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
647631
}
648632

649633
fn walk_callee(&mut self, call: &ast::Expr, callee: &ast::Expr) {
650-
let callee_ty = ty::expr_ty_adjusted(self.tcx(), callee);
634+
let callee_ty = self.typer.expr_ty_adjusted(callee);
651635
debug!("walk_callee: callee={} callee_ty={}",
652636
callee.repr(self.tcx()), callee_ty.repr(self.tcx()));
653637
let call_scope = region::CodeExtent::from_node_id(call.id);
@@ -670,22 +654,19 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
670654
}
671655
_ => {
672656
let overloaded_call_type =
673-
match self.tcx()
674-
.method_map
675-
.borrow()
676-
.get(&MethodCall::expr(call.id)) {
677-
Some(ref method_callee) => {
678-
OverloadedCallType::from_method_origin(
679-
self.tcx(),
680-
&method_callee.origin)
681-
}
682-
None => {
683-
self.tcx().sess.span_bug(
684-
callee.span,
685-
format!("unexpected callee type {}",
686-
callee_ty.repr(self.tcx()))[])
687-
}
688-
};
657+
match self.typer.node_method_origin(MethodCall::expr(call.id)) {
658+
Some(method_origin) => {
659+
OverloadedCallType::from_method_origin(
660+
self.tcx(),
661+
&method_origin)
662+
}
663+
None => {
664+
self.tcx().sess.span_bug(
665+
callee.span,
666+
format!("unexpected callee type {}",
667+
callee_ty.repr(self.tcx())).as_slice())
668+
}
669+
};
689670
match overloaded_call_type {
690671
FnMutOverloadedCall => {
691672
self.borrow_expr(callee,
@@ -747,7 +728,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
747728
// "assigns", which is handled by
748729
// `walk_pat`:
749730
self.walk_expr(&**expr);
750-
let init_cmt = return_if_err!(self.mc.cat_expr(&**expr));
731+
let init_cmt = self.mc.cat_expr(&**expr);
751732
self.walk_irrefutable_pat(init_cmt, &*local.pat);
752733
}
753734
}
@@ -781,7 +762,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
781762
None => { return; }
782763
};
783764

784-
let with_cmt = return_if_err!(self.mc.cat_expr(&*with_expr));
765+
let with_cmt = self.mc.cat_expr(&*with_expr);
785766

786767
// Select just those fields of the `with`
787768
// expression that will actually be used
@@ -836,7 +817,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
836817
// rvalue.
837818
debug!("walk_adjustment(AutoAddEnv|AdjustReifyFnPointer)");
838819
let cmt_unadjusted =
839-
return_if_err!(self.mc.cat_expr_unadjusted(expr));
820+
self.mc.cat_expr_unadjusted(expr);
840821
self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
841822
}
842823
ty::AdjustDerefRef(ty::AutoDerefRef {
@@ -870,7 +851,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
870851
match self.typer.node_method_ty(deref_id) {
871852
None => {}
872853
Some(method_ty) => {
873-
let cmt = return_if_err!(self.mc.cat_expr_autoderefd(expr, i));
854+
let cmt = self.mc.cat_expr_autoderefd(expr, i);
874855
let self_ty = ty::ty_fn_args(method_ty)[0];
875856
let (m, r) = match self_ty.sty {
876857
ty::ty_rptr(r, ref m) => (m.mutbl, r),
@@ -900,15 +881,14 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
900881
assert!(n == 1, format!("Expected exactly 1 deref with Uniq \
901882
AutoRefs, found: {}", n));
902883
let cmt_unadjusted =
903-
return_if_err!(self.mc.cat_expr_unadjusted(expr));
884+
self.mc.cat_expr_unadjusted(expr);
904885
self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
905886
return;
906887
}
907888
_ => {}
908889
}
909890

910-
let cmt_derefd = return_if_err!(
911-
self.mc.cat_expr_autoderefd(expr, n));
891+
let cmt_derefd = self.mc.cat_expr_autoderefd(expr, n);
912892
debug!("walk_adjustment: cmt_derefd={}",
913893
cmt_derefd.repr(self.tcx()));
914894

@@ -1001,7 +981,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
1001981
mode: &mut TrackMatchMode<Span>) {
1002982
debug!("determine_pat_move_mode cmt_discr={} pat={}", cmt_discr.repr(self.tcx()),
1003983
pat.repr(self.tcx()));
1004-
return_if_err!(self.mc.cat_pattern(cmt_discr, pat, |_mc, cmt_pat, pat| {
984+
self.mc.cat_pattern(cmt_discr, pat, |_mc, cmt_pat, pat| {
1005985
let tcx = self.typer.tcx();
1006986
let def_map = &self.typer.tcx().def_map;
1007987
if pat_util::pat_is_binding(def_map, pat) {
@@ -1024,7 +1004,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
10241004
}
10251005
}
10261006
}
1027-
}));
1007+
});
10281008
}
10291009

10301010
/// The core driver for walking a pattern; `match_mode` must be
@@ -1039,11 +1019,11 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
10391019

10401020
let mc = &self.mc;
10411021
let typer = self.typer;
1042-
let tcx = typer.tcx();
10431022
let def_map = &self.typer.tcx().def_map;
10441023
let delegate = &mut self.delegate;
10451024
let param_env = &mut self.param_env;
1046-
return_if_err!(mc.cat_pattern(cmt_discr.clone(), pat, |mc, cmt_pat, pat| {
1025+
1026+
mc.cat_pattern(cmt_discr.clone(), pat, |mc, cmt_pat, pat| {
10471027
if pat_util::pat_is_binding(def_map, pat) {
10481028
let tcx = typer.tcx();
10491029

@@ -1053,17 +1033,13 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
10531033
match_mode);
10541034

10551035
// pat_ty: the type of the binding being produced.
1056-
let pat_ty = return_if_err!(typer.node_ty(pat.id));
1036+
let pat_ty = typer.node_ty(pat.id);
10571037

10581038
// Each match binding is effectively an assignment to the
10591039
// binding being produced.
10601040
let def = def_map.borrow()[pat.id].clone();
1061-
match mc.cat_def(pat.id, pat.span, pat_ty, def) {
1062-
Ok(binding_cmt) => {
1063-
delegate.mutate(pat.id, pat.span, binding_cmt, Init);
1064-
}
1065-
Err(_) => { }
1066-
}
1041+
let binding_cmt = mc.cat_def(pat.id, pat.span, pat_ty, def);
1042+
delegate.mutate(pat.id, pat.span, binding_cmt, Init);
10671043

10681044
// It is also a borrow or copy/move of the value being matched.
10691045
match pat.node {
@@ -1097,15 +1073,8 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
10971073
// borrow of the elements of the vector being
10981074
// matched.
10991075

1100-
let (slice_cmt, slice_mutbl, slice_r) = {
1101-
match mc.cat_slice_pattern(cmt_pat, &**slice_pat) {
1102-
Ok(v) => v,
1103-
Err(()) => {
1104-
tcx.sess.span_bug(slice_pat.span,
1105-
"Err from mc")
1106-
}
1107-
}
1108-
};
1076+
let (slice_cmt, slice_mutbl, slice_r) =
1077+
mc.cat_slice_pattern(cmt_pat, &**slice_pat);
11091078

11101079
// Note: We declare here that the borrow
11111080
// occurs upon entering the `[...]`
@@ -1135,13 +1104,13 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
11351104
_ => { }
11361105
}
11371106
}
1138-
}));
1107+
});
11391108

11401109
// Do a second pass over the pattern, calling `matched_pat` on
11411110
// the interior nodes (enum variants and structs), as opposed
11421111
// to the above loop's visit of than the bindings that form
11431112
// the leaves of the pattern tree structure.
1144-
return_if_err!(mc.cat_pattern(cmt_discr, pat, |mc, cmt_pat, pat| {
1113+
mc.cat_pattern(cmt_discr, pat, |mc, cmt_pat, pat| {
11451114
let def_map = def_map.borrow();
11461115
let tcx = typer.tcx();
11471116

@@ -1222,7 +1191,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
12221191
// cases either.
12231192
}
12241193
}
1225-
}));
1194+
});
12261195
}
12271196

12281197
fn walk_captures(&mut self, closure_expr: &ast::Expr) {
@@ -1246,15 +1215,15 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
12461215
freevars: &[ty::Freevar]) {
12471216
for freevar in freevars.iter() {
12481217
let id_var = freevar.def.def_id().node;
1249-
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.id,
1250-
closure_expr.span,
1251-
freevar.def));
1218+
let cmt_var = self.cat_captured_var(closure_expr.id,
1219+
closure_expr.span,
1220+
freevar.def);
12521221

12531222
// Lookup the kind of borrow the callee requires, as
12541223
// inferred by regionbk
12551224
let upvar_id = ty::UpvarId { var_id: id_var,
12561225
closure_expr_id: closure_expr.id };
1257-
let upvar_borrow = self.tcx().upvar_borrow_map.borrow()[upvar_id].clone();
1226+
let upvar_borrow = self.typer.upvar_borrow(upvar_id);
12581227

12591228
self.delegate.borrow(closure_expr.id,
12601229
closure_expr.span,
@@ -1269,13 +1238,11 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
12691238
closure_expr: &ast::Expr,
12701239
freevars: &[ty::Freevar]) {
12711240
for freevar in freevars.iter() {
1272-
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.id,
1273-
closure_expr.span,
1274-
freevar.def));
1275-
let mode = copy_or_move(self.tcx(),
1276-
cmt_var.ty,
1277-
&self.param_env,
1278-
CaptureMove);
1241+
let cmt_var = self.cat_captured_var(closure_expr.id,
1242+
closure_expr.span,
1243+
freevar.def);
1244+
let mode = copy_or_move(self.tcx(), cmt_var.ty,
1245+
&self.param_env, CaptureMove);
12791246
self.delegate.consume(closure_expr.id, freevar.span, cmt_var, mode);
12801247
}
12811248
}
@@ -1284,11 +1251,11 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
12841251
closure_id: ast::NodeId,
12851252
closure_span: Span,
12861253
upvar_def: def::Def)
1287-
-> mc::McResult<mc::cmt<'tcx>> {
1254+
-> mc::cmt<'tcx> {
12881255
// Create the cmt for the variable being borrowed, from the
12891256
// caller's perspective
12901257
let var_id = upvar_def.def_id().node;
1291-
let var_ty = try!(self.typer.node_ty(var_id));
1258+
let var_ty = self.typer.node_ty(var_id);
12921259
self.mc.cat_def(closure_id, closure_span, var_ty, upvar_def)
12931260
}
12941261
}

0 commit comments

Comments
 (0)