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;
15 use hair::cx::to_ref::ToRef;
16 use rustc::hir::def::{Def, CtorKind};
17 use rustc::middle::const_val::ConstVal;
18 use rustc::ty::{self, AdtKind, VariantDef, Ty};
19 use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability};
20 use rustc::mir::interpret::{Value, PrimVal};
21 use rustc::ty::cast::CastKind as TyCastKind;
23 use rustc::hir::def_id::LocalDefId;
24 use rustc::mir::{BorrowKind};
26 impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
27 type Output = Expr<'tcx>;
29 fn make_mirror<'a, 'gcx>(self, cx: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
30 let temp_lifetime = cx.region_scope_tree.temporary_scope(self.hir_id.local_id);
31 let expr_scope = region::Scope::Node(self.hir_id.local_id);
33 debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
35 let mut expr = make_mirror_unadjusted(cx, self);
37 // Now apply adjustments, if any.
38 for adjustment in cx.tables().expr_adjustments(self) {
39 debug!("make_mirror: expr={:?} applying adjustment={:?}",
42 expr = apply_adjustment(cx, self, expr, adjustment);
45 // Next, wrap this up in the expr's scope.
50 kind: ExprKind::Scope {
51 region_scope: expr_scope,
53 lint_level: cx.lint_level_of(self.id),
57 // Finally, create a destruction scope, if any.
58 if let Some(region_scope) =
59 cx.region_scope_tree.opt_destruction_scope(self.hir_id.local_id) {
64 kind: ExprKind::Scope {
67 lint_level: LintLevel::Inherited,
77 fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
78 hir_expr: &'tcx hir::Expr,
80 adjustment: &Adjustment<'tcx>)
82 let Expr { temp_lifetime, span, .. } = expr;
83 let kind = match adjustment.kind {
84 Adjust::ReifyFnPointer => {
85 ExprKind::ReifyFnPointer { source: expr.to_ref() }
87 Adjust::UnsafeFnPointer => {
88 ExprKind::UnsafeFnPointer { source: expr.to_ref() }
90 Adjust::ClosureFnPointer => {
91 ExprKind::ClosureFnPointer { source: expr.to_ref() }
93 Adjust::NeverToAny => {
94 ExprKind::NeverToAny { source: expr.to_ref() }
96 Adjust::MutToConstPointer => {
97 ExprKind::Cast { source: expr.to_ref() }
99 Adjust::Deref(None) => {
100 ExprKind::Deref { arg: expr.to_ref() }
102 Adjust::Deref(Some(deref)) => {
103 let call = deref.method_call(cx.tcx(), expr.ty);
107 ty: cx.tcx.mk_ref(deref.region,
113 kind: ExprKind::Borrow {
114 region: deref.region,
115 borrow_kind: deref.mutbl.to_borrow_kind(),
120 overloaded_place(cx, hir_expr, adjustment.target, Some(call), vec![expr.to_ref()])
122 Adjust::Borrow(AutoBorrow::Ref(r, m)) => {
125 borrow_kind: m.to_borrow_kind(),
129 Adjust::Borrow(AutoBorrow::RawPtr(m)) => {
130 // Convert this to a suitable `&foo` and
131 // then an unsafe coercion. Limit the region to be just this
133 let region = ty::ReScope(region::Scope::Node(hir_expr.hir_id.local_id));
134 let region = cx.tcx.mk_region(region);
137 ty: cx.tcx.mk_ref(region,
143 kind: ExprKind::Borrow {
145 borrow_kind: m.to_borrow_kind(),
149 let cast_expr = Expr {
151 ty: adjustment.target,
153 kind: ExprKind::Cast { source: expr.to_ref() }
156 // To ensure that both implicit and explicit coercions are
157 // handled the same way, we insert an extra layer of indirection here.
158 // For explicit casts (e.g. 'foo as *const T'), the source of the 'Use'
159 // will be an ExprKind::Hair with the appropriate cast expression. Here,
160 // we make our Use source the generated Cast from the original coercion.
162 // In both cases, this outer 'Use' ensures that the inner 'Cast' is handled by
163 // as_operand, not by as_rvalue - causing the cast result to be stored in a temporary.
164 // Ordinary, this is identical to using the cast directly as an rvalue. However, if the
165 // source of the cast was previously borrowed as mutable, storing the cast in a
166 // temporary gives the source a chance to expire before the cast is used. For
167 // structs with a self-referential *mut ptr, this allows assignment to work as
170 // For example, consider the type 'struct Foo { field: *mut Foo }',
171 // The method 'fn bar(&mut self) { self.field = self }'
172 // triggers a coercion from '&mut self' to '*mut self'. In order
173 // for the assignment to be valid, the implicit borrow
174 // of 'self' involved in the coercion needs to end before the local
175 // containing the '*mut T' is assigned to 'self.field' - otherwise,
176 // we end up trying to assign to 'self.field' while we have another mutable borrow
179 // We only need to worry about this kind of thing for coercions from refs to ptrs,
180 // since they get rid of a borrow implicitly.
181 ExprKind::Use { source: cast_expr.to_ref() }
184 ExprKind::Unsize { source: expr.to_ref() }
190 ty: adjustment.target,
196 fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
197 expr: &'tcx hir::Expr)
199 let expr_ty = cx.tables().expr_ty(expr);
200 let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
202 let kind = match expr.node {
203 // Here comes the interesting stuff:
204 hir::ExprMethodCall(.., ref args) => {
205 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
206 let expr = method_callee(cx, expr, None);
207 let args = args.iter()
217 hir::ExprCall(ref fun, ref args) => {
218 if cx.tables().is_method_call(expr) {
219 // The callee is something implementing Fn, FnMut, or FnOnce.
220 // Find the actual method implementation being called and
221 // build the appropriate UFCS call expression with the
222 // callee-object as expr parameter.
224 // rewrite f(u, v) into FnOnce::call_once(f, (u, v))
226 let method = method_callee(cx, expr, None);
228 let arg_tys = args.iter().map(|e| cx.tables().expr_ty_adjusted(e));
229 let tupled_args = Expr {
230 ty: cx.tcx.mk_tup(arg_tys, false),
233 kind: ExprKind::Tuple { fields: args.iter().map(ToRef::to_ref).collect() },
238 fun: method.to_ref(),
239 args: vec![fun.to_ref(), tupled_args.to_ref()],
242 let adt_data = if let hir::ExprPath(hir::QPath::Resolved(_, ref path)) = fun.node {
243 // Tuple-like ADTs are represented as ExprCall. We convert them here.
244 expr_ty.ty_adt_def().and_then(|adt_def| {
246 Def::VariantCtor(variant_id, CtorKind::Fn) => {
247 Some((adt_def, adt_def.variant_index_with_id(variant_id)))
249 Def::StructCtor(_, CtorKind::Fn) => Some((adt_def, 0)),
256 if let Some((adt_def, index)) = adt_data {
257 let substs = cx.tables().node_substs(fun.hir_id);
258 let field_refs = args.iter()
262 name: Field::new(idx),
270 variant_index: index,
276 ty: cx.tables().node_id_to_type(fun.hir_id),
284 hir::ExprAddrOf(mutbl, ref expr) => {
285 let region = match expr_ty.sty {
286 ty::TyRef(r, _) => r,
287 _ => span_bug!(expr.span, "type of & not region"),
291 borrow_kind: mutbl.to_borrow_kind(),
296 hir::ExprBlock(ref blk) => ExprKind::Block { body: &blk },
298 hir::ExprAssign(ref lhs, ref rhs) => {
305 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
306 if cx.tables().is_method_call(expr) {
307 overloaded_operator(cx, expr, vec![lhs.to_ref(), rhs.to_ref()])
317 hir::ExprLit(ref lit) => ExprKind::Literal {
318 literal: cx.const_eval_literal(&lit.node, expr_ty, lit.span, false),
321 hir::ExprBinary(op, ref lhs, ref rhs) => {
322 if cx.tables().is_method_call(expr) {
323 overloaded_operator(cx, expr, vec![lhs.to_ref(), rhs.to_ref()])
326 match (op.node, cx.constness) {
327 // FIXME(eddyb) use logical ops in constants when
328 // they can handle that kind of control-flow.
329 (hir::BinOp_::BiAnd, hir::Constness::Const) => {
336 (hir::BinOp_::BiOr, hir::Constness::Const) => {
344 (hir::BinOp_::BiAnd, hir::Constness::NotConst) => {
345 ExprKind::LogicalOp {
351 (hir::BinOp_::BiOr, hir::Constness::NotConst) => {
352 ExprKind::LogicalOp {
360 let op = bin_op(op.node);
371 hir::ExprIndex(ref lhs, ref index) => {
372 if cx.tables().is_method_call(expr) {
373 overloaded_place(cx, expr, expr_ty, None, vec![lhs.to_ref(), index.to_ref()])
377 index: index.to_ref(),
382 hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
383 if cx.tables().is_method_call(expr) {
384 overloaded_place(cx, expr, expr_ty, None, vec![arg.to_ref()])
386 ExprKind::Deref { arg: arg.to_ref() }
390 hir::ExprUnary(hir::UnOp::UnNot, ref arg) => {
391 if cx.tables().is_method_call(expr) {
392 overloaded_operator(cx, expr, vec![arg.to_ref()])
401 hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
402 if cx.tables().is_method_call(expr) {
403 overloaded_operator(cx, expr, vec![arg.to_ref()])
405 if let hir::ExprLit(ref lit) = arg.node {
407 literal: cx.const_eval_literal(&lit.node, expr_ty, lit.span, true),
418 hir::ExprStruct(ref qpath, ref fields, ref base) => {
420 ty::TyAdt(adt, substs) => {
421 match adt.adt_kind() {
422 AdtKind::Struct | AdtKind::Union => {
423 let field_refs = field_refs(&adt.variants[0], fields);
429 base: base.as_ref().map(|base| {
432 field_types: cx.tables()
433 .fru_field_types()[expr.hir_id]
440 let def = match *qpath {
441 hir::QPath::Resolved(_, ref path) => path.def,
442 hir::QPath::TypeRelative(..) => Def::Err,
445 Def::Variant(variant_id) => {
446 assert!(base.is_none());
448 let index = adt.variant_index_with_id(variant_id);
449 let field_refs = field_refs(&adt.variants[index], fields);
452 variant_index: index,
459 span_bug!(expr.span, "unexpected def: {:?}", def);
467 "unexpected type for struct literal: {:?}",
473 hir::ExprClosure(..) => {
474 let closure_ty = cx.tables().expr_ty(expr);
475 let (def_id, substs, interior) = match closure_ty.sty {
476 ty::TyClosure(def_id, substs) => (def_id, substs, None),
477 ty::TyGenerator(def_id, substs, interior) => (def_id, substs, Some(interior)),
479 span_bug!(expr.span, "closure expr w/o closure type: {:?}", closure_ty);
482 let upvars = cx.tcx.with_freevars(expr.id, |freevars| {
484 .zip(substs.upvar_tys(def_id, cx.tcx))
485 .map(|(fv, ty)| capture_freevar(cx, expr, fv, ty))
496 hir::ExprPath(ref qpath) => {
497 let def = cx.tables().qpath_def(qpath, expr.hir_id);
498 convert_path_expr(cx, expr, def)
501 hir::ExprInlineAsm(ref asm, ref outputs, ref inputs) => {
502 ExprKind::InlineAsm {
504 outputs: outputs.to_ref(),
505 inputs: inputs.to_ref(),
509 // Now comes the rote stuff:
510 hir::ExprRepeat(ref v, count) => {
511 let c = &cx.tcx.hir.body(count).value;
512 let def_id = cx.tcx.hir.body_owner_def_id(count);
513 let substs = Substs::identity_for_item(cx.tcx.global_tcx(), def_id);
514 let count = match cx.tcx.at(c.span).const_eval(cx.param_env.and((def_id, substs))) {
515 Ok(cv) => cv.val.unwrap_usize(cx.tcx),
516 Err(s) => cx.fatal_const_eval_err(&s, c.span, "expression")
524 hir::ExprRet(ref v) => ExprKind::Return { value: v.to_ref() },
525 hir::ExprBreak(dest, ref value) => {
526 match dest.target_id {
527 hir::ScopeTarget::Block(target_id) |
528 hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(target_id)) => ExprKind::Break {
529 label: region::Scope::Node(cx.tcx.hir.node_to_hir_id(target_id).local_id),
530 value: value.to_ref(),
532 hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
533 bug!("invalid loop id for break: {}", err)
536 hir::ExprAgain(dest) => {
537 match dest.target_id {
538 hir::ScopeTarget::Block(_) => bug!("cannot continue to blocks"),
539 hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(loop_id)) => ExprKind::Continue {
540 label: region::Scope::Node(cx.tcx.hir.node_to_hir_id(loop_id).local_id),
542 hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
543 bug!("invalid loop id for continue: {}", err)
546 hir::ExprMatch(ref discr, ref arms, _) => {
548 discriminant: discr.to_ref(),
549 arms: arms.iter().map(|a| convert_arm(cx, a)).collect(),
552 hir::ExprIf(ref cond, ref then, ref otherwise) => {
554 condition: cond.to_ref(),
556 otherwise: otherwise.to_ref(),
559 hir::ExprWhile(ref cond, ref body, _) => {
561 condition: Some(cond.to_ref()),
562 body: block::to_expr_ref(cx, body),
565 hir::ExprLoop(ref body, _, _) => {
568 body: block::to_expr_ref(cx, body),
571 hir::ExprField(ref source, name) => {
572 let index = match cx.tables().expr_ty_adjusted(source).sty {
573 ty::TyAdt(adt_def, _) => adt_def.variants[0].index_of_field_named(name.node),
574 ref ty => span_bug!(expr.span, "field of non-ADT: {:?}", ty),
577 index.unwrap_or_else(|| {
578 span_bug!(expr.span, "no index found for field `{}`", name.node)
581 lhs: source.to_ref(),
582 name: Field::new(index),
585 hir::ExprTupField(ref source, index) => {
587 lhs: source.to_ref(),
588 name: Field::new(index.node as usize),
591 hir::ExprCast(ref source, _) => {
592 // Check to see if this cast is a "coercion cast", where the cast is actually done
593 // using a coercion (or is a no-op).
594 if let Some(&TyCastKind::CoercionCast) = cx.tables()
596 .get(source.hir_id) {
597 // Convert the lexpr to a vexpr.
598 ExprKind::Use { source: source.to_ref() }
600 ExprKind::Cast { source: source.to_ref() }
603 hir::ExprType(ref source, _) => return source.make_mirror(cx),
604 hir::ExprBox(ref value) => {
606 value: value.to_ref(),
609 hir::ExprArray(ref fields) => ExprKind::Array { fields: fields.to_ref() },
610 hir::ExprTup(ref fields) => ExprKind::Tuple { fields: fields.to_ref() },
612 hir::ExprYield(ref v) => ExprKind::Yield { value: v.to_ref() },
623 fn method_callee<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
625 custom_callee: Option<(DefId, &'tcx Substs<'tcx>)>)
627 let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
628 let (def_id, substs) = custom_callee.unwrap_or_else(|| {
629 (cx.tables().type_dependent_defs()[expr.hir_id].def_id(),
630 cx.tables().node_substs(expr.hir_id))
632 let ty = cx.tcx().mk_fn_def(def_id, substs);
637 kind: ExprKind::Literal {
638 literal: Literal::Value {
639 value: cx.tcx().mk_const(ty::Const {
640 val: ConstVal::Value(Value::ByVal(PrimVal::Undef)),
648 trait ToBorrowKind { fn to_borrow_kind(&self) -> BorrowKind; }
650 impl ToBorrowKind for AutoBorrowMutability {
651 fn to_borrow_kind(&self) -> BorrowKind {
653 AutoBorrowMutability::Mutable { allow_two_phase_borrow } =>
654 BorrowKind::Mut { allow_two_phase_borrow },
655 AutoBorrowMutability::Immutable =>
661 impl ToBorrowKind for hir::Mutability {
662 fn to_borrow_kind(&self) -> BorrowKind {
664 hir::MutMutable => BorrowKind::Mut { allow_two_phase_borrow: false },
665 hir::MutImmutable => BorrowKind::Shared,
670 fn convert_arm<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, arm: &'tcx hir::Arm) -> Arm<'tcx> {
672 patterns: arm.pats.iter().map(|p| cx.pattern_from_hir(p)).collect(),
673 guard: arm.guard.to_ref(),
674 body: arm.body.to_ref(),
676 lint_level: LintLevel::Inherited,
680 fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
681 expr: &'tcx hir::Expr,
684 let substs = cx.tables().node_substs(expr.hir_id);
686 // A regular function, constructor function or a constant.
689 Def::StructCtor(_, CtorKind::Fn) |
690 Def::VariantCtor(_, CtorKind::Fn) => ExprKind::Literal {
691 literal: Literal::Value {
692 value: cx.tcx.mk_const(ty::Const {
693 val: ConstVal::Value(Value::ByVal(PrimVal::Undef)),
694 ty: cx.tables().node_id_to_type(expr.hir_id)
700 Def::AssociatedConst(def_id) => ExprKind::Literal {
701 literal: Literal::Value {
702 value: cx.tcx.mk_const(ty::Const {
703 val: ConstVal::Unevaluated(def_id, substs),
704 ty: cx.tables().node_id_to_type(expr.hir_id)
709 Def::StructCtor(def_id, CtorKind::Const) |
710 Def::VariantCtor(def_id, CtorKind::Const) => {
711 match cx.tables().node_id_to_type(expr.hir_id).sty {
712 // A unit struct/variant which is used as a value.
713 // We return a completely different ExprKind here to account for this special case.
714 ty::TyAdt(adt_def, substs) => {
717 variant_index: adt_def.variant_index_with_id(def_id),
723 ref sty => bug!("unexpected sty: {:?}", sty),
727 Def::Static(node_id, _) => ExprKind::StaticRef { id: node_id },
729 Def::Local(..) | Def::Upvar(..) => convert_var(cx, expr, def),
731 _ => span_bug!(expr.span, "def `{:?}` not yet implemented", def),
735 fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
736 expr: &'tcx hir::Expr,
739 let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
742 Def::Local(id) => ExprKind::VarRef { id },
744 Def::Upvar(var_id, index, closure_expr_id) => {
745 debug!("convert_var(upvar({:?}, {:?}, {:?}))",
749 let var_hir_id = cx.tcx.hir.node_to_hir_id(var_id);
750 let var_ty = cx.tables().node_id_to_type(var_hir_id);
752 // FIXME free regions in closures are not right
753 let closure_ty = cx.tables()
754 .node_id_to_type(cx.tcx.hir.node_to_hir_id(closure_expr_id));
756 // FIXME we're just hard-coding the idea that the
757 // signature will be &self or &mut self and hence will
758 // have a bound region with number 0
759 let closure_def_id = cx.tcx.hir.local_def_id(closure_expr_id);
760 let region = ty::ReFree(ty::FreeRegion {
761 scope: closure_def_id,
762 bound_region: ty::BoundRegion::BrAnon(0),
764 let region = cx.tcx.mk_region(region);
766 let self_expr = if let ty::TyClosure(_, closure_substs) = closure_ty.sty {
767 match cx.infcx.closure_kind(closure_def_id, closure_substs).unwrap() {
768 ty::ClosureKind::Fn => {
769 let ref_closure_ty = cx.tcx.mk_ref(region,
772 mutbl: hir::MutImmutable,
776 temp_lifetime: temp_lifetime,
778 kind: ExprKind::Deref {
783 kind: ExprKind::SelfRef,
789 ty::ClosureKind::FnMut => {
790 let ref_closure_ty = cx.tcx.mk_ref(region,
793 mutbl: hir::MutMutable,
799 kind: ExprKind::Deref {
804 kind: ExprKind::SelfRef,
809 ty::ClosureKind::FnOnce => {
814 kind: ExprKind::SelfRef,
823 kind: ExprKind::SelfRef,
827 // at this point we have `self.n`, which loads up the upvar
828 let field_kind = ExprKind::Field {
829 lhs: self_expr.to_ref(),
830 name: Field::new(index),
833 // ...but the upvar might be an `&T` or `&mut T` capture, at which
834 // point we need an implicit deref
835 let upvar_id = ty::UpvarId {
837 closure_expr_id: LocalDefId::from_def_id(closure_def_id),
839 match cx.tables().upvar_capture(upvar_id) {
840 ty::UpvarCapture::ByValue => field_kind,
841 ty::UpvarCapture::ByRef(borrow) => {
845 ty: cx.tcx.mk_ref(borrow.region,
848 mutbl: borrow.kind.to_mutbl_lossy(),
858 _ => span_bug!(expr.span, "type of & not region"),
863 fn bin_op(op: hir::BinOp_) -> BinOp {
865 hir::BinOp_::BiAdd => BinOp::Add,
866 hir::BinOp_::BiSub => BinOp::Sub,
867 hir::BinOp_::BiMul => BinOp::Mul,
868 hir::BinOp_::BiDiv => BinOp::Div,
869 hir::BinOp_::BiRem => BinOp::Rem,
870 hir::BinOp_::BiBitXor => BinOp::BitXor,
871 hir::BinOp_::BiBitAnd => BinOp::BitAnd,
872 hir::BinOp_::BiBitOr => BinOp::BitOr,
873 hir::BinOp_::BiShl => BinOp::Shl,
874 hir::BinOp_::BiShr => BinOp::Shr,
875 hir::BinOp_::BiEq => BinOp::Eq,
876 hir::BinOp_::BiLt => BinOp::Lt,
877 hir::BinOp_::BiLe => BinOp::Le,
878 hir::BinOp_::BiNe => BinOp::Ne,
879 hir::BinOp_::BiGe => BinOp::Ge,
880 hir::BinOp_::BiGt => BinOp::Gt,
881 _ => bug!("no equivalent for ast binop {:?}", op),
885 fn overloaded_operator<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
886 expr: &'tcx hir::Expr,
887 args: Vec<ExprRef<'tcx>>)
889 let fun = method_callee(cx, expr, None);
897 fn overloaded_place<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
898 expr: &'tcx hir::Expr,
900 custom_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
901 args: Vec<ExprRef<'tcx>>)
903 // For an overloaded *x or x[y] expression of type T, the method
904 // call returns an &T and we must add the deref so that the types
905 // line up (this is because `*x` and `x[y]` represent places):
907 let recv_ty = match args[0] {
908 ExprRef::Hair(e) => cx.tables().expr_ty_adjusted(e),
909 ExprRef::Mirror(ref e) => e.ty
912 // Reconstruct the output assuming it's a reference with the
913 // same region and mutability as the receiver. This holds for
914 // `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`.
915 let (region, mt) = match recv_ty.sty {
916 ty::TyRef(region, mt) => (region, mt),
917 _ => span_bug!(expr.span, "overloaded_place: receiver is not a reference"),
919 let ref_ty = cx.tcx.mk_ref(region, ty::TypeAndMut {
924 // construct the complete expression `foo()` for the overloaded call,
925 // which will yield the &T type
926 let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
927 let fun = method_callee(cx, expr, custom_callee);
928 let ref_expr = Expr {
932 kind: ExprKind::Call {
939 // construct and return a deref wrapper `*foo()`
940 ExprKind::Deref { arg: ref_expr.to_ref() }
943 fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
944 closure_expr: &'tcx hir::Expr,
945 freevar: &hir::Freevar,
946 freevar_ty: Ty<'tcx>)
948 let var_hir_id = cx.tcx.hir.node_to_hir_id(freevar.var_id());
949 let upvar_id = ty::UpvarId {
951 closure_expr_id: cx.tcx.hir.local_def_id(closure_expr.id).to_local(),
953 let upvar_capture = cx.tables().upvar_capture(upvar_id);
954 let temp_lifetime = cx.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
955 let var_ty = cx.tables().node_id_to_type(var_hir_id);
956 let captured_var = Expr {
959 span: closure_expr.span,
960 kind: convert_var(cx, closure_expr, freevar.def),
962 match upvar_capture {
963 ty::UpvarCapture::ByValue => captured_var.to_ref(),
964 ty::UpvarCapture::ByRef(upvar_borrow) => {
965 let borrow_kind = match upvar_borrow.kind {
966 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
967 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
968 ty::BorrowKind::MutBorrow => BorrowKind::Mut { allow_two_phase_borrow: false }
973 span: closure_expr.span,
974 kind: ExprKind::Borrow {
975 region: upvar_borrow.region,
977 arg: captured_var.to_ref(),
984 /// Converts a list of named fields (i.e. for struct-like struct/enum ADTs) into FieldExprRef.
985 fn field_refs<'tcx>(variant: &'tcx VariantDef,
986 fields: &'tcx [hir::Field])
987 -> Vec<FieldExprRef<'tcx>> {
991 name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
992 expr: field.expr.to_ref(),