]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/thir/cx/expr.rs
Auto merge of #94515 - estebank:tweak-move-error, r=davidtwco
[rust.git] / compiler / rustc_mir_build / src / thir / cx / expr.rs
1 use crate::thir::cx::Cx;
2 use crate::thir::util::UserAnnotatedTyHelpers;
3 use rustc_data_structures::stack::ensure_sufficient_stack;
4 use rustc_hir as hir;
5 use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
6 use rustc_index::vec::Idx;
7 use rustc_middle::hir::place::Place as HirPlace;
8 use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
9 use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
10 use rustc_middle::middle::region;
11 use rustc_middle::mir::{BinOp, BorrowKind, Field, UnOp};
12 use rustc_middle::thir::*;
13 use rustc_middle::ty::adjustment::{
14     Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, PointerCast,
15 };
16 use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
17 use rustc_middle::ty::{self, AdtKind, Ty, UpvarSubsts, UserType};
18 use rustc_span::def_id::DefId;
19 use rustc_span::Span;
20 use rustc_target::abi::VariantIdx;
21
22 impl<'tcx> Cx<'tcx> {
23     crate fn mirror_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) -> ExprId {
24         // `mirror_expr` is recursing very deep. Make sure the stack doesn't overflow.
25         ensure_sufficient_stack(|| self.mirror_expr_inner(expr))
26     }
27
28     crate fn mirror_exprs(&mut self, exprs: &'tcx [hir::Expr<'tcx>]) -> Box<[ExprId]> {
29         exprs.iter().map(|expr| self.mirror_expr_inner(expr)).collect()
30     }
31
32     pub(super) fn mirror_expr_inner(&mut self, hir_expr: &'tcx hir::Expr<'tcx>) -> ExprId {
33         let temp_lifetime = self.region_scope_tree.temporary_scope(hir_expr.hir_id.local_id);
34         let expr_scope =
35             region::Scope { id: hir_expr.hir_id.local_id, data: region::ScopeData::Node };
36
37         debug!("Expr::make_mirror(): id={}, span={:?}", hir_expr.hir_id, hir_expr.span);
38
39         let mut expr = self.make_mirror_unadjusted(hir_expr);
40
41         let adjustment_span = match self.adjustment_span {
42             Some((hir_id, span)) if hir_id == hir_expr.hir_id => Some(span),
43             _ => None,
44         };
45
46         // Now apply adjustments, if any.
47         for adjustment in self.typeck_results.expr_adjustments(hir_expr) {
48             debug!("make_mirror: expr={:?} applying adjustment={:?}", expr, adjustment);
49             let span = expr.span;
50             expr =
51                 self.apply_adjustment(hir_expr, expr, adjustment, adjustment_span.unwrap_or(span));
52         }
53
54         // Next, wrap this up in the expr's scope.
55         expr = Expr {
56             temp_lifetime,
57             ty: expr.ty,
58             span: hir_expr.span,
59             kind: ExprKind::Scope {
60                 region_scope: expr_scope,
61                 value: self.thir.exprs.push(expr),
62                 lint_level: LintLevel::Explicit(hir_expr.hir_id),
63             },
64         };
65
66         // Finally, create a destruction scope, if any.
67         if let Some(region_scope) =
68             self.region_scope_tree.opt_destruction_scope(hir_expr.hir_id.local_id)
69         {
70             expr = Expr {
71                 temp_lifetime,
72                 ty: expr.ty,
73                 span: hir_expr.span,
74                 kind: ExprKind::Scope {
75                     region_scope,
76                     value: self.thir.exprs.push(expr),
77                     lint_level: LintLevel::Inherited,
78                 },
79             };
80         }
81
82         // OK, all done!
83         self.thir.exprs.push(expr)
84     }
85
86     fn apply_adjustment(
87         &mut self,
88         hir_expr: &'tcx hir::Expr<'tcx>,
89         mut expr: Expr<'tcx>,
90         adjustment: &Adjustment<'tcx>,
91         mut span: Span,
92     ) -> Expr<'tcx> {
93         let Expr { temp_lifetime, .. } = expr;
94
95         // Adjust the span from the block, to the last expression of the
96         // block. This is a better span when returning a mutable reference
97         // with too short a lifetime. The error message will use the span
98         // from the assignment to the return place, which should only point
99         // at the returned value, not the entire function body.
100         //
101         // fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
102         //      x
103         //   // ^ error message points at this expression.
104         // }
105         let mut adjust_span = |expr: &mut Expr<'tcx>| {
106             if let ExprKind::Block { body } = &expr.kind {
107                 if let Some(last_expr) = body.expr {
108                     span = self.thir[last_expr].span;
109                     expr.span = span;
110                 }
111             }
112         };
113
114         let kind = match adjustment.kind {
115             Adjust::Pointer(PointerCast::Unsize) => {
116                 adjust_span(&mut expr);
117                 ExprKind::Pointer { cast: PointerCast::Unsize, source: self.thir.exprs.push(expr) }
118             }
119             Adjust::Pointer(cast) => ExprKind::Pointer { cast, source: self.thir.exprs.push(expr) },
120             Adjust::NeverToAny => ExprKind::NeverToAny { source: self.thir.exprs.push(expr) },
121             Adjust::Deref(None) => {
122                 adjust_span(&mut expr);
123                 ExprKind::Deref { arg: self.thir.exprs.push(expr) }
124             }
125             Adjust::Deref(Some(deref)) => {
126                 // We don't need to do call adjust_span here since
127                 // deref coercions always start with a built-in deref.
128                 let call = deref.method_call(self.tcx(), expr.ty);
129
130                 expr = Expr {
131                     temp_lifetime,
132                     ty: self
133                         .tcx
134                         .mk_ref(deref.region, ty::TypeAndMut { ty: expr.ty, mutbl: deref.mutbl }),
135                     span,
136                     kind: ExprKind::Borrow {
137                         borrow_kind: deref.mutbl.to_borrow_kind(),
138                         arg: self.thir.exprs.push(expr),
139                     },
140                 };
141
142                 let expr = Box::new([self.thir.exprs.push(expr)]);
143
144                 self.overloaded_place(hir_expr, adjustment.target, Some(call), expr, deref.span)
145             }
146             Adjust::Borrow(AutoBorrow::Ref(_, m)) => ExprKind::Borrow {
147                 borrow_kind: m.to_borrow_kind(),
148                 arg: self.thir.exprs.push(expr),
149             },
150             Adjust::Borrow(AutoBorrow::RawPtr(mutability)) => {
151                 ExprKind::AddressOf { mutability, arg: self.thir.exprs.push(expr) }
152             }
153         };
154
155         Expr { temp_lifetime, ty: adjustment.target, span, kind }
156     }
157
158     fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
159         let expr_ty = self.typeck_results().expr_ty(expr);
160         let expr_span = expr.span;
161         let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
162
163         let kind = match expr.kind {
164             // Here comes the interesting stuff:
165             hir::ExprKind::MethodCall(segment, ref args, fn_span) => {
166                 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
167                 let expr = self.method_callee(expr, segment.ident.span, None);
168                 // When we apply adjustments to the receiver, use the span of
169                 // the overall method call for better diagnostics. args[0]
170                 // is guaranteed to exist, since a method call always has a receiver.
171                 let old_adjustment_span = self.adjustment_span.replace((args[0].hir_id, expr_span));
172                 tracing::info!("Using method span: {:?}", expr.span);
173                 let args = self.mirror_exprs(args);
174                 self.adjustment_span = old_adjustment_span;
175                 ExprKind::Call {
176                     ty: expr.ty,
177                     fun: self.thir.exprs.push(expr),
178                     args,
179                     from_hir_call: true,
180                     fn_span,
181                 }
182             }
183
184             hir::ExprKind::Call(ref fun, ref args) => {
185                 if self.typeck_results().is_method_call(expr) {
186                     // The callee is something implementing Fn, FnMut, or FnOnce.
187                     // Find the actual method implementation being called and
188                     // build the appropriate UFCS call expression with the
189                     // callee-object as expr parameter.
190
191                     // rewrite f(u, v) into FnOnce::call_once(f, (u, v))
192
193                     let method = self.method_callee(expr, fun.span, None);
194
195                     let arg_tys = args.iter().map(|e| self.typeck_results().expr_ty_adjusted(e));
196                     let tupled_args = Expr {
197                         ty: self.tcx.mk_tup(arg_tys),
198                         temp_lifetime,
199                         span: expr.span,
200                         kind: ExprKind::Tuple { fields: self.mirror_exprs(args) },
201                     };
202                     let tupled_args = self.thir.exprs.push(tupled_args);
203
204                     ExprKind::Call {
205                         ty: method.ty,
206                         fun: self.thir.exprs.push(method),
207                         args: Box::new([self.mirror_expr(fun), tupled_args]),
208                         from_hir_call: true,
209                         fn_span: expr.span,
210                     }
211                 } else {
212                     let adt_data =
213                         if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = fun.kind {
214                             // Tuple-like ADTs are represented as ExprKind::Call. We convert them here.
215                             expr_ty.ty_adt_def().and_then(|adt_def| match path.res {
216                                 Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_id) => {
217                                     Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
218                                 }
219                                 Res::SelfCtor(..) => Some((adt_def, VariantIdx::new(0))),
220                                 _ => None,
221                             })
222                         } else {
223                             None
224                         };
225                     if let Some((adt_def, index)) = adt_data {
226                         let substs = self.typeck_results().node_substs(fun.hir_id);
227                         let user_provided_types = self.typeck_results().user_provided_types();
228                         let user_ty =
229                             user_provided_types.get(fun.hir_id).copied().map(|mut u_ty| {
230                                 if let UserType::TypeOf(ref mut did, _) = &mut u_ty.value {
231                                     *did = adt_def.did;
232                                 }
233                                 u_ty
234                             });
235                         debug!("make_mirror_unadjusted: (call) user_ty={:?}", user_ty);
236
237                         let field_refs = args
238                             .iter()
239                             .enumerate()
240                             .map(|(idx, e)| FieldExpr {
241                                 name: Field::new(idx),
242                                 expr: self.mirror_expr(e),
243                             })
244                             .collect();
245                         ExprKind::Adt(Box::new(Adt {
246                             adt_def,
247                             substs,
248                             variant_index: index,
249                             fields: field_refs,
250                             user_ty,
251                             base: None,
252                         }))
253                     } else {
254                         ExprKind::Call {
255                             ty: self.typeck_results().node_type(fun.hir_id),
256                             fun: self.mirror_expr(fun),
257                             args: self.mirror_exprs(args),
258                             from_hir_call: true,
259                             fn_span: expr.span,
260                         }
261                     }
262                 }
263             }
264
265             hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mutbl, ref arg) => {
266                 ExprKind::Borrow { borrow_kind: mutbl.to_borrow_kind(), arg: self.mirror_expr(arg) }
267             }
268
269             hir::ExprKind::AddrOf(hir::BorrowKind::Raw, mutability, ref arg) => {
270                 ExprKind::AddressOf { mutability, arg: self.mirror_expr(arg) }
271             }
272
273             hir::ExprKind::Block(ref blk, _) => ExprKind::Block { body: self.mirror_block(blk) },
274
275             hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
276                 ExprKind::Assign { lhs: self.mirror_expr(lhs), rhs: self.mirror_expr(rhs) }
277             }
278
279             hir::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
280                 if self.typeck_results().is_method_call(expr) {
281                     let lhs = self.mirror_expr(lhs);
282                     let rhs = self.mirror_expr(rhs);
283                     self.overloaded_operator(expr, Box::new([lhs, rhs]))
284                 } else {
285                     ExprKind::AssignOp {
286                         op: bin_op(op.node),
287                         lhs: self.mirror_expr(lhs),
288                         rhs: self.mirror_expr(rhs),
289                     }
290                 }
291             }
292
293             hir::ExprKind::Lit(ref lit) => ExprKind::Literal {
294                 literal: self.const_eval_literal(&lit.node, expr_ty, lit.span, false),
295                 user_ty: None,
296                 const_id: None,
297             },
298
299             hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
300                 if self.typeck_results().is_method_call(expr) {
301                     let lhs = self.mirror_expr(lhs);
302                     let rhs = self.mirror_expr(rhs);
303                     self.overloaded_operator(expr, Box::new([lhs, rhs]))
304                 } else {
305                     // FIXME overflow
306                     match op.node {
307                         hir::BinOpKind::And => ExprKind::LogicalOp {
308                             op: LogicalOp::And,
309                             lhs: self.mirror_expr(lhs),
310                             rhs: self.mirror_expr(rhs),
311                         },
312                         hir::BinOpKind::Or => ExprKind::LogicalOp {
313                             op: LogicalOp::Or,
314                             lhs: self.mirror_expr(lhs),
315                             rhs: self.mirror_expr(rhs),
316                         },
317                         _ => {
318                             let op = bin_op(op.node);
319                             ExprKind::Binary {
320                                 op,
321                                 lhs: self.mirror_expr(lhs),
322                                 rhs: self.mirror_expr(rhs),
323                             }
324                         }
325                     }
326                 }
327             }
328
329             hir::ExprKind::Index(ref lhs, ref index) => {
330                 if self.typeck_results().is_method_call(expr) {
331                     let lhs = self.mirror_expr(lhs);
332                     let index = self.mirror_expr(index);
333                     self.overloaded_place(expr, expr_ty, None, Box::new([lhs, index]), expr.span)
334                 } else {
335                     ExprKind::Index { lhs: self.mirror_expr(lhs), index: self.mirror_expr(index) }
336                 }
337             }
338
339             hir::ExprKind::Unary(hir::UnOp::Deref, ref arg) => {
340                 if self.typeck_results().is_method_call(expr) {
341                     let arg = self.mirror_expr(arg);
342                     self.overloaded_place(expr, expr_ty, None, Box::new([arg]), expr.span)
343                 } else {
344                     ExprKind::Deref { arg: self.mirror_expr(arg) }
345                 }
346             }
347
348             hir::ExprKind::Unary(hir::UnOp::Not, ref arg) => {
349                 if self.typeck_results().is_method_call(expr) {
350                     let arg = self.mirror_expr(arg);
351                     self.overloaded_operator(expr, Box::new([arg]))
352                 } else {
353                     ExprKind::Unary { op: UnOp::Not, arg: self.mirror_expr(arg) }
354                 }
355             }
356
357             hir::ExprKind::Unary(hir::UnOp::Neg, ref arg) => {
358                 if self.typeck_results().is_method_call(expr) {
359                     let arg = self.mirror_expr(arg);
360                     self.overloaded_operator(expr, Box::new([arg]))
361                 } else if let hir::ExprKind::Lit(ref lit) = arg.kind {
362                     ExprKind::Literal {
363                         literal: self.const_eval_literal(&lit.node, expr_ty, lit.span, true),
364                         user_ty: None,
365                         const_id: None,
366                     }
367                 } else {
368                     ExprKind::Unary { op: UnOp::Neg, arg: self.mirror_expr(arg) }
369                 }
370             }
371
372             hir::ExprKind::Struct(ref qpath, ref fields, ref base) => match expr_ty.kind() {
373                 ty::Adt(adt, substs) => match adt.adt_kind() {
374                     AdtKind::Struct | AdtKind::Union => {
375                         let user_provided_types = self.typeck_results().user_provided_types();
376                         let user_ty = user_provided_types.get(expr.hir_id).copied();
377                         debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty);
378                         ExprKind::Adt(Box::new(Adt {
379                             adt_def: adt,
380                             variant_index: VariantIdx::new(0),
381                             substs,
382                             user_ty,
383                             fields: self.field_refs(fields),
384                             base: base.as_ref().map(|base| FruInfo {
385                                 base: self.mirror_expr(base),
386                                 field_types: self.typeck_results().fru_field_types()[expr.hir_id]
387                                     .iter()
388                                     .copied()
389                                     .collect(),
390                             }),
391                         }))
392                     }
393                     AdtKind::Enum => {
394                         let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
395                         match res {
396                             Res::Def(DefKind::Variant, variant_id) => {
397                                 assert!(base.is_none());
398
399                                 let index = adt.variant_index_with_id(variant_id);
400                                 let user_provided_types =
401                                     self.typeck_results().user_provided_types();
402                                 let user_ty = user_provided_types.get(expr.hir_id).copied();
403                                 debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty);
404                                 ExprKind::Adt(Box::new(Adt {
405                                     adt_def: adt,
406                                     variant_index: index,
407                                     substs,
408                                     user_ty,
409                                     fields: self.field_refs(fields),
410                                     base: None,
411                                 }))
412                             }
413                             _ => {
414                                 span_bug!(expr.span, "unexpected res: {:?}", res);
415                             }
416                         }
417                     }
418                 },
419                 _ => {
420                     span_bug!(expr.span, "unexpected type for struct literal: {:?}", expr_ty);
421                 }
422             },
423
424             hir::ExprKind::Closure(..) => {
425                 let closure_ty = self.typeck_results().expr_ty(expr);
426                 let (def_id, substs, movability) = match *closure_ty.kind() {
427                     ty::Closure(def_id, substs) => (def_id, UpvarSubsts::Closure(substs), None),
428                     ty::Generator(def_id, substs, movability) => {
429                         (def_id, UpvarSubsts::Generator(substs), Some(movability))
430                     }
431                     _ => {
432                         span_bug!(expr.span, "closure expr w/o closure type: {:?}", closure_ty);
433                     }
434                 };
435
436                 let upvars = self
437                     .typeck_results
438                     .closure_min_captures_flattened(def_id)
439                     .zip(substs.upvar_tys())
440                     .map(|(captured_place, ty)| {
441                         let upvars = self.capture_upvar(expr, captured_place, ty);
442                         self.thir.exprs.push(upvars)
443                     })
444                     .collect();
445
446                 // Convert the closure fake reads, if any, from hir `Place` to ExprRef
447                 let fake_reads = match self.typeck_results.closure_fake_reads.get(&def_id) {
448                     Some(fake_reads) => fake_reads
449                         .iter()
450                         .map(|(place, cause, hir_id)| {
451                             let expr = self.convert_captured_hir_place(expr, place.clone());
452                             (self.thir.exprs.push(expr), *cause, *hir_id)
453                         })
454                         .collect(),
455                     None => Vec::new(),
456                 };
457
458                 ExprKind::Closure { closure_id: def_id, substs, upvars, movability, fake_reads }
459             }
460
461             hir::ExprKind::Path(ref qpath) => {
462                 let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
463                 self.convert_path_expr(expr, res)
464             }
465
466             hir::ExprKind::InlineAsm(ref asm) => ExprKind::InlineAsm {
467                 template: asm.template,
468                 operands: asm
469                     .operands
470                     .iter()
471                     .map(|(op, _op_sp)| {
472                         match *op {
473                             hir::InlineAsmOperand::In { reg, ref expr } => {
474                                 InlineAsmOperand::In { reg, expr: self.mirror_expr(expr) }
475                             }
476                             hir::InlineAsmOperand::Out { reg, late, ref expr } => {
477                                 InlineAsmOperand::Out {
478                                     reg,
479                                     late,
480                                     expr: expr.as_ref().map(|expr| self.mirror_expr(expr)),
481                                 }
482                             }
483                             hir::InlineAsmOperand::InOut { reg, late, ref expr } => {
484                                 InlineAsmOperand::InOut { reg, late, expr: self.mirror_expr(expr) }
485                             }
486                             hir::InlineAsmOperand::SplitInOut {
487                                 reg,
488                                 late,
489                                 ref in_expr,
490                                 ref out_expr,
491                             } => InlineAsmOperand::SplitInOut {
492                                 reg,
493                                 late,
494                                 in_expr: self.mirror_expr(in_expr),
495                                 out_expr: out_expr.as_ref().map(|expr| self.mirror_expr(expr)),
496                             },
497                             hir::InlineAsmOperand::Const { ref anon_const } => {
498                                 let anon_const_def_id =
499                                     self.tcx.hir().local_def_id(anon_const.hir_id);
500                                 let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
501                                 let span = self.tcx.hir().span(anon_const.hir_id);
502
503                                 InlineAsmOperand::Const { value, span }
504                             }
505                             hir::InlineAsmOperand::Sym { ref expr } => {
506                                 let hir::ExprKind::Path(ref qpath) = expr.kind else {
507                                     span_bug!(
508                                         expr.span,
509                                         "asm `sym` operand should be a path, found {:?}",
510                                         expr.kind
511                                     );
512                                 };
513                                 let temp_lifetime =
514                                     self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
515                                 let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
516                                 let ty;
517                                 match res {
518                                     Res::Def(DefKind::Fn, _) | Res::Def(DefKind::AssocFn, _) => {
519                                         ty = self.typeck_results().node_type(expr.hir_id);
520                                         let user_ty =
521                                             self.user_substs_applied_to_res(expr.hir_id, res);
522                                         InlineAsmOperand::SymFn {
523                                             expr: self.thir.exprs.push(Expr {
524                                                 ty,
525                                                 temp_lifetime,
526                                                 span: expr.span,
527                                                 kind: ExprKind::Literal {
528                                                     literal: ty::Const::zero_sized(self.tcx, ty),
529                                                     user_ty,
530                                                     const_id: None,
531                                                 },
532                                             }),
533                                         }
534                                     }
535
536                                     Res::Def(DefKind::Static, def_id) => {
537                                         InlineAsmOperand::SymStatic { def_id }
538                                     }
539
540                                     _ => {
541                                         self.tcx.sess.span_err(
542                                             expr.span,
543                                             "asm `sym` operand must point to a fn or static",
544                                         );
545
546                                         // Not a real fn, but we're not reaching codegen anyways...
547                                         ty = self.tcx.ty_error();
548                                         InlineAsmOperand::SymFn {
549                                             expr: self.thir.exprs.push(Expr {
550                                                 ty,
551                                                 temp_lifetime,
552                                                 span: expr.span,
553                                                 kind: ExprKind::Literal {
554                                                     literal: ty::Const::zero_sized(self.tcx, ty),
555                                                     user_ty: None,
556                                                     const_id: None,
557                                                 },
558                                             }),
559                                         }
560                                     }
561                                 }
562                             }
563                         }
564                     })
565                     .collect(),
566                 options: asm.options,
567                 line_spans: asm.line_spans,
568             },
569
570             hir::ExprKind::ConstBlock(ref anon_const) => {
571                 let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
572                 let value = ty::Const::from_inline_const(self.tcx, anon_const_def_id);
573
574                 ExprKind::ConstBlock { value }
575             }
576             // Now comes the rote stuff:
577             hir::ExprKind::Repeat(ref v, _) => {
578                 let ty = self.typeck_results().expr_ty(expr);
579                 let ty::Array(_, count) = ty.kind() else {
580                     span_bug!(expr.span, "unexpected repeat expr ty: {:?}", ty);
581                 };
582
583                 ExprKind::Repeat { value: self.mirror_expr(v), count: *count }
584             }
585             hir::ExprKind::Ret(ref v) => {
586                 ExprKind::Return { value: v.as_ref().map(|v| self.mirror_expr(v)) }
587             }
588             hir::ExprKind::Break(dest, ref value) => match dest.target_id {
589                 Ok(target_id) => ExprKind::Break {
590                     label: region::Scope { id: target_id.local_id, data: region::ScopeData::Node },
591                     value: value.as_ref().map(|value| self.mirror_expr(value)),
592                 },
593                 Err(err) => bug!("invalid loop id for break: {}", err),
594             },
595             hir::ExprKind::Continue(dest) => match dest.target_id {
596                 Ok(loop_id) => ExprKind::Continue {
597                     label: region::Scope { id: loop_id.local_id, data: region::ScopeData::Node },
598                 },
599                 Err(err) => bug!("invalid loop id for continue: {}", err),
600             },
601             hir::ExprKind::Let(let_expr) => ExprKind::Let {
602                 expr: self.mirror_expr(let_expr.init),
603                 pat: self.pattern_from_hir(let_expr.pat),
604             },
605             hir::ExprKind::If(cond, then, else_opt) => ExprKind::If {
606                 if_then_scope: region::Scope {
607                     id: then.hir_id.local_id,
608                     data: region::ScopeData::IfThen,
609                 },
610                 cond: self.mirror_expr(cond),
611                 then: self.mirror_expr(then),
612                 else_opt: else_opt.map(|el| self.mirror_expr(el)),
613             },
614             hir::ExprKind::Match(ref discr, ref arms, _) => ExprKind::Match {
615                 scrutinee: self.mirror_expr(discr),
616                 arms: arms.iter().map(|a| self.convert_arm(a)).collect(),
617             },
618             hir::ExprKind::Loop(ref body, ..) => {
619                 let block_ty = self.typeck_results().node_type(body.hir_id);
620                 let temp_lifetime = self.region_scope_tree.temporary_scope(body.hir_id.local_id);
621                 let block = self.mirror_block(body);
622                 let body = self.thir.exprs.push(Expr {
623                     ty: block_ty,
624                     temp_lifetime,
625                     span: block.span,
626                     kind: ExprKind::Block { body: block },
627                 });
628                 ExprKind::Loop { body }
629             }
630             hir::ExprKind::Field(ref source, ..) => ExprKind::Field {
631                 lhs: self.mirror_expr(source),
632                 name: Field::new(self.tcx.field_index(expr.hir_id, self.typeck_results)),
633             },
634             hir::ExprKind::Cast(ref source, ref cast_ty) => {
635                 // Check for a user-given type annotation on this `cast`
636                 let user_provided_types = self.typeck_results.user_provided_types();
637                 let user_ty = user_provided_types.get(cast_ty.hir_id);
638
639                 debug!(
640                     "cast({:?}) has ty w/ hir_id {:?} and user provided ty {:?}",
641                     expr, cast_ty.hir_id, user_ty,
642                 );
643
644                 // Check to see if this cast is a "coercion cast", where the cast is actually done
645                 // using a coercion (or is a no-op).
646                 let cast = if self.typeck_results().is_coercion_cast(source.hir_id) {
647                     // Convert the lexpr to a vexpr.
648                     ExprKind::Use { source: self.mirror_expr(source) }
649                 } else if self.typeck_results().expr_ty(source).is_region_ptr() {
650                     // Special cased so that we can type check that the element
651                     // type of the source matches the pointed to type of the
652                     // destination.
653                     ExprKind::Pointer {
654                         source: self.mirror_expr(source),
655                         cast: PointerCast::ArrayToPointer,
656                     }
657                 } else {
658                     // check whether this is casting an enum variant discriminant
659                     // to prevent cycles, we refer to the discriminant initializer
660                     // which is always an integer and thus doesn't need to know the
661                     // enum's layout (or its tag type) to compute it during const eval
662                     // Example:
663                     // enum Foo {
664                     //     A,
665                     //     B = A as isize + 4,
666                     // }
667                     // The correct solution would be to add symbolic computations to miri,
668                     // so we wouldn't have to compute and store the actual value
669                     let var = if let hir::ExprKind::Path(ref qpath) = source.kind {
670                         let res = self.typeck_results().qpath_res(qpath, source.hir_id);
671                         self.typeck_results().node_type(source.hir_id).ty_adt_def().and_then(
672                             |adt_def| match res {
673                                 Res::Def(
674                                     DefKind::Ctor(CtorOf::Variant, CtorKind::Const),
675                                     variant_ctor_id,
676                                 ) => {
677                                     let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id);
678                                     let (d, o) = adt_def.discriminant_def_for_variant(idx);
679                                     use rustc_middle::ty::util::IntTypeExt;
680                                     let ty = adt_def.repr.discr_type();
681                                     let ty = ty.to_ty(self.tcx());
682                                     Some((d, o, ty))
683                                 }
684                                 _ => None,
685                             },
686                         )
687                     } else {
688                         None
689                     };
690
691                     let source = if let Some((did, offset, var_ty)) = var {
692                         let mk_const = |literal| Expr {
693                             temp_lifetime,
694                             ty: var_ty,
695                             span: expr.span,
696                             kind: ExprKind::Literal { literal, user_ty: None, const_id: None },
697                         };
698                         let offset = self.thir.exprs.push(mk_const(ty::Const::from_bits(
699                             self.tcx,
700                             offset as u128,
701                             self.param_env.and(var_ty),
702                         )));
703                         match did {
704                             Some(did) => {
705                                 // in case we are offsetting from a computed discriminant
706                                 // and not the beginning of discriminants (which is always `0`)
707                                 let substs = InternalSubsts::identity_for_item(self.tcx(), did);
708                                 let lhs = ty::ConstS {
709                                     val: ty::ConstKind::Unevaluated(ty::Unevaluated::new(
710                                         ty::WithOptConstParam::unknown(did),
711                                         substs,
712                                     )),
713                                     ty: var_ty,
714                                 };
715                                 let lhs = self.thir.exprs.push(mk_const(self.tcx().mk_const(lhs)));
716                                 let bin =
717                                     ExprKind::Binary { op: BinOp::Add, lhs: lhs, rhs: offset };
718                                 self.thir.exprs.push(Expr {
719                                     temp_lifetime,
720                                     ty: var_ty,
721                                     span: expr.span,
722                                     kind: bin,
723                                 })
724                             }
725                             None => offset,
726                         }
727                     } else {
728                         self.mirror_expr(source)
729                     };
730
731                     ExprKind::Cast { source: source }
732                 };
733
734                 if let Some(user_ty) = user_ty {
735                     // NOTE: Creating a new Expr and wrapping a Cast inside of it may be
736                     //       inefficient, revisit this when performance becomes an issue.
737                     let cast_expr = self.thir.exprs.push(Expr {
738                         temp_lifetime,
739                         ty: expr_ty,
740                         span: expr.span,
741                         kind: cast,
742                     });
743                     debug!("make_mirror_unadjusted: (cast) user_ty={:?}", user_ty);
744
745                     ExprKind::ValueTypeAscription { source: cast_expr, user_ty: Some(*user_ty) }
746                 } else {
747                     cast
748                 }
749             }
750             hir::ExprKind::Type(ref source, ref ty) => {
751                 let user_provided_types = self.typeck_results.user_provided_types();
752                 let user_ty = user_provided_types.get(ty.hir_id).copied();
753                 debug!("make_mirror_unadjusted: (type) user_ty={:?}", user_ty);
754                 let mirrored = self.mirror_expr(source);
755                 if source.is_syntactic_place_expr() {
756                     ExprKind::PlaceTypeAscription { source: mirrored, user_ty }
757                 } else {
758                     ExprKind::ValueTypeAscription { source: mirrored, user_ty }
759                 }
760             }
761             hir::ExprKind::DropTemps(ref source) => {
762                 ExprKind::Use { source: self.mirror_expr(source) }
763             }
764             hir::ExprKind::Box(ref value) => ExprKind::Box { value: self.mirror_expr(value) },
765             hir::ExprKind::Array(ref fields) => {
766                 ExprKind::Array { fields: self.mirror_exprs(fields) }
767             }
768             hir::ExprKind::Tup(ref fields) => ExprKind::Tuple { fields: self.mirror_exprs(fields) },
769
770             hir::ExprKind::Yield(ref v, _) => ExprKind::Yield { value: self.mirror_expr(v) },
771             hir::ExprKind::Err => unreachable!(),
772         };
773
774         Expr { temp_lifetime, ty: expr_ty, span: expr.span, kind }
775     }
776
777     fn user_substs_applied_to_res(
778         &mut self,
779         hir_id: hir::HirId,
780         res: Res,
781     ) -> Option<ty::CanonicalUserType<'tcx>> {
782         debug!("user_substs_applied_to_res: res={:?}", res);
783         let user_provided_type = match res {
784             // A reference to something callable -- e.g., a fn, method, or
785             // a tuple-struct or tuple-variant. This has the type of a
786             // `Fn` but with the user-given substitutions.
787             Res::Def(DefKind::Fn, _)
788             | Res::Def(DefKind::AssocFn, _)
789             | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
790             | Res::Def(DefKind::Const, _)
791             | Res::Def(DefKind::AssocConst, _) => {
792                 self.typeck_results().user_provided_types().get(hir_id).copied()
793             }
794
795             // A unit struct/variant which is used as a value (e.g.,
796             // `None`). This has the type of the enum/struct that defines
797             // this variant -- but with the substitutions given by the
798             // user.
799             Res::Def(DefKind::Ctor(_, CtorKind::Const), _) => {
800                 self.user_substs_applied_to_ty_of_hir_id(hir_id)
801             }
802
803             // `Self` is used in expression as a tuple struct constructor or a unit struct constructor
804             Res::SelfCtor(_) => self.user_substs_applied_to_ty_of_hir_id(hir_id),
805
806             _ => bug!("user_substs_applied_to_res: unexpected res {:?} at {:?}", res, hir_id),
807         };
808         debug!("user_substs_applied_to_res: user_provided_type={:?}", user_provided_type);
809         user_provided_type
810     }
811
812     fn method_callee(
813         &mut self,
814         expr: &hir::Expr<'_>,
815         span: Span,
816         overloaded_callee: Option<(DefId, SubstsRef<'tcx>)>,
817     ) -> Expr<'tcx> {
818         let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
819         let (def_id, substs, user_ty) = match overloaded_callee {
820             Some((def_id, substs)) => (def_id, substs, None),
821             None => {
822                 let (kind, def_id) =
823                     self.typeck_results().type_dependent_def(expr.hir_id).unwrap_or_else(|| {
824                         span_bug!(expr.span, "no type-dependent def for method callee")
825                     });
826                 let user_ty = self.user_substs_applied_to_res(expr.hir_id, Res::Def(kind, def_id));
827                 debug!("method_callee: user_ty={:?}", user_ty);
828                 (def_id, self.typeck_results().node_substs(expr.hir_id), user_ty)
829             }
830         };
831         let ty = self.tcx().mk_fn_def(def_id, substs);
832         Expr {
833             temp_lifetime,
834             ty,
835             span,
836             kind: ExprKind::Literal {
837                 literal: ty::Const::zero_sized(self.tcx(), ty),
838                 user_ty,
839                 const_id: None,
840             },
841         }
842     }
843
844     fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
845         let arm = Arm {
846             pattern: self.pattern_from_hir(&arm.pat),
847             guard: arm.guard.as_ref().map(|g| match g {
848                 hir::Guard::If(ref e) => Guard::If(self.mirror_expr(e)),
849                 hir::Guard::IfLet(ref pat, ref e) => {
850                     Guard::IfLet(self.pattern_from_hir(pat), self.mirror_expr(e))
851                 }
852             }),
853             body: self.mirror_expr(arm.body),
854             lint_level: LintLevel::Explicit(arm.hir_id),
855             scope: region::Scope { id: arm.hir_id.local_id, data: region::ScopeData::Node },
856             span: arm.span,
857         };
858         self.thir.arms.push(arm)
859     }
860
861     fn convert_path_expr(&mut self, expr: &'tcx hir::Expr<'tcx>, res: Res) -> ExprKind<'tcx> {
862         let substs = self.typeck_results().node_substs(expr.hir_id);
863         match res {
864             // A regular function, constructor function or a constant.
865             Res::Def(DefKind::Fn, _)
866             | Res::Def(DefKind::AssocFn, _)
867             | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
868             | Res::SelfCtor(..) => {
869                 let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
870                 debug!("convert_path_expr: user_ty={:?}", user_ty);
871                 ExprKind::Literal {
872                     literal: ty::Const::zero_sized(
873                         self.tcx,
874                         self.typeck_results().node_type(expr.hir_id),
875                     ),
876                     user_ty,
877                     const_id: None,
878                 }
879             }
880
881             Res::Def(DefKind::ConstParam, def_id) => {
882                 let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
883                 let item_id = self.tcx.hir().get_parent_node(hir_id);
884                 let item_def_id = self.tcx.hir().local_def_id(item_id);
885                 let generics = self.tcx.generics_of(item_def_id);
886                 let index = generics.param_def_id_to_index[&def_id];
887                 let name = self.tcx.hir().name(hir_id);
888                 let val = ty::ConstKind::Param(ty::ParamConst::new(index, name));
889                 ExprKind::Literal {
890                     literal: self.tcx.mk_const(ty::ConstS {
891                         val,
892                         ty: self.typeck_results().node_type(expr.hir_id),
893                     }),
894                     user_ty: None,
895                     const_id: Some(def_id),
896                 }
897             }
898
899             Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
900                 let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
901                 debug!("convert_path_expr: (const) user_ty={:?}", user_ty);
902                 ExprKind::Literal {
903                     literal: self.tcx.mk_const(ty::ConstS {
904                         val: ty::ConstKind::Unevaluated(ty::Unevaluated::new(
905                             ty::WithOptConstParam::unknown(def_id),
906                             substs,
907                         )),
908                         ty: self.typeck_results().node_type(expr.hir_id),
909                     }),
910                     user_ty,
911                     const_id: Some(def_id),
912                 }
913             }
914
915             Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id) => {
916                 let user_provided_types = self.typeck_results.user_provided_types();
917                 let user_provided_type = user_provided_types.get(expr.hir_id).copied();
918                 debug!("convert_path_expr: user_provided_type={:?}", user_provided_type);
919                 let ty = self.typeck_results().node_type(expr.hir_id);
920                 match ty.kind() {
921                     // A unit struct/variant which is used as a value.
922                     // We return a completely different ExprKind here to account for this special case.
923                     ty::Adt(adt_def, substs) => ExprKind::Adt(Box::new(Adt {
924                         adt_def,
925                         variant_index: adt_def.variant_index_with_ctor_id(def_id),
926                         substs,
927                         user_ty: user_provided_type,
928                         fields: Box::new([]),
929                         base: None,
930                     })),
931                     _ => bug!("unexpected ty: {:?}", ty),
932                 }
933             }
934
935             // We encode uses of statics as a `*&STATIC` where the `&STATIC` part is
936             // a constant reference (or constant raw pointer for `static mut`) in MIR
937             Res::Def(DefKind::Static, id) => {
938                 let ty = self.tcx.static_ptr_ty(id);
939                 let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
940                 let kind = if self.tcx.is_thread_local_static(id) {
941                     ExprKind::ThreadLocalRef(id)
942                 } else {
943                     let alloc_id = self.tcx.create_static_alloc(id);
944                     ExprKind::StaticRef { alloc_id, ty, def_id: id }
945                 };
946                 ExprKind::Deref {
947                     arg: self.thir.exprs.push(Expr { ty, temp_lifetime, span: expr.span, kind }),
948                 }
949             }
950
951             Res::Local(var_hir_id) => self.convert_var(var_hir_id),
952
953             _ => span_bug!(expr.span, "res `{:?}` not yet implemented", res),
954         }
955     }
956
957     fn convert_var(&mut self, var_hir_id: hir::HirId) -> ExprKind<'tcx> {
958         // We want upvars here not captures.
959         // Captures will be handled in MIR.
960         let is_upvar = self
961             .tcx
962             .upvars_mentioned(self.body_owner)
963             .map_or(false, |upvars| upvars.contains_key(&var_hir_id));
964
965         debug!(
966             "convert_var({:?}): is_upvar={}, body_owner={:?}",
967             var_hir_id, is_upvar, self.body_owner
968         );
969
970         if is_upvar {
971             ExprKind::UpvarRef { closure_def_id: self.body_owner, var_hir_id }
972         } else {
973             ExprKind::VarRef { id: var_hir_id }
974         }
975     }
976
977     fn overloaded_operator(
978         &mut self,
979         expr: &'tcx hir::Expr<'tcx>,
980         args: Box<[ExprId]>,
981     ) -> ExprKind<'tcx> {
982         let fun = self.method_callee(expr, expr.span, None);
983         let fun = self.thir.exprs.push(fun);
984         ExprKind::Call {
985             ty: self.thir[fun].ty,
986             fun,
987             args,
988             from_hir_call: false,
989             fn_span: expr.span,
990         }
991     }
992
993     fn overloaded_place(
994         &mut self,
995         expr: &'tcx hir::Expr<'tcx>,
996         place_ty: Ty<'tcx>,
997         overloaded_callee: Option<(DefId, SubstsRef<'tcx>)>,
998         args: Box<[ExprId]>,
999         span: Span,
1000     ) -> ExprKind<'tcx> {
1001         // For an overloaded *x or x[y] expression of type T, the method
1002         // call returns an &T and we must add the deref so that the types
1003         // line up (this is because `*x` and `x[y]` represent places):
1004
1005         // Reconstruct the output assuming it's a reference with the
1006         // same region and mutability as the receiver. This holds for
1007         // `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`.
1008         let ty::Ref(region, _, mutbl) = *self.thir[args[0]].ty.kind() else {
1009             span_bug!(span, "overloaded_place: receiver is not a reference");
1010         };
1011         let ref_ty = self.tcx.mk_ref(region, ty::TypeAndMut { ty: place_ty, mutbl });
1012
1013         // construct the complete expression `foo()` for the overloaded call,
1014         // which will yield the &T type
1015         let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
1016         let fun = self.method_callee(expr, span, overloaded_callee);
1017         let fun = self.thir.exprs.push(fun);
1018         let fun_ty = self.thir[fun].ty;
1019         let ref_expr = self.thir.exprs.push(Expr {
1020             temp_lifetime,
1021             ty: ref_ty,
1022             span,
1023             kind: ExprKind::Call { ty: fun_ty, fun, args, from_hir_call: false, fn_span: span },
1024         });
1025
1026         // construct and return a deref wrapper `*foo()`
1027         ExprKind::Deref { arg: ref_expr }
1028     }
1029
1030     fn convert_captured_hir_place(
1031         &mut self,
1032         closure_expr: &'tcx hir::Expr<'tcx>,
1033         place: HirPlace<'tcx>,
1034     ) -> Expr<'tcx> {
1035         let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
1036         let var_ty = place.base_ty;
1037
1038         // The result of capture analysis in `rustc_typeck/check/upvar.rs`represents a captured path
1039         // as it's seen for use within the closure and not at the time of closure creation.
1040         //
1041         // That is we see expect to see it start from a captured upvar and not something that is local
1042         // to the closure's parent.
1043         let var_hir_id = match place.base {
1044             HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
1045             base => bug!("Expected an upvar, found {:?}", base),
1046         };
1047
1048         let mut captured_place_expr = Expr {
1049             temp_lifetime,
1050             ty: var_ty,
1051             span: closure_expr.span,
1052             kind: self.convert_var(var_hir_id),
1053         };
1054
1055         for proj in place.projections.iter() {
1056             let kind = match proj.kind {
1057                 HirProjectionKind::Deref => {
1058                     ExprKind::Deref { arg: self.thir.exprs.push(captured_place_expr) }
1059                 }
1060                 HirProjectionKind::Field(field, ..) => {
1061                     // Variant index will always be 0, because for multi-variant
1062                     // enums, we capture the enum entirely.
1063                     ExprKind::Field {
1064                         lhs: self.thir.exprs.push(captured_place_expr),
1065                         name: Field::new(field as usize),
1066                     }
1067                 }
1068                 HirProjectionKind::Index | HirProjectionKind::Subslice => {
1069                     // We don't capture these projections, so we can ignore them here
1070                     continue;
1071                 }
1072             };
1073
1074             captured_place_expr =
1075                 Expr { temp_lifetime, ty: proj.ty, span: closure_expr.span, kind };
1076         }
1077
1078         captured_place_expr
1079     }
1080
1081     fn capture_upvar(
1082         &mut self,
1083         closure_expr: &'tcx hir::Expr<'tcx>,
1084         captured_place: &'tcx ty::CapturedPlace<'tcx>,
1085         upvar_ty: Ty<'tcx>,
1086     ) -> Expr<'tcx> {
1087         let upvar_capture = captured_place.info.capture_kind;
1088         let captured_place_expr =
1089             self.convert_captured_hir_place(closure_expr, captured_place.place.clone());
1090         let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
1091
1092         match upvar_capture {
1093             ty::UpvarCapture::ByValue => captured_place_expr,
1094             ty::UpvarCapture::ByRef(upvar_borrow) => {
1095                 let borrow_kind = match upvar_borrow {
1096                     ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
1097                     ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
1098                     ty::BorrowKind::MutBorrow => BorrowKind::Mut { allow_two_phase_borrow: false },
1099                 };
1100                 Expr {
1101                     temp_lifetime,
1102                     ty: upvar_ty,
1103                     span: closure_expr.span,
1104                     kind: ExprKind::Borrow {
1105                         borrow_kind,
1106                         arg: self.thir.exprs.push(captured_place_expr),
1107                     },
1108                 }
1109             }
1110         }
1111     }
1112
1113     /// Converts a list of named fields (i.e., for struct-like struct/enum ADTs) into FieldExpr.
1114     fn field_refs(&mut self, fields: &'tcx [hir::ExprField<'tcx>]) -> Box<[FieldExpr]> {
1115         fields
1116             .iter()
1117             .map(|field| FieldExpr {
1118                 name: Field::new(self.tcx.field_index(field.hir_id, self.typeck_results)),
1119                 expr: self.mirror_expr(field.expr),
1120             })
1121             .collect()
1122     }
1123 }
1124
1125 trait ToBorrowKind {
1126     fn to_borrow_kind(&self) -> BorrowKind;
1127 }
1128
1129 impl ToBorrowKind for AutoBorrowMutability {
1130     fn to_borrow_kind(&self) -> BorrowKind {
1131         use rustc_middle::ty::adjustment::AllowTwoPhase;
1132         match *self {
1133             AutoBorrowMutability::Mut { allow_two_phase_borrow } => BorrowKind::Mut {
1134                 allow_two_phase_borrow: match allow_two_phase_borrow {
1135                     AllowTwoPhase::Yes => true,
1136                     AllowTwoPhase::No => false,
1137                 },
1138             },
1139             AutoBorrowMutability::Not => BorrowKind::Shared,
1140         }
1141     }
1142 }
1143
1144 impl ToBorrowKind for hir::Mutability {
1145     fn to_borrow_kind(&self) -> BorrowKind {
1146         match *self {
1147             hir::Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
1148             hir::Mutability::Not => BorrowKind::Shared,
1149         }
1150     }
1151 }
1152
1153 fn bin_op(op: hir::BinOpKind) -> BinOp {
1154     match op {
1155         hir::BinOpKind::Add => BinOp::Add,
1156         hir::BinOpKind::Sub => BinOp::Sub,
1157         hir::BinOpKind::Mul => BinOp::Mul,
1158         hir::BinOpKind::Div => BinOp::Div,
1159         hir::BinOpKind::Rem => BinOp::Rem,
1160         hir::BinOpKind::BitXor => BinOp::BitXor,
1161         hir::BinOpKind::BitAnd => BinOp::BitAnd,
1162         hir::BinOpKind::BitOr => BinOp::BitOr,
1163         hir::BinOpKind::Shl => BinOp::Shl,
1164         hir::BinOpKind::Shr => BinOp::Shr,
1165         hir::BinOpKind::Eq => BinOp::Eq,
1166         hir::BinOpKind::Lt => BinOp::Lt,
1167         hir::BinOpKind::Le => BinOp::Le,
1168         hir::BinOpKind::Ne => BinOp::Ne,
1169         hir::BinOpKind::Ge => BinOp::Ge,
1170         hir::BinOpKind::Gt => BinOp::Gt,
1171         _ => bug!("no equivalent for ast binop {:?}", op),
1172     }
1173 }