1 //! A different sort of visitor for walking fn bodies. Unlike the
2 //! normal visitor, which just walks the entire body in one shot, the
3 //! `ExprUseVisitor` determines how expressions are being used.
5 // Export these here so that Clippy can use them.
6 pub use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, Projection};
8 use rustc_data_structures::fx::FxIndexMap;
10 use rustc_hir::def::Res;
11 use rustc_hir::def_id::LocalDefId;
12 use rustc_hir::PatKind;
13 use rustc_index::vec::Idx;
14 use rustc_infer::infer::InferCtxt;
15 use rustc_middle::hir::place::ProjectionKind;
16 use rustc_middle::mir::FakeReadCause;
17 use rustc_middle::ty::{self, adjustment, TyCtxt};
18 use rustc_target::abi::VariantIdx;
21 use crate::mem_categorization as mc;
23 ///////////////////////////////////////////////////////////////////////////
26 /// This trait defines the callbacks you can expect to receive when
27 /// employing the ExprUseVisitor.
28 pub trait Delegate<'tcx> {
29 // The value found at `place` is moved, depending
30 // on `mode`. Where `diag_expr_id` is the id used for diagnostics for `place`.
32 // Use of a `Copy` type in a ByValue context is considered a use
33 // by `ImmBorrow` and `borrow` is called instead. This is because
34 // a shared borrow is the "minimum access" that would be needed
38 // The parameter `diag_expr_id` indicates the HIR id that ought to be used for
39 // diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
40 // id will be the id of the expression `expr` but the place itself will have
41 // the id of the binding in the pattern `pat`.
42 fn consume(&mut self, place_with_id: &PlaceWithHirId<'tcx>, diag_expr_id: hir::HirId);
44 // The value found at `place` is being borrowed with kind `bk`.
45 // `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
48 place_with_id: &PlaceWithHirId<'tcx>,
49 diag_expr_id: hir::HirId,
53 // The path at `assignee_place` is being assigned to.
54 // `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
55 fn mutate(&mut self, assignee_place: &PlaceWithHirId<'tcx>, diag_expr_id: hir::HirId);
57 // The `place` should be a fake read because of specified `cause`.
58 fn fake_read(&mut self, place: Place<'tcx>, cause: FakeReadCause, diag_expr_id: hir::HirId);
61 #[derive(Copy, Clone, PartialEq, Debug)]
63 Copy, // reference to x where x has a type that copies
64 Move, // reference to x where x has a type that moves
67 #[derive(Copy, Clone, PartialEq, Debug)]
71 WriteAndRead, // x += y
74 ///////////////////////////////////////////////////////////////////////////
75 // The ExprUseVisitor type
77 // This is the code that actually walks the tree.
78 pub struct ExprUseVisitor<'a, 'tcx> {
79 mc: mc::MemCategorizationContext<'a, 'tcx>,
80 body_owner: LocalDefId,
81 delegate: &'a mut dyn Delegate<'tcx>,
84 // If the MC results in an error, it's because the type check
85 // failed (or will fail, when the error is uncovered and reported
86 // during writeback). In this case, we just ignore this part of the
89 // Note that this macro appears similar to try!(), but, unlike try!(),
90 // it does not propagate the error.
91 macro_rules! return_if_err {
96 debug!("mc reported err");
103 impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
104 /// Creates the ExprUseVisitor, configuring it with the various options provided:
106 /// - `delegate` -- who receives the callbacks
107 /// - `param_env` --- parameter environment for trait lookups (esp. pertaining to `Copy`)
108 /// - `typeck_results` --- typeck results for the code being analyzed
110 delegate: &'a mut (dyn Delegate<'tcx> + 'a),
111 infcx: &'a InferCtxt<'a, 'tcx>,
112 body_owner: LocalDefId,
113 param_env: ty::ParamEnv<'tcx>,
114 typeck_results: &'a ty::TypeckResults<'tcx>,
117 mc: mc::MemCategorizationContext::new(infcx, param_env, body_owner, typeck_results),
123 pub fn consume_body(&mut self, body: &hir::Body<'_>) {
124 debug!("consume_body(body={:?})", body);
126 for param in body.params {
127 let param_ty = return_if_err!(self.mc.pat_ty_adjusted(¶m.pat));
128 debug!("consume_body: param_ty = {:?}", param_ty);
130 let param_place = self.mc.cat_rvalue(param.hir_id, param.pat.span, param_ty);
132 self.walk_irrefutable_pat(¶m_place, ¶m.pat);
135 self.consume_expr(&body.value);
138 fn tcx(&self) -> TyCtxt<'tcx> {
142 fn delegate_consume(&mut self, place_with_id: &PlaceWithHirId<'tcx>, diag_expr_id: hir::HirId) {
143 delegate_consume(&self.mc, self.delegate, place_with_id, diag_expr_id)
146 fn consume_exprs(&mut self, exprs: &[hir::Expr<'_>]) {
148 self.consume_expr(&expr);
152 pub fn consume_expr(&mut self, expr: &hir::Expr<'_>) {
153 debug!("consume_expr(expr={:?})", expr);
155 let place_with_id = return_if_err!(self.mc.cat_expr(expr));
156 self.delegate_consume(&place_with_id, place_with_id.hir_id);
157 self.walk_expr(expr);
160 fn mutate_expr(&mut self, expr: &hir::Expr<'_>) {
161 let place_with_id = return_if_err!(self.mc.cat_expr(expr));
162 self.delegate.mutate(&place_with_id, place_with_id.hir_id);
163 self.walk_expr(expr);
166 fn borrow_expr(&mut self, expr: &hir::Expr<'_>, bk: ty::BorrowKind) {
167 debug!("borrow_expr(expr={:?}, bk={:?})", expr, bk);
169 let place_with_id = return_if_err!(self.mc.cat_expr(expr));
170 self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk);
175 fn select_from_expr(&mut self, expr: &hir::Expr<'_>) {
179 pub fn walk_expr(&mut self, expr: &hir::Expr<'_>) {
180 debug!("walk_expr(expr={:?})", expr);
182 self.walk_adjustment(expr);
185 hir::ExprKind::Path(_) => {}
187 hir::ExprKind::Type(ref subexpr, _) => self.walk_expr(subexpr),
189 hir::ExprKind::Unary(hir::UnOp::Deref, ref base) => {
191 self.select_from_expr(base);
194 hir::ExprKind::Field(ref base, _) => {
196 self.select_from_expr(base);
199 hir::ExprKind::Index(ref lhs, ref rhs) => {
201 self.select_from_expr(lhs);
202 self.consume_expr(rhs);
205 hir::ExprKind::Call(ref callee, ref args) => {
207 self.consume_expr(callee);
208 self.consume_exprs(args);
211 hir::ExprKind::MethodCall(.., ref args, _) => {
213 self.consume_exprs(args);
216 hir::ExprKind::Struct(_, ref fields, ref opt_with) => {
217 self.walk_struct_expr(fields, opt_with);
220 hir::ExprKind::Tup(ref exprs) => {
221 self.consume_exprs(exprs);
224 hir::ExprKind::If(ref cond_expr, ref then_expr, ref opt_else_expr) => {
225 self.consume_expr(&cond_expr);
226 self.consume_expr(&then_expr);
227 if let Some(ref else_expr) = *opt_else_expr {
228 self.consume_expr(&else_expr);
232 hir::ExprKind::Match(ref discr, arms, _) => {
233 let discr_place = return_if_err!(self.mc.cat_expr(&discr));
235 // Matching should not always be considered a use of the place, hence
236 // discr does not necessarily need to be borrowed.
237 // We only want to borrow discr if the pattern contain something other
239 let ExprUseVisitor { ref mc, body_owner: _, delegate: _ } = *self;
240 let mut needs_to_be_read = false;
241 for arm in arms.iter() {
242 return_if_err!(mc.cat_pattern(discr_place.clone(), &arm.pat, |place, pat| {
244 PatKind::Binding(.., opt_sub_pat) => {
245 // If the opt_sub_pat is None, than the binding does not count as
246 // a wildcard for the purpose of borrowing discr.
247 if opt_sub_pat.is_none() {
248 needs_to_be_read = true;
251 PatKind::TupleStruct(..)
253 | PatKind::Struct(..)
254 | PatKind::Tuple(..) => {
255 // If the PatKind is a TupleStruct, Path, Struct or Tuple then we want to check
256 // whether the Variant is a MultiVariant or a SingleVariant. We only want
257 // to borrow discr if it is a MultiVariant.
258 // If it is a SingleVariant and creates a binding we will handle that when
259 // this callback gets called again.
261 // Get the type of the Place after all projections have been applied
262 let place_ty = place.place.ty();
264 if let ty::Adt(def, _) = place_ty.kind() {
265 if def.variants.len() > 1 {
266 needs_to_be_read = true;
271 // If the PatKind is a Lit then we want
273 needs_to_be_read = true;
280 if needs_to_be_read {
281 self.borrow_expr(&discr, ty::ImmBorrow);
283 let closure_def_id = match discr_place.place.base {
284 PlaceBase::Upvar(upvar_id) => Some(upvar_id.closure_expr_id.to_def_id()),
288 self.delegate.fake_read(
289 discr_place.place.clone(),
290 FakeReadCause::ForMatchedPlace(closure_def_id),
294 // We always want to walk the discriminant. We want to make sure, for instance,
295 // that the discriminant has been initialized.
296 self.walk_expr(&discr);
299 // treatment of the discriminant is handled while walking the arms.
301 self.walk_arm(&discr_place, arm);
305 hir::ExprKind::Array(ref exprs) => {
306 self.consume_exprs(exprs);
309 hir::ExprKind::AddrOf(_, m, ref base) => {
311 // make sure that the thing we are pointing out stays valid
312 // for the lifetime `scope_r` of the resulting ptr:
313 let bk = ty::BorrowKind::from_mutbl(m);
314 self.borrow_expr(&base, bk);
317 hir::ExprKind::InlineAsm(ref asm) => {
318 for (op, _op_sp) in asm.operands {
320 hir::InlineAsmOperand::In { expr, .. }
321 | hir::InlineAsmOperand::Sym { expr, .. } => self.consume_expr(expr),
322 hir::InlineAsmOperand::Out { expr, .. } => {
323 if let Some(expr) = expr {
324 self.mutate_expr(expr);
327 hir::InlineAsmOperand::InOut { expr, .. } => {
328 self.mutate_expr(expr);
330 hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
331 self.consume_expr(in_expr);
332 if let Some(out_expr) = out_expr {
333 self.mutate_expr(out_expr);
336 hir::InlineAsmOperand::Const { .. } => {}
341 hir::ExprKind::LlvmInlineAsm(ref ia) => {
342 for (o, output) in iter::zip(&ia.inner.outputs, ia.outputs_exprs) {
344 self.consume_expr(output);
346 self.mutate_expr(output);
349 self.consume_exprs(&ia.inputs_exprs);
352 hir::ExprKind::Continue(..)
353 | hir::ExprKind::Lit(..)
354 | hir::ExprKind::ConstBlock(..)
355 | hir::ExprKind::Err => {}
357 hir::ExprKind::Loop(ref blk, ..) => {
358 self.walk_block(blk);
361 hir::ExprKind::Unary(_, ref lhs) => {
362 self.consume_expr(lhs);
365 hir::ExprKind::Binary(_, ref lhs, ref rhs) => {
366 self.consume_expr(lhs);
367 self.consume_expr(rhs);
370 hir::ExprKind::Block(ref blk, _) => {
371 self.walk_block(blk);
374 hir::ExprKind::Break(_, ref opt_expr) | hir::ExprKind::Ret(ref opt_expr) => {
375 if let Some(ref expr) = *opt_expr {
376 self.consume_expr(expr);
380 hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
381 self.mutate_expr(lhs);
382 self.consume_expr(rhs);
385 hir::ExprKind::Cast(ref base, _) => {
386 self.consume_expr(base);
389 hir::ExprKind::DropTemps(ref expr) => {
390 self.consume_expr(expr);
393 hir::ExprKind::AssignOp(_, ref lhs, ref rhs) => {
394 if self.mc.typeck_results.is_method_call(expr) {
395 self.consume_expr(lhs);
397 self.mutate_expr(lhs);
399 self.consume_expr(rhs);
402 hir::ExprKind::Repeat(ref base, _) => {
403 self.consume_expr(base);
406 hir::ExprKind::Closure(..) => {
407 self.walk_captures(expr);
410 hir::ExprKind::Box(ref base) => {
411 self.consume_expr(base);
414 hir::ExprKind::Yield(ref value, _) => {
415 self.consume_expr(value);
420 fn walk_stmt(&mut self, stmt: &hir::Stmt<'_>) {
422 hir::StmtKind::Local(ref local) => {
423 self.walk_local(&local);
426 hir::StmtKind::Item(_) => {
427 // We don't visit nested items in this visitor,
428 // only the fn body we were given.
431 hir::StmtKind::Expr(ref expr) | hir::StmtKind::Semi(ref expr) => {
432 self.consume_expr(&expr);
437 fn walk_local(&mut self, local: &hir::Local<'_>) {
438 if let Some(ref expr) = local.init {
439 // Variable declarations with
440 // initializers are considered
441 // "assigns", which is handled by
443 self.walk_expr(&expr);
444 let init_place = return_if_err!(self.mc.cat_expr(&expr));
445 self.walk_irrefutable_pat(&init_place, &local.pat);
449 /// Indicates that the value of `blk` will be consumed, meaning either copied or moved
450 /// depending on its type.
451 fn walk_block(&mut self, blk: &hir::Block<'_>) {
452 debug!("walk_block(blk.hir_id={})", blk.hir_id);
454 for stmt in blk.stmts {
455 self.walk_stmt(stmt);
458 if let Some(ref tail_expr) = blk.expr {
459 self.consume_expr(&tail_expr);
465 fields: &[hir::ExprField<'_>],
466 opt_with: &Option<&'hir hir::Expr<'_>>,
468 // Consume the expressions supplying values for each field.
469 for field in fields {
470 self.consume_expr(&field.expr);
473 let with_expr = match *opt_with {
480 let with_place = return_if_err!(self.mc.cat_expr(&with_expr));
482 // Select just those fields of the `with`
483 // expression that will actually be used
484 match with_place.place.ty().kind() {
485 ty::Adt(adt, substs) if adt.is_struct() => {
486 // Consume those fields of the with expression that are needed.
487 for (f_index, with_field) in adt.non_enum_variant().fields.iter().enumerate() {
488 let is_mentioned = fields.iter().any(|f| {
489 self.tcx().field_index(f.hir_id, self.mc.typeck_results) == f_index
492 let field_place = self.mc.cat_projection(
495 with_field.ty(self.tcx(), substs),
496 ProjectionKind::Field(f_index as u32, VariantIdx::new(0)),
498 self.delegate_consume(&field_place, field_place.hir_id);
503 // the base expression should always evaluate to a
504 // struct; however, when EUV is run during typeck, it
505 // may not. This will generate an error earlier in typeck,
506 // so we can just ignore it.
507 if !self.tcx().sess.has_errors() {
508 span_bug!(with_expr.span, "with expression doesn't evaluate to a struct");
513 // walk the with expression so that complex expressions
514 // are properly handled.
515 self.walk_expr(with_expr);
518 // Invoke the appropriate delegate calls for anything that gets
519 // consumed or borrowed as part of the automatic adjustment
521 fn walk_adjustment(&mut self, expr: &hir::Expr<'_>) {
522 let adjustments = self.mc.typeck_results.expr_adjustments(expr);
523 let mut place_with_id = return_if_err!(self.mc.cat_expr_unadjusted(expr));
524 for adjustment in adjustments {
525 debug!("walk_adjustment expr={:?} adj={:?}", expr, adjustment);
526 match adjustment.kind {
527 adjustment::Adjust::NeverToAny | adjustment::Adjust::Pointer(_) => {
528 // Creating a closure/fn-pointer or unsizing consumes
529 // the input and stores it into the resulting rvalue.
530 self.delegate_consume(&place_with_id, place_with_id.hir_id);
533 adjustment::Adjust::Deref(None) => {}
535 // Autoderefs for overloaded Deref calls in fact reference
536 // their receiver. That is, if we have `(*x)` where `x`
537 // is of type `Rc<T>`, then this in fact is equivalent to
538 // `x.deref()`. Since `deref()` is declared with `&self`,
539 // this is an autoref of `x`.
540 adjustment::Adjust::Deref(Some(ref deref)) => {
541 let bk = ty::BorrowKind::from_mutbl(deref.mutbl);
542 self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk);
545 adjustment::Adjust::Borrow(ref autoref) => {
546 self.walk_autoref(expr, &place_with_id, autoref);
550 return_if_err!(self.mc.cat_expr_adjusted(expr, place_with_id, &adjustment));
554 /// Walks the autoref `autoref` applied to the autoderef'd
555 /// `expr`. `base_place` is the mem-categorized form of `expr`
556 /// after all relevant autoderefs have occurred.
559 expr: &hir::Expr<'_>,
560 base_place: &PlaceWithHirId<'tcx>,
561 autoref: &adjustment::AutoBorrow<'tcx>,
564 "walk_autoref(expr.hir_id={} base_place={:?} autoref={:?})",
565 expr.hir_id, base_place, autoref
569 adjustment::AutoBorrow::Ref(_, m) => {
570 self.delegate.borrow(
573 ty::BorrowKind::from_mutbl(m.into()),
577 adjustment::AutoBorrow::RawPtr(m) => {
578 debug!("walk_autoref: expr.hir_id={} base_place={:?}", expr.hir_id, base_place);
580 self.delegate.borrow(base_place, base_place.hir_id, ty::BorrowKind::from_mutbl(m));
585 fn walk_arm(&mut self, discr_place: &PlaceWithHirId<'tcx>, arm: &hir::Arm<'_>) {
586 let closure_def_id = match discr_place.place.base {
587 PlaceBase::Upvar(upvar_id) => Some(upvar_id.closure_expr_id.to_def_id()),
591 self.delegate.fake_read(
592 discr_place.place.clone(),
593 FakeReadCause::ForMatchedPlace(closure_def_id),
596 self.walk_pat(discr_place, &arm.pat);
598 if let Some(hir::Guard::If(ref e)) = arm.guard {
602 self.consume_expr(&arm.body);
605 /// Walks a pat that occurs in isolation (i.e., top-level of fn argument or
606 /// let binding, and *not* a match arm or nested pat.)
607 fn walk_irrefutable_pat(&mut self, discr_place: &PlaceWithHirId<'tcx>, pat: &hir::Pat<'_>) {
608 let closure_def_id = match discr_place.place.base {
609 PlaceBase::Upvar(upvar_id) => Some(upvar_id.closure_expr_id.to_def_id()),
613 self.delegate.fake_read(
614 discr_place.place.clone(),
615 FakeReadCause::ForLet(closure_def_id),
618 self.walk_pat(discr_place, pat);
621 /// The core driver for walking a pattern
622 fn walk_pat(&mut self, discr_place: &PlaceWithHirId<'tcx>, pat: &hir::Pat<'_>) {
623 debug!("walk_pat(discr_place={:?}, pat={:?})", discr_place, pat);
625 let tcx = self.tcx();
626 let ExprUseVisitor { ref mc, body_owner: _, ref mut delegate } = *self;
627 return_if_err!(mc.cat_pattern(discr_place.clone(), pat, |place, pat| {
628 if let PatKind::Binding(_, canonical_id, ..) = pat.kind {
629 debug!("walk_pat: binding place={:?} pat={:?}", place, pat,);
631 mc.typeck_results.extract_binding_mode(tcx.sess, pat.hir_id, pat.span)
633 debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
635 // pat_ty: the type of the binding being produced.
636 let pat_ty = return_if_err!(mc.node_ty(pat.hir_id));
637 debug!("walk_pat: pat_ty={:?}", pat_ty);
639 // Each match binding is effectively an assignment to the
640 // binding being produced.
641 let def = Res::Local(canonical_id);
642 if let Ok(ref binding_place) = mc.cat_res(pat.hir_id, pat.span, pat_ty, def) {
643 delegate.mutate(binding_place, binding_place.hir_id);
646 // It is also a borrow or copy/move of the value being matched.
647 // In a cases of pattern like `let pat = upvar`, don't use the span
648 // of the pattern, as this just looks confusing, instead use the span
649 // of the discriminant.
651 ty::BindByReference(m) => {
652 let bk = ty::BorrowKind::from_mutbl(m);
653 delegate.borrow(place, discr_place.hir_id, bk);
655 ty::BindByValue(..) => {
656 debug!("walk_pat binding consuming pat");
657 delegate_consume(mc, *delegate, place, discr_place.hir_id);
665 /// Handle the case where the current body contains a closure.
667 /// When the current body being handled is a closure, then we must make sure that
668 /// - The parent closure only captures Places from the nested closure that are not local to it.
670 /// In the following example the closures `c` only captures `p.x`` even though `incr`
671 /// is a capture of the nested closure
673 /// ```rust,ignore(cannot-test-this-because-pseudo-code)
677 /// let nested = || p.x += incr;
681 /// - When reporting the Place back to the Delegate, ensure that the UpvarId uses the enclosing
682 /// closure as the DefId.
683 fn walk_captures(&mut self, closure_expr: &hir::Expr<'_>) {
684 fn upvar_is_local_variable(
685 upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
686 upvar_id: &hir::HirId,
687 body_owner_is_closure: bool,
689 upvars.map(|upvars| !upvars.contains_key(upvar_id)).unwrap_or(body_owner_is_closure)
692 debug!("walk_captures({:?})", closure_expr);
694 let closure_def_id = self.tcx().hir().local_def_id(closure_expr.hir_id).to_def_id();
695 let upvars = self.tcx().upvars_mentioned(self.body_owner);
697 // For purposes of this function, generator and closures are equivalent.
698 let body_owner_is_closure = matches!(
699 self.tcx().type_of(self.body_owner.to_def_id()).kind(),
700 ty::Closure(..) | ty::Generator(..)
703 // If we have a nested closure, we want to include the fake reads present in the nested closure.
704 if let Some(fake_reads) = self.mc.typeck_results.closure_fake_reads.get(&closure_def_id) {
705 for (fake_read, cause, hir_id) in fake_reads.iter() {
706 match fake_read.base {
707 PlaceBase::Upvar(upvar_id) => {
708 if upvar_is_local_variable(
710 &upvar_id.var_path.hir_id,
711 body_owner_is_closure,
713 // The nested closure might be fake reading the current (enclosing) closure's local variables.
714 // The only places we want to fake read before creating the parent closure are the ones that
715 // are not local to it/ defined by it.
717 // ```rust,ignore(cannot-test-this-because-pseudo-code)
719 // let c = || { // fake reads: v1
721 // let e = || { // fake reads: v1, v2
727 // This check is performed when visiting the body of the outermost closure (`c`) and ensures
728 // that we don't add a fake read of v2 in c.
734 "Do not know how to get HirId out of Rvalue and StaticItem {:?}",
739 self.delegate.fake_read(fake_read.clone(), *cause, *hir_id);
743 if let Some(min_captures) = self.mc.typeck_results.closure_min_captures.get(&closure_def_id)
745 for (var_hir_id, min_list) in min_captures.iter() {
746 if upvars.map_or(body_owner_is_closure, |upvars| !upvars.contains_key(var_hir_id)) {
747 // The nested closure might be capturing the current (enclosing) closure's local variables.
748 // We check if the root variable is ever mentioned within the enclosing closure, if not
749 // then for the current body (if it's a closure) these aren't captures, we will ignore them.
752 for captured_place in min_list {
753 let place = &captured_place.place;
754 let capture_info = captured_place.info;
756 let place_base = if body_owner_is_closure {
757 // Mark the place to be captured by the enclosing closure
758 PlaceBase::Upvar(ty::UpvarId::new(*var_hir_id, self.body_owner))
760 // If the body owner isn't a closure then the variable must
761 // be a local variable
762 PlaceBase::Local(*var_hir_id)
764 let place_with_id = PlaceWithHirId::new(
765 capture_info.path_expr_id.unwrap_or(
766 capture_info.capture_kind_expr_id.unwrap_or(closure_expr.hir_id),
770 place.projections.clone(),
773 match capture_info.capture_kind {
774 ty::UpvarCapture::ByValue(_) => {
775 self.delegate_consume(&place_with_id, place_with_id.hir_id);
777 ty::UpvarCapture::ByRef(upvar_borrow) => {
778 self.delegate.borrow(
780 place_with_id.hir_id,
791 fn copy_or_move<'a, 'tcx>(
792 mc: &mc::MemCategorizationContext<'a, 'tcx>,
793 place_with_id: &PlaceWithHirId<'tcx>,
795 if !mc.type_is_copy_modulo_regions(
796 place_with_id.place.ty(),
797 mc.tcx().hir().span(place_with_id.hir_id),
805 // - If a place is used in a `ByValue` context then move it if it's not a `Copy` type.
806 // - If the place that is a `Copy` type consider it a `ImmBorrow`.
807 fn delegate_consume<'a, 'tcx>(
808 mc: &mc::MemCategorizationContext<'a, 'tcx>,
809 delegate: &mut (dyn Delegate<'tcx> + 'a),
810 place_with_id: &PlaceWithHirId<'tcx>,
811 diag_expr_id: hir::HirId,
813 debug!("delegate_consume(place_with_id={:?})", place_with_id);
815 let mode = copy_or_move(&mc, place_with_id);
818 ConsumeMode::Move => delegate.consume(place_with_id, diag_expr_id),
819 ConsumeMode::Copy => {
820 delegate.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow)