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.
13 use rustc_data_structures::fnv::FnvHashMap;
17 use tcx::pattern::PatNode;
18 use tcx::rustc::front::map;
19 use tcx::rustc::middle::def;
20 use tcx::rustc::middle::def_id::DefId;
21 use tcx::rustc::middle::region::CodeExtent;
22 use tcx::rustc::middle::pat_util;
23 use tcx::rustc::middle::ty::{self, Ty};
24 use tcx::rustc_front::hir;
25 use tcx::rustc_front::util as hir_util;
27 use tcx::syntax::codemap::Span;
28 use tcx::syntax::parse::token;
29 use tcx::syntax::ptr::P;
30 use tcx::to_ref::ToRef;
32 impl<'a,'tcx:'a> Mirror<Cx<'a,'tcx>> for &'tcx hir::Expr {
33 type Output = Expr<Cx<'a,'tcx>>;
35 fn make_mirror(self, cx: &mut Cx<'a,'tcx>) -> Expr<Cx<'a,'tcx>> {
36 debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
38 let expr_ty = cx.tcx.expr_ty(self); // note: no adjustments (yet)!
40 let kind = match self.node {
41 // Here comes the interesting stuff:
43 hir::ExprMethodCall(_, _, ref args) => {
44 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
45 let expr = method_callee(cx, self, ty::MethodCall::expr(self.id));
46 let args = args.iter()
55 hir::ExprAddrOf(mutbl, ref expr) => {
56 let region = match expr_ty.sty {
58 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region")
60 ExprKind::Borrow { region: *region,
61 borrow_kind: to_borrow_kind(mutbl),
65 hir::ExprBlock(ref blk) => {
71 hir::ExprAssign(ref lhs, ref rhs) => {
78 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
79 let op = bin_op(op.node);
87 hir::ExprLit(ref lit) => {
88 let literal = convert_literal(cx, self.span, expr_ty, lit);
89 ExprKind::Literal { literal: literal }
92 hir::ExprBinary(op, ref lhs, ref rhs) => {
93 if cx.tcx.is_method_call(self.id) {
94 let pass_args = if hir_util::is_by_value_binop(op.node) {
99 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
100 pass_args, lhs.to_ref(), vec![rhs])
104 hir::BinOp_::BiAnd => {
105 ExprKind::LogicalOp { op: LogicalOp::And,
109 hir::BinOp_::BiOr => {
110 ExprKind::LogicalOp { op: LogicalOp::Or,
115 let op = bin_op(op.node);
116 ExprKind::Binary { op: op,
124 hir::ExprIndex(ref lhs, ref index) => {
125 if cx.tcx.is_method_call(self.id) {
126 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
127 PassArgs::ByValue, lhs.to_ref(), vec![index])
129 ExprKind::Index { lhs: lhs.to_ref(),
130 index: index.to_ref() }
134 hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
135 if cx.tcx.is_method_call(self.id) {
136 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
137 PassArgs::ByValue, arg.to_ref(), vec![])
139 ExprKind::Deref { arg: arg.to_ref() }
143 hir::ExprUnary(hir::UnOp::UnUniq, ref arg) => {
144 assert!(!cx.tcx.is_method_call(self.id));
145 ExprKind::Box { place: None, value: arg.to_ref() }
148 hir::ExprUnary(op, ref arg) => {
149 if cx.tcx.is_method_call(self.id) {
150 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
151 PassArgs::ByValue, arg.to_ref(), vec![])
155 hir::UnOp::UnNot => UnOp::Not,
156 hir::UnOp::UnNeg => UnOp::Neg,
157 hir::UnOp::UnUniq | hir::UnOp::UnDeref => {
158 cx.tcx.sess.span_bug(
160 &format!("operator should have been handled elsewhere {:?}", op));
163 ExprKind::Unary { op: op, arg: arg.to_ref() }
167 hir::ExprStruct(_, ref fields, ref base) => {
169 ty::TyStruct(adt, substs) => {
174 fields: fields.to_ref(),
178 ty::TyEnum(adt, substs) => {
179 match cx.tcx.def_map.borrow()[&self.id].full_def() {
180 def::DefVariant(enum_id, variant_id, true) => {
181 debug_assert!(adt.did == enum_id);
182 let index = adt.variant_index_with_id(variant_id);
185 variant_index: index,
187 fields: fields.to_ref(),
192 cx.tcx.sess.span_bug(
194 &format!("unexpected def: {:?}", def));
199 cx.tcx.sess.span_bug(
201 &format!("unexpected type for struct literal: {:?}", expr_ty));
206 hir::ExprClosure(..) => {
207 let closure_ty = cx.tcx.expr_ty(self);
208 let (def_id, substs) = match closure_ty.sty {
209 ty::TyClosure(def_id, ref substs) => (def_id, substs),
211 cx.tcx.sess.span_bug(self.span,
212 &format!("closure expr w/o closure type: {:?}",
216 let upvars = cx.tcx.with_freevars(self.id, |freevars| {
219 .map(|(i, fv)| capture_freevar(cx, self, fv, substs.upvar_tys[i]))
229 hir::ExprRange(ref start, ref end) => {
230 let range_ty = cx.tcx.expr_ty(self);
231 let (adt_def, substs) = match range_ty.sty {
232 ty::TyStruct(adt_def, substs) => (adt_def, substs),
234 cx.tcx.sess.span_bug(
236 &format!("unexpanded ast"));
240 let field_expr_ref = |s: &'tcx P<hir::Expr>, nm: &str| {
241 FieldExprRef { name: Field::Named(token::intern(nm)),
245 let start_field = start.as_ref()
247 .map(|s| field_expr_ref(s, "start"));
249 let end_field = end.as_ref()
251 .map(|e| field_expr_ref(e, "end"));
253 ExprKind::Adt { adt_def: adt_def,
256 fields: start_field.chain(end_field).collect(),
260 hir::ExprPath(..) => {
261 convert_path_expr(cx, self)
264 hir::ExprInlineAsm(ref asm) => {
265 ExprKind::InlineAsm { asm: asm }
268 // Now comes the rote stuff:
270 hir::ExprParen(ref p) =>
271 ExprKind::Paren { arg: p.to_ref() },
272 hir::ExprRepeat(ref v, ref c) =>
273 ExprKind::Repeat { value: v.to_ref(), count: c.to_ref() },
274 hir::ExprRet(ref v) =>
275 ExprKind::Return { value: v.to_ref() },
276 hir::ExprBreak(label) =>
277 ExprKind::Break { label: label.map(|_| loop_label(cx, self)) },
278 hir::ExprAgain(label) =>
279 ExprKind::Continue { label: label.map(|_| loop_label(cx, self)) },
280 hir::ExprMatch(ref discr, ref arms, _) =>
281 ExprKind::Match { discriminant: discr.to_ref(),
282 arms: arms.iter().map(|a| convert_arm(cx, a)).collect() },
283 hir::ExprIf(ref cond, ref then, ref otherwise) =>
284 ExprKind::If { condition: cond.to_ref(),
285 then: block::to_expr_ref(cx, then),
286 otherwise: otherwise.to_ref() },
287 hir::ExprWhile(ref cond, ref body, _) =>
288 ExprKind::Loop { condition: Some(cond.to_ref()),
289 body: block::to_expr_ref(cx, body) },
290 hir::ExprLoop(ref body, _) =>
291 ExprKind::Loop { condition: None,
292 body: block::to_expr_ref(cx, body) },
293 hir::ExprField(ref source, ident) =>
294 ExprKind::Field { lhs: source.to_ref(),
295 name: Field::Named(ident.node.name) },
296 hir::ExprTupField(ref source, ident) =>
297 ExprKind::Field { lhs: source.to_ref(),
298 name: Field::Indexed(ident.node) },
299 hir::ExprCast(ref source, _) =>
300 ExprKind::Cast { source: source.to_ref() },
301 hir::ExprBox(ref place, ref value) =>
302 ExprKind::Box { place: place.to_ref(), value: value.to_ref() },
303 hir::ExprVec(ref fields) =>
304 ExprKind::Vec { fields: fields.to_ref() },
305 hir::ExprTup(ref fields) =>
306 ExprKind::Tuple { fields: fields.to_ref() },
307 hir::ExprCall(ref fun, ref args) =>
308 ExprKind::Call { fun: fun.to_ref(), args: args.to_ref() },
311 let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
312 let expr_extent = cx.tcx.region_maps.node_extent(self.id);
314 let mut expr = Expr {
315 temp_lifetime: temp_lifetime,
321 // Now apply adjustments, if any.
322 match cx.tcx.tables.borrow().adjustments.get(&self.id) {
324 Some(&ty::adjustment::AdjustReifyFnPointer) => {
325 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
327 temp_lifetime: temp_lifetime,
330 kind: ExprKind::ReifyFnPointer { source: expr.to_ref() },
333 Some(&ty::adjustment::AdjustUnsafeFnPointer) => {
334 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
336 temp_lifetime: temp_lifetime,
339 kind: ExprKind::UnsafeFnPointer { source: expr.to_ref() },
342 Some(&ty::adjustment::AdjustDerefRef(ref adj)) => {
343 for i in 0..adj.autoderefs {
346 expr.ty.adjust_for_autoderef(
351 |mc| cx.tcx.tables.borrow().method_map.get(&mc).map(|m| m.ty));
352 let kind = if cx.tcx.is_overloaded_autoderef(self.id, i) {
353 overloaded_lvalue(cx, self, ty::MethodCall::autoderef(self.id, i),
354 PassArgs::ByValue, expr.to_ref(), vec![])
356 ExprKind::Deref { arg: expr.to_ref() }
359 temp_lifetime: temp_lifetime,
366 if let Some(target) = adj.unsize {
368 temp_lifetime: temp_lifetime,
371 kind: ExprKind::Unsize { source: expr.to_ref() }
373 } else if let Some(autoref) = adj.autoref {
374 let adjusted_ty = expr.ty.adjust_for_autoref(cx.tcx, Some(autoref));
376 ty::adjustment::AutoPtr(r, m) => {
378 temp_lifetime: temp_lifetime,
381 kind: ExprKind::Borrow { region: *r,
382 borrow_kind: to_borrow_kind(m),
386 ty::adjustment::AutoUnsafe(m) => {
387 // Convert this to a suitable `&foo` and
388 // then an unsafe coercion. Limit the region to be just this
390 let region = ty::ReScope(expr_extent);
391 let region = cx.tcx.mk_region(region);
393 temp_lifetime: temp_lifetime,
394 ty: cx.tcx.mk_ref(region, ty::TypeAndMut { ty: expr.ty, mutbl: m }),
396 kind: ExprKind::Borrow { region: *region,
397 borrow_kind: to_borrow_kind(m),
401 temp_lifetime: temp_lifetime,
404 kind: ExprKind::Cast { source: expr.to_ref() }
412 // Next, wrap this up in the expr's scope.
414 temp_lifetime: temp_lifetime,
417 kind: ExprKind::Scope { extent: expr_extent,
418 value: expr.to_ref() }
421 // Finally, create a destruction scope, if any.
422 if let Some(extent) = cx.tcx.region_maps.opt_destruction_extent(self.id) {
424 temp_lifetime: temp_lifetime,
427 kind: ExprKind::Scope { extent: extent, value: expr.to_ref() }
436 fn method_callee<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
438 method_call: ty::MethodCall)
439 -> Expr<Cx<'a,'tcx>> {
440 let tables = cx.tcx.tables.borrow();
441 let callee = &tables.method_map[&method_call];
442 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
444 temp_lifetime: temp_lifetime,
447 kind: ExprKind::Literal {
448 literal: Literal::Item {
449 def_id: callee.def_id,
450 substs: callee.substs,
456 fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
458 hir::MutMutable => BorrowKind::Mut,
459 hir::MutImmutable => BorrowKind::Shared,
463 fn convert_literal<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
467 -> Literal<Cx<'a,'tcx>>
469 use repr::IntegralBits::*;
470 match (&literal.node, &expr_ty.sty) {
471 (&ast::LitStr(ref text, _), _) =>
472 Literal::String { value: text.clone() },
473 (&ast::LitByteStr(ref bytes), _) =>
474 Literal::Bytes { value: bytes.clone() },
475 (&ast::LitByte(c), _) =>
476 Literal::Uint { bits: B8, value: c as u64 },
477 (&ast::LitChar(c), _) =>
478 Literal::Char { c: c },
479 (&ast::LitInt(v, _), &ty::TyUint(ast::TyU8)) =>
480 Literal::Uint { bits: B8, value: v },
481 (&ast::LitInt(v, _), &ty::TyUint(ast::TyU16)) =>
482 Literal::Uint { bits: B16, value: v },
483 (&ast::LitInt(v, _), &ty::TyUint(ast::TyU32)) =>
484 Literal::Uint { bits: B32, value: v },
485 (&ast::LitInt(v, _), &ty::TyUint(ast::TyU64)) =>
486 Literal::Uint { bits: B64, value: v },
487 (&ast::LitInt(v, _), &ty::TyUint(ast::TyUs)) =>
488 Literal::Uint { bits: BSize, value: v },
489 (&ast::LitInt(v, ast::SignedIntLit(_, ast::Sign::Minus)), &ty::TyInt(ast::TyI8)) =>
490 Literal::Int { bits: B8, value: -(v as i64) },
491 (&ast::LitInt(v, ast::SignedIntLit(_, ast::Sign::Minus)), &ty::TyInt(ast::TyI16)) =>
492 Literal::Int { bits: B16, value: -(v as i64) },
493 (&ast::LitInt(v, ast::SignedIntLit(_, ast::Sign::Minus)), &ty::TyInt(ast::TyI32)) =>
494 Literal::Int { bits: B32, value: -(v as i64) },
495 (&ast::LitInt(v, ast::SignedIntLit(_, ast::Sign::Minus)), &ty::TyInt(ast::TyI64)) =>
496 Literal::Int { bits: B64, value: -(v as i64) },
497 (&ast::LitInt(v, ast::SignedIntLit(_, ast::Sign::Minus)), &ty::TyInt(ast::TyIs)) =>
498 Literal::Int { bits: BSize, value: -(v as i64) },
499 (&ast::LitInt(v, _), &ty::TyInt(ast::TyI8)) =>
500 Literal::Int { bits: B8, value: v as i64 },
501 (&ast::LitInt(v, _), &ty::TyInt(ast::TyI16)) =>
502 Literal::Int { bits: B16, value: v as i64 },
503 (&ast::LitInt(v, _), &ty::TyInt(ast::TyI32)) =>
504 Literal::Int { bits: B32, value: v as i64 },
505 (&ast::LitInt(v, _), &ty::TyInt(ast::TyI64)) =>
506 Literal::Int { bits: B64, value: v as i64 },
507 (&ast::LitInt(v, _), &ty::TyInt(ast::TyIs)) =>
508 Literal::Int { bits: BSize, value: v as i64 },
509 (&ast::LitFloat(ref v, _), &ty::TyFloat(ast::TyF32)) |
510 (&ast::LitFloatUnsuffixed(ref v), &ty::TyFloat(ast::TyF32)) =>
511 Literal::Float { bits: FloatBits::F32, value: v.parse::<f64>().unwrap() },
512 (&ast::LitFloat(ref v, _), &ty::TyFloat(ast::TyF64)) |
513 (&ast::LitFloatUnsuffixed(ref v), &ty::TyFloat(ast::TyF64)) =>
514 Literal::Float { bits: FloatBits::F64, value: v.parse::<f64>().unwrap() },
515 (&ast::LitBool(v), _) =>
516 Literal::Bool { value: v },
518 cx.tcx.sess.span_bug(
520 &format!("Invalid literal/type combination: {:?},{:?}", l, t))
524 fn convert_arm<'a,'tcx:'a>(cx: &Cx<'a,'tcx>, arm: &'tcx hir::Arm) -> Arm<Cx<'a,'tcx>> {
525 let map = if arm.pats.len() == 1 {
528 let mut map = FnvHashMap();
529 pat_util::pat_bindings(&cx.tcx.def_map, &arm.pats[0], |_, p_id, _, path| {
530 map.insert(path.node, p_id);
535 Arm { patterns: arm.pats.iter().map(|p| PatNode::new(p, map.clone()).to_ref()).collect(),
536 guard: arm.guard.to_ref(),
537 body: arm.body.to_ref() }
540 fn convert_path_expr<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
541 expr: &'tcx hir::Expr)
542 -> ExprKind<Cx<'a,'tcx>>
544 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(expr.id).substs);
545 match cx.tcx.def_map.borrow()[&expr.id].full_def() {
546 def::DefVariant(_, def_id, false) |
547 def::DefStruct(def_id) |
548 def::DefFn(def_id, _) |
549 def::DefConst(def_id) |
550 def::DefMethod(def_id) |
551 def::DefAssociatedConst(def_id) =>
553 literal: Literal::Item { def_id: def_id, substs: substs }
556 def::DefStatic(node_id, _) =>
557 ExprKind::StaticRef {
561 def @ def::DefLocal(..) |
562 def @ def::DefUpvar(..) =>
563 convert_var(cx, expr, def),
566 cx.tcx.sess.span_bug(
568 &format!("def `{:?}` not yet implemented", def)),
572 fn convert_var<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
573 expr: &'tcx hir::Expr,
575 -> ExprKind<Cx<'a,'tcx>>
577 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
580 def::DefLocal(node_id) => {
586 def::DefUpvar(id_var, index, closure_expr_id) => {
587 debug!("convert_var(upvar({:?}, {:?}, {:?}))", id_var, index, closure_expr_id);
588 let var_ty = cx.tcx.node_id_to_type(id_var);
590 let body_id = match cx.tcx.map.find(closure_expr_id) {
591 Some(map::NodeExpr(expr)) => {
593 hir::ExprClosure(_, _, ref body) => body.id,
595 cx.tcx.sess.span_bug(expr.span,
596 &format!("closure expr is not a closure expr"));
601 cx.tcx.sess.span_bug(expr.span,
602 &format!("ast-map has garbage for closure expr"));
606 // FIXME free regions in closures are not right
608 cx.tcx.node_id_to_type(closure_expr_id);
610 // FIXME we're just hard-coding the idea that the
611 // signature will be &self or &mut self and hence will
612 // have a bound region with number 0
616 scope: cx.tcx.region_maps.node_extent(body_id),
617 bound_region: ty::BoundRegion::BrAnon(0)
620 cx.tcx.mk_region(region);
622 let self_expr = match cx.tcx.closure_kind(DefId::local(closure_expr_id)) {
623 ty::ClosureKind::FnClosureKind => {
625 cx.tcx.mk_ref(region,
626 ty::TypeAndMut { ty: closure_ty,
627 mutbl: hir::MutImmutable });
630 temp_lifetime: temp_lifetime,
632 kind: ExprKind::Deref {
635 temp_lifetime: temp_lifetime,
637 kind: ExprKind::SelfRef
642 ty::ClosureKind::FnMutClosureKind => {
644 cx.tcx.mk_ref(region,
645 ty::TypeAndMut { ty: closure_ty,
646 mutbl: hir::MutMutable });
649 temp_lifetime: temp_lifetime,
651 kind: ExprKind::Deref {
654 temp_lifetime: temp_lifetime,
656 kind: ExprKind::SelfRef
661 ty::ClosureKind::FnOnceClosureKind => {
664 temp_lifetime: temp_lifetime,
666 kind: ExprKind::SelfRef
671 // at this point we have `self.n`, which loads up the upvar
673 ExprKind::Field { lhs: self_expr.to_ref(),
674 name: Field::Indexed(index) };
676 // ...but the upvar might be an `&T` or `&mut T` capture, at which
677 // point we need an implicit deref
678 let upvar_id = ty::UpvarId { var_id: id_var, closure_expr_id: closure_expr_id };
679 let upvar_capture = match cx.tcx.upvar_capture(upvar_id) {
682 cx.tcx.sess.span_bug(
684 &format!("no upvar_capture for {:?}", upvar_id));
687 match upvar_capture {
688 ty::UpvarCapture::ByValue => field_kind,
689 ty::UpvarCapture::ByRef(_) => {
692 temp_lifetime: temp_lifetime,
702 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region")
707 fn bin_op(op: hir::BinOp_) -> BinOp {
709 hir::BinOp_::BiAdd => BinOp::Add,
710 hir::BinOp_::BiSub => BinOp::Sub,
711 hir::BinOp_::BiMul => BinOp::Mul,
712 hir::BinOp_::BiDiv => BinOp::Div,
713 hir::BinOp_::BiRem => BinOp::Rem,
714 hir::BinOp_::BiBitXor => BinOp::BitXor,
715 hir::BinOp_::BiBitAnd => BinOp::BitAnd,
716 hir::BinOp_::BiBitOr => BinOp::BitOr,
717 hir::BinOp_::BiShl => BinOp::Shl,
718 hir::BinOp_::BiShr => BinOp::Shr,
719 hir::BinOp_::BiEq => BinOp::Eq,
720 hir::BinOp_::BiLt => BinOp::Lt,
721 hir::BinOp_::BiLe => BinOp::Le,
722 hir::BinOp_::BiNe => BinOp::Ne,
723 hir::BinOp_::BiGe => BinOp::Ge,
724 hir::BinOp_::BiGt => BinOp::Gt,
725 _ => panic!("no equivalent for ast binop {:?}", op)
734 fn overloaded_operator<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
735 expr: &'tcx hir::Expr,
736 method_call: ty::MethodCall,
738 receiver: ExprRef<Cx<'a,'tcx>>,
739 args: Vec<&'tcx P<hir::Expr>>)
740 -> ExprKind<Cx<'a,'tcx>>
742 // the receiver has all the adjustments that are needed, so we can
743 // just push a reference to it
744 let mut argrefs = vec![receiver];
746 // the arguments, unfortunately, do not, so if this is a ByRef
747 // operator, we have to gin up the autorefs (but by value is easy)
749 PassArgs::ByValue => {
752 .map(|arg| arg.to_ref()))
756 let scope = cx.tcx.region_maps.node_extent(expr.id);
757 let region = cx.tcx.mk_region(ty::ReScope(scope));
758 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
762 let arg_ty = cx.tcx.expr_ty_adjusted(arg);
764 cx.tcx.mk_ref(region,
765 ty::TypeAndMut { ty: arg_ty,
766 mutbl: hir::MutImmutable });
768 temp_lifetime: temp_lifetime,
771 kind: ExprKind::Borrow { region: *region,
772 borrow_kind: BorrowKind::Shared,
779 // now create the call itself
780 let fun = method_callee(cx, expr, method_call);
787 fn overloaded_lvalue<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
788 expr: &'tcx hir::Expr,
789 method_call: ty::MethodCall,
791 receiver: ExprRef<Cx<'a,'tcx>>,
792 args: Vec<&'tcx P<hir::Expr>>)
793 -> ExprKind<Cx<'a,'tcx>>
795 // For an overloaded *x or x[y] expression of type T, the method
796 // call returns an &T and we must add the deref so that the types
797 // line up (this is because `*x` and `x[y]` represent lvalues):
799 // to find the type &T of the content returned by the method;
800 let tables = cx.tcx.tables.borrow();
801 let callee = &tables.method_map[&method_call];
802 let ref_ty = callee.ty.fn_ret();
803 let ref_ty = cx.tcx.no_late_bound_regions(&ref_ty).unwrap().unwrap();
805 // (1) callees always have all late-bound regions fully instantiated,
806 // (2) overloaded methods don't return `!`
808 // construct the complete expression `foo()` for the overloaded call,
809 // which will yield the &T type
810 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
811 let ref_kind = overloaded_operator(cx, expr, method_call, pass_args, receiver, args);
812 let ref_expr = Expr {
813 temp_lifetime: temp_lifetime,
819 // construct and return a deref wrapper `*foo()`
820 ExprKind::Deref { arg: ref_expr.to_ref() }
823 fn capture_freevar<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
824 closure_expr: &'tcx hir::Expr,
825 freevar: &ty::Freevar,
826 freevar_ty: Ty<'tcx>)
827 -> ExprRef<Cx<'a,'tcx>> {
828 let id_var = freevar.def.def_id().node;
829 let upvar_id = ty::UpvarId { var_id: id_var, closure_expr_id: closure_expr.id };
830 let upvar_capture = cx.tcx.upvar_capture(upvar_id).unwrap();
831 let temp_lifetime = cx.tcx.region_maps.temporary_scope(closure_expr.id);
832 let var_ty = cx.tcx.node_id_to_type(id_var);
833 let captured_var = Expr { temp_lifetime: temp_lifetime,
835 span: closure_expr.span,
836 kind: convert_var(cx, closure_expr, freevar.def) };
837 match upvar_capture {
838 ty::UpvarCapture::ByValue => {
839 captured_var.to_ref()
841 ty::UpvarCapture::ByRef(upvar_borrow) => {
842 let borrow_kind = match upvar_borrow.kind {
843 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
844 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
845 ty::BorrowKind::MutBorrow => BorrowKind::Mut,
848 temp_lifetime: temp_lifetime,
850 span: closure_expr.span,
851 kind: ExprKind::Borrow { region: upvar_borrow.region,
852 borrow_kind: borrow_kind,
853 arg: captured_var.to_ref() }
859 fn loop_label<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
860 expr: &'tcx hir::Expr)
863 match cx.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
864 Some(def::DefLabel(loop_id)) => cx.tcx.region_maps.node_extent(loop_id),
866 cx.tcx.sess.span_bug(
868 &format!("loop scope resolved to {:?}", d));