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::indexed_vec::Idx;
13 use rustc_const_math::ConstInt;
16 use hair::cx::to_ref::ToRef;
18 use rustc::hir::def::Def;
19 use rustc::middle::const_val::ConstVal;
20 use rustc_const_eval as const_eval;
21 use rustc::middle::region::CodeExtent;
22 use rustc::ty::{self, VariantDef, Ty};
23 use rustc::ty::cast::CastKind as TyCastKind;
24 use rustc::mir::repr::*;
28 impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
29 type Output = Expr<'tcx>;
31 fn make_mirror<'a, 'gcx>(self, cx: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
32 let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
33 let expr_extent = cx.tcx.region_maps.node_extent(self.id);
35 debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
37 let mut expr = make_mirror_unadjusted(cx, self);
39 debug!("make_mirror: unadjusted-expr={:?} applying adjustments={:?}",
40 expr, cx.tcx.tables.borrow().adjustments.get(&self.id));
42 // Now apply adjustments, if any.
43 match cx.tcx.tables.borrow().adjustments.get(&self.id) {
45 Some(&ty::adjustment::AdjustReifyFnPointer) => {
46 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
48 temp_lifetime: temp_lifetime,
51 kind: ExprKind::ReifyFnPointer { source: expr.to_ref() },
54 Some(&ty::adjustment::AdjustUnsafeFnPointer) => {
55 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
57 temp_lifetime: temp_lifetime,
60 kind: ExprKind::UnsafeFnPointer { source: expr.to_ref() },
63 Some(&ty::adjustment::AdjustNeverToAny(adjusted_ty)) => {
65 temp_lifetime: temp_lifetime,
68 kind: ExprKind::NeverToAny { source: expr.to_ref() },
71 Some(&ty::adjustment::AdjustMutToConstPointer) => {
72 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
74 temp_lifetime: temp_lifetime,
77 kind: ExprKind::Cast { source: expr.to_ref() },
80 Some(&ty::adjustment::AdjustDerefRef(ref adj)) => {
81 for i in 0..adj.autoderefs {
84 expr.ty.adjust_for_autoderef(
89 |mc| cx.tcx.tables.borrow().method_map.get(&mc).map(|m| m.ty));
90 debug!("make_mirror: autoderef #{}, adjusted_ty={:?}", i, adjusted_ty);
91 let method_key = ty::MethodCall::autoderef(self.id, i);
93 cx.tcx.tables.borrow().method_map.get(&method_key).map(|m| m.ty);
94 let kind = if let Some(meth_ty) = meth_ty {
95 debug!("make_mirror: overloaded autoderef (meth_ty={:?})", meth_ty);
97 let ref_ty = cx.tcx.no_late_bound_regions(&meth_ty.fn_ret());
98 let (region, mutbl) = match ref_ty {
100 sty: ty::TyRef(region, mt), ..
101 }) => (region, mt.mutbl),
102 _ => span_bug!(expr.span, "autoderef returned bad type")
106 temp_lifetime: temp_lifetime,
108 region, ty::TypeAndMut { ty: expr.ty, mutbl: mutbl }),
110 kind: ExprKind::Borrow {
112 borrow_kind: to_borrow_kind(mutbl),
117 overloaded_lvalue(cx, self, method_key,
118 PassArgs::ByRef, expr.to_ref(), vec![])
120 debug!("make_mirror: built-in autoderef");
121 ExprKind::Deref { arg: expr.to_ref() }
124 temp_lifetime: temp_lifetime,
131 if let Some(autoref) = adj.autoref {
132 let adjusted_ty = expr.ty.adjust_for_autoref(cx.tcx, Some(autoref));
134 ty::adjustment::AutoPtr(r, m) => {
136 temp_lifetime: temp_lifetime,
139 kind: ExprKind::Borrow {
141 borrow_kind: to_borrow_kind(m),
146 ty::adjustment::AutoUnsafe(m) => {
147 // Convert this to a suitable `&foo` and
148 // then an unsafe coercion. Limit the region to be just this
150 let region = ty::ReScope(expr_extent);
151 let region = cx.tcx.mk_region(region);
153 temp_lifetime: temp_lifetime,
154 ty: cx.tcx.mk_ref(region, ty::TypeAndMut { ty: expr.ty, mutbl: m }),
156 kind: ExprKind::Borrow {
158 borrow_kind: to_borrow_kind(m),
163 temp_lifetime: temp_lifetime,
166 kind: ExprKind::Cast { source: expr.to_ref() },
172 if let Some(target) = adj.unsize {
174 temp_lifetime: temp_lifetime,
177 kind: ExprKind::Unsize { source: expr.to_ref() },
183 // Next, wrap this up in the expr's scope.
185 temp_lifetime: temp_lifetime,
188 kind: ExprKind::Scope {
190 value: expr.to_ref(),
194 // Finally, create a destruction scope, if any.
195 if let Some(extent) = cx.tcx.region_maps.opt_destruction_extent(self.id) {
197 temp_lifetime: temp_lifetime,
200 kind: ExprKind::Scope {
202 value: expr.to_ref(),
212 fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
213 expr: &'tcx hir::Expr)
215 let expr_ty = cx.tcx.expr_ty(expr);
216 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
218 let kind = match expr.node {
219 // Here comes the interesting stuff:
220 hir::ExprMethodCall(_, _, ref args) => {
221 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
222 let expr = method_callee(cx, expr, ty::MethodCall::expr(expr.id));
223 let args = args.iter()
233 hir::ExprCall(ref fun, ref args) => {
234 if cx.tcx.is_method_call(expr.id) {
235 // The callee is something implementing Fn, FnMut, or FnOnce.
236 // Find the actual method implementation being called and
237 // build the appropriate UFCS call expression with the
238 // callee-object as expr parameter.
240 // rewrite f(u, v) into FnOnce::call_once(f, (u, v))
242 let method = method_callee(cx, expr, ty::MethodCall::expr(expr.id));
244 let sig = match method.ty.sty {
245 ty::TyFnDef(_, _, fn_ty) => &fn_ty.sig,
246 _ => span_bug!(expr.span, "type of method is not an fn")
249 let sig = cx.tcx.no_late_bound_regions(sig).unwrap_or_else(|| {
250 span_bug!(expr.span, "method call has late-bound regions")
253 assert_eq!(sig.inputs.len(), 2);
255 let tupled_args = Expr {
257 temp_lifetime: temp_lifetime,
259 kind: ExprKind::Tuple {
260 fields: args.iter().map(ToRef::to_ref).collect()
266 fun: method.to_ref(),
267 args: vec![fun.to_ref(), tupled_args.to_ref()]
270 let adt_data = if let hir::ExprPath(..) = fun.node {
271 // Tuple-like ADTs are represented as ExprCall. We convert them here.
272 expr_ty.ty_adt_def().and_then(|adt_def|{
273 match cx.tcx.expect_def(fun.id) {
274 Def::Variant(_, variant_id) => {
275 Some((adt_def, adt_def.variant_index_with_id(variant_id)))
284 if let Some((adt_def, index)) = adt_data {
285 let substs = cx.tcx.node_id_item_substs(fun.id).substs;
286 let field_refs = args.iter().enumerate().map(|(idx, e)| FieldExprRef {
287 name: Field::new(idx),
293 variant_index: index,
299 ty: cx.tcx.node_id_to_type(fun.id),
307 hir::ExprAddrOf(mutbl, ref expr) => {
308 let region = match expr_ty.sty {
309 ty::TyRef(r, _) => r,
310 _ => span_bug!(expr.span, "type of & not region"),
314 borrow_kind: to_borrow_kind(mutbl),
319 hir::ExprBlock(ref blk) => {
320 ExprKind::Block { body: &blk }
323 hir::ExprAssign(ref lhs, ref rhs) => {
330 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
331 if cx.tcx.is_method_call(expr.id) {
332 let pass_args = if op.node.is_by_value() {
337 overloaded_operator(cx, expr, ty::MethodCall::expr(expr.id),
338 pass_args, lhs.to_ref(), vec![rhs])
348 hir::ExprLit(..) => ExprKind::Literal {
349 literal: cx.const_eval_literal(expr)
352 hir::ExprBinary(op, ref lhs, ref rhs) => {
353 if cx.tcx.is_method_call(expr.id) {
354 let pass_args = if op.node.is_by_value() {
359 overloaded_operator(cx, expr, ty::MethodCall::expr(expr.id),
360 pass_args, lhs.to_ref(), vec![rhs])
363 match (op.node, cx.constness) {
364 // FIXME(eddyb) use logical ops in constants when
365 // they can handle that kind of control-flow.
366 (hir::BinOp_::BiAnd, hir::Constness::Const) => {
373 (hir::BinOp_::BiOr, hir::Constness::Const) => {
381 (hir::BinOp_::BiAnd, hir::Constness::NotConst) => {
382 ExprKind::LogicalOp {
388 (hir::BinOp_::BiOr, hir::Constness::NotConst) => {
389 ExprKind::LogicalOp {
397 let op = bin_op(op.node);
408 hir::ExprIndex(ref lhs, ref index) => {
409 if cx.tcx.is_method_call(expr.id) {
410 overloaded_lvalue(cx, expr, ty::MethodCall::expr(expr.id),
411 PassArgs::ByValue, lhs.to_ref(), vec![index])
415 index: index.to_ref(),
420 hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
421 if cx.tcx.is_method_call(expr.id) {
422 overloaded_lvalue(cx, expr, ty::MethodCall::expr(expr.id),
423 PassArgs::ByValue, arg.to_ref(), vec![])
425 ExprKind::Deref { arg: arg.to_ref() }
429 hir::ExprUnary(hir::UnOp::UnNot, ref arg) => {
430 if cx.tcx.is_method_call(expr.id) {
431 overloaded_operator(cx, expr, ty::MethodCall::expr(expr.id),
432 PassArgs::ByValue, arg.to_ref(), vec![])
441 hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
442 if cx.tcx.is_method_call(expr.id) {
443 overloaded_operator(cx, expr, ty::MethodCall::expr(expr.id),
444 PassArgs::ByValue, arg.to_ref(), vec![])
446 // FIXME runtime-overflow
447 if let hir::ExprLit(_) = arg.node {
449 literal: cx.const_eval_literal(expr),
460 hir::ExprStruct(_, ref fields, ref base) => {
462 ty::TyStruct(adt, substs) => {
463 let field_refs = field_refs(&adt.variants[0], fields);
469 base: base.as_ref().map(|base| {
472 field_types: cx.tcx.tables
474 .fru_field_types[&expr.id]
480 ty::TyEnum(adt, substs) => {
481 match cx.tcx.expect_def(expr.id) {
482 Def::Variant(enum_id, variant_id) => {
483 debug_assert!(adt.did == enum_id);
484 assert!(base.is_none());
486 let index = adt.variant_index_with_id(variant_id);
487 let field_refs = field_refs(&adt.variants[index], fields);
490 variant_index: index,
499 "unexpected def: {:?}",
507 "unexpected type for struct literal: {:?}",
513 hir::ExprClosure(..) => {
514 let closure_ty = cx.tcx.expr_ty(expr);
515 let (def_id, substs) = match closure_ty.sty {
516 ty::TyClosure(def_id, substs) => (def_id, substs),
519 "closure expr w/o closure type: {:?}",
523 let upvars = cx.tcx.with_freevars(expr.id, |freevars| {
526 .map(|(i, fv)| capture_freevar(cx, expr, fv, substs.upvar_tys[i]))
536 hir::ExprPath(..) => {
537 convert_path_expr(cx, expr)
540 hir::ExprInlineAsm(ref asm, ref outputs, ref inputs) => {
541 ExprKind::InlineAsm {
543 outputs: outputs.to_ref(),
544 inputs: inputs.to_ref()
548 // Now comes the rote stuff:
550 hir::ExprRepeat(ref v, ref c) => ExprKind::Repeat {
552 count: TypedConstVal {
553 ty: cx.tcx.expr_ty(c),
555 value: match const_eval::eval_const_expr(cx.tcx.global_tcx(), c) {
556 ConstVal::Integral(ConstInt::Usize(u)) => u,
557 other => bug!("constant evaluation of repeat count yielded {:?}", other),
561 hir::ExprRet(ref v) =>
562 ExprKind::Return { value: v.to_ref() },
563 hir::ExprBreak(label) =>
564 ExprKind::Break { label: label.map(|_| loop_label(cx, expr)) },
565 hir::ExprAgain(label) =>
566 ExprKind::Continue { label: label.map(|_| loop_label(cx, expr)) },
567 hir::ExprMatch(ref discr, ref arms, _) =>
568 ExprKind::Match { discriminant: discr.to_ref(),
569 arms: arms.iter().map(|a| convert_arm(cx, a)).collect() },
570 hir::ExprIf(ref cond, ref then, ref otherwise) =>
571 ExprKind::If { condition: cond.to_ref(),
572 then: block::to_expr_ref(cx, then),
573 otherwise: otherwise.to_ref() },
574 hir::ExprWhile(ref cond, ref body, _) =>
575 ExprKind::Loop { condition: Some(cond.to_ref()),
576 body: block::to_expr_ref(cx, body) },
577 hir::ExprLoop(ref body, _) =>
578 ExprKind::Loop { condition: None,
579 body: block::to_expr_ref(cx, body) },
580 hir::ExprField(ref source, name) => {
581 let index = match cx.tcx.expr_ty_adjusted(source).sty {
582 ty::TyStruct(adt_def, _) =>
583 adt_def.variants[0].index_of_field_named(name.node),
587 "field of non-struct: {:?}",
590 let index = index.unwrap_or_else(|| {
593 "no index found for field `{}`",
596 ExprKind::Field { lhs: source.to_ref(), name: Field::new(index) }
598 hir::ExprTupField(ref source, index) =>
599 ExprKind::Field { lhs: source.to_ref(),
600 name: Field::new(index.node as usize) },
601 hir::ExprCast(ref source, _) => {
602 // Check to see if this cast is a "coercion cast", where the cast is actually done
603 // using a coercion (or is a no-op).
604 if let Some(&TyCastKind::CoercionCast) = cx.tcx.cast_kinds.borrow().get(&source.id) {
605 // Skip the actual cast itexpr, as it's now a no-op.
606 return source.make_mirror(cx);
608 ExprKind::Cast { source: source.to_ref() }
611 hir::ExprType(ref source, _) =>
612 return source.make_mirror(cx),
613 hir::ExprBox(ref value) =>
615 value: value.to_ref(),
616 value_extents: cx.tcx.region_maps.node_extent(value.id)
618 hir::ExprVec(ref fields) =>
619 ExprKind::Vec { fields: fields.to_ref() },
620 hir::ExprTup(ref fields) =>
621 ExprKind::Tuple { fields: fields.to_ref() },
625 temp_lifetime: temp_lifetime,
632 fn method_callee<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
634 method_call: ty::MethodCall)
636 let tables = cx.tcx.tables.borrow();
637 let callee = &tables.method_map[&method_call];
638 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
640 temp_lifetime: temp_lifetime,
643 kind: ExprKind::Literal {
644 literal: Literal::Item {
645 def_id: callee.def_id,
646 substs: callee.substs,
652 fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
654 hir::MutMutable => BorrowKind::Mut,
655 hir::MutImmutable => BorrowKind::Shared,
659 fn convert_arm<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
660 arm: &'tcx hir::Arm) -> Arm<'tcx> {
662 patterns: arm.pats.iter().map(|p| cx.refutable_pat(p)).collect(),
663 guard: arm.guard.to_ref(),
664 body: arm.body.to_ref(),
668 fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
669 expr: &'tcx hir::Expr)
671 let substs = cx.tcx.node_id_item_substs(expr.id).substs;
672 // Otherwise there may be def_map borrow conflicts
673 let def = cx.tcx.expect_def(expr.id);
674 let def_id = match def {
675 // A regular function.
676 Def::Fn(def_id) | Def::Method(def_id) => def_id,
677 Def::Struct(def_id) => match cx.tcx.node_id_to_type(expr.id).sty {
678 // A tuple-struct constructor. Should only be reached if not called in the same
680 ty::TyFnDef(..) => def_id,
681 // A unit struct which is used as a value. We return a completely different ExprKind
682 // here to account for this special case.
683 ty::TyStruct(adt_def, substs) => return ExprKind::Adt {
690 ref sty => bug!("unexpected sty: {:?}", sty)
692 Def::Variant(enum_id, variant_id) => match cx.tcx.node_id_to_type(expr.id).sty {
693 // A variant constructor. Should only be reached if not called in the same
695 ty::TyFnDef(..) => variant_id,
696 // A unit variant, similar special case to the struct case above.
697 ty::TyEnum(adt_def, substs) => {
698 debug_assert!(adt_def.did == enum_id);
699 let index = adt_def.variant_index_with_id(variant_id);
700 return ExprKind::Adt {
703 variant_index: index,
708 ref sty => bug!("unexpected sty: {:?}", sty)
711 Def::AssociatedConst(def_id) => def_id,
713 Def::Static(node_id, _) => return ExprKind::StaticRef {
717 Def::Local(..) | Def::Upvar(..) => return convert_var(cx, expr, def),
719 _ => span_bug!(expr.span, "def `{:?}` not yet implemented", def),
722 literal: Literal::Item { def_id: def_id, substs: substs }
726 fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
727 expr: &'tcx hir::Expr,
730 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
733 Def::Local(_, node_id) => {
739 Def::Upvar(_, id_var, index, closure_expr_id) => {
740 debug!("convert_var(upvar({:?}, {:?}, {:?}))", id_var, index, closure_expr_id);
741 let var_ty = cx.tcx.node_id_to_type(id_var);
743 let body_id = match cx.tcx.map.find(closure_expr_id) {
744 Some(map::NodeExpr(expr)) => {
746 hir::ExprClosure(_, _, ref body, _) => body.id,
748 span_bug!(expr.span, "closure expr is not a closure expr");
753 span_bug!(expr.span, "ast-map has garbage for closure expr");
757 // FIXME free regions in closures are not right
758 let closure_ty = cx.tcx.node_id_to_type(closure_expr_id);
760 // FIXME we're just hard-coding the idea that the
761 // signature will be &self or &mut self and hence will
762 // have a bound region with number 0
763 let region = ty::Region::ReFree(ty::FreeRegion {
764 scope: cx.tcx.region_maps.node_extent(body_id),
765 bound_region: ty::BoundRegion::BrAnon(0),
767 let region = cx.tcx.mk_region(region);
769 let self_expr = match cx.tcx.closure_kind(cx.tcx.map.local_def_id(closure_expr_id)) {
770 ty::ClosureKind::Fn => {
772 cx.tcx.mk_ref(region,
773 ty::TypeAndMut { ty: closure_ty,
774 mutbl: hir::MutImmutable });
777 temp_lifetime: temp_lifetime,
779 kind: ExprKind::Deref {
782 temp_lifetime: temp_lifetime,
784 kind: ExprKind::SelfRef
789 ty::ClosureKind::FnMut => {
791 cx.tcx.mk_ref(region,
792 ty::TypeAndMut { ty: closure_ty,
793 mutbl: hir::MutMutable });
796 temp_lifetime: temp_lifetime,
798 kind: ExprKind::Deref {
801 temp_lifetime: temp_lifetime,
803 kind: ExprKind::SelfRef
808 ty::ClosureKind::FnOnce => {
811 temp_lifetime: temp_lifetime,
813 kind: ExprKind::SelfRef,
818 // at this point we have `self.n`, which loads up the upvar
819 let field_kind = ExprKind::Field {
820 lhs: self_expr.to_ref(),
821 name: Field::new(index),
824 // ...but the upvar might be an `&T` or `&mut T` capture, at which
825 // point we need an implicit deref
826 let upvar_id = ty::UpvarId {
828 closure_expr_id: closure_expr_id,
830 let upvar_capture = match cx.tcx.upvar_capture(upvar_id) {
835 "no upvar_capture for {:?}",
839 match upvar_capture {
840 ty::UpvarCapture::ByValue => field_kind,
841 ty::UpvarCapture::ByRef(borrow) => {
844 temp_lifetime: temp_lifetime,
846 cx.tcx.mk_region(borrow.region),
849 mutbl: borrow.kind.to_mutbl_lossy()
859 _ => span_bug!(expr.span, "type of & not region"),
864 fn bin_op(op: hir::BinOp_) -> BinOp {
866 hir::BinOp_::BiAdd => BinOp::Add,
867 hir::BinOp_::BiSub => BinOp::Sub,
868 hir::BinOp_::BiMul => BinOp::Mul,
869 hir::BinOp_::BiDiv => BinOp::Div,
870 hir::BinOp_::BiRem => BinOp::Rem,
871 hir::BinOp_::BiBitXor => BinOp::BitXor,
872 hir::BinOp_::BiBitAnd => BinOp::BitAnd,
873 hir::BinOp_::BiBitOr => BinOp::BitOr,
874 hir::BinOp_::BiShl => BinOp::Shl,
875 hir::BinOp_::BiShr => BinOp::Shr,
876 hir::BinOp_::BiEq => BinOp::Eq,
877 hir::BinOp_::BiLt => BinOp::Lt,
878 hir::BinOp_::BiLe => BinOp::Le,
879 hir::BinOp_::BiNe => BinOp::Ne,
880 hir::BinOp_::BiGe => BinOp::Ge,
881 hir::BinOp_::BiGt => BinOp::Gt,
882 _ => bug!("no equivalent for ast binop {:?}", op),
891 fn overloaded_operator<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
892 expr: &'tcx hir::Expr,
893 method_call: ty::MethodCall,
895 receiver: ExprRef<'tcx>,
896 args: Vec<&'tcx P<hir::Expr>>)
898 // the receiver has all the adjustments that are needed, so we can
899 // just push a reference to it
900 let mut argrefs = vec![receiver];
902 // the arguments, unfortunately, do not, so if this is a ByRef
903 // operator, we have to gin up the autorefs (but by value is easy)
905 PassArgs::ByValue => {
906 argrefs.extend(args.iter().map(|arg| arg.to_ref()))
910 let scope = cx.tcx.region_maps.node_extent(expr.id);
911 let region = cx.tcx.mk_region(ty::ReScope(scope));
912 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
916 let arg_ty = cx.tcx.expr_ty_adjusted(arg);
918 cx.tcx.mk_ref(region,
919 ty::TypeAndMut { ty: arg_ty,
920 mutbl: hir::MutImmutable });
922 temp_lifetime: temp_lifetime,
925 kind: ExprKind::Borrow { region: *region,
926 borrow_kind: BorrowKind::Shared,
933 // now create the call itself
934 let fun = method_callee(cx, expr, method_call);
942 fn overloaded_lvalue<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
943 expr: &'tcx hir::Expr,
944 method_call: ty::MethodCall,
946 receiver: ExprRef<'tcx>,
947 args: Vec<&'tcx P<hir::Expr>>)
949 // For an overloaded *x or x[y] expression of type T, the method
950 // call returns an &T and we must add the deref so that the types
951 // line up (this is because `*x` and `x[y]` represent lvalues):
953 // to find the type &T of the content returned by the method;
954 let tables = cx.tcx.tables.borrow();
955 let callee = &tables.method_map[&method_call];
956 let ref_ty = callee.ty.fn_ret();
957 let ref_ty = cx.tcx.no_late_bound_regions(&ref_ty).unwrap();
958 // callees always have all late-bound regions fully instantiated,
960 // construct the complete expression `foo()` for the overloaded call,
961 // which will yield the &T type
962 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
963 let ref_kind = overloaded_operator(cx, expr, method_call, pass_args, receiver, args);
964 let ref_expr = Expr {
965 temp_lifetime: temp_lifetime,
971 // construct and return a deref wrapper `*foo()`
972 ExprKind::Deref { arg: ref_expr.to_ref() }
975 fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
976 closure_expr: &'tcx hir::Expr,
977 freevar: &hir::Freevar,
978 freevar_ty: Ty<'tcx>)
980 let id_var = freevar.def.var_id();
981 let upvar_id = ty::UpvarId {
983 closure_expr_id: closure_expr.id,
985 let upvar_capture = cx.tcx.upvar_capture(upvar_id).unwrap();
986 let temp_lifetime = cx.tcx.region_maps.temporary_scope(closure_expr.id);
987 let var_ty = cx.tcx.node_id_to_type(id_var);
988 let captured_var = Expr {
989 temp_lifetime: temp_lifetime,
991 span: closure_expr.span,
992 kind: convert_var(cx, closure_expr, freevar.def),
994 match upvar_capture {
995 ty::UpvarCapture::ByValue => {
996 captured_var.to_ref()
998 ty::UpvarCapture::ByRef(upvar_borrow) => {
999 let borrow_kind = match upvar_borrow.kind {
1000 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
1001 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
1002 ty::BorrowKind::MutBorrow => BorrowKind::Mut,
1005 temp_lifetime: temp_lifetime,
1007 span: closure_expr.span,
1008 kind: ExprKind::Borrow { region: upvar_borrow.region,
1009 borrow_kind: borrow_kind,
1010 arg: captured_var.to_ref() }
1016 fn loop_label<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
1017 expr: &'tcx hir::Expr) -> CodeExtent {
1018 match cx.tcx.expect_def(expr.id) {
1019 Def::Label(loop_id) => cx.tcx.region_maps.node_extent(loop_id),
1020 d => span_bug!(expr.span, "loop scope resolved to {:?}", d),
1024 /// Converts a list of named fields (i.e. for struct-like struct/enum ADTs) into FieldExprRef.
1025 fn field_refs<'tcx>(variant: VariantDef<'tcx>,
1026 fields: &'tcx [hir::Field])
1027 -> Vec<FieldExprRef<'tcx>>
1030 .map(|field| FieldExprRef {
1031 name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
1032 expr: field.expr.to_ref(),