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::region::CodeExtent;
19 use rustc::middle::pat_util;
20 use rustc::middle::ty::{self, VariantDef, Ty};
21 use rustc::mir::repr::*;
23 use rustc_front::util as hir_util;
24 use syntax::parse::token;
27 impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
28 type Output = Expr<'tcx>;
30 fn make_mirror<'a>(self, cx: &mut Cx<'a, 'tcx>) -> Expr<'tcx> {
31 debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
33 let expr_ty = cx.tcx.expr_ty(self); // note: no adjustments (yet)!
35 let kind = match self.node {
36 // Here comes the interesting stuff:
37 hir::ExprMethodCall(_, _, ref args) => {
38 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
39 let expr = method_callee(cx, self, ty::MethodCall::expr(self.id));
40 let args = args.iter()
50 hir::ExprCall(ref fun, ref args) => {
51 if cx.tcx.is_method_call(self.id) {
52 // The callee is something implementing Fn, FnMut, or FnOnce.
53 // Find the actual method implementation being called and
54 // build the appropriate UFCS call expression with the
55 // callee-object as self parameter.
56 let method = method_callee(cx, self, ty::MethodCall::expr(self.id));
57 let mut argrefs = vec![fun.to_ref()];
58 argrefs.extend(args.iter().map(|a| a.to_ref()));
66 let adt_data = if let hir::ExprPath(..) = fun.node {
67 // Tuple-like ADTs are represented as ExprCall. We convert them here.
68 expr_ty.ty_adt_def().and_then(|adt_def|{
69 match cx.tcx.def_map.borrow()[&fun.id].full_def() {
70 Def::Variant(_, variant_id) => {
71 Some((adt_def, adt_def.variant_index_with_id(variant_id)))
80 if let Some((adt_def, index)) = adt_data {
81 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(fun.id).substs);
82 let field_refs = args.iter().enumerate().map(|(idx, e)| FieldExprRef {
83 name: Field::new(idx),
95 ty: cx.tcx.node_id_to_type(fun.id),
103 hir::ExprAddrOf(mutbl, ref expr) => {
104 let region = match expr_ty.sty {
105 ty::TyRef(r, _) => r,
106 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
110 borrow_kind: to_borrow_kind(mutbl),
115 hir::ExprBlock(ref blk) => {
116 ExprKind::Block { body: &**blk }
119 hir::ExprAssign(ref lhs, ref rhs) => {
126 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
127 let op = bin_op(op.node);
135 hir::ExprLit(..) => ExprKind::Literal {
136 literal: cx.const_eval_literal(self)
139 hir::ExprBinary(op, ref lhs, ref rhs) => {
140 if cx.tcx.is_method_call(self.id) {
141 let pass_args = if hir_util::is_by_value_binop(op.node) {
146 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
147 pass_args, lhs.to_ref(), vec![rhs])
151 hir::BinOp_::BiAnd => {
152 ExprKind::LogicalOp {
158 hir::BinOp_::BiOr => {
159 ExprKind::LogicalOp {
166 let op = bin_op(op.node);
177 hir::ExprIndex(ref lhs, ref index) => {
178 if cx.tcx.is_method_call(self.id) {
179 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
180 PassArgs::ByValue, lhs.to_ref(), vec![index])
184 index: index.to_ref(),
189 hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
190 if cx.tcx.is_method_call(self.id) {
191 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
192 PassArgs::ByValue, arg.to_ref(), vec![])
194 ExprKind::Deref { arg: arg.to_ref() }
198 hir::ExprUnary(op, ref arg) => {
199 if cx.tcx.is_method_call(self.id) {
200 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
201 PassArgs::ByValue, arg.to_ref(), vec![])
205 hir::UnOp::UnNot => UnOp::Not,
206 hir::UnOp::UnNeg => UnOp::Neg,
207 hir::UnOp::UnDeref => {
208 cx.tcx.sess.span_bug(
210 "UnDeref should have been handled elsewhere");
220 hir::ExprStruct(_, ref fields, ref base) => {
222 ty::TyStruct(adt, substs) => {
223 let field_refs = field_refs(&adt.variants[0], fields);
232 ty::TyEnum(adt, substs) => {
233 match cx.tcx.def_map.borrow()[&self.id].full_def() {
234 Def::Variant(enum_id, variant_id) => {
235 debug_assert!(adt.did == enum_id);
236 let index = adt.variant_index_with_id(variant_id);
237 let field_refs = field_refs(&adt.variants[index], fields);
240 variant_index: index,
247 cx.tcx.sess.span_bug(
249 &format!("unexpected def: {:?}", def));
254 cx.tcx.sess.span_bug(
256 &format!("unexpected type for struct literal: {:?}", expr_ty));
261 hir::ExprClosure(..) => {
262 let closure_ty = cx.tcx.expr_ty(self);
263 let (def_id, substs) = match closure_ty.sty {
264 ty::TyClosure(def_id, ref substs) => (def_id, substs),
266 cx.tcx.sess.span_bug(self.span,
267 &format!("closure expr w/o closure type: {:?}",
271 let upvars = cx.tcx.with_freevars(self.id, |freevars| {
274 .map(|(i, fv)| capture_freevar(cx, self, fv, substs.upvar_tys[i]))
284 hir::ExprRange(ref start, ref end) => {
285 let range_ty = cx.tcx.expr_ty(self);
286 let (adt_def, substs) = match range_ty.sty {
287 ty::TyStruct(adt_def, substs) => (adt_def, substs),
289 cx.tcx.sess.span_bug(self.span, "unexpanded ast");
293 let field_expr_ref = |s: &'tcx P<hir::Expr>, name: &str| {
294 let name = token::intern(name);
295 let index = adt_def.variants[0].index_of_field_named(name).unwrap();
296 FieldExprRef { name: Field::new(index), expr: s.to_ref() }
299 let start_field = start.as_ref()
301 .map(|s| field_expr_ref(s, "start"));
303 let end_field = end.as_ref()
305 .map(|e| field_expr_ref(e, "end"));
311 fields: start_field.chain(end_field).collect(),
316 hir::ExprPath(..) => {
317 convert_path_expr(cx, self)
320 hir::ExprInlineAsm(ref asm) => {
321 ExprKind::InlineAsm { asm: asm }
324 // Now comes the rote stuff:
326 hir::ExprRepeat(ref v, ref c) => ExprKind::Repeat {
329 ty: cx.tcx.expr_ty(c),
331 literal: cx.const_eval_literal(c)
334 hir::ExprRet(ref v) =>
335 ExprKind::Return { value: v.to_ref() },
336 hir::ExprBreak(label) =>
337 ExprKind::Break { label: label.map(|_| loop_label(cx, self)) },
338 hir::ExprAgain(label) =>
339 ExprKind::Continue { label: label.map(|_| loop_label(cx, self)) },
340 hir::ExprMatch(ref discr, ref arms, _) =>
341 ExprKind::Match { discriminant: discr.to_ref(),
342 arms: arms.iter().map(|a| convert_arm(cx, a)).collect() },
343 hir::ExprIf(ref cond, ref then, ref otherwise) =>
344 ExprKind::If { condition: cond.to_ref(),
345 then: block::to_expr_ref(cx, then),
346 otherwise: otherwise.to_ref() },
347 hir::ExprWhile(ref cond, ref body, _) =>
348 ExprKind::Loop { condition: Some(cond.to_ref()),
349 body: block::to_expr_ref(cx, body) },
350 hir::ExprLoop(ref body, _) =>
351 ExprKind::Loop { condition: None,
352 body: block::to_expr_ref(cx, body) },
353 hir::ExprField(ref source, name) => {
354 let index = match cx.tcx.expr_ty_adjusted(source).sty {
355 ty::TyStruct(adt_def, _) =>
356 adt_def.variants[0].index_of_field_named(name.node),
358 cx.tcx.sess.span_bug(
360 &format!("field of non-struct: {:?}", ty)),
362 let index = index.unwrap_or_else(|| {
363 cx.tcx.sess.span_bug(
365 &format!("no index found for field `{}`", name.node));
367 ExprKind::Field { lhs: source.to_ref(), name: Field::new(index) }
369 hir::ExprTupField(ref source, index) =>
370 ExprKind::Field { lhs: source.to_ref(),
371 name: Field::new(index.node as usize) },
372 hir::ExprCast(ref source, _) =>
373 ExprKind::Cast { source: source.to_ref() },
374 hir::ExprType(ref source, _) =>
375 return source.make_mirror(cx),
376 hir::ExprBox(ref value) =>
377 ExprKind::Box { value: value.to_ref() },
378 hir::ExprVec(ref fields) =>
379 ExprKind::Vec { fields: fields.to_ref() },
380 hir::ExprTup(ref fields) =>
381 ExprKind::Tuple { fields: fields.to_ref() },
384 let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
385 let expr_extent = cx.tcx.region_maps.node_extent(self.id);
387 let mut expr = Expr {
388 temp_lifetime: temp_lifetime,
394 // Now apply adjustments, if any.
395 match cx.tcx.tables.borrow().adjustments.get(&self.id) {
397 Some(&ty::adjustment::AdjustReifyFnPointer) => {
398 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
400 temp_lifetime: temp_lifetime,
403 kind: ExprKind::ReifyFnPointer { source: expr.to_ref() },
406 Some(&ty::adjustment::AdjustUnsafeFnPointer) => {
407 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
409 temp_lifetime: temp_lifetime,
412 kind: ExprKind::UnsafeFnPointer { source: expr.to_ref() },
415 Some(&ty::adjustment::AdjustDerefRef(ref adj)) => {
416 for i in 0..adj.autoderefs {
419 expr.ty.adjust_for_autoderef(
424 |mc| cx.tcx.tables.borrow().method_map.get(&mc).map(|m| m.ty));
425 let kind = if cx.tcx.is_overloaded_autoderef(self.id, i) {
426 overloaded_lvalue(cx, self, ty::MethodCall::autoderef(self.id, i),
427 PassArgs::ByValue, expr.to_ref(), vec![])
429 ExprKind::Deref { arg: expr.to_ref() }
432 temp_lifetime: temp_lifetime,
439 if let Some(autoref) = adj.autoref {
440 let adjusted_ty = expr.ty.adjust_for_autoref(cx.tcx, Some(autoref));
442 ty::adjustment::AutoPtr(r, m) => {
444 temp_lifetime: temp_lifetime,
447 kind: ExprKind::Borrow {
449 borrow_kind: to_borrow_kind(m),
454 ty::adjustment::AutoUnsafe(m) => {
455 // Convert this to a suitable `&foo` and
456 // then an unsafe coercion. Limit the region to be just this
458 let region = ty::ReScope(expr_extent);
459 let region = cx.tcx.mk_region(region);
461 temp_lifetime: temp_lifetime,
462 ty: cx.tcx.mk_ref(region, ty::TypeAndMut { ty: expr.ty, mutbl: m }),
464 kind: ExprKind::Borrow {
466 borrow_kind: to_borrow_kind(m),
471 temp_lifetime: temp_lifetime,
474 kind: ExprKind::Cast { source: expr.to_ref() },
480 if let Some(target) = adj.unsize {
482 temp_lifetime: temp_lifetime,
485 kind: ExprKind::Unsize { source: expr.to_ref() },
491 // Next, wrap this up in the expr's scope.
493 temp_lifetime: temp_lifetime,
496 kind: ExprKind::Scope {
498 value: expr.to_ref(),
502 // Finally, create a destruction scope, if any.
503 if let Some(extent) = cx.tcx.region_maps.opt_destruction_extent(self.id) {
505 temp_lifetime: temp_lifetime,
508 kind: ExprKind::Scope {
510 value: expr.to_ref(),
520 fn method_callee<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
522 method_call: ty::MethodCall)
524 let tables = cx.tcx.tables.borrow();
525 let callee = &tables.method_map[&method_call];
526 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
528 temp_lifetime: temp_lifetime,
531 kind: ExprKind::Literal {
532 literal: Literal::Item {
533 def_id: callee.def_id,
534 kind: ItemKind::Method,
535 substs: callee.substs,
541 fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
543 hir::MutMutable => BorrowKind::Mut,
544 hir::MutImmutable => BorrowKind::Shared,
548 fn convert_arm<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, arm: &'tcx hir::Arm) -> Arm<'tcx> {
550 let opt_map = if arm.pats.len() == 1 {
554 pat_util::pat_bindings(&cx.tcx.def_map, &arm.pats[0], |_, p_id, _, path| {
555 map.insert(path.node, p_id);
561 patterns: arm.pats.iter().map(|p| cx.refutable_pat(opt_map, p)).collect(),
562 guard: arm.guard.to_ref(),
563 body: arm.body.to_ref(),
567 fn convert_path_expr<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> ExprKind<'tcx> {
568 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(expr.id).substs);
569 // Otherwise there may be def_map borrow conflicts
570 let def = cx.tcx.def_map.borrow()[&expr.id].full_def();
571 let (def_id, kind) = match def {
572 // A regular function.
573 Def::Fn(def_id) => (def_id, ItemKind::Function),
574 Def::Method(def_id) => (def_id, ItemKind::Method),
575 Def::Struct(def_id) => match cx.tcx.node_id_to_type(expr.id).sty {
576 // A tuple-struct constructor. Should only be reached if not called in the same
578 ty::TyBareFn(..) => (def_id, ItemKind::Function),
579 // A unit struct which is used as a value. We return a completely different ExprKind
580 // here to account for this special case.
581 ty::TyStruct(adt_def, substs) => return ExprKind::Adt {
588 ref sty => panic!("unexpected sty: {:?}", sty)
590 Def::Variant(enum_id, variant_id) => match cx.tcx.node_id_to_type(expr.id).sty {
591 // A variant constructor. Should only be reached if not called in the same
593 ty::TyBareFn(..) => (variant_id, ItemKind::Function),
594 // A unit variant, similar special case to the struct case above.
595 ty::TyEnum(adt_def, substs) => {
596 debug_assert!(adt_def.did == enum_id);
597 let index = adt_def.variant_index_with_id(variant_id);
598 return ExprKind::Adt {
601 variant_index: index,
606 ref sty => panic!("unexpected sty: {:?}", sty)
609 Def::AssociatedConst(def_id) => {
610 if let Some(v) = cx.try_const_eval_literal(expr) {
611 return ExprKind::Literal { literal: v };
613 (def_id, ItemKind::Constant)
617 Def::Static(node_id, _) => return ExprKind::StaticRef {
621 def @ Def::Local(..) |
622 def @ Def::Upvar(..) => return convert_var(cx, expr, def),
625 cx.tcx.sess.span_bug(
627 &format!("def `{:?}` not yet implemented", def)),
630 literal: Literal::Item { def_id: def_id, kind: kind, substs: substs }
634 fn convert_var<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
635 expr: &'tcx hir::Expr,
638 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
641 Def::Local(_, node_id) => {
647 Def::Upvar(_, id_var, index, closure_expr_id) => {
648 debug!("convert_var(upvar({:?}, {:?}, {:?}))", id_var, index, closure_expr_id);
649 let var_ty = cx.tcx.node_id_to_type(id_var);
651 let body_id = match cx.tcx.map.find(closure_expr_id) {
652 Some(map::NodeExpr(expr)) => {
654 hir::ExprClosure(_, _, ref body) => body.id,
656 cx.tcx.sess.span_bug(expr.span, "closure expr is not a closure expr");
661 cx.tcx.sess.span_bug(expr.span, "ast-map has garbage for closure expr");
665 // FIXME free regions in closures are not right
666 let closure_ty = cx.tcx.node_id_to_type(closure_expr_id);
668 // FIXME we're just hard-coding the idea that the
669 // signature will be &self or &mut self and hence will
670 // have a bound region with number 0
671 let region = ty::Region::ReFree(ty::FreeRegion {
672 scope: cx.tcx.region_maps.node_extent(body_id),
673 bound_region: ty::BoundRegion::BrAnon(0),
675 let region = cx.tcx.mk_region(region);
677 let self_expr = match cx.tcx.closure_kind(cx.tcx.map.local_def_id(closure_expr_id)) {
678 ty::ClosureKind::FnClosureKind => {
680 cx.tcx.mk_ref(region,
681 ty::TypeAndMut { ty: closure_ty,
682 mutbl: hir::MutImmutable });
685 temp_lifetime: temp_lifetime,
687 kind: ExprKind::Deref {
690 temp_lifetime: temp_lifetime,
692 kind: ExprKind::SelfRef
697 ty::ClosureKind::FnMutClosureKind => {
699 cx.tcx.mk_ref(region,
700 ty::TypeAndMut { ty: closure_ty,
701 mutbl: hir::MutMutable });
704 temp_lifetime: temp_lifetime,
706 kind: ExprKind::Deref {
709 temp_lifetime: temp_lifetime,
711 kind: ExprKind::SelfRef
716 ty::ClosureKind::FnOnceClosureKind => {
719 temp_lifetime: temp_lifetime,
721 kind: ExprKind::SelfRef,
726 // at this point we have `self.n`, which loads up the upvar
727 let field_kind = ExprKind::Field {
728 lhs: self_expr.to_ref(),
729 name: Field::new(index),
732 // ...but the upvar might be an `&T` or `&mut T` capture, at which
733 // point we need an implicit deref
734 let upvar_id = ty::UpvarId {
736 closure_expr_id: closure_expr_id,
738 let upvar_capture = match cx.tcx.upvar_capture(upvar_id) {
741 cx.tcx.sess.span_bug(
743 &format!("no upvar_capture for {:?}", upvar_id));
746 match upvar_capture {
747 ty::UpvarCapture::ByValue => field_kind,
748 ty::UpvarCapture::ByRef(_) => {
751 temp_lifetime: temp_lifetime,
761 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
766 fn bin_op(op: hir::BinOp_) -> BinOp {
768 hir::BinOp_::BiAdd => BinOp::Add,
769 hir::BinOp_::BiSub => BinOp::Sub,
770 hir::BinOp_::BiMul => BinOp::Mul,
771 hir::BinOp_::BiDiv => BinOp::Div,
772 hir::BinOp_::BiRem => BinOp::Rem,
773 hir::BinOp_::BiBitXor => BinOp::BitXor,
774 hir::BinOp_::BiBitAnd => BinOp::BitAnd,
775 hir::BinOp_::BiBitOr => BinOp::BitOr,
776 hir::BinOp_::BiShl => BinOp::Shl,
777 hir::BinOp_::BiShr => BinOp::Shr,
778 hir::BinOp_::BiEq => BinOp::Eq,
779 hir::BinOp_::BiLt => BinOp::Lt,
780 hir::BinOp_::BiLe => BinOp::Le,
781 hir::BinOp_::BiNe => BinOp::Ne,
782 hir::BinOp_::BiGe => BinOp::Ge,
783 hir::BinOp_::BiGt => BinOp::Gt,
784 _ => panic!("no equivalent for ast binop {:?}", op),
793 fn overloaded_operator<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
794 expr: &'tcx hir::Expr,
795 method_call: ty::MethodCall,
797 receiver: ExprRef<'tcx>,
798 args: Vec<&'tcx P<hir::Expr>>)
800 // the receiver has all the adjustments that are needed, so we can
801 // just push a reference to it
802 let mut argrefs = vec![receiver];
804 // the arguments, unfortunately, do not, so if this is a ByRef
805 // operator, we have to gin up the autorefs (but by value is easy)
807 PassArgs::ByValue => {
808 argrefs.extend(args.iter().map(|arg| arg.to_ref()))
812 let scope = cx.tcx.region_maps.node_extent(expr.id);
813 let region = cx.tcx.mk_region(ty::ReScope(scope));
814 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
818 let arg_ty = cx.tcx.expr_ty_adjusted(arg);
820 cx.tcx.mk_ref(region,
821 ty::TypeAndMut { ty: arg_ty,
822 mutbl: hir::MutImmutable });
824 temp_lifetime: temp_lifetime,
827 kind: ExprKind::Borrow { region: *region,
828 borrow_kind: BorrowKind::Shared,
835 // now create the call itself
836 let fun = method_callee(cx, expr, method_call);
844 fn overloaded_lvalue<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
845 expr: &'tcx hir::Expr,
846 method_call: ty::MethodCall,
848 receiver: ExprRef<'tcx>,
849 args: Vec<&'tcx P<hir::Expr>>)
851 // For an overloaded *x or x[y] expression of type T, the method
852 // call returns an &T and we must add the deref so that the types
853 // line up (this is because `*x` and `x[y]` represent lvalues):
855 // to find the type &T of the content returned by the method;
856 let tables = cx.tcx.tables.borrow();
857 let callee = &tables.method_map[&method_call];
858 let ref_ty = callee.ty.fn_ret();
859 let ref_ty = cx.tcx.no_late_bound_regions(&ref_ty).unwrap().unwrap();
861 // (1) callees always have all late-bound regions fully instantiated,
862 // (2) overloaded methods don't return `!`
864 // construct the complete expression `foo()` for the overloaded call,
865 // which will yield the &T type
866 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
867 let ref_kind = overloaded_operator(cx, expr, method_call, pass_args, receiver, args);
868 let ref_expr = Expr {
869 temp_lifetime: temp_lifetime,
875 // construct and return a deref wrapper `*foo()`
876 ExprKind::Deref { arg: ref_expr.to_ref() }
879 fn capture_freevar<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
880 closure_expr: &'tcx hir::Expr,
881 freevar: &ty::Freevar,
882 freevar_ty: Ty<'tcx>)
884 let id_var = freevar.def.var_id();
885 let upvar_id = ty::UpvarId {
887 closure_expr_id: closure_expr.id,
889 let upvar_capture = cx.tcx.upvar_capture(upvar_id).unwrap();
890 let temp_lifetime = cx.tcx.region_maps.temporary_scope(closure_expr.id);
891 let var_ty = cx.tcx.node_id_to_type(id_var);
892 let captured_var = Expr {
893 temp_lifetime: temp_lifetime,
895 span: closure_expr.span,
896 kind: convert_var(cx, closure_expr, freevar.def),
898 match upvar_capture {
899 ty::UpvarCapture::ByValue => {
900 captured_var.to_ref()
902 ty::UpvarCapture::ByRef(upvar_borrow) => {
903 let borrow_kind = match upvar_borrow.kind {
904 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
905 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
906 ty::BorrowKind::MutBorrow => BorrowKind::Mut,
909 temp_lifetime: temp_lifetime,
911 span: closure_expr.span,
912 kind: ExprKind::Borrow { region: upvar_borrow.region,
913 borrow_kind: borrow_kind,
914 arg: captured_var.to_ref() }
920 fn loop_label<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> CodeExtent {
921 match cx.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
922 Some(Def::Label(loop_id)) => cx.tcx.region_maps.node_extent(loop_id),
924 cx.tcx.sess.span_bug(expr.span, &format!("loop scope resolved to {:?}", d));
929 /// Converts a list of named fields (i.e. for struct-like struct/enum ADTs) into FieldExprRef.
930 fn field_refs<'tcx>(variant: VariantDef<'tcx>,
931 fields: &'tcx [hir::Field])
932 -> Vec<FieldExprRef<'tcx>>
935 .map(|field| FieldExprRef {
936 name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
937 expr: field.expr.to_ref(),