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.
58 // rewrite f(u, v) into FnOnce::call_once(f, (u, v))
60 let method = method_callee(cx, self, ty::MethodCall::expr(self.id));
62 let sig = match method.ty.sty {
63 ty::TyBareFn(_, fn_ty) => &fn_ty.sig,
64 _ => cx.tcx.sess.span_bug(self.span, "type of method is not an fn")
67 let sig = cx.tcx.no_late_bound_regions(sig).unwrap_or_else(|| {
68 cx.tcx.sess.span_bug(self.span, "method call has late-bound regions")
71 assert_eq!(sig.inputs.len(), 2);
73 let tupled_args = Expr {
75 temp_lifetime: cx.tcx.region_maps.temporary_scope(self.id),
77 kind: ExprKind::Tuple {
78 fields: args.iter().map(ToRef::to_ref).collect()
85 args: vec![fun.to_ref(), tupled_args.to_ref()]
88 let adt_data = if let hir::ExprPath(..) = fun.node {
89 // Tuple-like ADTs are represented as ExprCall. We convert them here.
90 expr_ty.ty_adt_def().and_then(|adt_def|{
91 match cx.tcx.def_map.borrow()[&fun.id].full_def() {
92 Def::Variant(_, variant_id) => {
93 Some((adt_def, adt_def.variant_index_with_id(variant_id)))
102 if let Some((adt_def, index)) = adt_data {
103 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(fun.id).substs);
104 let field_refs = args.iter().enumerate().map(|(idx, e)| FieldExprRef {
105 name: Field::new(idx),
111 variant_index: index,
117 ty: cx.tcx.node_id_to_type(fun.id),
125 hir::ExprAddrOf(mutbl, ref expr) => {
126 let region = match expr_ty.sty {
127 ty::TyRef(r, _) => r,
128 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
132 borrow_kind: to_borrow_kind(mutbl),
137 hir::ExprBlock(ref blk) => {
138 ExprKind::Block { body: &blk }
141 hir::ExprAssign(ref lhs, ref rhs) => {
148 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
149 let op = bin_op(op.node);
157 hir::ExprLit(..) => ExprKind::Literal {
158 literal: cx.const_eval_literal(self)
161 hir::ExprBinary(op, ref lhs, ref rhs) => {
162 if cx.tcx.is_method_call(self.id) {
163 let pass_args = if hir_util::is_by_value_binop(op.node) {
168 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
169 pass_args, lhs.to_ref(), vec![rhs])
173 hir::BinOp_::BiAnd => {
174 ExprKind::LogicalOp {
180 hir::BinOp_::BiOr => {
181 ExprKind::LogicalOp {
188 let op = bin_op(op.node);
199 hir::ExprIndex(ref lhs, ref index) => {
200 if cx.tcx.is_method_call(self.id) {
201 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
202 PassArgs::ByValue, lhs.to_ref(), vec![index])
206 index: index.to_ref(),
211 hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
212 if cx.tcx.is_method_call(self.id) {
213 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
214 PassArgs::ByValue, arg.to_ref(), vec![])
216 ExprKind::Deref { arg: arg.to_ref() }
220 hir::ExprUnary(op, ref arg) => {
221 if cx.tcx.is_method_call(self.id) {
222 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
223 PassArgs::ByValue, arg.to_ref(), vec![])
227 hir::UnOp::UnNot => UnOp::Not,
228 hir::UnOp::UnNeg => UnOp::Neg,
229 hir::UnOp::UnDeref => {
230 cx.tcx.sess.span_bug(
232 "UnDeref should have been handled elsewhere");
242 hir::ExprStruct(_, ref fields, ref base) => {
244 ty::TyStruct(adt, substs) => {
245 let field_refs = field_refs(&adt.variants[0], fields);
254 ty::TyEnum(adt, substs) => {
255 match cx.tcx.def_map.borrow()[&self.id].full_def() {
256 Def::Variant(enum_id, variant_id) => {
257 debug_assert!(adt.did == enum_id);
258 let index = adt.variant_index_with_id(variant_id);
259 let field_refs = field_refs(&adt.variants[index], fields);
262 variant_index: index,
269 cx.tcx.sess.span_bug(
271 &format!("unexpected def: {:?}", def));
276 cx.tcx.sess.span_bug(
278 &format!("unexpected type for struct literal: {:?}", expr_ty));
283 hir::ExprClosure(..) => {
284 let closure_ty = cx.tcx.expr_ty(self);
285 let (def_id, substs) = match closure_ty.sty {
286 ty::TyClosure(def_id, ref substs) => (def_id, substs),
288 cx.tcx.sess.span_bug(self.span,
289 &format!("closure expr w/o closure type: {:?}",
293 let upvars = cx.tcx.with_freevars(self.id, |freevars| {
296 .map(|(i, fv)| capture_freevar(cx, self, fv, substs.upvar_tys[i]))
306 hir::ExprRange(ref start, ref end) => {
307 let range_ty = cx.tcx.expr_ty(self);
308 let (adt_def, substs) = match range_ty.sty {
309 ty::TyStruct(adt_def, substs) => (adt_def, substs),
311 cx.tcx.sess.span_bug(self.span, "unexpanded ast");
315 let field_expr_ref = |s: &'tcx P<hir::Expr>, name: &str| {
316 let name = token::intern(name);
317 let index = adt_def.variants[0].index_of_field_named(name).unwrap();
318 FieldExprRef { name: Field::new(index), expr: s.to_ref() }
321 let start_field = start.as_ref()
323 .map(|s| field_expr_ref(s, "start"));
325 let end_field = end.as_ref()
327 .map(|e| field_expr_ref(e, "end"));
333 fields: start_field.chain(end_field).collect(),
338 hir::ExprPath(..) => {
339 convert_path_expr(cx, self)
342 hir::ExprInlineAsm(ref asm) => {
343 ExprKind::InlineAsm { asm: asm }
346 // Now comes the rote stuff:
348 hir::ExprRepeat(ref v, ref c) => ExprKind::Repeat {
350 count: TypedConstVal {
351 ty: cx.tcx.expr_ty(c),
353 value: const_eval::eval_const_expr(cx.tcx, c)
356 hir::ExprRet(ref v) =>
357 ExprKind::Return { value: v.to_ref() },
358 hir::ExprBreak(label) =>
359 ExprKind::Break { label: label.map(|_| loop_label(cx, self)) },
360 hir::ExprAgain(label) =>
361 ExprKind::Continue { label: label.map(|_| loop_label(cx, self)) },
362 hir::ExprMatch(ref discr, ref arms, _) =>
363 ExprKind::Match { discriminant: discr.to_ref(),
364 arms: arms.iter().map(|a| convert_arm(cx, a)).collect() },
365 hir::ExprIf(ref cond, ref then, ref otherwise) =>
366 ExprKind::If { condition: cond.to_ref(),
367 then: block::to_expr_ref(cx, then),
368 otherwise: otherwise.to_ref() },
369 hir::ExprWhile(ref cond, ref body, _) =>
370 ExprKind::Loop { condition: Some(cond.to_ref()),
371 body: block::to_expr_ref(cx, body) },
372 hir::ExprLoop(ref body, _) =>
373 ExprKind::Loop { condition: None,
374 body: block::to_expr_ref(cx, body) },
375 hir::ExprField(ref source, name) => {
376 let index = match cx.tcx.expr_ty_adjusted(source).sty {
377 ty::TyStruct(adt_def, _) =>
378 adt_def.variants[0].index_of_field_named(name.node),
380 cx.tcx.sess.span_bug(
382 &format!("field of non-struct: {:?}", ty)),
384 let index = index.unwrap_or_else(|| {
385 cx.tcx.sess.span_bug(
387 &format!("no index found for field `{}`", name.node));
389 ExprKind::Field { lhs: source.to_ref(), name: Field::new(index) }
391 hir::ExprTupField(ref source, index) =>
392 ExprKind::Field { lhs: source.to_ref(),
393 name: Field::new(index.node as usize) },
394 hir::ExprCast(ref source, _) =>
395 ExprKind::Cast { source: source.to_ref() },
396 hir::ExprType(ref source, _) =>
397 return source.make_mirror(cx),
398 hir::ExprBox(ref value) =>
400 value: value.to_ref(),
401 value_extents: cx.tcx.region_maps.node_extent(value.id)
403 hir::ExprVec(ref fields) =>
404 ExprKind::Vec { fields: fields.to_ref() },
405 hir::ExprTup(ref fields) =>
406 ExprKind::Tuple { fields: fields.to_ref() },
409 let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
410 let expr_extent = cx.tcx.region_maps.node_extent(self.id);
412 let mut expr = Expr {
413 temp_lifetime: temp_lifetime,
419 debug!("unadjusted-expr={:?} applying adjustments={:?}",
420 expr, cx.tcx.tables.borrow().adjustments.get(&self.id));
422 // Now apply adjustments, if any.
423 match cx.tcx.tables.borrow().adjustments.get(&self.id) {
425 Some(&ty::adjustment::AdjustReifyFnPointer) => {
426 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
428 temp_lifetime: temp_lifetime,
431 kind: ExprKind::ReifyFnPointer { source: expr.to_ref() },
434 Some(&ty::adjustment::AdjustUnsafeFnPointer) => {
435 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
437 temp_lifetime: temp_lifetime,
440 kind: ExprKind::UnsafeFnPointer { source: expr.to_ref() },
443 Some(&ty::adjustment::AdjustMutToConstPointer) => {
444 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
446 temp_lifetime: temp_lifetime,
449 kind: ExprKind::Cast { source: expr.to_ref() },
452 Some(&ty::adjustment::AdjustDerefRef(ref adj)) => {
453 for i in 0..adj.autoderefs {
456 expr.ty.adjust_for_autoderef(
461 |mc| cx.tcx.tables.borrow().method_map.get(&mc).map(|m| m.ty));
462 let kind = if cx.tcx.is_overloaded_autoderef(self.id, i) {
463 overloaded_lvalue(cx, self, ty::MethodCall::autoderef(self.id, i),
464 PassArgs::ByValue, expr.to_ref(), vec![])
466 ExprKind::Deref { arg: expr.to_ref() }
469 temp_lifetime: temp_lifetime,
476 if let Some(autoref) = adj.autoref {
477 let adjusted_ty = expr.ty.adjust_for_autoref(cx.tcx, Some(autoref));
479 ty::adjustment::AutoPtr(r, m) => {
481 temp_lifetime: temp_lifetime,
484 kind: ExprKind::Borrow {
486 borrow_kind: to_borrow_kind(m),
491 ty::adjustment::AutoUnsafe(m) => {
492 // Convert this to a suitable `&foo` and
493 // then an unsafe coercion. Limit the region to be just this
495 let region = ty::ReScope(expr_extent);
496 let region = cx.tcx.mk_region(region);
498 temp_lifetime: temp_lifetime,
499 ty: cx.tcx.mk_ref(region, ty::TypeAndMut { ty: expr.ty, mutbl: m }),
501 kind: ExprKind::Borrow {
503 borrow_kind: to_borrow_kind(m),
508 temp_lifetime: temp_lifetime,
511 kind: ExprKind::Cast { source: expr.to_ref() },
517 if let Some(target) = adj.unsize {
519 temp_lifetime: temp_lifetime,
522 kind: ExprKind::Unsize { source: expr.to_ref() },
528 // Next, wrap this up in the expr's scope.
530 temp_lifetime: temp_lifetime,
533 kind: ExprKind::Scope {
535 value: expr.to_ref(),
539 // Finally, create a destruction scope, if any.
540 if let Some(extent) = cx.tcx.region_maps.opt_destruction_extent(self.id) {
542 temp_lifetime: temp_lifetime,
545 kind: ExprKind::Scope {
547 value: expr.to_ref(),
557 fn method_callee<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
559 method_call: ty::MethodCall)
561 let tables = cx.tcx.tables.borrow();
562 let callee = &tables.method_map[&method_call];
563 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
565 temp_lifetime: temp_lifetime,
568 kind: ExprKind::Literal {
569 literal: Literal::Item {
570 def_id: callee.def_id,
571 kind: ItemKind::Method,
572 substs: callee.substs,
578 fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
580 hir::MutMutable => BorrowKind::Mut,
581 hir::MutImmutable => BorrowKind::Shared,
585 fn convert_arm<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, arm: &'tcx hir::Arm) -> Arm<'tcx> {
587 let opt_map = if arm.pats.len() == 1 {
591 pat_util::pat_bindings(&cx.tcx.def_map, &arm.pats[0], |_, p_id, _, path| {
592 map.insert(path.node, p_id);
598 patterns: arm.pats.iter().map(|p| cx.refutable_pat(opt_map, p)).collect(),
599 guard: arm.guard.to_ref(),
600 body: arm.body.to_ref(),
604 fn convert_path_expr<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> ExprKind<'tcx> {
605 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(expr.id).substs);
606 // Otherwise there may be def_map borrow conflicts
607 let def = cx.tcx.def_map.borrow()[&expr.id].full_def();
608 let (def_id, kind) = match def {
609 // A regular function.
610 Def::Fn(def_id) => (def_id, ItemKind::Function),
611 Def::Method(def_id) => (def_id, ItemKind::Method),
612 Def::Struct(def_id) => match cx.tcx.node_id_to_type(expr.id).sty {
613 // A tuple-struct constructor. Should only be reached if not called in the same
615 ty::TyBareFn(..) => (def_id, ItemKind::Function),
616 // A unit struct which is used as a value. We return a completely different ExprKind
617 // here to account for this special case.
618 ty::TyStruct(adt_def, substs) => return ExprKind::Adt {
625 ref sty => panic!("unexpected sty: {:?}", sty)
627 Def::Variant(enum_id, variant_id) => match cx.tcx.node_id_to_type(expr.id).sty {
628 // A variant constructor. Should only be reached if not called in the same
630 ty::TyBareFn(..) => (variant_id, ItemKind::Function),
631 // A unit variant, similar special case to the struct case above.
632 ty::TyEnum(adt_def, substs) => {
633 debug_assert!(adt_def.did == enum_id);
634 let index = adt_def.variant_index_with_id(variant_id);
635 return ExprKind::Adt {
638 variant_index: index,
643 ref sty => panic!("unexpected sty: {:?}", sty)
646 Def::AssociatedConst(def_id) => {
647 if let Some(v) = cx.try_const_eval_literal(expr) {
648 return ExprKind::Literal { literal: v };
650 (def_id, ItemKind::Constant)
654 Def::Static(node_id, _) => return ExprKind::StaticRef {
658 def @ Def::Local(..) |
659 def @ Def::Upvar(..) => return convert_var(cx, expr, def),
662 cx.tcx.sess.span_bug(
664 &format!("def `{:?}` not yet implemented", def)),
667 literal: Literal::Item { def_id: def_id, kind: kind, substs: substs }
671 fn convert_var<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
672 expr: &'tcx hir::Expr,
675 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
678 Def::Local(_, node_id) => {
684 Def::Upvar(_, id_var, index, closure_expr_id) => {
685 debug!("convert_var(upvar({:?}, {:?}, {:?}))", id_var, index, closure_expr_id);
686 let var_ty = cx.tcx.node_id_to_type(id_var);
688 let body_id = match cx.tcx.map.find(closure_expr_id) {
689 Some(map::NodeExpr(expr)) => {
691 hir::ExprClosure(_, _, ref body) => body.id,
693 cx.tcx.sess.span_bug(expr.span, "closure expr is not a closure expr");
698 cx.tcx.sess.span_bug(expr.span, "ast-map has garbage for closure expr");
702 // FIXME free regions in closures are not right
703 let closure_ty = cx.tcx.node_id_to_type(closure_expr_id);
705 // FIXME we're just hard-coding the idea that the
706 // signature will be &self or &mut self and hence will
707 // have a bound region with number 0
708 let region = ty::Region::ReFree(ty::FreeRegion {
709 scope: cx.tcx.region_maps.node_extent(body_id),
710 bound_region: ty::BoundRegion::BrAnon(0),
712 let region = cx.tcx.mk_region(region);
714 let self_expr = match cx.tcx.closure_kind(cx.tcx.map.local_def_id(closure_expr_id)) {
715 ty::ClosureKind::FnClosureKind => {
717 cx.tcx.mk_ref(region,
718 ty::TypeAndMut { ty: closure_ty,
719 mutbl: hir::MutImmutable });
722 temp_lifetime: temp_lifetime,
724 kind: ExprKind::Deref {
727 temp_lifetime: temp_lifetime,
729 kind: ExprKind::SelfRef
734 ty::ClosureKind::FnMutClosureKind => {
736 cx.tcx.mk_ref(region,
737 ty::TypeAndMut { ty: closure_ty,
738 mutbl: hir::MutMutable });
741 temp_lifetime: temp_lifetime,
743 kind: ExprKind::Deref {
746 temp_lifetime: temp_lifetime,
748 kind: ExprKind::SelfRef
753 ty::ClosureKind::FnOnceClosureKind => {
756 temp_lifetime: temp_lifetime,
758 kind: ExprKind::SelfRef,
763 // at this point we have `self.n`, which loads up the upvar
764 let field_kind = ExprKind::Field {
765 lhs: self_expr.to_ref(),
766 name: Field::new(index),
769 // ...but the upvar might be an `&T` or `&mut T` capture, at which
770 // point we need an implicit deref
771 let upvar_id = ty::UpvarId {
773 closure_expr_id: closure_expr_id,
775 let upvar_capture = match cx.tcx.upvar_capture(upvar_id) {
778 cx.tcx.sess.span_bug(
780 &format!("no upvar_capture for {:?}", upvar_id));
783 match upvar_capture {
784 ty::UpvarCapture::ByValue => field_kind,
785 ty::UpvarCapture::ByRef(_) => {
788 temp_lifetime: temp_lifetime,
798 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
803 fn bin_op(op: hir::BinOp_) -> BinOp {
805 hir::BinOp_::BiAdd => BinOp::Add,
806 hir::BinOp_::BiSub => BinOp::Sub,
807 hir::BinOp_::BiMul => BinOp::Mul,
808 hir::BinOp_::BiDiv => BinOp::Div,
809 hir::BinOp_::BiRem => BinOp::Rem,
810 hir::BinOp_::BiBitXor => BinOp::BitXor,
811 hir::BinOp_::BiBitAnd => BinOp::BitAnd,
812 hir::BinOp_::BiBitOr => BinOp::BitOr,
813 hir::BinOp_::BiShl => BinOp::Shl,
814 hir::BinOp_::BiShr => BinOp::Shr,
815 hir::BinOp_::BiEq => BinOp::Eq,
816 hir::BinOp_::BiLt => BinOp::Lt,
817 hir::BinOp_::BiLe => BinOp::Le,
818 hir::BinOp_::BiNe => BinOp::Ne,
819 hir::BinOp_::BiGe => BinOp::Ge,
820 hir::BinOp_::BiGt => BinOp::Gt,
821 _ => panic!("no equivalent for ast binop {:?}", op),
830 fn overloaded_operator<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
831 expr: &'tcx hir::Expr,
832 method_call: ty::MethodCall,
834 receiver: ExprRef<'tcx>,
835 args: Vec<&'tcx P<hir::Expr>>)
837 // the receiver has all the adjustments that are needed, so we can
838 // just push a reference to it
839 let mut argrefs = vec![receiver];
841 // the arguments, unfortunately, do not, so if this is a ByRef
842 // operator, we have to gin up the autorefs (but by value is easy)
844 PassArgs::ByValue => {
845 argrefs.extend(args.iter().map(|arg| arg.to_ref()))
849 let scope = cx.tcx.region_maps.node_extent(expr.id);
850 let region = cx.tcx.mk_region(ty::ReScope(scope));
851 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
855 let arg_ty = cx.tcx.expr_ty_adjusted(arg);
857 cx.tcx.mk_ref(region,
858 ty::TypeAndMut { ty: arg_ty,
859 mutbl: hir::MutImmutable });
861 temp_lifetime: temp_lifetime,
864 kind: ExprKind::Borrow { region: *region,
865 borrow_kind: BorrowKind::Shared,
872 // now create the call itself
873 let fun = method_callee(cx, expr, method_call);
881 fn overloaded_lvalue<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
882 expr: &'tcx hir::Expr,
883 method_call: ty::MethodCall,
885 receiver: ExprRef<'tcx>,
886 args: Vec<&'tcx P<hir::Expr>>)
888 // For an overloaded *x or x[y] expression of type T, the method
889 // call returns an &T and we must add the deref so that the types
890 // line up (this is because `*x` and `x[y]` represent lvalues):
892 // to find the type &T of the content returned by the method;
893 let tables = cx.tcx.tables.borrow();
894 let callee = &tables.method_map[&method_call];
895 let ref_ty = callee.ty.fn_ret();
896 let ref_ty = cx.tcx.no_late_bound_regions(&ref_ty).unwrap().unwrap();
898 // (1) callees always have all late-bound regions fully instantiated,
899 // (2) overloaded methods don't return `!`
901 // construct the complete expression `foo()` for the overloaded call,
902 // which will yield the &T type
903 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
904 let ref_kind = overloaded_operator(cx, expr, method_call, pass_args, receiver, args);
905 let ref_expr = Expr {
906 temp_lifetime: temp_lifetime,
912 // construct and return a deref wrapper `*foo()`
913 ExprKind::Deref { arg: ref_expr.to_ref() }
916 fn capture_freevar<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
917 closure_expr: &'tcx hir::Expr,
918 freevar: &ty::Freevar,
919 freevar_ty: Ty<'tcx>)
921 let id_var = freevar.def.var_id();
922 let upvar_id = ty::UpvarId {
924 closure_expr_id: closure_expr.id,
926 let upvar_capture = cx.tcx.upvar_capture(upvar_id).unwrap();
927 let temp_lifetime = cx.tcx.region_maps.temporary_scope(closure_expr.id);
928 let var_ty = cx.tcx.node_id_to_type(id_var);
929 let captured_var = Expr {
930 temp_lifetime: temp_lifetime,
932 span: closure_expr.span,
933 kind: convert_var(cx, closure_expr, freevar.def),
935 match upvar_capture {
936 ty::UpvarCapture::ByValue => {
937 captured_var.to_ref()
939 ty::UpvarCapture::ByRef(upvar_borrow) => {
940 let borrow_kind = match upvar_borrow.kind {
941 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
942 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
943 ty::BorrowKind::MutBorrow => BorrowKind::Mut,
946 temp_lifetime: temp_lifetime,
948 span: closure_expr.span,
949 kind: ExprKind::Borrow { region: upvar_borrow.region,
950 borrow_kind: borrow_kind,
951 arg: captured_var.to_ref() }
957 fn loop_label<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> CodeExtent {
958 match cx.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
959 Some(Def::Label(loop_id)) => cx.tcx.region_maps.node_extent(loop_id),
961 cx.tcx.sess.span_bug(expr.span, &format!("loop scope resolved to {:?}", d));
966 /// Converts a list of named fields (i.e. for struct-like struct/enum ADTs) into FieldExprRef.
967 fn field_refs<'tcx>(variant: VariantDef<'tcx>,
968 fields: &'tcx [hir::Field])
969 -> Vec<FieldExprRef<'tcx>>
972 .map(|field| FieldExprRef {
973 name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
974 expr: field.expr.to_ref(),