1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use rustc_data_structures::fnv::FnvHashMap;
15 use hair::cx::to_ref::ToRef;
16 use rustc::front::map;
17 use rustc::middle::def::Def;
18 use rustc::middle::const_eval;
19 use rustc::middle::region::CodeExtent;
20 use rustc::middle::pat_util;
21 use rustc::middle::ty::{self, VariantDef, Ty};
22 use rustc::mir::repr::*;
24 use rustc_front::util as hir_util;
25 use syntax::parse::token;
28 impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
29 type Output = Expr<'tcx>;
31 fn make_mirror<'a>(self, cx: &mut Cx<'a, 'tcx>) -> Expr<'tcx> {
32 debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
34 let expr_ty = cx.tcx.expr_ty(self); // note: no adjustments (yet)!
36 let kind = match self.node {
37 // Here comes the interesting stuff:
38 hir::ExprMethodCall(_, _, ref args) => {
39 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
40 let expr = method_callee(cx, self, ty::MethodCall::expr(self.id));
41 let args = args.iter()
51 hir::ExprCall(ref fun, ref args) => {
52 if cx.tcx.is_method_call(self.id) {
53 // The callee is something implementing Fn, FnMut, or FnOnce.
54 // Find the actual method implementation being called and
55 // build the appropriate UFCS call expression with the
56 // callee-object as self parameter.
57 let method = method_callee(cx, self, ty::MethodCall::expr(self.id));
58 let mut argrefs = vec![fun.to_ref()];
59 argrefs.extend(args.iter().map(|a| a.to_ref()));
67 let adt_data = if let hir::ExprPath(..) = fun.node {
68 // Tuple-like ADTs are represented as ExprCall. We convert them here.
69 expr_ty.ty_adt_def().and_then(|adt_def|{
70 match cx.tcx.def_map.borrow()[&fun.id].full_def() {
71 Def::Variant(_, variant_id) => {
72 Some((adt_def, adt_def.variant_index_with_id(variant_id)))
81 if let Some((adt_def, index)) = adt_data {
82 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(fun.id).substs);
83 let field_refs = args.iter().enumerate().map(|(idx, e)| FieldExprRef {
84 name: Field::new(idx),
96 ty: cx.tcx.node_id_to_type(fun.id),
104 hir::ExprAddrOf(mutbl, ref expr) => {
105 let region = match expr_ty.sty {
106 ty::TyRef(r, _) => r,
107 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
111 borrow_kind: to_borrow_kind(mutbl),
116 hir::ExprBlock(ref blk) => {
117 ExprKind::Block { body: &**blk }
120 hir::ExprAssign(ref lhs, ref rhs) => {
127 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
128 let op = bin_op(op.node);
136 hir::ExprLit(..) => ExprKind::Literal {
137 literal: cx.const_eval_literal(self)
140 hir::ExprBinary(op, ref lhs, ref rhs) => {
141 if cx.tcx.is_method_call(self.id) {
142 let pass_args = if hir_util::is_by_value_binop(op.node) {
147 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
148 pass_args, lhs.to_ref(), vec![rhs])
152 hir::BinOp_::BiAnd => {
153 ExprKind::LogicalOp {
159 hir::BinOp_::BiOr => {
160 ExprKind::LogicalOp {
167 let op = bin_op(op.node);
178 hir::ExprIndex(ref lhs, ref index) => {
179 if cx.tcx.is_method_call(self.id) {
180 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
181 PassArgs::ByValue, lhs.to_ref(), vec![index])
185 index: index.to_ref(),
190 hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
191 if cx.tcx.is_method_call(self.id) {
192 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
193 PassArgs::ByValue, arg.to_ref(), vec![])
195 ExprKind::Deref { arg: arg.to_ref() }
199 hir::ExprUnary(op, ref arg) => {
200 if cx.tcx.is_method_call(self.id) {
201 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
202 PassArgs::ByValue, arg.to_ref(), vec![])
206 hir::UnOp::UnNot => UnOp::Not,
207 hir::UnOp::UnNeg => UnOp::Neg,
208 hir::UnOp::UnDeref => {
209 cx.tcx.sess.span_bug(
211 "UnDeref should have been handled elsewhere");
221 hir::ExprStruct(_, ref fields, ref base) => {
223 ty::TyStruct(adt, substs) => {
224 let field_refs = field_refs(&adt.variants[0], fields);
233 ty::TyEnum(adt, substs) => {
234 match cx.tcx.def_map.borrow()[&self.id].full_def() {
235 Def::Variant(enum_id, variant_id) => {
236 debug_assert!(adt.did == enum_id);
237 let index = adt.variant_index_with_id(variant_id);
238 let field_refs = field_refs(&adt.variants[index], fields);
241 variant_index: index,
248 cx.tcx.sess.span_bug(
250 &format!("unexpected def: {:?}", def));
255 cx.tcx.sess.span_bug(
257 &format!("unexpected type for struct literal: {:?}", expr_ty));
262 hir::ExprClosure(..) => {
263 let closure_ty = cx.tcx.expr_ty(self);
264 let (def_id, substs) = match closure_ty.sty {
265 ty::TyClosure(def_id, ref substs) => (def_id, substs),
267 cx.tcx.sess.span_bug(self.span,
268 &format!("closure expr w/o closure type: {:?}",
272 let upvars = cx.tcx.with_freevars(self.id, |freevars| {
275 .map(|(i, fv)| capture_freevar(cx, self, fv, substs.upvar_tys[i]))
285 hir::ExprRange(ref start, ref end) => {
286 let range_ty = cx.tcx.expr_ty(self);
287 let (adt_def, substs) = match range_ty.sty {
288 ty::TyStruct(adt_def, substs) => (adt_def, substs),
290 cx.tcx.sess.span_bug(self.span, "unexpanded ast");
294 let field_expr_ref = |s: &'tcx P<hir::Expr>, name: &str| {
295 let name = token::intern(name);
296 let index = adt_def.variants[0].index_of_field_named(name).unwrap();
297 FieldExprRef { name: Field::new(index), expr: s.to_ref() }
300 let start_field = start.as_ref()
302 .map(|s| field_expr_ref(s, "start"));
304 let end_field = end.as_ref()
306 .map(|e| field_expr_ref(e, "end"));
312 fields: start_field.chain(end_field).collect(),
317 hir::ExprPath(..) => {
318 convert_path_expr(cx, self)
321 hir::ExprInlineAsm(ref asm) => {
322 ExprKind::InlineAsm { asm: asm }
325 // Now comes the rote stuff:
327 hir::ExprRepeat(ref v, ref c) => ExprKind::Repeat {
329 count: TypedConstVal {
330 ty: cx.tcx.expr_ty(c),
332 value: const_eval::eval_const_expr(cx.tcx, c)
335 hir::ExprRet(ref v) =>
336 ExprKind::Return { value: v.to_ref() },
337 hir::ExprBreak(label) =>
338 ExprKind::Break { label: label.map(|_| loop_label(cx, self)) },
339 hir::ExprAgain(label) =>
340 ExprKind::Continue { label: label.map(|_| loop_label(cx, self)) },
341 hir::ExprMatch(ref discr, ref arms, _) =>
342 ExprKind::Match { discriminant: discr.to_ref(),
343 arms: arms.iter().map(|a| convert_arm(cx, a)).collect() },
344 hir::ExprIf(ref cond, ref then, ref otherwise) =>
345 ExprKind::If { condition: cond.to_ref(),
346 then: block::to_expr_ref(cx, then),
347 otherwise: otherwise.to_ref() },
348 hir::ExprWhile(ref cond, ref body, _) =>
349 ExprKind::Loop { condition: Some(cond.to_ref()),
350 body: block::to_expr_ref(cx, body) },
351 hir::ExprLoop(ref body, _) =>
352 ExprKind::Loop { condition: None,
353 body: block::to_expr_ref(cx, body) },
354 hir::ExprField(ref source, name) => {
355 let index = match cx.tcx.expr_ty_adjusted(source).sty {
356 ty::TyStruct(adt_def, _) =>
357 adt_def.variants[0].index_of_field_named(name.node),
359 cx.tcx.sess.span_bug(
361 &format!("field of non-struct: {:?}", ty)),
363 let index = index.unwrap_or_else(|| {
364 cx.tcx.sess.span_bug(
366 &format!("no index found for field `{}`", name.node));
368 ExprKind::Field { lhs: source.to_ref(), name: Field::new(index) }
370 hir::ExprTupField(ref source, index) =>
371 ExprKind::Field { lhs: source.to_ref(),
372 name: Field::new(index.node as usize) },
373 hir::ExprCast(ref source, _) =>
374 ExprKind::Cast { source: source.to_ref() },
375 hir::ExprType(ref source, _) =>
376 return source.make_mirror(cx),
377 hir::ExprBox(ref value) =>
379 value: value.to_ref(),
380 value_extents: cx.tcx.region_maps.node_extent(value.id)
382 hir::ExprVec(ref fields) =>
383 ExprKind::Vec { fields: fields.to_ref() },
384 hir::ExprTup(ref fields) =>
385 ExprKind::Tuple { fields: fields.to_ref() },
388 let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
389 let expr_extent = cx.tcx.region_maps.node_extent(self.id);
391 let mut expr = Expr {
392 temp_lifetime: temp_lifetime,
398 // Now apply adjustments, if any.
399 match cx.tcx.tables.borrow().adjustments.get(&self.id) {
401 Some(&ty::adjustment::AdjustReifyFnPointer) => {
402 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
404 temp_lifetime: temp_lifetime,
407 kind: ExprKind::ReifyFnPointer { source: expr.to_ref() },
410 Some(&ty::adjustment::AdjustUnsafeFnPointer) => {
411 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
413 temp_lifetime: temp_lifetime,
416 kind: ExprKind::UnsafeFnPointer { source: expr.to_ref() },
419 Some(&ty::adjustment::AdjustDerefRef(ref adj)) => {
420 for i in 0..adj.autoderefs {
423 expr.ty.adjust_for_autoderef(
428 |mc| cx.tcx.tables.borrow().method_map.get(&mc).map(|m| m.ty));
429 let kind = if cx.tcx.is_overloaded_autoderef(self.id, i) {
430 overloaded_lvalue(cx, self, ty::MethodCall::autoderef(self.id, i),
431 PassArgs::ByValue, expr.to_ref(), vec![])
433 ExprKind::Deref { arg: expr.to_ref() }
436 temp_lifetime: temp_lifetime,
443 if let Some(autoref) = adj.autoref {
444 let adjusted_ty = expr.ty.adjust_for_autoref(cx.tcx, Some(autoref));
446 ty::adjustment::AutoPtr(r, m) => {
448 temp_lifetime: temp_lifetime,
451 kind: ExprKind::Borrow {
453 borrow_kind: to_borrow_kind(m),
458 ty::adjustment::AutoUnsafe(m) => {
459 // Convert this to a suitable `&foo` and
460 // then an unsafe coercion. Limit the region to be just this
462 let region = ty::ReScope(expr_extent);
463 let region = cx.tcx.mk_region(region);
465 temp_lifetime: temp_lifetime,
466 ty: cx.tcx.mk_ref(region, ty::TypeAndMut { ty: expr.ty, mutbl: m }),
468 kind: ExprKind::Borrow {
470 borrow_kind: to_borrow_kind(m),
475 temp_lifetime: temp_lifetime,
478 kind: ExprKind::Cast { source: expr.to_ref() },
484 if let Some(target) = adj.unsize {
486 temp_lifetime: temp_lifetime,
489 kind: ExprKind::Unsize { source: expr.to_ref() },
495 // Next, wrap this up in the expr's scope.
497 temp_lifetime: temp_lifetime,
500 kind: ExprKind::Scope {
502 value: expr.to_ref(),
506 // Finally, create a destruction scope, if any.
507 if let Some(extent) = cx.tcx.region_maps.opt_destruction_extent(self.id) {
509 temp_lifetime: temp_lifetime,
512 kind: ExprKind::Scope {
514 value: expr.to_ref(),
524 fn method_callee<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
526 method_call: ty::MethodCall)
528 let tables = cx.tcx.tables.borrow();
529 let callee = &tables.method_map[&method_call];
530 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
532 temp_lifetime: temp_lifetime,
535 kind: ExprKind::Literal {
536 literal: Literal::Item {
537 def_id: callee.def_id,
538 kind: ItemKind::Method,
539 substs: callee.substs,
545 fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
547 hir::MutMutable => BorrowKind::Mut,
548 hir::MutImmutable => BorrowKind::Shared,
552 fn convert_arm<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, arm: &'tcx hir::Arm) -> Arm<'tcx> {
554 let opt_map = if arm.pats.len() == 1 {
558 pat_util::pat_bindings(&cx.tcx.def_map, &arm.pats[0], |_, p_id, _, path| {
559 map.insert(path.node, p_id);
565 patterns: arm.pats.iter().map(|p| cx.refutable_pat(opt_map, p)).collect(),
566 guard: arm.guard.to_ref(),
567 body: arm.body.to_ref(),
571 fn convert_path_expr<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> ExprKind<'tcx> {
572 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(expr.id).substs);
573 // Otherwise there may be def_map borrow conflicts
574 let def = cx.tcx.def_map.borrow()[&expr.id].full_def();
575 let (def_id, kind) = match def {
576 // A regular function.
577 Def::Fn(def_id) => (def_id, ItemKind::Function),
578 Def::Method(def_id) => (def_id, ItemKind::Method),
579 Def::Struct(def_id) => match cx.tcx.node_id_to_type(expr.id).sty {
580 // A tuple-struct constructor. Should only be reached if not called in the same
582 ty::TyBareFn(..) => (def_id, ItemKind::Function),
583 // A unit struct which is used as a value. We return a completely different ExprKind
584 // here to account for this special case.
585 ty::TyStruct(adt_def, substs) => return ExprKind::Adt {
592 ref sty => panic!("unexpected sty: {:?}", sty)
594 Def::Variant(enum_id, variant_id) => match cx.tcx.node_id_to_type(expr.id).sty {
595 // A variant constructor. Should only be reached if not called in the same
597 ty::TyBareFn(..) => (variant_id, ItemKind::Function),
598 // A unit variant, similar special case to the struct case above.
599 ty::TyEnum(adt_def, substs) => {
600 debug_assert!(adt_def.did == enum_id);
601 let index = adt_def.variant_index_with_id(variant_id);
602 return ExprKind::Adt {
605 variant_index: index,
610 ref sty => panic!("unexpected sty: {:?}", sty)
613 Def::AssociatedConst(def_id) => {
614 if let Some(v) = cx.try_const_eval_literal(expr) {
615 return ExprKind::Literal { literal: v };
617 (def_id, ItemKind::Constant)
621 Def::Static(node_id, _) => return ExprKind::StaticRef {
625 def @ Def::Local(..) |
626 def @ Def::Upvar(..) => return convert_var(cx, expr, def),
629 cx.tcx.sess.span_bug(
631 &format!("def `{:?}` not yet implemented", def)),
634 literal: Literal::Item { def_id: def_id, kind: kind, substs: substs }
638 fn convert_var<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
639 expr: &'tcx hir::Expr,
642 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
645 Def::Local(_, node_id) => {
651 Def::Upvar(_, id_var, index, closure_expr_id) => {
652 debug!("convert_var(upvar({:?}, {:?}, {:?}))", id_var, index, closure_expr_id);
653 let var_ty = cx.tcx.node_id_to_type(id_var);
655 let body_id = match cx.tcx.map.find(closure_expr_id) {
656 Some(map::NodeExpr(expr)) => {
658 hir::ExprClosure(_, _, ref body) => body.id,
660 cx.tcx.sess.span_bug(expr.span, "closure expr is not a closure expr");
665 cx.tcx.sess.span_bug(expr.span, "ast-map has garbage for closure expr");
669 // FIXME free regions in closures are not right
670 let closure_ty = cx.tcx.node_id_to_type(closure_expr_id);
672 // FIXME we're just hard-coding the idea that the
673 // signature will be &self or &mut self and hence will
674 // have a bound region with number 0
675 let region = ty::Region::ReFree(ty::FreeRegion {
676 scope: cx.tcx.region_maps.node_extent(body_id),
677 bound_region: ty::BoundRegion::BrAnon(0),
679 let region = cx.tcx.mk_region(region);
681 let self_expr = match cx.tcx.closure_kind(cx.tcx.map.local_def_id(closure_expr_id)) {
682 ty::ClosureKind::FnClosureKind => {
684 cx.tcx.mk_ref(region,
685 ty::TypeAndMut { ty: closure_ty,
686 mutbl: hir::MutImmutable });
689 temp_lifetime: temp_lifetime,
691 kind: ExprKind::Deref {
694 temp_lifetime: temp_lifetime,
696 kind: ExprKind::SelfRef
701 ty::ClosureKind::FnMutClosureKind => {
703 cx.tcx.mk_ref(region,
704 ty::TypeAndMut { ty: closure_ty,
705 mutbl: hir::MutMutable });
708 temp_lifetime: temp_lifetime,
710 kind: ExprKind::Deref {
713 temp_lifetime: temp_lifetime,
715 kind: ExprKind::SelfRef
720 ty::ClosureKind::FnOnceClosureKind => {
723 temp_lifetime: temp_lifetime,
725 kind: ExprKind::SelfRef,
730 // at this point we have `self.n`, which loads up the upvar
731 let field_kind = ExprKind::Field {
732 lhs: self_expr.to_ref(),
733 name: Field::new(index),
736 // ...but the upvar might be an `&T` or `&mut T` capture, at which
737 // point we need an implicit deref
738 let upvar_id = ty::UpvarId {
740 closure_expr_id: closure_expr_id,
742 let upvar_capture = match cx.tcx.upvar_capture(upvar_id) {
745 cx.tcx.sess.span_bug(
747 &format!("no upvar_capture for {:?}", upvar_id));
750 match upvar_capture {
751 ty::UpvarCapture::ByValue => field_kind,
752 ty::UpvarCapture::ByRef(_) => {
755 temp_lifetime: temp_lifetime,
765 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
770 fn bin_op(op: hir::BinOp_) -> BinOp {
772 hir::BinOp_::BiAdd => BinOp::Add,
773 hir::BinOp_::BiSub => BinOp::Sub,
774 hir::BinOp_::BiMul => BinOp::Mul,
775 hir::BinOp_::BiDiv => BinOp::Div,
776 hir::BinOp_::BiRem => BinOp::Rem,
777 hir::BinOp_::BiBitXor => BinOp::BitXor,
778 hir::BinOp_::BiBitAnd => BinOp::BitAnd,
779 hir::BinOp_::BiBitOr => BinOp::BitOr,
780 hir::BinOp_::BiShl => BinOp::Shl,
781 hir::BinOp_::BiShr => BinOp::Shr,
782 hir::BinOp_::BiEq => BinOp::Eq,
783 hir::BinOp_::BiLt => BinOp::Lt,
784 hir::BinOp_::BiLe => BinOp::Le,
785 hir::BinOp_::BiNe => BinOp::Ne,
786 hir::BinOp_::BiGe => BinOp::Ge,
787 hir::BinOp_::BiGt => BinOp::Gt,
788 _ => panic!("no equivalent for ast binop {:?}", op),
797 fn overloaded_operator<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
798 expr: &'tcx hir::Expr,
799 method_call: ty::MethodCall,
801 receiver: ExprRef<'tcx>,
802 args: Vec<&'tcx P<hir::Expr>>)
804 // the receiver has all the adjustments that are needed, so we can
805 // just push a reference to it
806 let mut argrefs = vec![receiver];
808 // the arguments, unfortunately, do not, so if this is a ByRef
809 // operator, we have to gin up the autorefs (but by value is easy)
811 PassArgs::ByValue => {
812 argrefs.extend(args.iter().map(|arg| arg.to_ref()))
816 let scope = cx.tcx.region_maps.node_extent(expr.id);
817 let region = cx.tcx.mk_region(ty::ReScope(scope));
818 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
822 let arg_ty = cx.tcx.expr_ty_adjusted(arg);
824 cx.tcx.mk_ref(region,
825 ty::TypeAndMut { ty: arg_ty,
826 mutbl: hir::MutImmutable });
828 temp_lifetime: temp_lifetime,
831 kind: ExprKind::Borrow { region: *region,
832 borrow_kind: BorrowKind::Shared,
839 // now create the call itself
840 let fun = method_callee(cx, expr, method_call);
848 fn overloaded_lvalue<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
849 expr: &'tcx hir::Expr,
850 method_call: ty::MethodCall,
852 receiver: ExprRef<'tcx>,
853 args: Vec<&'tcx P<hir::Expr>>)
855 // For an overloaded *x or x[y] expression of type T, the method
856 // call returns an &T and we must add the deref so that the types
857 // line up (this is because `*x` and `x[y]` represent lvalues):
859 // to find the type &T of the content returned by the method;
860 let tables = cx.tcx.tables.borrow();
861 let callee = &tables.method_map[&method_call];
862 let ref_ty = callee.ty.fn_ret();
863 let ref_ty = cx.tcx.no_late_bound_regions(&ref_ty).unwrap().unwrap();
865 // (1) callees always have all late-bound regions fully instantiated,
866 // (2) overloaded methods don't return `!`
868 // construct the complete expression `foo()` for the overloaded call,
869 // which will yield the &T type
870 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
871 let ref_kind = overloaded_operator(cx, expr, method_call, pass_args, receiver, args);
872 let ref_expr = Expr {
873 temp_lifetime: temp_lifetime,
879 // construct and return a deref wrapper `*foo()`
880 ExprKind::Deref { arg: ref_expr.to_ref() }
883 fn capture_freevar<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
884 closure_expr: &'tcx hir::Expr,
885 freevar: &ty::Freevar,
886 freevar_ty: Ty<'tcx>)
888 let id_var = freevar.def.var_id();
889 let upvar_id = ty::UpvarId {
891 closure_expr_id: closure_expr.id,
893 let upvar_capture = cx.tcx.upvar_capture(upvar_id).unwrap();
894 let temp_lifetime = cx.tcx.region_maps.temporary_scope(closure_expr.id);
895 let var_ty = cx.tcx.node_id_to_type(id_var);
896 let captured_var = Expr {
897 temp_lifetime: temp_lifetime,
899 span: closure_expr.span,
900 kind: convert_var(cx, closure_expr, freevar.def),
902 match upvar_capture {
903 ty::UpvarCapture::ByValue => {
904 captured_var.to_ref()
906 ty::UpvarCapture::ByRef(upvar_borrow) => {
907 let borrow_kind = match upvar_borrow.kind {
908 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
909 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
910 ty::BorrowKind::MutBorrow => BorrowKind::Mut,
913 temp_lifetime: temp_lifetime,
915 span: closure_expr.span,
916 kind: ExprKind::Borrow { region: upvar_borrow.region,
917 borrow_kind: borrow_kind,
918 arg: captured_var.to_ref() }
924 fn loop_label<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> CodeExtent {
925 match cx.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
926 Some(Def::Label(loop_id)) => cx.tcx.region_maps.node_extent(loop_id),
928 cx.tcx.sess.span_bug(expr.span, &format!("loop scope resolved to {:?}", d));
933 /// Converts a list of named fields (i.e. for struct-like struct/enum ADTs) into FieldExprRef.
934 fn field_refs<'tcx>(variant: VariantDef<'tcx>,
935 fields: &'tcx [hir::Field])
936 -> Vec<FieldExprRef<'tcx>>
939 .map(|field| FieldExprRef {
940 name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
941 expr: field.expr.to_ref(),