1 use crate::thir::cx::region::Scope;
2 use crate::thir::cx::Cx;
3 use crate::thir::util::UserAnnotatedTyHelpers;
4 use rustc_data_structures::stack::ensure_sufficient_stack;
6 use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
7 use rustc_index::vec::Idx;
8 use rustc_middle::hir::place::Place as HirPlace;
9 use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
10 use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
11 use rustc_middle::middle::region;
12 use rustc_middle::mir::{self, BinOp, BorrowKind, Field, UnOp};
13 use rustc_middle::thir::*;
14 use rustc_middle::ty::adjustment::{
15 Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, PointerCast,
17 use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
18 use rustc_middle::ty::{
19 self, AdtKind, InlineConstSubsts, InlineConstSubstsParts, ScalarInt, Ty, UpvarSubsts, UserType,
21 use rustc_span::def_id::DefId;
23 use rustc_target::abi::VariantIdx;
26 pub(crate) fn mirror_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) -> ExprId {
27 // `mirror_expr` is recursing very deep. Make sure the stack doesn't overflow.
28 ensure_sufficient_stack(|| self.mirror_expr_inner(expr))
31 pub(crate) fn mirror_exprs(&mut self, exprs: &'tcx [hir::Expr<'tcx>]) -> Box<[ExprId]> {
32 exprs.iter().map(|expr| self.mirror_expr_inner(expr)).collect()
35 #[instrument(level = "trace", skip(self, hir_expr))]
36 pub(super) fn mirror_expr_inner(&mut self, hir_expr: &'tcx hir::Expr<'tcx>) -> ExprId {
38 self.rvalue_scopes.temporary_scope(self.region_scope_tree, hir_expr.hir_id.local_id);
40 region::Scope { id: hir_expr.hir_id.local_id, data: region::ScopeData::Node };
42 trace!(?hir_expr.hir_id, ?hir_expr.span);
44 let mut expr = self.make_mirror_unadjusted(hir_expr);
46 let adjustment_span = match self.adjustment_span {
47 Some((hir_id, span)) if hir_id == hir_expr.hir_id => Some(span),
53 // Now apply adjustments, if any.
54 for adjustment in self.typeck_results.expr_adjustments(hir_expr) {
55 trace!(?expr, ?adjustment);
58 self.apply_adjustment(hir_expr, expr, adjustment, adjustment_span.unwrap_or(span));
61 trace!(?expr.ty, "after adjustments");
63 // Next, wrap this up in the expr's scope.
68 kind: ExprKind::Scope {
69 region_scope: expr_scope,
70 value: self.thir.exprs.push(expr),
71 lint_level: LintLevel::Explicit(hir_expr.hir_id),
75 // Finally, create a destruction scope, if any.
76 if let Some(region_scope) =
77 self.region_scope_tree.opt_destruction_scope(hir_expr.hir_id.local_id)
83 kind: ExprKind::Scope {
85 value: self.thir.exprs.push(expr),
86 lint_level: LintLevel::Inherited,
92 self.thir.exprs.push(expr)
97 hir_expr: &'tcx hir::Expr<'tcx>,
99 adjustment: &Adjustment<'tcx>,
102 let Expr { temp_lifetime, .. } = expr;
104 // Adjust the span from the block, to the last expression of the
105 // block. This is a better span when returning a mutable reference
106 // with too short a lifetime. The error message will use the span
107 // from the assignment to the return place, which should only point
108 // at the returned value, not the entire function body.
110 // fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
112 // // ^ error message points at this expression.
114 let mut adjust_span = |expr: &mut Expr<'tcx>| {
115 if let ExprKind::Block { block } = expr.kind {
116 if let Some(last_expr) = self.thir[block].expr {
117 span = self.thir[last_expr].span;
123 let kind = match adjustment.kind {
124 Adjust::Pointer(PointerCast::Unsize) => {
125 adjust_span(&mut expr);
126 ExprKind::Pointer { cast: PointerCast::Unsize, source: self.thir.exprs.push(expr) }
128 Adjust::Pointer(cast) => ExprKind::Pointer { cast, source: self.thir.exprs.push(expr) },
129 Adjust::NeverToAny => ExprKind::NeverToAny { source: self.thir.exprs.push(expr) },
130 Adjust::Deref(None) => {
131 adjust_span(&mut expr);
132 ExprKind::Deref { arg: self.thir.exprs.push(expr) }
134 Adjust::Deref(Some(deref)) => {
135 // We don't need to do call adjust_span here since
136 // deref coercions always start with a built-in deref.
137 let call = deref.method_call(self.tcx(), expr.ty);
143 .mk_ref(deref.region, ty::TypeAndMut { ty: expr.ty, mutbl: deref.mutbl }),
145 kind: ExprKind::Borrow {
146 borrow_kind: deref.mutbl.to_borrow_kind(),
147 arg: self.thir.exprs.push(expr),
151 let expr = Box::new([self.thir.exprs.push(expr)]);
153 self.overloaded_place(hir_expr, adjustment.target, Some(call), expr, deref.span)
155 Adjust::Borrow(AutoBorrow::Ref(_, m)) => ExprKind::Borrow {
156 borrow_kind: m.to_borrow_kind(),
157 arg: self.thir.exprs.push(expr),
159 Adjust::Borrow(AutoBorrow::RawPtr(mutability)) => {
160 ExprKind::AddressOf { mutability, arg: self.thir.exprs.push(expr) }
164 Expr { temp_lifetime, ty: adjustment.target, span, kind }
167 /// Lowers a cast expression.
169 /// Dealing with user type annotations is left to the caller.
172 source: &'tcx hir::Expr<'tcx>,
173 temp_lifetime: Option<Scope>,
175 ) -> ExprKind<'tcx> {
178 // Check to see if this cast is a "coercion cast", where the cast is actually done
179 // using a coercion (or is a no-op).
180 if self.typeck_results().is_coercion_cast(source.hir_id) {
181 // Convert the lexpr to a vexpr.
182 ExprKind::Use { source: self.mirror_expr(source) }
183 } else if self.typeck_results().expr_ty(source).is_region_ptr() {
184 // Special cased so that we can type check that the element
185 // type of the source matches the pointed to type of the
188 source: self.mirror_expr(source),
189 cast: PointerCast::ArrayToPointer,
192 // check whether this is casting an enum variant discriminant
193 // to prevent cycles, we refer to the discriminant initializer
194 // which is always an integer and thus doesn't need to know the
195 // enum's layout (or its tag type) to compute it during const eval
199 // B = A as isize + 4,
201 // The correct solution would be to add symbolic computations to miri,
202 // so we wouldn't have to compute and store the actual value
204 let hir::ExprKind::Path(ref qpath) = source.kind else {
205 return ExprKind::Cast { source: self.mirror_expr(source)};
208 let res = self.typeck_results().qpath_res(qpath, source.hir_id);
209 let ty = self.typeck_results().node_type(source.hir_id);
210 let ty::Adt(adt_def, substs) = ty.kind() else {
211 return ExprKind::Cast { source: self.mirror_expr(source)};
214 let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res else {
215 return ExprKind::Cast { source: self.mirror_expr(source)};
218 let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id);
219 let (discr_did, discr_offset) = adt_def.discriminant_def_for_variant(idx);
221 use rustc_middle::ty::util::IntTypeExt;
222 let ty = adt_def.repr().discr_type();
223 let discr_ty = ty.to_ty(tcx);
225 let param_env_ty = self.param_env.and(discr_ty);
227 .layout_of(param_env_ty)
228 .unwrap_or_else(|e| {
229 panic!("could not compute layout for {:?}: {:?}", param_env_ty, e)
233 let lit = ScalarInt::try_from_uint(discr_offset as u128, size).unwrap();
234 let kind = ExprKind::NonHirLiteral { lit, user_ty: None };
235 let offset = self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind });
237 let source = match discr_did {
238 // in case we are offsetting from a computed discriminant
239 // and not the beginning of discriminants (which is always `0`)
241 let kind = ExprKind::NamedConst { def_id: did, substs, user_ty: None };
243 self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind });
244 let bin = ExprKind::Binary { op: BinOp::Add, lhs, rhs: offset };
245 self.thir.exprs.push(Expr {
255 ExprKind::Cast { source }
259 fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
261 let expr_ty = self.typeck_results().expr_ty(expr);
262 let expr_span = expr.span;
264 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
266 let kind = match expr.kind {
267 // Here comes the interesting stuff:
268 hir::ExprKind::MethodCall(segment, receiver, ref args, fn_span) => {
269 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
270 let expr = self.method_callee(expr, segment.ident.span, None);
271 // When we apply adjustments to the receiver, use the span of
272 // the overall method call for better diagnostics. args[0]
273 // is guaranteed to exist, since a method call always has a receiver.
274 let old_adjustment_span =
275 self.adjustment_span.replace((receiver.hir_id, expr_span));
276 info!("Using method span: {:?}", expr.span);
277 let args = std::iter::once(receiver)
279 .map(|expr| self.mirror_expr(expr))
281 self.adjustment_span = old_adjustment_span;
284 fun: self.thir.exprs.push(expr),
291 hir::ExprKind::Call(ref fun, ref args) => {
292 if self.typeck_results().is_method_call(expr) {
293 // The callee is something implementing Fn, FnMut, or FnOnce.
294 // Find the actual method implementation being called and
295 // build the appropriate UFCS call expression with the
296 // callee-object as expr parameter.
298 // rewrite f(u, v) into FnOnce::call_once(f, (u, v))
300 let method = self.method_callee(expr, fun.span, None);
302 let arg_tys = args.iter().map(|e| self.typeck_results().expr_ty_adjusted(e));
303 let tupled_args = Expr {
304 ty: tcx.mk_tup(arg_tys),
307 kind: ExprKind::Tuple { fields: self.mirror_exprs(args) },
309 let tupled_args = self.thir.exprs.push(tupled_args);
313 fun: self.thir.exprs.push(method),
314 args: Box::new([self.mirror_expr(fun), tupled_args]),
320 if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = fun.kind {
321 // Tuple-like ADTs are represented as ExprKind::Call. We convert them here.
322 expr_ty.ty_adt_def().and_then(|adt_def| match path.res {
323 Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_id) => {
324 Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
326 Res::SelfCtor(..) => Some((adt_def, VariantIdx::new(0))),
332 if let Some((adt_def, index)) = adt_data {
333 let substs = self.typeck_results().node_substs(fun.hir_id);
334 let user_provided_types = self.typeck_results().user_provided_types();
336 user_provided_types.get(fun.hir_id).copied().map(|mut u_ty| {
337 if let UserType::TypeOf(ref mut did, _) = &mut u_ty.value {
338 *did = adt_def.did();
342 debug!("make_mirror_unadjusted: (call) user_ty={:?}", user_ty);
344 let field_refs = args
347 .map(|(idx, e)| FieldExpr {
348 name: Field::new(idx),
349 expr: self.mirror_expr(e),
352 ExprKind::Adt(Box::new(AdtExpr {
355 variant_index: index,
362 ty: self.typeck_results().node_type(fun.hir_id),
363 fun: self.mirror_expr(fun),
364 args: self.mirror_exprs(args),
372 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mutbl, ref arg) => {
373 ExprKind::Borrow { borrow_kind: mutbl.to_borrow_kind(), arg: self.mirror_expr(arg) }
376 hir::ExprKind::AddrOf(hir::BorrowKind::Raw, mutability, ref arg) => {
377 ExprKind::AddressOf { mutability, arg: self.mirror_expr(arg) }
380 hir::ExprKind::Block(ref blk, _) => ExprKind::Block { block: self.mirror_block(blk) },
382 hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
383 ExprKind::Assign { lhs: self.mirror_expr(lhs), rhs: self.mirror_expr(rhs) }
386 hir::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
387 if self.typeck_results().is_method_call(expr) {
388 let lhs = self.mirror_expr(lhs);
389 let rhs = self.mirror_expr(rhs);
390 self.overloaded_operator(expr, Box::new([lhs, rhs]))
394 lhs: self.mirror_expr(lhs),
395 rhs: self.mirror_expr(rhs),
400 hir::ExprKind::Lit(ref lit) => ExprKind::Literal { lit, neg: false },
402 hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
403 if self.typeck_results().is_method_call(expr) {
404 let lhs = self.mirror_expr(lhs);
405 let rhs = self.mirror_expr(rhs);
406 self.overloaded_operator(expr, Box::new([lhs, rhs]))
410 hir::BinOpKind::And => ExprKind::LogicalOp {
412 lhs: self.mirror_expr(lhs),
413 rhs: self.mirror_expr(rhs),
415 hir::BinOpKind::Or => ExprKind::LogicalOp {
417 lhs: self.mirror_expr(lhs),
418 rhs: self.mirror_expr(rhs),
421 let op = bin_op(op.node);
424 lhs: self.mirror_expr(lhs),
425 rhs: self.mirror_expr(rhs),
432 hir::ExprKind::Index(ref lhs, ref index) => {
433 if self.typeck_results().is_method_call(expr) {
434 let lhs = self.mirror_expr(lhs);
435 let index = self.mirror_expr(index);
436 self.overloaded_place(expr, expr_ty, None, Box::new([lhs, index]), expr.span)
438 ExprKind::Index { lhs: self.mirror_expr(lhs), index: self.mirror_expr(index) }
442 hir::ExprKind::Unary(hir::UnOp::Deref, ref arg) => {
443 if self.typeck_results().is_method_call(expr) {
444 let arg = self.mirror_expr(arg);
445 self.overloaded_place(expr, expr_ty, None, Box::new([arg]), expr.span)
447 ExprKind::Deref { arg: self.mirror_expr(arg) }
451 hir::ExprKind::Unary(hir::UnOp::Not, ref arg) => {
452 if self.typeck_results().is_method_call(expr) {
453 let arg = self.mirror_expr(arg);
454 self.overloaded_operator(expr, Box::new([arg]))
456 ExprKind::Unary { op: UnOp::Not, arg: self.mirror_expr(arg) }
460 hir::ExprKind::Unary(hir::UnOp::Neg, ref arg) => {
461 if self.typeck_results().is_method_call(expr) {
462 let arg = self.mirror_expr(arg);
463 self.overloaded_operator(expr, Box::new([arg]))
464 } else if let hir::ExprKind::Lit(ref lit) = arg.kind {
465 ExprKind::Literal { lit, neg: true }
467 ExprKind::Unary { op: UnOp::Neg, arg: self.mirror_expr(arg) }
471 hir::ExprKind::Struct(ref qpath, ref fields, ref base) => match expr_ty.kind() {
472 ty::Adt(adt, substs) => match adt.adt_kind() {
473 AdtKind::Struct | AdtKind::Union => {
474 let user_provided_types = self.typeck_results().user_provided_types();
475 let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
476 debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty);
477 ExprKind::Adt(Box::new(AdtExpr {
479 variant_index: VariantIdx::new(0),
482 fields: self.field_refs(fields),
483 base: base.as_ref().map(|base| FruInfo {
484 base: self.mirror_expr(base),
485 field_types: self.typeck_results().fru_field_types()[expr.hir_id]
493 let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
495 Res::Def(DefKind::Variant, variant_id) => {
496 assert!(base.is_none());
498 let index = adt.variant_index_with_id(variant_id);
499 let user_provided_types =
500 self.typeck_results().user_provided_types();
502 user_provided_types.get(expr.hir_id).copied().map(Box::new);
503 debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty);
504 ExprKind::Adt(Box::new(AdtExpr {
506 variant_index: index,
509 fields: self.field_refs(fields),
514 span_bug!(expr.span, "unexpected res: {:?}", res);
520 span_bug!(expr.span, "unexpected type for struct literal: {:?}", expr_ty);
524 hir::ExprKind::Closure { .. } => {
525 let closure_ty = self.typeck_results().expr_ty(expr);
526 let (def_id, substs, movability) = match *closure_ty.kind() {
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))
532 span_bug!(expr.span, "closure expr w/o closure type: {:?}", closure_ty);
535 let def_id = def_id.expect_local();
539 .closure_min_captures_flattened(def_id)
540 .zip(substs.upvar_tys())
541 .map(|(captured_place, ty)| {
542 let upvars = self.capture_upvar(expr, captured_place, ty);
543 self.thir.exprs.push(upvars)
547 // Convert the closure fake reads, if any, from hir `Place` to ExprRef
548 let fake_reads = match self.typeck_results.closure_fake_reads.get(&def_id) {
549 Some(fake_reads) => fake_reads
551 .map(|(place, cause, hir_id)| {
552 let expr = self.convert_captured_hir_place(expr, place.clone());
553 (self.thir.exprs.push(expr), *cause, *hir_id)
559 ExprKind::Closure(Box::new(ClosureExpr {
568 hir::ExprKind::Path(ref qpath) => {
569 let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
570 self.convert_path_expr(expr, res)
573 hir::ExprKind::InlineAsm(ref asm) => ExprKind::InlineAsm(Box::new(InlineAsmExpr {
574 template: asm.template,
578 .map(|(op, _op_sp)| match *op {
579 hir::InlineAsmOperand::In { reg, ref expr } => {
580 InlineAsmOperand::In { reg, expr: self.mirror_expr(expr) }
582 hir::InlineAsmOperand::Out { reg, late, ref expr } => {
583 InlineAsmOperand::Out {
586 expr: expr.as_ref().map(|expr| self.mirror_expr(expr)),
589 hir::InlineAsmOperand::InOut { reg, late, ref expr } => {
590 InlineAsmOperand::InOut { reg, late, expr: self.mirror_expr(expr) }
592 hir::InlineAsmOperand::SplitInOut {
597 } => InlineAsmOperand::SplitInOut {
600 in_expr: self.mirror_expr(in_expr),
601 out_expr: out_expr.as_ref().map(|expr| self.mirror_expr(expr)),
603 hir::InlineAsmOperand::Const { ref anon_const } => {
604 let anon_const_def_id = tcx.hir().local_def_id(anon_const.hir_id);
605 let value = mir::ConstantKind::from_anon_const(
610 let span = tcx.hir().span(anon_const.hir_id);
612 InlineAsmOperand::Const { value, span }
614 hir::InlineAsmOperand::SymFn { ref anon_const } => {
615 let anon_const_def_id = tcx.hir().local_def_id(anon_const.hir_id);
616 let value = mir::ConstantKind::from_anon_const(
621 let span = tcx.hir().span(anon_const.hir_id);
623 InlineAsmOperand::SymFn { value, span }
625 hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
626 InlineAsmOperand::SymStatic { def_id }
630 options: asm.options,
631 line_spans: asm.line_spans,
634 hir::ExprKind::ConstBlock(ref anon_const) => {
635 let ty = self.typeck_results().node_type(anon_const.hir_id);
636 let did = tcx.hir().local_def_id(anon_const.hir_id).to_def_id();
637 let typeck_root_def_id = tcx.typeck_root_def_id(did);
639 tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id));
641 InlineConstSubsts::new(tcx, InlineConstSubstsParts { parent_substs, ty })
644 ExprKind::ConstBlock { did, substs }
646 // Now comes the rote stuff:
647 hir::ExprKind::Repeat(ref v, _) => {
648 let ty = self.typeck_results().expr_ty(expr);
649 let ty::Array(_, count) = ty.kind() else {
650 span_bug!(expr.span, "unexpected repeat expr ty: {:?}", ty);
653 ExprKind::Repeat { value: self.mirror_expr(v), count: *count }
655 hir::ExprKind::Ret(ref v) => {
656 ExprKind::Return { value: v.as_ref().map(|v| self.mirror_expr(v)) }
658 hir::ExprKind::Break(dest, ref value) => match dest.target_id {
659 Ok(target_id) => ExprKind::Break {
660 label: region::Scope { id: target_id.local_id, data: region::ScopeData::Node },
661 value: value.as_ref().map(|value| self.mirror_expr(value)),
663 Err(err) => bug!("invalid loop id for break: {}", err),
665 hir::ExprKind::Continue(dest) => match dest.target_id {
666 Ok(loop_id) => ExprKind::Continue {
667 label: region::Scope { id: loop_id.local_id, data: region::ScopeData::Node },
669 Err(err) => bug!("invalid loop id for continue: {}", err),
671 hir::ExprKind::Let(let_expr) => ExprKind::Let {
672 expr: self.mirror_expr(let_expr.init),
673 pat: self.pattern_from_hir(let_expr.pat),
675 hir::ExprKind::If(cond, then, else_opt) => ExprKind::If {
676 if_then_scope: region::Scope {
677 id: then.hir_id.local_id,
678 data: region::ScopeData::IfThen,
680 cond: self.mirror_expr(cond),
681 then: self.mirror_expr(then),
682 else_opt: else_opt.map(|el| self.mirror_expr(el)),
684 hir::ExprKind::Match(ref discr, ref arms, _) => ExprKind::Match {
685 scrutinee: self.mirror_expr(discr),
686 arms: arms.iter().map(|a| self.convert_arm(a)).collect(),
688 hir::ExprKind::Loop(ref body, ..) => {
689 let block_ty = self.typeck_results().node_type(body.hir_id);
690 let temp_lifetime = self
692 .temporary_scope(self.region_scope_tree, body.hir_id.local_id);
693 let block = self.mirror_block(body);
694 let body = self.thir.exprs.push(Expr {
697 span: self.thir[block].span,
698 kind: ExprKind::Block { block },
700 ExprKind::Loop { body }
702 hir::ExprKind::Field(ref source, ..) => ExprKind::Field {
703 lhs: self.mirror_expr(source),
704 variant_index: VariantIdx::new(0),
705 name: Field::new(tcx.field_index(expr.hir_id, self.typeck_results)),
707 hir::ExprKind::Cast(ref source, ref cast_ty) => {
708 // Check for a user-given type annotation on this `cast`
709 let user_provided_types = self.typeck_results.user_provided_types();
710 let user_ty = user_provided_types.get(cast_ty.hir_id);
713 "cast({:?}) has ty w/ hir_id {:?} and user provided ty {:?}",
714 expr, cast_ty.hir_id, user_ty,
717 let cast = self.mirror_expr_cast(*source, temp_lifetime, expr.span);
719 if let Some(user_ty) = user_ty {
720 // NOTE: Creating a new Expr and wrapping a Cast inside of it may be
721 // inefficient, revisit this when performance becomes an issue.
722 let cast_expr = self.thir.exprs.push(Expr {
728 debug!("make_mirror_unadjusted: (cast) user_ty={:?}", user_ty);
730 ExprKind::ValueTypeAscription {
732 user_ty: Some(Box::new(*user_ty)),
738 hir::ExprKind::Type(ref source, ref ty) => {
739 let user_provided_types = self.typeck_results.user_provided_types();
740 let user_ty = user_provided_types.get(ty.hir_id).copied().map(Box::new);
741 debug!("make_mirror_unadjusted: (type) user_ty={:?}", user_ty);
742 let mirrored = self.mirror_expr(source);
743 if source.is_syntactic_place_expr() {
744 ExprKind::PlaceTypeAscription { source: mirrored, user_ty }
746 ExprKind::ValueTypeAscription { source: mirrored, user_ty }
749 hir::ExprKind::DropTemps(ref source) => {
750 ExprKind::Use { source: self.mirror_expr(source) }
752 hir::ExprKind::Box(ref value) => ExprKind::Box { value: self.mirror_expr(value) },
753 hir::ExprKind::Array(ref fields) => {
754 ExprKind::Array { fields: self.mirror_exprs(fields) }
756 hir::ExprKind::Tup(ref fields) => ExprKind::Tuple { fields: self.mirror_exprs(fields) },
758 hir::ExprKind::Yield(ref v, _) => ExprKind::Yield { value: self.mirror_expr(v) },
759 hir::ExprKind::Err => unreachable!(),
762 Expr { temp_lifetime, ty: expr_ty, span: expr.span, kind }
765 fn user_substs_applied_to_res(
769 ) -> Option<Box<ty::CanonicalUserType<'tcx>>> {
770 debug!("user_substs_applied_to_res: res={:?}", res);
771 let user_provided_type = match res {
772 // A reference to something callable -- e.g., a fn, method, or
773 // a tuple-struct or tuple-variant. This has the type of a
774 // `Fn` but with the user-given substitutions.
775 Res::Def(DefKind::Fn, _)
776 | Res::Def(DefKind::AssocFn, _)
777 | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
778 | Res::Def(DefKind::Const, _)
779 | Res::Def(DefKind::AssocConst, _) => {
780 self.typeck_results().user_provided_types().get(hir_id).copied().map(Box::new)
783 // A unit struct/variant which is used as a value (e.g.,
784 // `None`). This has the type of the enum/struct that defines
785 // this variant -- but with the substitutions given by the
787 Res::Def(DefKind::Ctor(_, CtorKind::Const), _) => {
788 self.user_substs_applied_to_ty_of_hir_id(hir_id).map(Box::new)
791 // `Self` is used in expression as a tuple struct constructor or a unit struct constructor
792 Res::SelfCtor(_) => self.user_substs_applied_to_ty_of_hir_id(hir_id).map(Box::new),
794 _ => bug!("user_substs_applied_to_res: unexpected res {:?} at {:?}", res, hir_id),
796 debug!("user_substs_applied_to_res: user_provided_type={:?}", user_provided_type);
802 expr: &hir::Expr<'_>,
804 overloaded_callee: Option<(DefId, SubstsRef<'tcx>)>,
807 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
808 let (def_id, substs, user_ty) = match overloaded_callee {
809 Some((def_id, substs)) => (def_id, substs, None),
812 self.typeck_results().type_dependent_def(expr.hir_id).unwrap_or_else(|| {
813 span_bug!(expr.span, "no type-dependent def for method callee")
815 let user_ty = self.user_substs_applied_to_res(expr.hir_id, Res::Def(kind, def_id));
816 debug!("method_callee: user_ty={:?}", user_ty);
817 (def_id, self.typeck_results().node_substs(expr.hir_id), user_ty)
820 let ty = self.tcx().mk_fn_def(def_id, substs);
821 Expr { temp_lifetime, ty, span, kind: ExprKind::ZstLiteral { user_ty } }
824 fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
826 pattern: self.pattern_from_hir(&arm.pat),
827 guard: arm.guard.as_ref().map(|g| match g {
828 hir::Guard::If(ref e) => Guard::If(self.mirror_expr(e)),
829 hir::Guard::IfLet(ref l) => {
830 Guard::IfLet(self.pattern_from_hir(l.pat), self.mirror_expr(l.init))
833 body: self.mirror_expr(arm.body),
834 lint_level: LintLevel::Explicit(arm.hir_id),
835 scope: region::Scope { id: arm.hir_id.local_id, data: region::ScopeData::Node },
838 self.thir.arms.push(arm)
841 fn convert_path_expr(&mut self, expr: &'tcx hir::Expr<'tcx>, res: Res) -> ExprKind<'tcx> {
842 let substs = self.typeck_results().node_substs(expr.hir_id);
844 // A regular function, constructor function or a constant.
845 Res::Def(DefKind::Fn, _)
846 | Res::Def(DefKind::AssocFn, _)
847 | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
848 | Res::SelfCtor(_) => {
849 let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
850 ExprKind::ZstLiteral { user_ty }
853 Res::Def(DefKind::ConstParam, def_id) => {
854 let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
855 let item_id = self.tcx.hir().get_parent_node(hir_id);
856 let item_def_id = self.tcx.hir().local_def_id(item_id);
857 let generics = self.tcx.generics_of(item_def_id);
858 let index = generics.param_def_id_to_index[&def_id];
859 let name = self.tcx.hir().name(hir_id);
860 let param = ty::ParamConst::new(index, name);
862 ExprKind::ConstParam { param, def_id }
865 Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
866 let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
867 ExprKind::NamedConst { def_id, substs, user_ty }
870 Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id) => {
871 let user_provided_types = self.typeck_results.user_provided_types();
872 let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
873 debug!("convert_path_expr: user_ty={:?}", user_ty);
874 let ty = self.typeck_results().node_type(expr.hir_id);
876 // A unit struct/variant which is used as a value.
877 // We return a completely different ExprKind here to account for this special case.
878 ty::Adt(adt_def, substs) => ExprKind::Adt(Box::new(AdtExpr {
880 variant_index: adt_def.variant_index_with_ctor_id(def_id),
883 fields: Box::new([]),
886 _ => bug!("unexpected ty: {:?}", ty),
890 // We encode uses of statics as a `*&STATIC` where the `&STATIC` part is
891 // a constant reference (or constant raw pointer for `static mut`) in MIR
892 Res::Def(DefKind::Static(_), id) => {
893 let ty = self.tcx.static_ptr_ty(id);
894 let temp_lifetime = self
896 .temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
897 let kind = if self.tcx.is_thread_local_static(id) {
898 ExprKind::ThreadLocalRef(id)
900 let alloc_id = self.tcx.create_static_alloc(id);
901 ExprKind::StaticRef { alloc_id, ty, def_id: id }
904 arg: self.thir.exprs.push(Expr { ty, temp_lifetime, span: expr.span, kind }),
908 Res::Local(var_hir_id) => self.convert_var(var_hir_id),
910 _ => span_bug!(expr.span, "res `{:?}` not yet implemented", res),
914 fn convert_var(&mut self, var_hir_id: hir::HirId) -> ExprKind<'tcx> {
915 // We want upvars here not captures.
916 // Captures will be handled in MIR.
919 .upvars_mentioned(self.body_owner)
920 .map_or(false, |upvars| upvars.contains_key(&var_hir_id));
923 "convert_var({:?}): is_upvar={}, body_owner={:?}",
924 var_hir_id, is_upvar, self.body_owner
929 closure_def_id: self.body_owner,
930 var_hir_id: LocalVarId(var_hir_id),
933 ExprKind::VarRef { id: LocalVarId(var_hir_id) }
937 fn overloaded_operator(
939 expr: &'tcx hir::Expr<'tcx>,
941 ) -> ExprKind<'tcx> {
942 let fun = self.method_callee(expr, expr.span, None);
943 let fun = self.thir.exprs.push(fun);
945 ty: self.thir[fun].ty,
948 from_hir_call: false,
955 expr: &'tcx hir::Expr<'tcx>,
957 overloaded_callee: Option<(DefId, SubstsRef<'tcx>)>,
960 ) -> ExprKind<'tcx> {
961 // For an overloaded *x or x[y] expression of type T, the method
962 // call returns an &T and we must add the deref so that the types
963 // line up (this is because `*x` and `x[y]` represent places):
965 // Reconstruct the output assuming it's a reference with the
966 // same region and mutability as the receiver. This holds for
967 // `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`.
968 let ty::Ref(region, _, mutbl) = *self.thir[args[0]].ty.kind() else {
969 span_bug!(span, "overloaded_place: receiver is not a reference");
971 let ref_ty = self.tcx.mk_ref(region, ty::TypeAndMut { ty: place_ty, mutbl });
973 // construct the complete expression `foo()` for the overloaded call,
974 // which will yield the &T type
976 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
977 let fun = self.method_callee(expr, span, overloaded_callee);
978 let fun = self.thir.exprs.push(fun);
979 let fun_ty = self.thir[fun].ty;
980 let ref_expr = self.thir.exprs.push(Expr {
984 kind: ExprKind::Call { ty: fun_ty, fun, args, from_hir_call: false, fn_span: span },
987 // construct and return a deref wrapper `*foo()`
988 ExprKind::Deref { arg: ref_expr }
991 fn convert_captured_hir_place(
993 closure_expr: &'tcx hir::Expr<'tcx>,
994 place: HirPlace<'tcx>,
996 let temp_lifetime = self
998 .temporary_scope(self.region_scope_tree, closure_expr.hir_id.local_id);
999 let var_ty = place.base_ty;
1001 // The result of capture analysis in `rustc_typeck/check/upvar.rs`represents a captured path
1002 // as it's seen for use within the closure and not at the time of closure creation.
1004 // That is we see expect to see it start from a captured upvar and not something that is local
1005 // to the closure's parent.
1006 let var_hir_id = match place.base {
1007 HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
1008 base => bug!("Expected an upvar, found {:?}", base),
1011 let mut captured_place_expr = Expr {
1014 span: closure_expr.span,
1015 kind: self.convert_var(var_hir_id),
1018 for proj in place.projections.iter() {
1019 let kind = match proj.kind {
1020 HirProjectionKind::Deref => {
1021 ExprKind::Deref { arg: self.thir.exprs.push(captured_place_expr) }
1023 HirProjectionKind::Field(field, variant_index) => ExprKind::Field {
1024 lhs: self.thir.exprs.push(captured_place_expr),
1026 name: Field::new(field as usize),
1028 HirProjectionKind::Index | HirProjectionKind::Subslice => {
1029 // We don't capture these projections, so we can ignore them here
1034 captured_place_expr =
1035 Expr { temp_lifetime, ty: proj.ty, span: closure_expr.span, kind };
1043 closure_expr: &'tcx hir::Expr<'tcx>,
1044 captured_place: &'tcx ty::CapturedPlace<'tcx>,
1047 let upvar_capture = captured_place.info.capture_kind;
1048 let captured_place_expr =
1049 self.convert_captured_hir_place(closure_expr, captured_place.place.clone());
1050 let temp_lifetime = self
1052 .temporary_scope(self.region_scope_tree, closure_expr.hir_id.local_id);
1054 match upvar_capture {
1055 ty::UpvarCapture::ByValue => captured_place_expr,
1056 ty::UpvarCapture::ByRef(upvar_borrow) => {
1057 let borrow_kind = match upvar_borrow {
1058 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
1059 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
1060 ty::BorrowKind::MutBorrow => BorrowKind::Mut { allow_two_phase_borrow: false },
1065 span: closure_expr.span,
1066 kind: ExprKind::Borrow {
1068 arg: self.thir.exprs.push(captured_place_expr),
1075 /// Converts a list of named fields (i.e., for struct-like struct/enum ADTs) into FieldExpr.
1076 fn field_refs(&mut self, fields: &'tcx [hir::ExprField<'tcx>]) -> Box<[FieldExpr]> {
1079 .map(|field| FieldExpr {
1080 name: Field::new(self.tcx.field_index(field.hir_id, self.typeck_results)),
1081 expr: self.mirror_expr(field.expr),
1087 trait ToBorrowKind {
1088 fn to_borrow_kind(&self) -> BorrowKind;
1091 impl ToBorrowKind for AutoBorrowMutability {
1092 fn to_borrow_kind(&self) -> BorrowKind {
1093 use rustc_middle::ty::adjustment::AllowTwoPhase;
1095 AutoBorrowMutability::Mut { allow_two_phase_borrow } => BorrowKind::Mut {
1096 allow_two_phase_borrow: match allow_two_phase_borrow {
1097 AllowTwoPhase::Yes => true,
1098 AllowTwoPhase::No => false,
1101 AutoBorrowMutability::Not => BorrowKind::Shared,
1106 impl ToBorrowKind for hir::Mutability {
1107 fn to_borrow_kind(&self) -> BorrowKind {
1109 hir::Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
1110 hir::Mutability::Not => BorrowKind::Shared,
1115 fn bin_op(op: hir::BinOpKind) -> BinOp {
1117 hir::BinOpKind::Add => BinOp::Add,
1118 hir::BinOpKind::Sub => BinOp::Sub,
1119 hir::BinOpKind::Mul => BinOp::Mul,
1120 hir::BinOpKind::Div => BinOp::Div,
1121 hir::BinOpKind::Rem => BinOp::Rem,
1122 hir::BinOpKind::BitXor => BinOp::BitXor,
1123 hir::BinOpKind::BitAnd => BinOp::BitAnd,
1124 hir::BinOpKind::BitOr => BinOp::BitOr,
1125 hir::BinOpKind::Shl => BinOp::Shl,
1126 hir::BinOpKind::Shr => BinOp::Shr,
1127 hir::BinOpKind::Eq => BinOp::Eq,
1128 hir::BinOpKind::Lt => BinOp::Lt,
1129 hir::BinOpKind::Le => BinOp::Le,
1130 hir::BinOpKind::Ne => BinOp::Ne,
1131 hir::BinOpKind::Ge => BinOp::Ge,
1132 hir::BinOpKind::Gt => BinOp::Gt,
1133 _ => bug!("no equivalent for ast binop {:?}", op),