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;
17 use rustc::hir::def::{Def, CtorKind};
18 use rustc::middle::const_val::ConstVal;
19 use rustc::ty::{self, AdtKind, VariantDef, Ty};
20 use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
21 use rustc::ty::cast::CastKind as TyCastKind;
23 use rustc::hir::def_id::LocalDefId;
25 impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
26 type Output = Expr<'tcx>;
28 fn make_mirror<'a, 'gcx>(self, cx: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
29 let temp_lifetime = cx.region_scope_tree.temporary_scope(self.hir_id.local_id);
30 let expr_scope = region::Scope::Node(self.hir_id.local_id);
32 debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
34 let mut expr = make_mirror_unadjusted(cx, self);
36 // Now apply adjustments, if any.
37 for adjustment in cx.tables().expr_adjustments(self) {
38 debug!("make_mirror: expr={:?} applying adjustment={:?}",
41 expr = apply_adjustment(cx, self, expr, adjustment);
44 // Next, wrap this up in the expr's scope.
49 kind: ExprKind::Scope {
50 region_scope: expr_scope,
52 lint_level: cx.lint_level_of(self.id),
56 // Finally, create a destruction scope, if any.
57 if let Some(region_scope) =
58 cx.region_scope_tree.opt_destruction_scope(self.hir_id.local_id) {
63 kind: ExprKind::Scope {
66 lint_level: LintLevel::Inherited,
76 fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
77 hir_expr: &'tcx hir::Expr,
79 adjustment: &Adjustment<'tcx>)
81 let Expr { temp_lifetime, span, .. } = expr;
82 let kind = match adjustment.kind {
83 Adjust::ReifyFnPointer => {
84 ExprKind::ReifyFnPointer { source: expr.to_ref() }
86 Adjust::UnsafeFnPointer => {
87 ExprKind::UnsafeFnPointer { source: expr.to_ref() }
89 Adjust::ClosureFnPointer => {
90 ExprKind::ClosureFnPointer { source: expr.to_ref() }
92 Adjust::NeverToAny => {
93 ExprKind::NeverToAny { source: expr.to_ref() }
95 Adjust::MutToConstPointer => {
96 ExprKind::Cast { source: expr.to_ref() }
98 Adjust::Deref(None) => {
99 ExprKind::Deref { arg: expr.to_ref() }
101 Adjust::Deref(Some(deref)) => {
102 let call = deref.method_call(cx.tcx, expr.ty);
106 ty: cx.tcx.mk_ref(deref.region,
112 kind: ExprKind::Borrow {
113 region: deref.region,
114 borrow_kind: to_borrow_kind(deref.mutbl),
119 overloaded_place(cx, hir_expr, adjustment.target, Some(call), vec![expr.to_ref()])
121 Adjust::Borrow(AutoBorrow::Ref(r, m)) => {
124 borrow_kind: to_borrow_kind(m),
128 Adjust::Borrow(AutoBorrow::RawPtr(m)) => {
129 // Convert this to a suitable `&foo` and
130 // then an unsafe coercion. Limit the region to be just this
132 let region = ty::ReScope(region::Scope::Node(hir_expr.hir_id.local_id));
133 let region = cx.tcx.mk_region(region);
136 ty: cx.tcx.mk_ref(region,
142 kind: ExprKind::Borrow {
144 borrow_kind: to_borrow_kind(m),
148 let cast_expr = Expr {
150 ty: adjustment.target,
152 kind: ExprKind::Cast { source: expr.to_ref() }
155 // To ensure that both implicit and explicit coercions are
156 // handled the same way, we insert an extra layer of indirection here.
157 // For explicit casts (e.g. 'foo as *const T'), the source of the 'Use'
158 // will be an ExprKind::Hair with the appropriate cast expression. Here,
159 // we make our Use source the generated Cast from the original coercion.
161 // In both cases, this outer 'Use' ensures that the inner 'Cast' is handled by
162 // as_operand, not by as_rvalue - causing the cast result to be stored in a temporary.
163 // Ordinary, this is identical to using the cast directly as an rvalue. However, if the
164 // source of the cast was previously borrowed as mutable, storing the cast in a
165 // temporary gives the source a chance to expire before the cast is used. For
166 // structs with a self-referential *mut ptr, this allows assignment to work as
169 // For example, consider the type 'struct Foo { field: *mut Foo }',
170 // The method 'fn bar(&mut self) { self.field = self }'
171 // triggers a coercion from '&mut self' to '*mut self'. In order
172 // for the assignment to be valid, the implicit borrow
173 // of 'self' involved in the coercion needs to end before the local
174 // containing the '*mut T' is assigned to 'self.field' - otherwise,
175 // we end up trying to assign to 'self.field' while we have another mutable borrow
178 // We only need to worry about this kind of thing for coercions from refs to ptrs,
179 // since they get rid of a borrow implicitly.
180 ExprKind::Use { source: cast_expr.to_ref() }
183 ExprKind::Unsize { source: expr.to_ref() }
189 ty: adjustment.target,
195 fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
196 expr: &'tcx hir::Expr)
198 let expr_ty = cx.tables().expr_ty(expr);
199 let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
201 let kind = match expr.node {
202 // Here comes the interesting stuff:
203 hir::ExprMethodCall(.., ref args) => {
204 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
205 let expr = method_callee(cx, expr, None);
206 let args = args.iter()
216 hir::ExprCall(ref fun, ref args) => {
217 if cx.tables().is_method_call(expr) {
218 // The callee is something implementing Fn, FnMut, or FnOnce.
219 // Find the actual method implementation being called and
220 // build the appropriate UFCS call expression with the
221 // callee-object as expr parameter.
223 // rewrite f(u, v) into FnOnce::call_once(f, (u, v))
225 let method = method_callee(cx, expr, None);
227 let arg_tys = args.iter().map(|e| cx.tables().expr_ty_adjusted(e));
228 let tupled_args = Expr {
229 ty: cx.tcx.mk_tup(arg_tys, false),
232 kind: ExprKind::Tuple { fields: args.iter().map(ToRef::to_ref).collect() },
237 fun: method.to_ref(),
238 args: vec![fun.to_ref(), tupled_args.to_ref()],
241 let adt_data = if let hir::ExprPath(hir::QPath::Resolved(_, ref path)) = fun.node {
242 // Tuple-like ADTs are represented as ExprCall. We convert them here.
243 expr_ty.ty_adt_def().and_then(|adt_def| {
245 Def::VariantCtor(variant_id, CtorKind::Fn) => {
246 Some((adt_def, adt_def.variant_index_with_id(variant_id)))
248 Def::StructCtor(_, CtorKind::Fn) => Some((adt_def, 0)),
255 if let Some((adt_def, index)) = adt_data {
256 let substs = cx.tables().node_substs(fun.hir_id);
257 let field_refs = args.iter()
261 name: Field::new(idx),
269 variant_index: index,
275 ty: cx.tables().node_id_to_type(fun.hir_id),
283 hir::ExprAddrOf(mutbl, ref expr) => {
284 let region = match expr_ty.sty {
285 ty::TyRef(r, _) => r,
286 _ => span_bug!(expr.span, "type of & not region"),
290 borrow_kind: to_borrow_kind(mutbl),
295 hir::ExprBlock(ref blk) => ExprKind::Block { body: &blk },
297 hir::ExprAssign(ref lhs, ref rhs) => {
304 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
305 if cx.tables().is_method_call(expr) {
306 overloaded_operator(cx, expr, vec![lhs.to_ref(), rhs.to_ref()])
316 hir::ExprLit(..) => ExprKind::Literal { literal: cx.const_eval_literal(expr) },
318 hir::ExprBinary(op, ref lhs, ref rhs) => {
319 if cx.tables().is_method_call(expr) {
320 overloaded_operator(cx, expr, vec![lhs.to_ref(), rhs.to_ref()])
323 match (op.node, cx.constness) {
324 // FIXME(eddyb) use logical ops in constants when
325 // they can handle that kind of control-flow.
326 (hir::BinOp_::BiAnd, hir::Constness::Const) => {
333 (hir::BinOp_::BiOr, hir::Constness::Const) => {
341 (hir::BinOp_::BiAnd, hir::Constness::NotConst) => {
342 ExprKind::LogicalOp {
348 (hir::BinOp_::BiOr, hir::Constness::NotConst) => {
349 ExprKind::LogicalOp {
357 let op = bin_op(op.node);
368 hir::ExprIndex(ref lhs, ref index) => {
369 if cx.tables().is_method_call(expr) {
370 overloaded_place(cx, expr, expr_ty, None, vec![lhs.to_ref(), index.to_ref()])
374 index: index.to_ref(),
379 hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
380 if cx.tables().is_method_call(expr) {
381 overloaded_place(cx, expr, expr_ty, None, vec![arg.to_ref()])
383 ExprKind::Deref { arg: arg.to_ref() }
387 hir::ExprUnary(hir::UnOp::UnNot, ref arg) => {
388 if cx.tables().is_method_call(expr) {
389 overloaded_operator(cx, expr, vec![arg.to_ref()])
398 hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
399 if cx.tables().is_method_call(expr) {
400 overloaded_operator(cx, expr, vec![arg.to_ref()])
402 // FIXME runtime-overflow
403 if let hir::ExprLit(_) = arg.node {
404 ExprKind::Literal { literal: cx.const_eval_literal(expr) }
414 hir::ExprStruct(ref qpath, ref fields, ref base) => {
416 ty::TyAdt(adt, substs) => {
417 match adt.adt_kind() {
418 AdtKind::Struct | AdtKind::Union => {
419 let field_refs = field_refs(&adt.variants[0], fields);
425 base: base.as_ref().map(|base| {
428 field_types: cx.tables()
429 .fru_field_types()[expr.hir_id]
436 let def = match *qpath {
437 hir::QPath::Resolved(_, ref path) => path.def,
438 hir::QPath::TypeRelative(..) => Def::Err,
441 Def::Variant(variant_id) => {
442 assert!(base.is_none());
444 let index = adt.variant_index_with_id(variant_id);
445 let field_refs = field_refs(&adt.variants[index], fields);
448 variant_index: index,
455 span_bug!(expr.span, "unexpected def: {:?}", def);
463 "unexpected type for struct literal: {:?}",
469 hir::ExprClosure(..) => {
470 let closure_ty = cx.tables().expr_ty(expr);
471 let (def_id, substs, interior) = match closure_ty.sty {
472 ty::TyClosure(def_id, substs) => (def_id, substs, None),
473 ty::TyGenerator(def_id, substs, interior) => (def_id, substs, Some(interior)),
475 span_bug!(expr.span, "closure expr w/o closure type: {:?}", closure_ty);
478 let upvars = cx.tcx.with_freevars(expr.id, |freevars| {
480 .zip(substs.upvar_tys(def_id, cx.tcx))
481 .map(|(fv, ty)| capture_freevar(cx, expr, fv, ty))
492 hir::ExprPath(ref qpath) => {
493 let def = cx.tables().qpath_def(qpath, expr.hir_id);
494 convert_path_expr(cx, expr, def)
497 hir::ExprInlineAsm(ref asm, ref outputs, ref inputs) => {
498 ExprKind::InlineAsm {
500 outputs: outputs.to_ref(),
501 inputs: inputs.to_ref(),
505 // Now comes the rote stuff:
506 hir::ExprRepeat(ref v, count) => {
507 let c = &cx.tcx.hir.body(count).value;
508 let def_id = cx.tcx.hir.body_owner_def_id(count);
509 let substs = Substs::identity_for_item(cx.tcx.global_tcx(), def_id);
510 let count = match cx.tcx.at(c.span).const_eval(cx.param_env.and((def_id, substs))) {
511 Ok(&ty::Const { val: ConstVal::Integral(ConstInt::Usize(u)), .. }) => u,
512 Ok(other) => bug!("constant evaluation of repeat count yielded {:?}", other),
513 Err(s) => cx.fatal_const_eval_err(&s, c.span, "expression")
521 hir::ExprRet(ref v) => ExprKind::Return { value: v.to_ref() },
522 hir::ExprBreak(dest, ref value) => {
523 match dest.target_id {
524 hir::ScopeTarget::Block(target_id) |
525 hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(target_id)) => ExprKind::Break {
526 label: region::Scope::Node(cx.tcx.hir.node_to_hir_id(target_id).local_id),
527 value: value.to_ref(),
529 hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
530 bug!("invalid loop id for break: {}", err)
533 hir::ExprAgain(dest) => {
534 match dest.target_id {
535 hir::ScopeTarget::Block(_) => bug!("cannot continue to blocks"),
536 hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(loop_id)) => ExprKind::Continue {
537 label: region::Scope::Node(cx.tcx.hir.node_to_hir_id(loop_id).local_id),
539 hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
540 bug!("invalid loop id for continue: {}", err)
543 hir::ExprMatch(ref discr, ref arms, _) => {
545 discriminant: discr.to_ref(),
546 arms: arms.iter().map(|a| convert_arm(cx, a)).collect(),
549 hir::ExprIf(ref cond, ref then, ref otherwise) => {
551 condition: cond.to_ref(),
553 otherwise: otherwise.to_ref(),
556 hir::ExprWhile(ref cond, ref body, _) => {
558 condition: Some(cond.to_ref()),
559 body: block::to_expr_ref(cx, body),
562 hir::ExprLoop(ref body, _, _) => {
565 body: block::to_expr_ref(cx, body),
568 hir::ExprField(ref source, name) => {
569 let index = match cx.tables().expr_ty_adjusted(source).sty {
570 ty::TyAdt(adt_def, _) => adt_def.variants[0].index_of_field_named(name.node),
571 ref ty => span_bug!(expr.span, "field of non-ADT: {:?}", ty),
574 index.unwrap_or_else(|| {
575 span_bug!(expr.span, "no index found for field `{}`", name.node)
578 lhs: source.to_ref(),
579 name: Field::new(index),
582 hir::ExprTupField(ref source, index) => {
584 lhs: source.to_ref(),
585 name: Field::new(index.node as usize),
588 hir::ExprCast(ref source, _) => {
589 // Check to see if this cast is a "coercion cast", where the cast is actually done
590 // using a coercion (or is a no-op).
591 if let Some(&TyCastKind::CoercionCast) = cx.tables()
593 .get(source.hir_id) {
594 // Convert the lexpr to a vexpr.
595 ExprKind::Use { source: source.to_ref() }
597 ExprKind::Cast { source: source.to_ref() }
600 hir::ExprType(ref source, _) => return source.make_mirror(cx),
601 hir::ExprBox(ref value) => {
603 value: value.to_ref(),
606 hir::ExprArray(ref fields) => ExprKind::Array { fields: fields.to_ref() },
607 hir::ExprTup(ref fields) => ExprKind::Tuple { fields: fields.to_ref() },
609 hir::ExprYield(ref v) => ExprKind::Yield { value: v.to_ref() },
620 fn method_callee<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
622 custom_callee: Option<(DefId, &'tcx Substs<'tcx>)>)
624 let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
625 let (def_id, substs) = custom_callee.unwrap_or_else(|| {
626 (cx.tables().type_dependent_defs()[expr.hir_id].def_id(),
627 cx.tables().node_substs(expr.hir_id))
629 let ty = cx.tcx().mk_fn_def(def_id, substs);
634 kind: ExprKind::Literal {
635 literal: Literal::Value {
636 value: cx.tcx.mk_const(ty::Const {
637 val: ConstVal::Function(def_id, substs),
645 fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
647 hir::MutMutable => BorrowKind::Mut,
648 hir::MutImmutable => BorrowKind::Shared,
652 fn convert_arm<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, arm: &'tcx hir::Arm) -> Arm<'tcx> {
654 patterns: arm.pats.iter().map(|p| cx.pattern_from_hir(p)).collect(),
655 guard: arm.guard.to_ref(),
656 body: arm.body.to_ref(),
658 lint_level: LintLevel::Inherited,
662 fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
663 expr: &'tcx hir::Expr,
666 let substs = cx.tables().node_substs(expr.hir_id);
668 // A regular function, constructor function or a constant.
670 Def::Method(def_id) |
671 Def::StructCtor(def_id, CtorKind::Fn) |
672 Def::VariantCtor(def_id, CtorKind::Fn) => ExprKind::Literal {
673 literal: Literal::Value {
674 value: cx.tcx.mk_const(ty::Const {
675 val: ConstVal::Function(def_id, substs),
676 ty: cx.tables().node_id_to_type(expr.hir_id)
682 Def::AssociatedConst(def_id) => ExprKind::Literal {
683 literal: Literal::Value {
684 value: cx.tcx.mk_const(ty::Const {
685 val: ConstVal::Unevaluated(def_id, substs),
686 ty: cx.tables().node_id_to_type(expr.hir_id)
691 Def::StructCtor(def_id, CtorKind::Const) |
692 Def::VariantCtor(def_id, CtorKind::Const) => {
693 match cx.tables().node_id_to_type(expr.hir_id).sty {
694 // A unit struct/variant which is used as a value.
695 // We return a completely different ExprKind here to account for this special case.
696 ty::TyAdt(adt_def, substs) => {
699 variant_index: adt_def.variant_index_with_id(def_id),
705 ref sty => bug!("unexpected sty: {:?}", sty),
709 Def::Static(node_id, _) => ExprKind::StaticRef { id: node_id },
711 Def::Local(..) | Def::Upvar(..) => convert_var(cx, expr, def),
713 _ => span_bug!(expr.span, "def `{:?}` not yet implemented", def),
717 fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
718 expr: &'tcx hir::Expr,
721 let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
724 Def::Local(id) => ExprKind::VarRef { id },
726 Def::Upvar(var_id, index, closure_expr_id) => {
727 debug!("convert_var(upvar({:?}, {:?}, {:?}))",
731 let var_hir_id = cx.tcx.hir.node_to_hir_id(var_id);
732 let var_ty = cx.tables().node_id_to_type(var_hir_id);
734 // FIXME free regions in closures are not right
735 let closure_ty = cx.tables()
736 .node_id_to_type(cx.tcx.hir.node_to_hir_id(closure_expr_id));
738 // FIXME we're just hard-coding the idea that the
739 // signature will be &self or &mut self and hence will
740 // have a bound region with number 0
741 let closure_def_id = cx.tcx.hir.local_def_id(closure_expr_id);
742 let region = ty::ReFree(ty::FreeRegion {
743 scope: closure_def_id,
744 bound_region: ty::BoundRegion::BrAnon(0),
746 let region = cx.tcx.mk_region(region);
748 let self_expr = if let ty::TyClosure(_, closure_substs) = closure_ty.sty {
749 match cx.infcx.closure_kind(closure_def_id, closure_substs).unwrap() {
750 ty::ClosureKind::Fn => {
751 let ref_closure_ty = cx.tcx.mk_ref(region,
754 mutbl: hir::MutImmutable,
758 temp_lifetime: temp_lifetime,
760 kind: ExprKind::Deref {
765 kind: ExprKind::SelfRef,
771 ty::ClosureKind::FnMut => {
772 let ref_closure_ty = cx.tcx.mk_ref(region,
775 mutbl: hir::MutMutable,
781 kind: ExprKind::Deref {
786 kind: ExprKind::SelfRef,
791 ty::ClosureKind::FnOnce => {
796 kind: ExprKind::SelfRef,
805 kind: ExprKind::SelfRef,
809 // at this point we have `self.n`, which loads up the upvar
810 let field_kind = ExprKind::Field {
811 lhs: self_expr.to_ref(),
812 name: Field::new(index),
815 // ...but the upvar might be an `&T` or `&mut T` capture, at which
816 // point we need an implicit deref
817 let upvar_id = ty::UpvarId {
819 closure_expr_id: LocalDefId::from_def_id(closure_def_id),
821 match cx.tables().upvar_capture(upvar_id) {
822 ty::UpvarCapture::ByValue => field_kind,
823 ty::UpvarCapture::ByRef(borrow) => {
827 ty: cx.tcx.mk_ref(borrow.region,
830 mutbl: borrow.kind.to_mutbl_lossy(),
840 _ => span_bug!(expr.span, "type of & not region"),
845 fn bin_op(op: hir::BinOp_) -> BinOp {
847 hir::BinOp_::BiAdd => BinOp::Add,
848 hir::BinOp_::BiSub => BinOp::Sub,
849 hir::BinOp_::BiMul => BinOp::Mul,
850 hir::BinOp_::BiDiv => BinOp::Div,
851 hir::BinOp_::BiRem => BinOp::Rem,
852 hir::BinOp_::BiBitXor => BinOp::BitXor,
853 hir::BinOp_::BiBitAnd => BinOp::BitAnd,
854 hir::BinOp_::BiBitOr => BinOp::BitOr,
855 hir::BinOp_::BiShl => BinOp::Shl,
856 hir::BinOp_::BiShr => BinOp::Shr,
857 hir::BinOp_::BiEq => BinOp::Eq,
858 hir::BinOp_::BiLt => BinOp::Lt,
859 hir::BinOp_::BiLe => BinOp::Le,
860 hir::BinOp_::BiNe => BinOp::Ne,
861 hir::BinOp_::BiGe => BinOp::Ge,
862 hir::BinOp_::BiGt => BinOp::Gt,
863 _ => bug!("no equivalent for ast binop {:?}", op),
867 fn overloaded_operator<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
868 expr: &'tcx hir::Expr,
869 args: Vec<ExprRef<'tcx>>)
871 let fun = method_callee(cx, expr, None);
879 fn overloaded_place<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
880 expr: &'tcx hir::Expr,
882 custom_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
883 args: Vec<ExprRef<'tcx>>)
885 // For an overloaded *x or x[y] expression of type T, the method
886 // call returns an &T and we must add the deref so that the types
887 // line up (this is because `*x` and `x[y]` represent places):
889 let recv_ty = match args[0] {
890 ExprRef::Hair(e) => cx.tables().expr_ty_adjusted(e),
891 ExprRef::Mirror(ref e) => e.ty
894 // Reconstruct the output assuming it's a reference with the
895 // same region and mutability as the receiver. This holds for
896 // `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`.
897 let (region, mt) = match recv_ty.sty {
898 ty::TyRef(region, mt) => (region, mt),
899 _ => span_bug!(expr.span, "overloaded_place: receiver is not a reference"),
901 let ref_ty = cx.tcx.mk_ref(region, ty::TypeAndMut {
906 // construct the complete expression `foo()` for the overloaded call,
907 // which will yield the &T type
908 let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
909 let fun = method_callee(cx, expr, custom_callee);
910 let ref_expr = Expr {
914 kind: ExprKind::Call {
921 // construct and return a deref wrapper `*foo()`
922 ExprKind::Deref { arg: ref_expr.to_ref() }
925 fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
926 closure_expr: &'tcx hir::Expr,
927 freevar: &hir::Freevar,
928 freevar_ty: Ty<'tcx>)
930 let var_hir_id = cx.tcx.hir.node_to_hir_id(freevar.var_id());
931 let upvar_id = ty::UpvarId {
933 closure_expr_id: cx.tcx.hir.local_def_id(closure_expr.id).to_local(),
935 let upvar_capture = cx.tables().upvar_capture(upvar_id);
936 let temp_lifetime = cx.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
937 let var_ty = cx.tables().node_id_to_type(var_hir_id);
938 let captured_var = Expr {
941 span: closure_expr.span,
942 kind: convert_var(cx, closure_expr, freevar.def),
944 match upvar_capture {
945 ty::UpvarCapture::ByValue => captured_var.to_ref(),
946 ty::UpvarCapture::ByRef(upvar_borrow) => {
947 let borrow_kind = match upvar_borrow.kind {
948 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
949 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
950 ty::BorrowKind::MutBorrow => BorrowKind::Mut,
955 span: closure_expr.span,
956 kind: ExprKind::Borrow {
957 region: upvar_borrow.region,
959 arg: captured_var.to_ref(),
966 /// Converts a list of named fields (i.e. for struct-like struct/enum ADTs) into FieldExprRef.
967 fn field_refs<'tcx>(variant: &'tcx VariantDef,
968 fields: &'tcx [hir::Field])
969 -> Vec<FieldExprRef<'tcx>> {
973 name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
974 expr: field.expr.to_ref(),