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;
26 use tcx::syntax::codemap::Span;
27 use tcx::syntax::parse::token;
28 use tcx::syntax::ptr::P;
29 use tcx::to_ref::ToRef;
31 impl<'a,'tcx:'a> Mirror<Cx<'a,'tcx>> for &'tcx hir::Expr {
32 type Output = Expr<Cx<'a,'tcx>>;
34 fn make_mirror(self, cx: &mut Cx<'a,'tcx>) -> Expr<Cx<'a,'tcx>> {
35 debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
37 let expr_ty = cx.tcx.expr_ty(self); // note: no adjustments (yet)!
39 let kind = match self.node {
40 // Here comes the interesting stuff:
42 hir::ExprMethodCall(_, _, ref args) => {
43 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
44 let expr = method_callee(cx, self, ty::MethodCall::expr(self.id));
45 let args = args.iter()
54 hir::ExprAddrOf(mutbl, ref expr) => {
55 let region = match expr_ty.sty {
57 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region")
59 ExprKind::Borrow { region: *region,
60 borrow_kind: to_borrow_kind(mutbl),
64 hir::ExprBlock(ref blk) => {
70 hir::ExprAssign(ref lhs, ref rhs) => {
77 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
78 let op = bin_op(op.node);
86 hir::ExprLit(ref lit) => {
87 let literal = convert_literal(cx, self.span, expr_ty, lit);
88 ExprKind::Literal { literal: literal }
91 hir::ExprBinary(op, ref lhs, ref rhs) => {
92 if cx.tcx.is_method_call(self.id) {
93 let pass_args = if hir_util::is_by_value_binop(op.node) {
98 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
99 pass_args, lhs.to_ref(), vec![rhs])
103 hir::BinOp_::BiAnd => {
104 ExprKind::LogicalOp { op: LogicalOp::And,
108 hir::BinOp_::BiOr => {
109 ExprKind::LogicalOp { op: LogicalOp::Or,
114 let op = bin_op(op.node);
115 ExprKind::Binary { op: op,
123 hir::ExprIndex(ref lhs, ref index) => {
124 if cx.tcx.is_method_call(self.id) {
125 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
126 PassArgs::ByValue, lhs.to_ref(), vec![index])
128 ExprKind::Index { lhs: lhs.to_ref(),
129 index: index.to_ref() }
133 hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
134 if cx.tcx.is_method_call(self.id) {
135 overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
136 PassArgs::ByValue, arg.to_ref(), vec![])
138 ExprKind::Deref { arg: arg.to_ref() }
142 hir::ExprUnary(hir::UnOp::UnUniq, ref arg) => {
143 assert!(!cx.tcx.is_method_call(self.id));
144 ExprKind::Box { place: None, value: arg.to_ref() }
147 hir::ExprUnary(op, ref arg) => {
148 if cx.tcx.is_method_call(self.id) {
149 overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
150 PassArgs::ByValue, arg.to_ref(), vec![])
154 hir::UnOp::UnNot => UnOp::Not,
155 hir::UnOp::UnNeg => UnOp::Neg,
156 hir::UnOp::UnUniq | hir::UnOp::UnDeref => {
157 cx.tcx.sess.span_bug(
159 &format!("operator should have been handled elsewhere {:?}", op));
162 ExprKind::Unary { op: op, arg: arg.to_ref() }
166 hir::ExprStruct(_, ref fields, ref base) => {
168 ty::TyStruct(adt, substs) => {
173 fields: fields.to_ref(),
177 ty::TyEnum(adt, substs) => {
178 match cx.tcx.def_map.borrow()[&self.id].full_def() {
179 def::DefVariant(enum_id, variant_id, true) => {
180 debug_assert!(adt.did == enum_id);
181 let index = adt.variant_index_with_id(variant_id);
184 variant_index: index,
186 fields: fields.to_ref(),
191 cx.tcx.sess.span_bug(
193 &format!("unexpected def: {:?}", def));
198 cx.tcx.sess.span_bug(
200 &format!("unexpected type for struct literal: {:?}", expr_ty));
205 hir::ExprClosure(..) => {
206 let closure_ty = cx.tcx.expr_ty(self);
207 let (def_id, substs) = match closure_ty.sty {
208 ty::TyClosure(def_id, ref substs) => (def_id, substs),
210 cx.tcx.sess.span_bug(self.span,
211 &format!("closure expr w/o closure type: {:?}",
215 let upvars = cx.tcx.with_freevars(self.id, |freevars| {
218 .map(|(i, fv)| capture_freevar(cx, self, fv, substs.upvar_tys[i]))
228 hir::ExprRange(ref start, ref end) => {
229 let range_ty = cx.tcx.expr_ty(self);
230 let (adt_def, substs) = match range_ty.sty {
231 ty::TyStruct(adt_def, substs) => (adt_def, substs),
233 cx.tcx.sess.span_bug(
235 &format!("unexpanded ast"));
239 let field_expr_ref = |s: &'tcx P<hir::Expr>, nm: &str| {
240 FieldExprRef { name: Field::Named(token::intern(nm)),
244 let start_field = start.as_ref()
246 .map(|s| field_expr_ref(s, "start"));
248 let end_field = end.as_ref()
250 .map(|e| field_expr_ref(e, "end"));
252 ExprKind::Adt { adt_def: adt_def,
255 fields: start_field.chain(end_field).collect(),
259 hir::ExprPath(..) => {
260 convert_path_expr(cx, self)
263 hir::ExprInlineAsm(ref asm) => {
264 ExprKind::InlineAsm { asm: asm }
267 // Now comes the rote stuff:
269 hir::ExprParen(ref p) =>
270 ExprKind::Paren { arg: p.to_ref() },
271 hir::ExprRepeat(ref v, ref c) =>
272 ExprKind::Repeat { value: v.to_ref(), count: c.to_ref() },
273 hir::ExprRet(ref v) =>
274 ExprKind::Return { value: v.to_ref() },
275 hir::ExprBreak(label) =>
276 ExprKind::Break { label: label.map(|_| loop_label(cx, self)) },
277 hir::ExprAgain(label) =>
278 ExprKind::Continue { label: label.map(|_| loop_label(cx, self)) },
279 hir::ExprMatch(ref discr, ref arms, _) =>
280 ExprKind::Match { discriminant: discr.to_ref(),
281 arms: arms.iter().map(|a| convert_arm(cx, a)).collect() },
282 hir::ExprIf(ref cond, ref then, ref otherwise) =>
283 ExprKind::If { condition: cond.to_ref(),
284 then: block::to_expr_ref(cx, then),
285 otherwise: otherwise.to_ref() },
286 hir::ExprWhile(ref cond, ref body, _) =>
287 ExprKind::Loop { condition: Some(cond.to_ref()),
288 body: block::to_expr_ref(cx, body) },
289 hir::ExprLoop(ref body, _) =>
290 ExprKind::Loop { condition: None,
291 body: block::to_expr_ref(cx, body) },
292 hir::ExprField(ref source, ident) =>
293 ExprKind::Field { lhs: source.to_ref(),
294 name: Field::Named(ident.node.name) },
295 hir::ExprTupField(ref source, ident) =>
296 ExprKind::Field { lhs: source.to_ref(),
297 name: Field::Indexed(ident.node) },
298 hir::ExprCast(ref source, _) =>
299 ExprKind::Cast { source: source.to_ref() },
300 hir::ExprBox(ref place, ref value) =>
301 ExprKind::Box { place: place.to_ref(), value: value.to_ref() },
302 hir::ExprVec(ref fields) =>
303 ExprKind::Vec { fields: fields.to_ref() },
304 hir::ExprTup(ref fields) =>
305 ExprKind::Tuple { fields: fields.to_ref() },
306 hir::ExprCall(ref fun, ref args) =>
307 ExprKind::Call { fun: fun.to_ref(), args: args.to_ref() },
310 let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
311 let expr_extent = cx.tcx.region_maps.node_extent(self.id);
313 let mut expr = Expr {
314 temp_lifetime: temp_lifetime,
320 // Now apply adjustments, if any.
321 match cx.tcx.tables.borrow().adjustments.get(&self.id) {
323 Some(&ty::adjustment::AdjustReifyFnPointer) => {
324 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
326 temp_lifetime: temp_lifetime,
329 kind: ExprKind::ReifyFnPointer { source: expr.to_ref() },
332 Some(&ty::adjustment::AdjustUnsafeFnPointer) => {
333 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
335 temp_lifetime: temp_lifetime,
338 kind: ExprKind::UnsafeFnPointer { source: expr.to_ref() },
341 Some(&ty::adjustment::AdjustDerefRef(ref adj)) => {
342 for i in 0..adj.autoderefs {
345 expr.ty.adjust_for_autoderef(
350 |mc| cx.tcx.tables.borrow().method_map.get(&mc).map(|m| m.ty));
351 let kind = if cx.tcx.is_overloaded_autoderef(self.id, i) {
352 overloaded_lvalue(cx, self, ty::MethodCall::autoderef(self.id, i),
353 PassArgs::ByValue, expr.to_ref(), vec![])
355 ExprKind::Deref { arg: expr.to_ref() }
358 temp_lifetime: temp_lifetime,
365 if let Some(target) = adj.unsize {
367 temp_lifetime: temp_lifetime,
370 kind: ExprKind::Unsize { source: expr.to_ref() }
372 } else if let Some(autoref) = adj.autoref {
373 let adjusted_ty = expr.ty.adjust_for_autoref(cx.tcx, Some(autoref));
375 ty::adjustment::AutoPtr(r, m) => {
377 temp_lifetime: temp_lifetime,
380 kind: ExprKind::Borrow { region: *r,
381 borrow_kind: to_borrow_kind(m),
385 ty::adjustment::AutoUnsafe(m) => {
386 // Convert this to a suitable `&foo` and
387 // then an unsafe coercion. Limit the region to be just this
389 let region = ty::ReScope(expr_extent);
390 let region = cx.tcx.mk_region(region);
392 temp_lifetime: temp_lifetime,
393 ty: cx.tcx.mk_ref(region, ty::TypeAndMut { ty: expr.ty, mutbl: m }),
395 kind: ExprKind::Borrow { region: *region,
396 borrow_kind: to_borrow_kind(m),
400 temp_lifetime: temp_lifetime,
403 kind: ExprKind::Cast { source: expr.to_ref() }
411 // Next, wrap this up in the expr's scope.
413 temp_lifetime: temp_lifetime,
416 kind: ExprKind::Scope { extent: expr_extent,
417 value: expr.to_ref() }
420 // Finally, create a destruction scope, if any.
421 if let Some(extent) = cx.tcx.region_maps.opt_destruction_extent(self.id) {
423 temp_lifetime: temp_lifetime,
426 kind: ExprKind::Scope { extent: extent, value: expr.to_ref() }
435 fn method_callee<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
437 method_call: ty::MethodCall)
438 -> Expr<Cx<'a,'tcx>> {
439 let tables = cx.tcx.tables.borrow();
440 let callee = &tables.method_map[&method_call];
441 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
443 temp_lifetime: temp_lifetime,
446 kind: ExprKind::Literal {
447 literal: Literal::Item {
448 def_id: callee.def_id,
449 substs: callee.substs,
455 fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
457 hir::MutMutable => BorrowKind::Mut,
458 hir::MutImmutable => BorrowKind::Shared,
462 fn convert_literal<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
466 -> Literal<Cx<'a,'tcx>>
468 use repr::IntegralBits::*;
469 match (&literal.node, &expr_ty.sty) {
470 (&hir::LitStr(ref text, _), _) =>
471 Literal::String { value: text.clone() },
472 (&hir::LitByteStr(ref bytes), _) =>
473 Literal::Bytes { value: bytes.clone() },
474 (&hir::LitByte(c), _) =>
475 Literal::Uint { bits: B8, value: c as u64 },
476 (&hir::LitChar(c), _) =>
477 Literal::Char { c: c },
478 (&hir::LitInt(v, _), &ty::TyUint(hir::TyU8)) =>
479 Literal::Uint { bits: B8, value: v },
480 (&hir::LitInt(v, _), &ty::TyUint(hir::TyU16)) =>
481 Literal::Uint { bits: B16, value: v },
482 (&hir::LitInt(v, _), &ty::TyUint(hir::TyU32)) =>
483 Literal::Uint { bits: B32, value: v },
484 (&hir::LitInt(v, _), &ty::TyUint(hir::TyU64)) =>
485 Literal::Uint { bits: B64, value: v },
486 (&hir::LitInt(v, _), &ty::TyUint(hir::TyUs)) =>
487 Literal::Uint { bits: BSize, value: v },
488 (&hir::LitInt(v, hir::SignedIntLit(_, hir::Sign::Minus)), &ty::TyInt(hir::TyI8)) =>
489 Literal::Int { bits: B8, value: -(v as i64) },
490 (&hir::LitInt(v, hir::SignedIntLit(_, hir::Sign::Minus)), &ty::TyInt(hir::TyI16)) =>
491 Literal::Int { bits: B16, value: -(v as i64) },
492 (&hir::LitInt(v, hir::SignedIntLit(_, hir::Sign::Minus)), &ty::TyInt(hir::TyI32)) =>
493 Literal::Int { bits: B32, value: -(v as i64) },
494 (&hir::LitInt(v, hir::SignedIntLit(_, hir::Sign::Minus)), &ty::TyInt(hir::TyI64)) =>
495 Literal::Int { bits: B64, value: -(v as i64) },
496 (&hir::LitInt(v, hir::SignedIntLit(_, hir::Sign::Minus)), &ty::TyInt(hir::TyIs)) =>
497 Literal::Int { bits: BSize, value: -(v as i64) },
498 (&hir::LitInt(v, _), &ty::TyInt(hir::TyI8)) =>
499 Literal::Int { bits: B8, value: v as i64 },
500 (&hir::LitInt(v, _), &ty::TyInt(hir::TyI16)) =>
501 Literal::Int { bits: B16, value: v as i64 },
502 (&hir::LitInt(v, _), &ty::TyInt(hir::TyI32)) =>
503 Literal::Int { bits: B32, value: v as i64 },
504 (&hir::LitInt(v, _), &ty::TyInt(hir::TyI64)) =>
505 Literal::Int { bits: B64, value: v as i64 },
506 (&hir::LitInt(v, _), &ty::TyInt(hir::TyIs)) =>
507 Literal::Int { bits: BSize, value: v as i64 },
508 (&hir::LitFloat(ref v, _), &ty::TyFloat(hir::TyF32)) |
509 (&hir::LitFloatUnsuffixed(ref v), &ty::TyFloat(hir::TyF32)) =>
510 Literal::Float { bits: FloatBits::F32, value: v.parse::<f64>().unwrap() },
511 (&hir::LitFloat(ref v, _), &ty::TyFloat(hir::TyF64)) |
512 (&hir::LitFloatUnsuffixed(ref v), &ty::TyFloat(hir::TyF64)) =>
513 Literal::Float { bits: FloatBits::F64, value: v.parse::<f64>().unwrap() },
514 (&hir::LitBool(v), _) =>
515 Literal::Bool { value: v },
517 cx.tcx.sess.span_bug(
519 &format!("Invalid literal/type combination: {:?},{:?}", l, t))
523 fn convert_arm<'a,'tcx:'a>(cx: &Cx<'a,'tcx>, arm: &'tcx hir::Arm) -> Arm<Cx<'a,'tcx>> {
524 let map = if arm.pats.len() == 1 {
527 let mut map = FnvHashMap();
528 pat_util::pat_bindings(&cx.tcx.def_map, &arm.pats[0], |_, p_id, _, path| {
529 map.insert(path.node, p_id);
534 Arm { patterns: arm.pats.iter().map(|p| PatNode::new(p, map.clone()).to_ref()).collect(),
535 guard: arm.guard.to_ref(),
536 body: arm.body.to_ref() }
539 fn convert_path_expr<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
540 expr: &'tcx hir::Expr)
541 -> ExprKind<Cx<'a,'tcx>>
543 let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(expr.id).substs);
544 match cx.tcx.def_map.borrow()[&expr.id].full_def() {
545 def::DefVariant(_, def_id, false) |
546 def::DefStruct(def_id) |
547 def::DefFn(def_id, _) |
548 def::DefConst(def_id) |
549 def::DefMethod(def_id) |
550 def::DefAssociatedConst(def_id) =>
552 literal: Literal::Item { def_id: def_id, substs: substs }
555 def::DefStatic(node_id, _) =>
556 ExprKind::StaticRef {
560 def @ def::DefLocal(..) |
561 def @ def::DefUpvar(..) =>
562 convert_var(cx, expr, def),
565 cx.tcx.sess.span_bug(
567 &format!("def `{:?}` not yet implemented", def)),
571 fn convert_var<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
572 expr: &'tcx hir::Expr,
574 -> ExprKind<Cx<'a,'tcx>>
576 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
579 def::DefLocal(node_id) => {
585 def::DefUpvar(id_var, index, closure_expr_id) => {
586 debug!("convert_var(upvar({:?}, {:?}, {:?}))", id_var, index, closure_expr_id);
587 let var_ty = cx.tcx.node_id_to_type(id_var);
589 let body_id = match cx.tcx.map.find(closure_expr_id) {
590 Some(map::NodeExpr(expr)) => {
592 hir::ExprClosure(_, _, ref body) => body.id,
594 cx.tcx.sess.span_bug(expr.span,
595 &format!("closure expr is not a closure expr"));
600 cx.tcx.sess.span_bug(expr.span,
601 &format!("ast-map has garbage for closure expr"));
605 // FIXME free regions in closures are not right
607 cx.tcx.node_id_to_type(closure_expr_id);
609 // FIXME we're just hard-coding the idea that the
610 // signature will be &self or &mut self and hence will
611 // have a bound region with number 0
615 scope: cx.tcx.region_maps.node_extent(body_id),
616 bound_region: ty::BoundRegion::BrAnon(0)
619 cx.tcx.mk_region(region);
621 let self_expr = match cx.tcx.closure_kind(DefId::local(closure_expr_id)) {
622 ty::ClosureKind::FnClosureKind => {
624 cx.tcx.mk_ref(region,
625 ty::TypeAndMut { ty: closure_ty,
626 mutbl: hir::MutImmutable });
629 temp_lifetime: temp_lifetime,
631 kind: ExprKind::Deref {
634 temp_lifetime: temp_lifetime,
636 kind: ExprKind::SelfRef
641 ty::ClosureKind::FnMutClosureKind => {
643 cx.tcx.mk_ref(region,
644 ty::TypeAndMut { ty: closure_ty,
645 mutbl: hir::MutMutable });
648 temp_lifetime: temp_lifetime,
650 kind: ExprKind::Deref {
653 temp_lifetime: temp_lifetime,
655 kind: ExprKind::SelfRef
660 ty::ClosureKind::FnOnceClosureKind => {
663 temp_lifetime: temp_lifetime,
665 kind: ExprKind::SelfRef
670 // at this point we have `self.n`, which loads up the upvar
672 ExprKind::Field { lhs: self_expr.to_ref(),
673 name: Field::Indexed(index) };
675 // ...but the upvar might be an `&T` or `&mut T` capture, at which
676 // point we need an implicit deref
677 let upvar_id = ty::UpvarId { var_id: id_var, closure_expr_id: closure_expr_id };
678 let upvar_capture = match cx.tcx.upvar_capture(upvar_id) {
681 cx.tcx.sess.span_bug(
683 &format!("no upvar_capture for {:?}", upvar_id));
686 match upvar_capture {
687 ty::UpvarCapture::ByValue => field_kind,
688 ty::UpvarCapture::ByRef(_) => {
691 temp_lifetime: temp_lifetime,
701 _ => cx.tcx.sess.span_bug(expr.span, "type of & not region")
706 fn bin_op(op: hir::BinOp_) -> BinOp {
708 hir::BinOp_::BiAdd => BinOp::Add,
709 hir::BinOp_::BiSub => BinOp::Sub,
710 hir::BinOp_::BiMul => BinOp::Mul,
711 hir::BinOp_::BiDiv => BinOp::Div,
712 hir::BinOp_::BiRem => BinOp::Rem,
713 hir::BinOp_::BiBitXor => BinOp::BitXor,
714 hir::BinOp_::BiBitAnd => BinOp::BitAnd,
715 hir::BinOp_::BiBitOr => BinOp::BitOr,
716 hir::BinOp_::BiShl => BinOp::Shl,
717 hir::BinOp_::BiShr => BinOp::Shr,
718 hir::BinOp_::BiEq => BinOp::Eq,
719 hir::BinOp_::BiLt => BinOp::Lt,
720 hir::BinOp_::BiLe => BinOp::Le,
721 hir::BinOp_::BiNe => BinOp::Ne,
722 hir::BinOp_::BiGe => BinOp::Ge,
723 hir::BinOp_::BiGt => BinOp::Gt,
724 _ => panic!("no equivalent for ast binop {:?}", op)
733 fn overloaded_operator<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
734 expr: &'tcx hir::Expr,
735 method_call: ty::MethodCall,
737 receiver: ExprRef<Cx<'a,'tcx>>,
738 args: Vec<&'tcx P<hir::Expr>>)
739 -> ExprKind<Cx<'a,'tcx>>
741 // the receiver has all the adjustments that are needed, so we can
742 // just push a reference to it
743 let mut argrefs = vec![receiver];
745 // the arguments, unfortunately, do not, so if this is a ByRef
746 // operator, we have to gin up the autorefs (but by value is easy)
748 PassArgs::ByValue => {
751 .map(|arg| arg.to_ref()))
755 let scope = cx.tcx.region_maps.node_extent(expr.id);
756 let region = cx.tcx.mk_region(ty::ReScope(scope));
757 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
761 let arg_ty = cx.tcx.expr_ty_adjusted(arg);
763 cx.tcx.mk_ref(region,
764 ty::TypeAndMut { ty: arg_ty,
765 mutbl: hir::MutImmutable });
767 temp_lifetime: temp_lifetime,
770 kind: ExprKind::Borrow { region: *region,
771 borrow_kind: BorrowKind::Shared,
778 // now create the call itself
779 let fun = method_callee(cx, expr, method_call);
786 fn overloaded_lvalue<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
787 expr: &'tcx hir::Expr,
788 method_call: ty::MethodCall,
790 receiver: ExprRef<Cx<'a,'tcx>>,
791 args: Vec<&'tcx P<hir::Expr>>)
792 -> ExprKind<Cx<'a,'tcx>>
794 // For an overloaded *x or x[y] expression of type T, the method
795 // call returns an &T and we must add the deref so that the types
796 // line up (this is because `*x` and `x[y]` represent lvalues):
798 // to find the type &T of the content returned by the method;
799 let tables = cx.tcx.tables.borrow();
800 let callee = &tables.method_map[&method_call];
801 let ref_ty = callee.ty.fn_ret();
802 let ref_ty = cx.tcx.no_late_bound_regions(&ref_ty).unwrap().unwrap();
804 // (1) callees always have all late-bound regions fully instantiated,
805 // (2) overloaded methods don't return `!`
807 // construct the complete expression `foo()` for the overloaded call,
808 // which will yield the &T type
809 let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
810 let ref_kind = overloaded_operator(cx, expr, method_call, pass_args, receiver, args);
811 let ref_expr = Expr {
812 temp_lifetime: temp_lifetime,
818 // construct and return a deref wrapper `*foo()`
819 ExprKind::Deref { arg: ref_expr.to_ref() }
822 fn capture_freevar<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
823 closure_expr: &'tcx hir::Expr,
824 freevar: &ty::Freevar,
825 freevar_ty: Ty<'tcx>)
826 -> ExprRef<Cx<'a,'tcx>> {
827 let id_var = freevar.def.def_id().node;
828 let upvar_id = ty::UpvarId { var_id: id_var, closure_expr_id: closure_expr.id };
829 let upvar_capture = cx.tcx.upvar_capture(upvar_id).unwrap();
830 let temp_lifetime = cx.tcx.region_maps.temporary_scope(closure_expr.id);
831 let var_ty = cx.tcx.node_id_to_type(id_var);
832 let captured_var = Expr { temp_lifetime: temp_lifetime,
834 span: closure_expr.span,
835 kind: convert_var(cx, closure_expr, freevar.def) };
836 match upvar_capture {
837 ty::UpvarCapture::ByValue => {
838 captured_var.to_ref()
840 ty::UpvarCapture::ByRef(upvar_borrow) => {
841 let borrow_kind = match upvar_borrow.kind {
842 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
843 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
844 ty::BorrowKind::MutBorrow => BorrowKind::Mut,
847 temp_lifetime: temp_lifetime,
849 span: closure_expr.span,
850 kind: ExprKind::Borrow { region: upvar_borrow.region,
851 borrow_kind: borrow_kind,
852 arg: captured_var.to_ref() }
858 fn loop_label<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
859 expr: &'tcx hir::Expr)
862 match cx.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
863 Some(def::DefLabel(loop_id)) => cx.tcx.region_maps.node_extent(loop_id),
865 cx.tcx.sess.span_bug(
867 &format!("loop scope resolved to {:?}", d));