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;
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)!
34 let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
35 let expr_extent = cx.tcx.region_maps.node_extent(self.id);
37 let kind = match self.node {
38 // Here comes the interesting stuff:
39 hir::ExprMethodCall(_, _, ref args) => {
40 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
41 let expr = method_callee(cx, self, ty::MethodCall::expr(self.id));
42 let args = args.iter()
52 hir::ExprCall(ref fun, ref args) => {
53 if cx.tcx.is_method_call(self.id) {
54 // The callee is something implementing Fn, FnMut, or FnOnce.
55 // Find the actual method implementation being called and
56 // build the appropriate UFCS call expression with the
57 // callee-object as self parameter.
59 // rewrite f(u, v) into FnOnce::call_once(f, (u, v))
61 let method = method_callee(cx, self, ty::MethodCall::expr(self.id));
63 let sig = match method.ty.sty {
64 ty::TyBareFn(_, fn_ty) => &fn_ty.sig,
65 _ => cx.tcx.sess.span_bug(self.span, "type of method is not an fn")
68 let sig = cx.tcx.no_late_bound_regions(sig).unwrap_or_else(|| {
69 cx.tcx.sess.span_bug(self.span, "method call has late-bound regions")
72 assert_eq!(sig.inputs.len(), 2);
74 let tupled_args = Expr {
76 temp_lifetime: temp_lifetime,
78 kind: ExprKind::Tuple {
79 fields: args.iter().map(ToRef::to_ref).collect()
86 args: vec![fun.to_ref(), tupled_args.to_ref()]
89 let adt_data = if let hir::ExprPath(..) = fun.node {
90 // Tuple-like ADTs are represented as ExprCall. We convert them here.
91 expr_ty.ty_adt_def().and_then(|adt_def|{
92 match cx.tcx.def_map.borrow()[&fun.id].full_def() {
93 Def::Variant(_, variant_id) => {
94 Some((adt_def, adt_def.variant_index_with_id(variant_id)))
103 if let Some((adt_def, index)) = adt_data {
104 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(fun.id).substs);
105 let field_refs = args.iter().enumerate().map(|(idx, e)| FieldExprRef {
106 name: Field::new(idx),
112 variant_index: index,
118 ty: cx.tcx.node_id_to_type(fun.id),
126 hir::ExprAddrOf(mutbl, ref expr) => {
127 let region = match expr_ty.sty {
128 ty::TyRef(r, _) => r,
129 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
133 borrow_kind: to_borrow_kind(mutbl),
138 hir::ExprBlock(ref blk) => {
139 ExprKind::Block { body: &blk }
142 hir::ExprAssign(ref lhs, ref rhs) => {
149 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
150 if cx.tcx.is_method_call(self.id) {
151 let pass_args = if hir_util::is_by_value_binop(op.node) {
156 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
157 pass_args, lhs.to_ref(), vec![rhs])
167 hir::ExprLit(..) => ExprKind::Literal {
168 literal: cx.const_eval_literal(self)
171 hir::ExprBinary(op, ref lhs, ref rhs) => {
172 if cx.tcx.is_method_call(self.id) {
173 let pass_args = if hir_util::is_by_value_binop(op.node) {
178 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
179 pass_args, lhs.to_ref(), vec![rhs])
183 hir::BinOp_::BiAnd => {
184 ExprKind::LogicalOp {
190 hir::BinOp_::BiOr => {
191 ExprKind::LogicalOp {
198 let op = bin_op(op.node);
209 hir::ExprIndex(ref lhs, ref index) => {
210 if cx.tcx.is_method_call(self.id) {
211 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
212 PassArgs::ByValue, lhs.to_ref(), vec![index])
216 index: index.to_ref(),
221 hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
222 if cx.tcx.is_method_call(self.id) {
223 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
224 PassArgs::ByValue, arg.to_ref(), vec![])
226 ExprKind::Deref { arg: arg.to_ref() }
230 hir::ExprUnary(op, ref arg) => {
231 if cx.tcx.is_method_call(self.id) {
232 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
233 PassArgs::ByValue, arg.to_ref(), vec![])
237 hir::UnOp::UnNot => UnOp::Not,
238 hir::UnOp::UnNeg => UnOp::Neg,
239 hir::UnOp::UnDeref => {
240 cx.tcx.sess.span_bug(
242 "UnDeref should have been handled elsewhere");
252 hir::ExprStruct(_, ref fields, ref base) => {
254 ty::TyStruct(adt, substs) => {
255 let field_refs = field_refs(&adt.variants[0], fields);
261 base: base.as_ref().map(|base| {
264 field_types: cx.tcx.tables
266 .fru_field_types[&self.id]
272 ty::TyEnum(adt, substs) => {
273 match cx.tcx.def_map.borrow()[&self.id].full_def() {
274 Def::Variant(enum_id, variant_id) => {
275 debug_assert!(adt.did == enum_id);
276 assert!(base.is_none());
278 let index = adt.variant_index_with_id(variant_id);
279 let field_refs = field_refs(&adt.variants[index], fields);
282 variant_index: index,
289 cx.tcx.sess.span_bug(
291 &format!("unexpected def: {:?}", def));
296 cx.tcx.sess.span_bug(
298 &format!("unexpected type for struct literal: {:?}", expr_ty));
303 hir::ExprClosure(..) => {
304 let closure_ty = cx.tcx.expr_ty(self);
305 let (def_id, substs) = match closure_ty.sty {
306 ty::TyClosure(def_id, ref substs) => (def_id, substs),
308 cx.tcx.sess.span_bug(self.span,
309 &format!("closure expr w/o closure type: {:?}",
313 let upvars = cx.tcx.with_freevars(self.id, |freevars| {
316 .map(|(i, fv)| capture_freevar(cx, self, fv, substs.upvar_tys[i]))
326 hir::ExprPath(..) => {
327 convert_path_expr(cx, self)
330 hir::ExprInlineAsm(ref asm) => {
331 ExprKind::InlineAsm { asm: asm }
334 // Now comes the rote stuff:
336 hir::ExprRepeat(ref v, ref c) => ExprKind::Repeat {
338 count: TypedConstVal {
339 ty: cx.tcx.expr_ty(c),
341 value: const_eval::eval_const_expr(cx.tcx, c)
344 hir::ExprRet(ref v) =>
345 ExprKind::Return { value: v.to_ref() },
346 hir::ExprBreak(label) =>
347 ExprKind::Break { label: label.map(|_| loop_label(cx, self)) },
348 hir::ExprAgain(label) =>
349 ExprKind::Continue { label: label.map(|_| loop_label(cx, self)) },
350 hir::ExprMatch(ref discr, ref arms, _) =>
351 ExprKind::Match { discriminant: discr.to_ref(),
352 arms: arms.iter().map(|a| convert_arm(cx, a)).collect() },
353 hir::ExprIf(ref cond, ref then, ref otherwise) =>
354 ExprKind::If { condition: cond.to_ref(),
355 then: block::to_expr_ref(cx, then),
356 otherwise: otherwise.to_ref() },
357 hir::ExprWhile(ref cond, ref body, _) =>
358 ExprKind::Loop { condition: Some(cond.to_ref()),
359 body: block::to_expr_ref(cx, body) },
360 hir::ExprLoop(ref body, _) =>
361 ExprKind::Loop { condition: None,
362 body: block::to_expr_ref(cx, body) },
363 hir::ExprField(ref source, name) => {
364 let index = match cx.tcx.expr_ty_adjusted(source).sty {
365 ty::TyStruct(adt_def, _) =>
366 adt_def.variants[0].index_of_field_named(name.node),
368 cx.tcx.sess.span_bug(
370 &format!("field of non-struct: {:?}", ty)),
372 let index = index.unwrap_or_else(|| {
373 cx.tcx.sess.span_bug(
375 &format!("no index found for field `{}`", name.node));
377 ExprKind::Field { lhs: source.to_ref(), name: Field::new(index) }
379 hir::ExprTupField(ref source, index) =>
380 ExprKind::Field { lhs: source.to_ref(),
381 name: Field::new(index.node as usize) },
382 hir::ExprCast(ref source, _) =>
383 ExprKind::Cast { source: source.to_ref() },
384 hir::ExprType(ref source, _) =>
385 return source.make_mirror(cx),
386 hir::ExprBox(ref value) =>
388 value: value.to_ref(),
389 value_extents: cx.tcx.region_maps.node_extent(value.id)
391 hir::ExprVec(ref fields) =>
392 ExprKind::Vec { fields: fields.to_ref() },
393 hir::ExprTup(ref fields) =>
394 ExprKind::Tuple { fields: fields.to_ref() },
397 let mut expr = Expr {
398 temp_lifetime: temp_lifetime,
404 debug!("make_mirror: unadjusted-expr={:?} applying adjustments={:?}",
405 expr, cx.tcx.tables.borrow().adjustments.get(&self.id));
407 // Now apply adjustments, if any.
408 match cx.tcx.tables.borrow().adjustments.get(&self.id) {
410 Some(&ty::adjustment::AdjustReifyFnPointer) => {
411 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
413 temp_lifetime: temp_lifetime,
416 kind: ExprKind::ReifyFnPointer { source: expr.to_ref() },
419 Some(&ty::adjustment::AdjustUnsafeFnPointer) => {
420 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
422 temp_lifetime: temp_lifetime,
425 kind: ExprKind::UnsafeFnPointer { source: expr.to_ref() },
428 Some(&ty::adjustment::AdjustMutToConstPointer) => {
429 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
431 temp_lifetime: temp_lifetime,
434 kind: ExprKind::Cast { source: expr.to_ref() },
437 Some(&ty::adjustment::AdjustDerefRef(ref adj)) => {
438 for i in 0..adj.autoderefs {
441 expr.ty.adjust_for_autoderef(
446 |mc| cx.tcx.tables.borrow().method_map.get(&mc).map(|m| m.ty));
447 debug!("make_mirror: autoderef #{}, adjusted_ty={:?}", i, adjusted_ty);
448 let method_key = ty::MethodCall::autoderef(self.id, i);
450 cx.tcx.tables.borrow().method_map.get(&method_key).map(|m| m.ty);
451 let kind = if let Some(meth_ty) = meth_ty {
452 debug!("make_mirror: overloaded autoderef (meth_ty={:?})", meth_ty);
454 let ref_ty = cx.tcx.no_late_bound_regions(&meth_ty.fn_ret());
455 let (region, mutbl) = match ref_ty {
456 Some(ty::FnConverging(&ty::TyS {
457 sty: ty::TyRef(region, mt), ..
458 })) => (region, mt.mutbl),
459 _ => cx.tcx.sess.span_bug(
460 expr.span, "autoderef returned bad type")
464 temp_lifetime: temp_lifetime,
466 region, ty::TypeAndMut { ty: expr.ty, mutbl: mutbl }),
468 kind: ExprKind::Borrow {
470 borrow_kind: to_borrow_kind(mutbl),
475 overloaded_lvalue(cx, self, method_key,
476 PassArgs::ByRef, expr.to_ref(), vec![])
478 debug!("make_mirror: built-in autoderef");
479 ExprKind::Deref { arg: expr.to_ref() }
482 temp_lifetime: temp_lifetime,
489 if let Some(autoref) = adj.autoref {
490 let adjusted_ty = expr.ty.adjust_for_autoref(cx.tcx, Some(autoref));
492 ty::adjustment::AutoPtr(r, m) => {
494 temp_lifetime: temp_lifetime,
497 kind: ExprKind::Borrow {
499 borrow_kind: to_borrow_kind(m),
504 ty::adjustment::AutoUnsafe(m) => {
505 // Convert this to a suitable `&foo` and
506 // then an unsafe coercion. Limit the region to be just this
508 let region = ty::ReScope(expr_extent);
509 let region = cx.tcx.mk_region(region);
511 temp_lifetime: temp_lifetime,
512 ty: cx.tcx.mk_ref(region, ty::TypeAndMut { ty: expr.ty, mutbl: m }),
514 kind: ExprKind::Borrow {
516 borrow_kind: to_borrow_kind(m),
521 temp_lifetime: temp_lifetime,
524 kind: ExprKind::Cast { source: expr.to_ref() },
530 if let Some(target) = adj.unsize {
532 temp_lifetime: temp_lifetime,
535 kind: ExprKind::Unsize { source: expr.to_ref() },
541 // Next, wrap this up in the expr's scope.
543 temp_lifetime: temp_lifetime,
546 kind: ExprKind::Scope {
548 value: expr.to_ref(),
552 // Finally, create a destruction scope, if any.
553 if let Some(extent) = cx.tcx.region_maps.opt_destruction_extent(self.id) {
555 temp_lifetime: temp_lifetime,
558 kind: ExprKind::Scope {
560 value: expr.to_ref(),
570 fn method_callee<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
572 method_call: ty::MethodCall)
574 let tables = cx.tcx.tables.borrow();
575 let callee = &tables.method_map[&method_call];
576 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
578 temp_lifetime: temp_lifetime,
581 kind: ExprKind::Literal {
582 literal: Literal::Item {
583 def_id: callee.def_id,
584 kind: ItemKind::Method,
585 substs: callee.substs,
591 fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
593 hir::MutMutable => BorrowKind::Mut,
594 hir::MutImmutable => BorrowKind::Shared,
598 fn convert_arm<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, arm: &'tcx hir::Arm) -> Arm<'tcx> {
600 let opt_map = if arm.pats.len() == 1 {
604 pat_util::pat_bindings(&cx.tcx.def_map, &arm.pats[0], |_, p_id, _, path| {
605 map.insert(path.node, p_id);
611 patterns: arm.pats.iter().map(|p| cx.refutable_pat(opt_map, p)).collect(),
612 guard: arm.guard.to_ref(),
613 body: arm.body.to_ref(),
617 fn convert_path_expr<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> ExprKind<'tcx> {
618 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(expr.id).substs);
619 // Otherwise there may be def_map borrow conflicts
620 let def = cx.tcx.def_map.borrow()[&expr.id].full_def();
621 let (def_id, kind) = match def {
622 // A regular function.
623 Def::Fn(def_id) => (def_id, ItemKind::Function),
624 Def::Method(def_id) => (def_id, ItemKind::Method),
625 Def::Struct(def_id) => match cx.tcx.node_id_to_type(expr.id).sty {
626 // A tuple-struct constructor. Should only be reached if not called in the same
628 ty::TyBareFn(..) => (def_id, ItemKind::Function),
629 // A unit struct which is used as a value. We return a completely different ExprKind
630 // here to account for this special case.
631 ty::TyStruct(adt_def, substs) => return ExprKind::Adt {
638 ref sty => panic!("unexpected sty: {:?}", sty)
640 Def::Variant(enum_id, variant_id) => match cx.tcx.node_id_to_type(expr.id).sty {
641 // A variant constructor. Should only be reached if not called in the same
643 ty::TyBareFn(..) => (variant_id, ItemKind::Function),
644 // A unit variant, similar special case to the struct case above.
645 ty::TyEnum(adt_def, substs) => {
646 debug_assert!(adt_def.did == enum_id);
647 let index = adt_def.variant_index_with_id(variant_id);
648 return ExprKind::Adt {
651 variant_index: index,
656 ref sty => panic!("unexpected sty: {:?}", sty)
659 Def::AssociatedConst(def_id) => {
660 if let Some(v) = cx.try_const_eval_literal(expr) {
661 return ExprKind::Literal { literal: v };
663 (def_id, ItemKind::Constant)
667 Def::Static(node_id, _) => return ExprKind::StaticRef {
671 def @ Def::Local(..) |
672 def @ Def::Upvar(..) => return convert_var(cx, expr, def),
675 cx.tcx.sess.span_bug(
677 &format!("def `{:?}` not yet implemented", def)),
680 literal: Literal::Item { def_id: def_id, kind: kind, substs: substs }
684 fn convert_var<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
685 expr: &'tcx hir::Expr,
688 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
691 Def::Local(_, node_id) => {
697 Def::Upvar(_, id_var, index, closure_expr_id) => {
698 debug!("convert_var(upvar({:?}, {:?}, {:?}))", id_var, index, closure_expr_id);
699 let var_ty = cx.tcx.node_id_to_type(id_var);
701 let body_id = match cx.tcx.map.find(closure_expr_id) {
702 Some(map::NodeExpr(expr)) => {
704 hir::ExprClosure(_, _, ref body) => body.id,
706 cx.tcx.sess.span_bug(expr.span, "closure expr is not a closure expr");
711 cx.tcx.sess.span_bug(expr.span, "ast-map has garbage for closure expr");
715 // FIXME free regions in closures are not right
716 let closure_ty = cx.tcx.node_id_to_type(closure_expr_id);
718 // FIXME we're just hard-coding the idea that the
719 // signature will be &self or &mut self and hence will
720 // have a bound region with number 0
721 let region = ty::Region::ReFree(ty::FreeRegion {
722 scope: cx.tcx.region_maps.node_extent(body_id),
723 bound_region: ty::BoundRegion::BrAnon(0),
725 let region = cx.tcx.mk_region(region);
727 let self_expr = match cx.tcx.closure_kind(cx.tcx.map.local_def_id(closure_expr_id)) {
728 ty::ClosureKind::FnClosureKind => {
730 cx.tcx.mk_ref(region,
731 ty::TypeAndMut { ty: closure_ty,
732 mutbl: hir::MutImmutable });
735 temp_lifetime: temp_lifetime,
737 kind: ExprKind::Deref {
740 temp_lifetime: temp_lifetime,
742 kind: ExprKind::SelfRef
747 ty::ClosureKind::FnMutClosureKind => {
749 cx.tcx.mk_ref(region,
750 ty::TypeAndMut { ty: closure_ty,
751 mutbl: hir::MutMutable });
754 temp_lifetime: temp_lifetime,
756 kind: ExprKind::Deref {
759 temp_lifetime: temp_lifetime,
761 kind: ExprKind::SelfRef
766 ty::ClosureKind::FnOnceClosureKind => {
769 temp_lifetime: temp_lifetime,
771 kind: ExprKind::SelfRef,
776 // at this point we have `self.n`, which loads up the upvar
777 let field_kind = ExprKind::Field {
778 lhs: self_expr.to_ref(),
779 name: Field::new(index),
782 // ...but the upvar might be an `&T` or `&mut T` capture, at which
783 // point we need an implicit deref
784 let upvar_id = ty::UpvarId {
786 closure_expr_id: closure_expr_id,
788 let upvar_capture = match cx.tcx.upvar_capture(upvar_id) {
791 cx.tcx.sess.span_bug(
793 &format!("no upvar_capture for {:?}", upvar_id));
796 match upvar_capture {
797 ty::UpvarCapture::ByValue => field_kind,
798 ty::UpvarCapture::ByRef(borrow) => {
801 temp_lifetime: temp_lifetime,
803 cx.tcx.mk_region(borrow.region),
806 mutbl: borrow.kind.to_mutbl_lossy()
816 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
821 fn bin_op(op: hir::BinOp_) -> BinOp {
823 hir::BinOp_::BiAdd => BinOp::Add,
824 hir::BinOp_::BiSub => BinOp::Sub,
825 hir::BinOp_::BiMul => BinOp::Mul,
826 hir::BinOp_::BiDiv => BinOp::Div,
827 hir::BinOp_::BiRem => BinOp::Rem,
828 hir::BinOp_::BiBitXor => BinOp::BitXor,
829 hir::BinOp_::BiBitAnd => BinOp::BitAnd,
830 hir::BinOp_::BiBitOr => BinOp::BitOr,
831 hir::BinOp_::BiShl => BinOp::Shl,
832 hir::BinOp_::BiShr => BinOp::Shr,
833 hir::BinOp_::BiEq => BinOp::Eq,
834 hir::BinOp_::BiLt => BinOp::Lt,
835 hir::BinOp_::BiLe => BinOp::Le,
836 hir::BinOp_::BiNe => BinOp::Ne,
837 hir::BinOp_::BiGe => BinOp::Ge,
838 hir::BinOp_::BiGt => BinOp::Gt,
839 _ => panic!("no equivalent for ast binop {:?}", op),
848 fn overloaded_operator<'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 // the receiver has all the adjustments that are needed, so we can
856 // just push a reference to it
857 let mut argrefs = vec![receiver];
859 // the arguments, unfortunately, do not, so if this is a ByRef
860 // operator, we have to gin up the autorefs (but by value is easy)
862 PassArgs::ByValue => {
863 argrefs.extend(args.iter().map(|arg| arg.to_ref()))
867 let scope = cx.tcx.region_maps.node_extent(expr.id);
868 let region = cx.tcx.mk_region(ty::ReScope(scope));
869 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
873 let arg_ty = cx.tcx.expr_ty_adjusted(arg);
875 cx.tcx.mk_ref(region,
876 ty::TypeAndMut { ty: arg_ty,
877 mutbl: hir::MutImmutable });
879 temp_lifetime: temp_lifetime,
882 kind: ExprKind::Borrow { region: *region,
883 borrow_kind: BorrowKind::Shared,
890 // now create the call itself
891 let fun = method_callee(cx, expr, method_call);
899 fn overloaded_lvalue<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
900 expr: &'tcx hir::Expr,
901 method_call: ty::MethodCall,
903 receiver: ExprRef<'tcx>,
904 args: Vec<&'tcx P<hir::Expr>>)
906 // For an overloaded *x or x[y] expression of type T, the method
907 // call returns an &T and we must add the deref so that the types
908 // line up (this is because `*x` and `x[y]` represent lvalues):
910 // to find the type &T of the content returned by the method;
911 let tables = cx.tcx.tables.borrow();
912 let callee = &tables.method_map[&method_call];
913 let ref_ty = callee.ty.fn_ret();
914 let ref_ty = cx.tcx.no_late_bound_regions(&ref_ty).unwrap().unwrap();
916 // (1) callees always have all late-bound regions fully instantiated,
917 // (2) overloaded methods don't return `!`
919 // construct the complete expression `foo()` for the overloaded call,
920 // which will yield the &T type
921 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
922 let ref_kind = overloaded_operator(cx, expr, method_call, pass_args, receiver, args);
923 let ref_expr = Expr {
924 temp_lifetime: temp_lifetime,
930 // construct and return a deref wrapper `*foo()`
931 ExprKind::Deref { arg: ref_expr.to_ref() }
934 fn capture_freevar<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
935 closure_expr: &'tcx hir::Expr,
936 freevar: &ty::Freevar,
937 freevar_ty: Ty<'tcx>)
939 let id_var = freevar.def.var_id();
940 let upvar_id = ty::UpvarId {
942 closure_expr_id: closure_expr.id,
944 let upvar_capture = cx.tcx.upvar_capture(upvar_id).unwrap();
945 let temp_lifetime = cx.tcx.region_maps.temporary_scope(closure_expr.id);
946 let var_ty = cx.tcx.node_id_to_type(id_var);
947 let captured_var = Expr {
948 temp_lifetime: temp_lifetime,
950 span: closure_expr.span,
951 kind: convert_var(cx, closure_expr, freevar.def),
953 match upvar_capture {
954 ty::UpvarCapture::ByValue => {
955 captured_var.to_ref()
957 ty::UpvarCapture::ByRef(upvar_borrow) => {
958 let borrow_kind = match upvar_borrow.kind {
959 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
960 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
961 ty::BorrowKind::MutBorrow => BorrowKind::Mut,
964 temp_lifetime: temp_lifetime,
966 span: closure_expr.span,
967 kind: ExprKind::Borrow { region: upvar_borrow.region,
968 borrow_kind: borrow_kind,
969 arg: captured_var.to_ref() }
975 fn loop_label<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> CodeExtent {
976 match cx.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
977 Some(Def::Label(loop_id)) => cx.tcx.region_maps.node_extent(loop_id),
979 cx.tcx.sess.span_bug(expr.span, &format!("loop scope resolved to {:?}", d));
984 /// Converts a list of named fields (i.e. for struct-like struct/enum ADTs) into FieldExprRef.
985 fn field_refs<'tcx>(variant: VariantDef<'tcx>,
986 fields: &'tcx [hir::Field])
987 -> Vec<FieldExprRef<'tcx>>
990 .map(|field| FieldExprRef {
991 name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
992 expr: field.expr.to_ref(),