]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/cx/expr.rs
Auto merge of #54813 - petrochenkov:uilocale, r=alexcrichton
[rust.git] / src / librustc_mir / hair / cx / expr.rs
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.
4 //
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.
10
11 use hair::*;
12 use rustc_data_structures::indexed_vec::Idx;
13 use hair::cx::Cx;
14 use hair::cx::block;
15 use hair::cx::to_ref::ToRef;
16 use rustc::hir::def::{Def, CtorKind};
17 use rustc::mir::interpret::GlobalId;
18 use rustc::ty::{self, AdtKind, Ty};
19 use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability};
20 use rustc::ty::cast::CastKind as TyCastKind;
21 use rustc::hir;
22 use rustc::hir::def_id::LocalDefId;
23 use rustc::mir::{BorrowKind};
24 use syntax_pos::Span;
25
26 impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
27     type Output = Expr<'tcx>;
28
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 {
32             id: self.hir_id.local_id,
33             data: region::ScopeData::Node
34         };
35
36         debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
37
38         let mut expr = make_mirror_unadjusted(cx, self);
39
40         // Now apply adjustments, if any.
41         for adjustment in cx.tables().expr_adjustments(self) {
42             debug!("make_mirror: expr={:?} applying adjustment={:?}",
43                    expr,
44                    adjustment);
45             expr = apply_adjustment(cx, self, expr, adjustment);
46         }
47
48         // Next, wrap this up in the expr's scope.
49         expr = Expr {
50             temp_lifetime,
51             ty: expr.ty,
52             span: self.span,
53             kind: ExprKind::Scope {
54                 region_scope: expr_scope,
55                 value: expr.to_ref(),
56                 lint_level: cx.lint_level_of(self.id),
57             },
58         };
59
60         // Finally, create a destruction scope, if any.
61         if let Some(region_scope) =
62             cx.region_scope_tree.opt_destruction_scope(self.hir_id.local_id) {
63                 expr = Expr {
64                     temp_lifetime,
65                     ty: expr.ty,
66                     span: self.span,
67                     kind: ExprKind::Scope {
68                         region_scope,
69                         value: expr.to_ref(),
70                         lint_level: LintLevel::Inherited,
71                     },
72                 };
73             }
74
75         // OK, all done!
76         expr
77     }
78 }
79
80 fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
81                                     hir_expr: &'tcx hir::Expr,
82                                     mut expr: Expr<'tcx>,
83                                     adjustment: &Adjustment<'tcx>)
84                                     -> Expr<'tcx> {
85     let Expr { temp_lifetime, mut span, .. } = expr;
86     let kind = match adjustment.kind {
87         Adjust::ReifyFnPointer => {
88             ExprKind::ReifyFnPointer { source: expr.to_ref() }
89         }
90         Adjust::UnsafeFnPointer => {
91             ExprKind::UnsafeFnPointer { source: expr.to_ref() }
92         }
93         Adjust::ClosureFnPointer => {
94             ExprKind::ClosureFnPointer { source: expr.to_ref() }
95         }
96         Adjust::NeverToAny => {
97             ExprKind::NeverToAny { source: expr.to_ref() }
98         }
99         Adjust::MutToConstPointer => {
100             ExprKind::Cast { source: expr.to_ref() }
101         }
102         Adjust::Deref(None) => {
103             // Adjust the span from the block, to the last expression of the
104             // block. This is a better span when returning a mutable reference
105             // with too short a lifetime. The error message will use the span
106             // from the assignment to the return place, which should only point
107             // at the returned value, not the entire function body.
108             //
109             // fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
110             //      x
111             //   // ^ error message points at this expression.
112             // }
113             //
114             // We don't need to do this adjustment in the next match arm since
115             // deref coercions always start with a built-in deref.
116             if let ExprKind::Block { body } = expr.kind {
117                 if let Some(ref last_expr) = body.expr {
118                     span = last_expr.span;
119                     expr.span = span;
120                 }
121             }
122             ExprKind::Deref { arg: expr.to_ref() }
123         }
124         Adjust::Deref(Some(deref)) => {
125             let call = deref.method_call(cx.tcx(), expr.ty);
126
127             expr = Expr {
128                 temp_lifetime,
129                 ty: cx.tcx.mk_ref(deref.region,
130                                   ty::TypeAndMut {
131                                     ty: expr.ty,
132                                     mutbl: deref.mutbl,
133                                   }),
134                 span,
135                 kind: ExprKind::Borrow {
136                     region: deref.region,
137                     borrow_kind: deref.mutbl.to_borrow_kind(),
138                     arg: expr.to_ref(),
139                 },
140             };
141
142             overloaded_place(cx, hir_expr, adjustment.target, Some(call), vec![expr.to_ref()])
143         }
144         Adjust::Borrow(AutoBorrow::Ref(r, m)) => {
145             ExprKind::Borrow {
146                 region: r,
147                 borrow_kind: m.to_borrow_kind(),
148                 arg: expr.to_ref(),
149             }
150         }
151         Adjust::Borrow(AutoBorrow::RawPtr(m)) => {
152             // Convert this to a suitable `&foo` and
153             // then an unsafe coercion. Limit the region to be just this
154             // expression.
155             let region = ty::ReScope(region::Scope {
156                 id: hir_expr.hir_id.local_id,
157                 data: region::ScopeData::Node
158             });
159             let region = cx.tcx.mk_region(region);
160             expr = Expr {
161                 temp_lifetime,
162                 ty: cx.tcx.mk_ref(region,
163                                   ty::TypeAndMut {
164                                     ty: expr.ty,
165                                     mutbl: m,
166                                   }),
167                 span,
168                 kind: ExprKind::Borrow {
169                     region,
170                     borrow_kind: m.to_borrow_kind(),
171                     arg: expr.to_ref(),
172                 },
173             };
174             let cast_expr = Expr {
175                 temp_lifetime,
176                 ty: adjustment.target,
177                 span,
178                 kind: ExprKind::Cast { source: expr.to_ref() }
179             };
180
181             // To ensure that both implicit and explicit coercions are
182             // handled the same way, we insert an extra layer of indirection here.
183             // For explicit casts (e.g. 'foo as *const T'), the source of the 'Use'
184             // will be an ExprKind::Hair with the appropriate cast expression. Here,
185             // we make our Use source the generated Cast from the original coercion.
186             //
187             // In both cases, this outer 'Use' ensures that the inner 'Cast' is handled by
188             // as_operand, not by as_rvalue - causing the cast result to be stored in a temporary.
189             // Ordinary, this is identical to using the cast directly as an rvalue. However, if the
190             // source of the cast was previously borrowed as mutable, storing the cast in a
191             // temporary gives the source a chance to expire before the cast is used. For
192             // structs with a self-referential *mut ptr, this allows assignment to work as
193             // expected.
194             //
195             // For example, consider the type 'struct Foo { field: *mut Foo }',
196             // The method 'fn bar(&mut self) { self.field = self }'
197             // triggers a coercion from '&mut self' to '*mut self'. In order
198             // for the assignment to be valid, the implicit borrow
199             // of 'self' involved in the coercion needs to end before the local
200             // containing the '*mut T' is assigned to 'self.field' - otherwise,
201             // we end up trying to assign to 'self.field' while we have another mutable borrow
202             // active.
203             //
204             // We only need to worry about this kind of thing for coercions from refs to ptrs,
205             // since they get rid of a borrow implicitly.
206             ExprKind::Use { source: cast_expr.to_ref() }
207         }
208         Adjust::Unsize => {
209             // See the above comment for Adjust::Deref
210             if let ExprKind::Block { body } = expr.kind {
211                 if let Some(ref last_expr) = body.expr {
212                     span = last_expr.span;
213                     expr.span = span;
214                 }
215             }
216             ExprKind::Unsize { source: expr.to_ref() }
217         }
218     };
219
220     Expr {
221         temp_lifetime,
222         ty: adjustment.target,
223         span,
224         kind,
225     }
226 }
227
228 fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
229                                           expr: &'tcx hir::Expr)
230                                           -> Expr<'tcx> {
231     let expr_ty = cx.tables().expr_ty(expr);
232     let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
233
234     let kind = match expr.node {
235         // Here comes the interesting stuff:
236         hir::ExprKind::MethodCall(_, method_span, ref args) => {
237             // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
238             let expr = method_callee(cx, expr, method_span,None);
239             let args = args.iter()
240                 .map(|e| e.to_ref())
241                 .collect();
242             ExprKind::Call {
243                 ty: expr.ty,
244                 fun: expr.to_ref(),
245                 args,
246                 from_hir_call: true,
247             }
248         }
249
250         hir::ExprKind::Call(ref fun, ref args) => {
251             if cx.tables().is_method_call(expr) {
252                 // The callee is something implementing Fn, FnMut, or FnOnce.
253                 // Find the actual method implementation being called and
254                 // build the appropriate UFCS call expression with the
255                 // callee-object as expr parameter.
256
257                 // rewrite f(u, v) into FnOnce::call_once(f, (u, v))
258
259                 let method = method_callee(cx, expr, fun.span,None);
260
261                 let arg_tys = args.iter().map(|e| cx.tables().expr_ty_adjusted(e));
262                 let tupled_args = Expr {
263                     ty: cx.tcx.mk_tup(arg_tys),
264                     temp_lifetime,
265                     span: expr.span,
266                     kind: ExprKind::Tuple { fields: args.iter().map(ToRef::to_ref).collect() },
267                 };
268
269                 ExprKind::Call {
270                     ty: method.ty,
271                     fun: method.to_ref(),
272                     args: vec![fun.to_ref(), tupled_args.to_ref()],
273                     from_hir_call: true,
274                 }
275             } else {
276                 let adt_data = if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) =
277                     fun.node
278                 {
279                     // Tuple-like ADTs are represented as ExprKind::Call. We convert them here.
280                     expr_ty.ty_adt_def().and_then(|adt_def| {
281                         match path.def {
282                             Def::VariantCtor(variant_id, CtorKind::Fn) => {
283                                 Some((adt_def, adt_def.variant_index_with_id(variant_id)))
284                             }
285                             Def::StructCtor(_, CtorKind::Fn) |
286                             Def::SelfCtor(..) => Some((adt_def, 0)),
287                             _ => None,
288                         }
289                     })
290                 } else {
291                     None
292                 };
293                 if let Some((adt_def, index)) = adt_data {
294                     let substs = cx.tables().node_substs(fun.hir_id);
295
296                     let user_ty = cx.tables().user_substs(fun.hir_id)
297                         .map(|user_substs| {
298                             user_substs.unchecked_map(|user_substs| {
299                                 // Here, we just pair an `AdtDef` with the
300                                 // `user_substs`, so no new types etc are introduced.
301                                 cx.tcx().mk_adt(adt_def, user_substs)
302                             })
303                         });
304
305                     let field_refs = args.iter()
306                         .enumerate()
307                         .map(|(idx, e)| {
308                             FieldExprRef {
309                                 name: Field::new(idx),
310                                 expr: e.to_ref(),
311                             }
312                         })
313                         .collect();
314                     ExprKind::Adt {
315                         adt_def,
316                         substs,
317                         variant_index: index,
318                         fields: field_refs,
319                         user_ty,
320                         base: None,
321                     }
322                 } else {
323                     ExprKind::Call {
324                         ty: cx.tables().node_id_to_type(fun.hir_id),
325                         fun: fun.to_ref(),
326                         args: args.to_ref(),
327                         from_hir_call: true,
328                     }
329                 }
330             }
331         }
332
333         hir::ExprKind::AddrOf(mutbl, ref expr) => {
334             let region = match expr_ty.sty {
335                 ty::Ref(r, _, _) => r,
336                 _ => span_bug!(expr.span, "type of & not region"),
337             };
338             ExprKind::Borrow {
339                 region,
340                 borrow_kind: mutbl.to_borrow_kind(),
341                 arg: expr.to_ref(),
342             }
343         }
344
345         hir::ExprKind::Block(ref blk, _) => ExprKind::Block { body: &blk },
346
347         hir::ExprKind::Assign(ref lhs, ref rhs) => {
348             ExprKind::Assign {
349                 lhs: lhs.to_ref(),
350                 rhs: rhs.to_ref(),
351             }
352         }
353
354         hir::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
355             if cx.tables().is_method_call(expr) {
356                 overloaded_operator(cx, expr, vec![lhs.to_ref(), rhs.to_ref()])
357             } else {
358                 ExprKind::AssignOp {
359                     op: bin_op(op.node),
360                     lhs: lhs.to_ref(),
361                     rhs: rhs.to_ref(),
362                 }
363             }
364         }
365
366         hir::ExprKind::Lit(ref lit) => ExprKind::Literal {
367             literal: cx.const_eval_literal(&lit.node, expr_ty, lit.span, false),
368             user_ty: None,
369         },
370
371         hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
372             if cx.tables().is_method_call(expr) {
373                 overloaded_operator(cx, expr, vec![lhs.to_ref(), rhs.to_ref()])
374             } else {
375                 // FIXME overflow
376                 match (op.node, cx.constness) {
377                     // FIXME(eddyb) use logical ops in constants when
378                     // they can handle that kind of control-flow.
379                     (hir::BinOpKind::And, hir::Constness::Const) => {
380                         ExprKind::Binary {
381                             op: BinOp::BitAnd,
382                             lhs: lhs.to_ref(),
383                             rhs: rhs.to_ref(),
384                         }
385                     }
386                     (hir::BinOpKind::Or, hir::Constness::Const) => {
387                         ExprKind::Binary {
388                             op: BinOp::BitOr,
389                             lhs: lhs.to_ref(),
390                             rhs: rhs.to_ref(),
391                         }
392                     }
393
394                     (hir::BinOpKind::And, hir::Constness::NotConst) => {
395                         ExprKind::LogicalOp {
396                             op: LogicalOp::And,
397                             lhs: lhs.to_ref(),
398                             rhs: rhs.to_ref(),
399                         }
400                     }
401                     (hir::BinOpKind::Or, hir::Constness::NotConst) => {
402                         ExprKind::LogicalOp {
403                             op: LogicalOp::Or,
404                             lhs: lhs.to_ref(),
405                             rhs: rhs.to_ref(),
406                         }
407                     }
408
409                     _ => {
410                         let op = bin_op(op.node);
411                         ExprKind::Binary {
412                             op,
413                             lhs: lhs.to_ref(),
414                             rhs: rhs.to_ref(),
415                         }
416                     }
417                 }
418             }
419         }
420
421         hir::ExprKind::Index(ref lhs, ref index) => {
422             if cx.tables().is_method_call(expr) {
423                 overloaded_place(cx, expr, expr_ty, None, vec![lhs.to_ref(), index.to_ref()])
424             } else {
425                 ExprKind::Index {
426                     lhs: lhs.to_ref(),
427                     index: index.to_ref(),
428                 }
429             }
430         }
431
432         hir::ExprKind::Unary(hir::UnOp::UnDeref, ref arg) => {
433             if cx.tables().is_method_call(expr) {
434                 overloaded_place(cx, expr, expr_ty, None, vec![arg.to_ref()])
435             } else {
436                 ExprKind::Deref { arg: arg.to_ref() }
437             }
438         }
439
440         hir::ExprKind::Unary(hir::UnOp::UnNot, ref arg) => {
441             if cx.tables().is_method_call(expr) {
442                 overloaded_operator(cx, expr, vec![arg.to_ref()])
443             } else {
444                 ExprKind::Unary {
445                     op: UnOp::Not,
446                     arg: arg.to_ref(),
447                 }
448             }
449         }
450
451         hir::ExprKind::Unary(hir::UnOp::UnNeg, ref arg) => {
452             if cx.tables().is_method_call(expr) {
453                 overloaded_operator(cx, expr, vec![arg.to_ref()])
454             } else {
455                 if let hir::ExprKind::Lit(ref lit) = arg.node {
456                     ExprKind::Literal {
457                         literal: cx.const_eval_literal(&lit.node, expr_ty, lit.span, true),
458                         user_ty: None,
459                     }
460                 } else {
461                     ExprKind::Unary {
462                         op: UnOp::Neg,
463                         arg: arg.to_ref(),
464                     }
465                 }
466             }
467         }
468
469         hir::ExprKind::Struct(ref qpath, ref fields, ref base) => {
470             match expr_ty.sty {
471                 ty::Adt(adt, substs) => {
472                     match adt.adt_kind() {
473                         AdtKind::Struct | AdtKind::Union => {
474                             ExprKind::Adt {
475                                 adt_def: adt,
476                                 variant_index: 0,
477                                 substs,
478                                 user_ty: user_annotated_ty_for_adt(cx, expr.hir_id, adt),
479                                 fields: field_refs(cx, fields),
480                                 base: base.as_ref().map(|base| {
481                                     FruInfo {
482                                         base: base.to_ref(),
483                                         field_types: cx.tables()
484                                                        .fru_field_types()[expr.hir_id]
485                                                        .clone(),
486                                     }
487                                 }),
488                             }
489                         }
490                         AdtKind::Enum => {
491                             let def = match *qpath {
492                                 hir::QPath::Resolved(_, ref path) => path.def,
493                                 hir::QPath::TypeRelative(..) => Def::Err,
494                             };
495                             match def {
496                                 Def::Variant(variant_id) => {
497                                     assert!(base.is_none());
498
499                                     let index = adt.variant_index_with_id(variant_id);
500                                     ExprKind::Adt {
501                                         adt_def: adt,
502                                         variant_index: index,
503                                         substs,
504                                         user_ty: user_annotated_ty_for_adt(cx, expr.hir_id, adt),
505                                         fields: field_refs(cx, fields),
506                                         base: None,
507                                     }
508                                 }
509                                 _ => {
510                                     span_bug!(expr.span, "unexpected def: {:?}", def);
511                                 }
512                             }
513                         }
514                     }
515                 }
516                 _ => {
517                     span_bug!(expr.span,
518                               "unexpected type for struct literal: {:?}",
519                               expr_ty);
520                 }
521             }
522         }
523
524         hir::ExprKind::Closure(..) => {
525             let closure_ty = cx.tables().expr_ty(expr);
526             let (def_id, substs, movability) = match closure_ty.sty {
527                 ty::Closure(def_id, substs) => (def_id, UpvarSubsts::Closure(substs), None),
528                 ty::Generator(def_id, substs, movability) => {
529                     (def_id, UpvarSubsts::Generator(substs), Some(movability))
530                 }
531                 _ => {
532                     span_bug!(expr.span, "closure expr w/o closure type: {:?}", closure_ty);
533                 }
534             };
535             let upvars = cx.tcx.with_freevars(expr.id, |freevars| {
536                 freevars.iter()
537                     .zip(substs.upvar_tys(def_id, cx.tcx))
538                     .map(|(fv, ty)| capture_freevar(cx, expr, fv, ty))
539                     .collect()
540             });
541             ExprKind::Closure {
542                 closure_id: def_id,
543                 substs,
544                 upvars,
545                 movability,
546             }
547         }
548
549         hir::ExprKind::Path(ref qpath) => {
550             let def = cx.tables().qpath_def(qpath, expr.hir_id);
551             convert_path_expr(cx, expr, def)
552         }
553
554         hir::ExprKind::InlineAsm(ref asm, ref outputs, ref inputs) => {
555             ExprKind::InlineAsm {
556                 asm,
557                 outputs: outputs.to_ref(),
558                 inputs: inputs.to_ref(),
559             }
560         }
561
562         // Now comes the rote stuff:
563         hir::ExprKind::Repeat(ref v, ref count) => {
564             let def_id = cx.tcx.hir.local_def_id(count.id);
565             let substs = Substs::identity_for_item(cx.tcx.global_tcx(), def_id);
566             let instance = ty::Instance::resolve(
567                 cx.tcx.global_tcx(),
568                 cx.param_env,
569                 def_id,
570                 substs,
571             ).unwrap();
572             let global_id = GlobalId {
573                 instance,
574                 promoted: None
575             };
576             let span = cx.tcx.def_span(def_id);
577             let count = match cx.tcx.at(span).const_eval(cx.param_env.and(global_id)) {
578                 Ok(cv) => cv.unwrap_usize(cx.tcx),
579                 Err(e) => {
580                     e.report_as_error(cx.tcx.at(span), "could not evaluate array length");
581                     0
582                 },
583             };
584
585             ExprKind::Repeat {
586                 value: v.to_ref(),
587                 count,
588             }
589         }
590         hir::ExprKind::Ret(ref v) => ExprKind::Return { value: v.to_ref() },
591         hir::ExprKind::Break(dest, ref value) => {
592             match dest.target_id {
593                 Ok(target_id) => ExprKind::Break {
594                     label: region::Scope {
595                         id: cx.tcx.hir.node_to_hir_id(target_id).local_id,
596                         data: region::ScopeData::Node
597                     },
598                     value: value.to_ref(),
599                 },
600                 Err(err) => bug!("invalid loop id for break: {}", err)
601             }
602         }
603         hir::ExprKind::Continue(dest) => {
604             match dest.target_id {
605                 Ok(loop_id) => ExprKind::Continue {
606                     label: region::Scope {
607                         id: cx.tcx.hir.node_to_hir_id(loop_id).local_id,
608                         data: region::ScopeData::Node
609                     },
610                 },
611                 Err(err) => bug!("invalid loop id for continue: {}", err)
612             }
613         }
614         hir::ExprKind::Match(ref discr, ref arms, _) => {
615             ExprKind::Match {
616                 discriminant: discr.to_ref(),
617                 arms: arms.iter().map(|a| convert_arm(cx, a)).collect(),
618             }
619         }
620         hir::ExprKind::If(ref cond, ref then, ref otherwise) => {
621             ExprKind::If {
622                 condition: cond.to_ref(),
623                 then: then.to_ref(),
624                 otherwise: otherwise.to_ref(),
625             }
626         }
627         hir::ExprKind::While(ref cond, ref body, _) => {
628             ExprKind::Loop {
629                 condition: Some(cond.to_ref()),
630                 body: block::to_expr_ref(cx, body),
631             }
632         }
633         hir::ExprKind::Loop(ref body, _, _) => {
634             ExprKind::Loop {
635                 condition: None,
636                 body: block::to_expr_ref(cx, body),
637             }
638         }
639         hir::ExprKind::Field(ref source, ..) => {
640             ExprKind::Field {
641                 lhs: source.to_ref(),
642                 name: Field::new(cx.tcx.field_index(expr.id, cx.tables)),
643             }
644         }
645         hir::ExprKind::Cast(ref source, _) => {
646             // Check to see if this cast is a "coercion cast", where the cast is actually done
647             // using a coercion (or is a no-op).
648             if let Some(&TyCastKind::CoercionCast) = cx.tables()
649                                                     .cast_kinds()
650                                                     .get(source.hir_id) {
651                 // Convert the lexpr to a vexpr.
652                 ExprKind::Use { source: source.to_ref() }
653             } else {
654                 // check whether this is casting an enum variant discriminant
655                 // to prevent cycles, we refer to the discriminant initializer
656                 // which is always an integer and thus doesn't need to know the
657                 // enum's layout (or its tag type) to compute it during const eval
658                 // Example:
659                 // enum Foo {
660                 //     A,
661                 //     B = A as isize + 4,
662                 // }
663                 // The correct solution would be to add symbolic computations to miri,
664                 // so we wouldn't have to compute and store the actual value
665                 let var = if let hir::ExprKind::Path(ref qpath) = source.node {
666                     let def = cx.tables().qpath_def(qpath, source.hir_id);
667                     cx
668                         .tables()
669                         .node_id_to_type(source.hir_id)
670                         .ty_adt_def()
671                         .and_then(|adt_def| {
672                         match def {
673                             Def::VariantCtor(variant_id, CtorKind::Const) => {
674                                 let idx = adt_def.variant_index_with_id(variant_id);
675                                 let (d, o) = adt_def.discriminant_def_for_variant(idx);
676                                 use rustc::ty::util::IntTypeExt;
677                                 let ty = adt_def.repr.discr_type();
678                                 let ty = ty.to_ty(cx.tcx());
679                                 Some((d, o, ty))
680                             }
681                             _ => None,
682                         }
683                     })
684                 } else {
685                     None
686                 };
687                 let source = if let Some((did, offset, ty)) = var {
688                     let mk_const = |literal| Expr {
689                         temp_lifetime,
690                         ty,
691                         span: expr.span,
692                         kind: ExprKind::Literal { literal, user_ty: None },
693                     }.to_ref();
694                     let offset = mk_const(ty::Const::from_bits(
695                         cx.tcx,
696                         offset as u128,
697                         cx.param_env.and(ty),
698                     ));
699                     match did {
700                         Some(did) => {
701                             // in case we are offsetting from a computed discriminant
702                             // and not the beginning of discriminants (which is always `0`)
703                             let substs = Substs::identity_for_item(cx.tcx(), did);
704                             let lhs = mk_const(ty::Const::unevaluated(cx.tcx(), did, substs, ty));
705                             let bin = ExprKind::Binary {
706                                 op: BinOp::Add,
707                                 lhs,
708                                 rhs: offset,
709                             };
710                             Expr {
711                                 temp_lifetime,
712                                 ty,
713                                 span: expr.span,
714                                 kind: bin,
715                             }.to_ref()
716                         },
717                         None => offset,
718                     }
719                 } else {
720                     source.to_ref()
721                 };
722                 ExprKind::Cast { source }
723             }
724         }
725         hir::ExprKind::Type(ref source, ref ty) => {
726             let user_provided_tys = cx.tables.user_provided_tys();
727             let user_ty = *user_provided_tys
728                 .get(ty.hir_id)
729                 .expect(&format!("{:?} not found in user_provided_tys, source: {:?}", ty, source));
730             if source.is_place_expr() {
731                 ExprKind::PlaceTypeAscription {
732                     source: source.to_ref(),
733                     user_ty,
734                 }
735             } else {
736                 ExprKind::ValueTypeAscription {
737                     source: source.to_ref(),
738                     user_ty,
739                 }
740             }
741         }
742         hir::ExprKind::Box(ref value) => {
743             ExprKind::Box {
744                 value: value.to_ref(),
745             }
746         }
747         hir::ExprKind::Array(ref fields) => ExprKind::Array { fields: fields.to_ref() },
748         hir::ExprKind::Tup(ref fields) => ExprKind::Tuple { fields: fields.to_ref() },
749
750         hir::ExprKind::Yield(ref v) => ExprKind::Yield { value: v.to_ref() },
751     };
752
753     Expr {
754         temp_lifetime,
755         ty: expr_ty,
756         span: expr.span,
757         kind,
758     }
759 }
760
761 fn user_annotated_ty_for_def(
762     cx: &mut Cx<'a, 'gcx, 'tcx>,
763     hir_id: hir::HirId,
764     def: &Def,
765 ) -> Option<CanonicalTy<'tcx>> {
766     match def {
767         // A reference to something callable -- e.g., a fn, method, or
768         // a tuple-struct or tuple-variant. This has the type of a
769         // `Fn` but with the user-given substitutions.
770         Def::Fn(_) |
771         Def::Method(_) |
772         Def::StructCtor(_, CtorKind::Fn) |
773         Def::VariantCtor(_, CtorKind::Fn) =>
774             Some(cx.tables().user_substs(hir_id)?.unchecked_map(|user_substs| {
775                 // Here, we just pair a `DefId` with the
776                 // `user_substs`, so no new types etc are introduced.
777                 cx.tcx().mk_fn_def(def.def_id(), user_substs)
778             })),
779
780         Def::Const(_def_id) |
781         Def::AssociatedConst(_def_id) =>
782             bug!("unimplemented"),
783
784         // A unit struct/variant which is used as a value (e.g.,
785         // `None`). This has the type of the enum/struct that defines
786         // this variant -- but with the substitutions given by the
787         // user.
788         Def::StructCtor(_def_id, CtorKind::Const) |
789         Def::VariantCtor(_def_id, CtorKind::Const) =>
790             match &cx.tables().node_id_to_type(hir_id).sty {
791                 ty::Adt(adt_def, _) => user_annotated_ty_for_adt(cx, hir_id, adt_def),
792                 sty => bug!("unexpected sty: {:?}", sty),
793             },
794
795         // `Self` is used in expression as a tuple struct constructor or an unit struct constructor
796         Def::SelfCtor(_) => {
797             let sty = &cx.tables().node_id_to_type(hir_id).sty;
798             match sty {
799                 ty::FnDef(ref def_id, _) => {
800                     Some(cx.tables().user_substs(hir_id)?.unchecked_map(|user_substs| {
801                         // Here, we just pair a `DefId` with the
802                         // `user_substs`, so no new types etc are introduced.
803                         cx.tcx().mk_fn_def(*def_id, user_substs)
804                     }))
805                 }
806                 ty::Adt(ref adt_def, _) => {
807                     user_annotated_ty_for_adt(cx, hir_id, adt_def)
808                 }
809                 _ => {
810                     bug!("unexpected sty: {:?}", sty)
811                 }
812             }
813         }
814         _ =>
815             bug!("user_annotated_ty_for_def: unexpected def {:?} at {:?}", def, hir_id)
816     }
817 }
818
819 fn user_annotated_ty_for_adt(
820     cx: &mut Cx<'a, 'gcx, 'tcx>,
821     hir_id: hir::HirId,
822     adt_def: &'tcx AdtDef,
823 ) -> Option<CanonicalTy<'tcx>> {
824     let user_substs = cx.tables().user_substs(hir_id)?;
825     Some(user_substs.unchecked_map(|user_substs| {
826         // Here, we just pair an `AdtDef` with the
827         // `user_substs`, so no new types etc are introduced.
828         cx.tcx().mk_adt(adt_def, user_substs)
829     }))
830 }
831
832 fn method_callee<'a, 'gcx, 'tcx>(
833     cx: &mut Cx<'a, 'gcx, 'tcx>,
834     expr: &hir::Expr,
835     span: Span,
836     overloaded_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
837 ) -> Expr<'tcx> {
838     let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
839     let (def_id, substs, user_ty) = match overloaded_callee {
840         Some((def_id, substs)) => (def_id, substs, None),
841         None => {
842             let type_dependent_defs = cx.tables().type_dependent_defs();
843             let def = type_dependent_defs
844                 .get(expr.hir_id)
845                 .unwrap_or_else(|| {
846                     span_bug!(expr.span, "no type-dependent def for method callee")
847                 });
848             let user_ty = user_annotated_ty_for_def(cx, expr.hir_id, def);
849             (def.def_id(), cx.tables().node_substs(expr.hir_id), user_ty)
850         }
851     };
852     let ty = cx.tcx().mk_fn_def(def_id, substs);
853     Expr {
854         temp_lifetime,
855         ty,
856         span,
857         kind: ExprKind::Literal {
858             literal: ty::Const::zero_sized(cx.tcx(), ty),
859             user_ty,
860         },
861     }
862 }
863
864 trait ToBorrowKind { fn to_borrow_kind(&self) -> BorrowKind; }
865
866 impl ToBorrowKind for AutoBorrowMutability {
867     fn to_borrow_kind(&self) -> BorrowKind {
868         use rustc::ty::adjustment::AllowTwoPhase;
869         match *self {
870             AutoBorrowMutability::Mutable { allow_two_phase_borrow } =>
871                 BorrowKind::Mut { allow_two_phase_borrow: match allow_two_phase_borrow {
872                     AllowTwoPhase::Yes => true,
873                     AllowTwoPhase::No => false
874                 }},
875             AutoBorrowMutability::Immutable =>
876                 BorrowKind::Shared,
877         }
878     }
879 }
880
881 impl ToBorrowKind for hir::Mutability {
882     fn to_borrow_kind(&self) -> BorrowKind {
883         match *self {
884             hir::MutMutable => BorrowKind::Mut { allow_two_phase_borrow: false },
885             hir::MutImmutable => BorrowKind::Shared,
886         }
887     }
888 }
889
890 fn convert_arm<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, arm: &'tcx hir::Arm) -> Arm<'tcx> {
891     Arm {
892         patterns: arm.pats.iter().map(|p| cx.pattern_from_hir(p)).collect(),
893         guard: match arm.guard {
894                 Some(hir::Guard::If(ref e)) => Some(Guard::If(e.to_ref())),
895                 _ => None,
896             },
897         body: arm.body.to_ref(),
898         // BUG: fix this
899         lint_level: LintLevel::Inherited,
900     }
901 }
902
903 fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
904                                      expr: &'tcx hir::Expr,
905                                      def: Def)
906                                      -> ExprKind<'tcx> {
907     let substs = cx.tables().node_substs(expr.hir_id);
908     match def {
909         // A regular function, constructor function or a constant.
910         Def::Fn(_) |
911         Def::Method(_) |
912         Def::StructCtor(_, CtorKind::Fn) |
913         Def::VariantCtor(_, CtorKind::Fn) |
914         Def::SelfCtor(..) => {
915             let user_ty = user_annotated_ty_for_def(cx, expr.hir_id, &def);
916             ExprKind::Literal {
917                 literal: ty::Const::zero_sized(
918                     cx.tcx,
919                     cx.tables().node_id_to_type(expr.hir_id),
920                 ),
921                 user_ty,
922             }
923         },
924
925         Def::Const(def_id) |
926         Def::AssociatedConst(def_id) => ExprKind::Literal {
927             literal: ty::Const::unevaluated(
928                 cx.tcx,
929                 def_id,
930                 substs,
931                 cx.tables().node_id_to_type(expr.hir_id),
932             ),
933             user_ty: None, // FIXME(#47184) -- user given type annot on constants
934         },
935
936         Def::StructCtor(def_id, CtorKind::Const) |
937         Def::VariantCtor(def_id, CtorKind::Const) => {
938             match cx.tables().node_id_to_type(expr.hir_id).sty {
939                 // A unit struct/variant which is used as a value.
940                 // We return a completely different ExprKind here to account for this special case.
941                 ty::Adt(adt_def, substs) => {
942                     ExprKind::Adt {
943                         adt_def,
944                         variant_index: adt_def.variant_index_with_id(def_id),
945                         substs,
946                         user_ty: user_annotated_ty_for_adt(cx, expr.hir_id, adt_def),
947                         fields: vec![],
948                         base: None,
949                     }
950                 }
951                 ref sty => bug!("unexpected sty: {:?}", sty),
952             }
953         }
954
955         Def::Static(node_id, _) => ExprKind::StaticRef { id: node_id },
956
957         Def::Local(..) | Def::Upvar(..) => convert_var(cx, expr, def),
958
959         _ => span_bug!(expr.span, "def `{:?}` not yet implemented", def),
960     }
961 }
962
963 fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
964                                expr: &'tcx hir::Expr,
965                                def: Def)
966                                -> ExprKind<'tcx> {
967     let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
968
969     match def {
970         Def::Local(id) => ExprKind::VarRef { id },
971
972         Def::Upvar(var_id, index, closure_expr_id) => {
973             debug!("convert_var(upvar({:?}, {:?}, {:?}))",
974                    var_id,
975                    index,
976                    closure_expr_id);
977             let var_hir_id = cx.tcx.hir.node_to_hir_id(var_id);
978             let var_ty = cx.tables().node_id_to_type(var_hir_id);
979
980             // FIXME free regions in closures are not right
981             let closure_ty = cx.tables()
982                                .node_id_to_type(cx.tcx.hir.node_to_hir_id(closure_expr_id));
983
984             // FIXME we're just hard-coding the idea that the
985             // signature will be &self or &mut self and hence will
986             // have a bound region with number 0
987             let closure_def_id = cx.tcx.hir.local_def_id(closure_expr_id);
988             let region = ty::ReFree(ty::FreeRegion {
989                 scope: closure_def_id,
990                 bound_region: ty::BoundRegion::BrAnon(0),
991             });
992             let region = cx.tcx.mk_region(region);
993
994             let self_expr = if let ty::Closure(_, closure_substs) = closure_ty.sty {
995                 match cx.infcx.closure_kind(closure_def_id, closure_substs).unwrap() {
996                     ty::ClosureKind::Fn => {
997                         let ref_closure_ty = cx.tcx.mk_ref(region,
998                                                            ty::TypeAndMut {
999                                                                ty: closure_ty,
1000                                                                mutbl: hir::MutImmutable,
1001                                                            });
1002                         Expr {
1003                             ty: closure_ty,
1004                             temp_lifetime: temp_lifetime,
1005                             span: expr.span,
1006                             kind: ExprKind::Deref {
1007                                 arg: Expr {
1008                                     ty: ref_closure_ty,
1009                                     temp_lifetime,
1010                                     span: expr.span,
1011                                     kind: ExprKind::SelfRef,
1012                                 }
1013                                 .to_ref(),
1014                             },
1015                         }
1016                     }
1017                     ty::ClosureKind::FnMut => {
1018                         let ref_closure_ty = cx.tcx.mk_ref(region,
1019                                                            ty::TypeAndMut {
1020                                                                ty: closure_ty,
1021                                                                mutbl: hir::MutMutable,
1022                                                            });
1023                         Expr {
1024                             ty: closure_ty,
1025                             temp_lifetime,
1026                             span: expr.span,
1027                             kind: ExprKind::Deref {
1028                                 arg: Expr {
1029                                     ty: ref_closure_ty,
1030                                     temp_lifetime,
1031                                     span: expr.span,
1032                                     kind: ExprKind::SelfRef,
1033                                 }.to_ref(),
1034                             },
1035                         }
1036                     }
1037                     ty::ClosureKind::FnOnce => {
1038                         Expr {
1039                             ty: closure_ty,
1040                             temp_lifetime,
1041                             span: expr.span,
1042                             kind: ExprKind::SelfRef,
1043                         }
1044                     }
1045                 }
1046             } else {
1047                 Expr {
1048                     ty: closure_ty,
1049                     temp_lifetime,
1050                     span: expr.span,
1051                     kind: ExprKind::SelfRef,
1052                 }
1053             };
1054
1055             // at this point we have `self.n`, which loads up the upvar
1056             let field_kind = ExprKind::Field {
1057                 lhs: self_expr.to_ref(),
1058                 name: Field::new(index),
1059             };
1060
1061             // ...but the upvar might be an `&T` or `&mut T` capture, at which
1062             // point we need an implicit deref
1063             let upvar_id = ty::UpvarId {
1064                 var_id: var_hir_id,
1065                 closure_expr_id: LocalDefId::from_def_id(closure_def_id),
1066             };
1067             match cx.tables().upvar_capture(upvar_id) {
1068                 ty::UpvarCapture::ByValue => field_kind,
1069                 ty::UpvarCapture::ByRef(borrow) => {
1070                     ExprKind::Deref {
1071                         arg: Expr {
1072                             temp_lifetime,
1073                             ty: cx.tcx.mk_ref(borrow.region,
1074                                               ty::TypeAndMut {
1075                                                   ty: var_ty,
1076                                                   mutbl: borrow.kind.to_mutbl_lossy(),
1077                                               }),
1078                             span: expr.span,
1079                             kind: field_kind,
1080                         }.to_ref(),
1081                     }
1082                 }
1083             }
1084         }
1085
1086         _ => span_bug!(expr.span, "type of & not region"),
1087     }
1088 }
1089
1090
1091 fn bin_op(op: hir::BinOpKind) -> BinOp {
1092     match op {
1093         hir::BinOpKind::Add => BinOp::Add,
1094         hir::BinOpKind::Sub => BinOp::Sub,
1095         hir::BinOpKind::Mul => BinOp::Mul,
1096         hir::BinOpKind::Div => BinOp::Div,
1097         hir::BinOpKind::Rem => BinOp::Rem,
1098         hir::BinOpKind::BitXor => BinOp::BitXor,
1099         hir::BinOpKind::BitAnd => BinOp::BitAnd,
1100         hir::BinOpKind::BitOr => BinOp::BitOr,
1101         hir::BinOpKind::Shl => BinOp::Shl,
1102         hir::BinOpKind::Shr => BinOp::Shr,
1103         hir::BinOpKind::Eq => BinOp::Eq,
1104         hir::BinOpKind::Lt => BinOp::Lt,
1105         hir::BinOpKind::Le => BinOp::Le,
1106         hir::BinOpKind::Ne => BinOp::Ne,
1107         hir::BinOpKind::Ge => BinOp::Ge,
1108         hir::BinOpKind::Gt => BinOp::Gt,
1109         _ => bug!("no equivalent for ast binop {:?}", op),
1110     }
1111 }
1112
1113 fn overloaded_operator<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
1114                                        expr: &'tcx hir::Expr,
1115                                        args: Vec<ExprRef<'tcx>>)
1116                                        -> ExprKind<'tcx> {
1117     let fun = method_callee(cx, expr, expr.span, None);
1118     ExprKind::Call {
1119         ty: fun.ty,
1120         fun: fun.to_ref(),
1121         args,
1122         from_hir_call: false,
1123     }
1124 }
1125
1126 fn overloaded_place<'a, 'gcx, 'tcx>(
1127     cx: &mut Cx<'a, 'gcx, 'tcx>,
1128     expr: &'tcx hir::Expr,
1129     place_ty: Ty<'tcx>,
1130     overloaded_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
1131     args: Vec<ExprRef<'tcx>>,
1132 ) -> ExprKind<'tcx> {
1133     // For an overloaded *x or x[y] expression of type T, the method
1134     // call returns an &T and we must add the deref so that the types
1135     // line up (this is because `*x` and `x[y]` represent places):
1136
1137     let recv_ty = match args[0] {
1138         ExprRef::Hair(e) => cx.tables().expr_ty_adjusted(e),
1139         ExprRef::Mirror(ref e) => e.ty
1140     };
1141
1142     // Reconstruct the output assuming it's a reference with the
1143     // same region and mutability as the receiver. This holds for
1144     // `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`.
1145     let (region, mutbl) = match recv_ty.sty {
1146         ty::Ref(region, _, mutbl) => (region, mutbl),
1147         _ => span_bug!(expr.span, "overloaded_place: receiver is not a reference"),
1148     };
1149     let ref_ty = cx.tcx.mk_ref(region, ty::TypeAndMut {
1150         ty: place_ty,
1151         mutbl,
1152     });
1153
1154     // construct the complete expression `foo()` for the overloaded call,
1155     // which will yield the &T type
1156     let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
1157     let fun = method_callee(cx, expr, expr.span, overloaded_callee);
1158     let ref_expr = Expr {
1159         temp_lifetime,
1160         ty: ref_ty,
1161         span: expr.span,
1162         kind: ExprKind::Call {
1163             ty: fun.ty,
1164             fun: fun.to_ref(),
1165             args,
1166             from_hir_call: false,
1167         },
1168     };
1169
1170     // construct and return a deref wrapper `*foo()`
1171     ExprKind::Deref { arg: ref_expr.to_ref() }
1172 }
1173
1174 fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
1175                                    closure_expr: &'tcx hir::Expr,
1176                                    freevar: &hir::Freevar,
1177                                    freevar_ty: Ty<'tcx>)
1178                                    -> ExprRef<'tcx> {
1179     let var_hir_id = cx.tcx.hir.node_to_hir_id(freevar.var_id());
1180     let upvar_id = ty::UpvarId {
1181         var_id: var_hir_id,
1182         closure_expr_id: cx.tcx.hir.local_def_id(closure_expr.id).to_local(),
1183     };
1184     let upvar_capture = cx.tables().upvar_capture(upvar_id);
1185     let temp_lifetime = cx.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
1186     let var_ty = cx.tables().node_id_to_type(var_hir_id);
1187     let captured_var = Expr {
1188         temp_lifetime,
1189         ty: var_ty,
1190         span: closure_expr.span,
1191         kind: convert_var(cx, closure_expr, freevar.def),
1192     };
1193     match upvar_capture {
1194         ty::UpvarCapture::ByValue => captured_var.to_ref(),
1195         ty::UpvarCapture::ByRef(upvar_borrow) => {
1196             let borrow_kind = match upvar_borrow.kind {
1197                 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
1198                 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
1199                 ty::BorrowKind::MutBorrow => BorrowKind::Mut { allow_two_phase_borrow: false }
1200             };
1201             Expr {
1202                 temp_lifetime,
1203                 ty: freevar_ty,
1204                 span: closure_expr.span,
1205                 kind: ExprKind::Borrow {
1206                     region: upvar_borrow.region,
1207                     borrow_kind,
1208                     arg: captured_var.to_ref(),
1209                 },
1210             }.to_ref()
1211         }
1212     }
1213 }
1214
1215 /// Converts a list of named fields (i.e. for struct-like struct/enum ADTs) into FieldExprRef.
1216 fn field_refs<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
1217                               fields: &'tcx [hir::Field])
1218                               -> Vec<FieldExprRef<'tcx>> {
1219     fields.iter()
1220         .map(|field| {
1221             FieldExprRef {
1222                 name: Field::new(cx.tcx.field_index(field.id, cx.tables)),
1223                 expr: field.expr.to_ref(),
1224             }
1225         })
1226         .collect()
1227 }