1use itertools::Itertools;
2use rustc_abi::{FIRST_VARIANT, FieldIdx};
3use rustc_ast::UnsafeBinderCastKind;
4use rustc_data_structures::stack::ensure_sufficient_stack;
5use rustc_hir as hir;
6use rustc_hir::attrs::AttributeKind;
7use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
8use rustc_hir::find_attr;
9use rustc_index::Idx;
10use rustc_middle::hir::place::{
11 Place as HirPlace, PlaceBase as HirPlaceBase, ProjectionKind as HirProjectionKind,
12};
13use rustc_middle::middle::region;
14use rustc_middle::mir::{self, AssignOp, BinOp, BorrowKind, UnOp};
15use rustc_middle::thir::*;
16use rustc_middle::ty::adjustment::{
17 Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, PointerCoercion,
18};
19use rustc_middle::ty::{
20 self, AdtKind, GenericArgs, InlineConstArgs, InlineConstArgsParts, ScalarInt, Ty, UpvarArgs,
21};
22use rustc_middle::{bug, span_bug};
23use rustc_span::{Span, sym};
24use tracing::{debug, info, instrument, trace};
25
26use crate::errors::*;
27use crate::thir::cx::ThirBuildCx;
28
29impl<'tcx> ThirBuildCx<'tcx> {
30 pub(crate) fn mirror_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) -> ExprId {
39 ensure_sufficient_stack(|| self.mirror_expr_inner(expr))
41 }
42
43 pub(crate) fn mirror_exprs(&mut self, exprs: &'tcx [hir::Expr<'tcx>]) -> Box<[ExprId]> {
44 ensure_sufficient_stack(|| exprs.iter().map(|expr| self.mirror_expr_inner(expr)).collect())
48 }
49
50 #[instrument(level = "trace", skip(self, hir_expr))]
51 pub(super) fn mirror_expr_inner(&mut self, hir_expr: &'tcx hir::Expr<'tcx>) -> ExprId {
52 let expr_scope =
53 region::Scope { local_id: hir_expr.hir_id.local_id, data: region::ScopeData::Node };
54
55 trace!(?hir_expr.hir_id, ?hir_expr.span);
56
57 let mut expr = self.make_mirror_unadjusted(hir_expr);
58
59 trace!(?expr.ty);
60
61 if self.apply_adjustments {
63 for adjustment in self.typeck_results.expr_adjustments(hir_expr) {
64 trace!(?expr, ?adjustment);
65 let span = expr.span;
66 expr = self.apply_adjustment(hir_expr, expr, adjustment, span);
67 }
68 }
69
70 trace!(?expr.ty, "after adjustments");
71
72 expr = Expr {
74 temp_lifetime: expr.temp_lifetime,
75 ty: expr.ty,
76 span: hir_expr.span,
77 kind: ExprKind::Scope {
78 region_scope: expr_scope,
79 value: self.thir.exprs.push(expr),
80 lint_level: LintLevel::Explicit(hir_expr.hir_id),
81 },
82 };
83
84 self.thir.exprs.push(expr)
86 }
87
88 #[instrument(level = "trace", skip(self, expr, span))]
89 fn apply_adjustment(
90 &mut self,
91 hir_expr: &'tcx hir::Expr<'tcx>,
92 mut expr: Expr<'tcx>,
93 adjustment: &Adjustment<'tcx>,
94 mut span: Span,
95 ) -> Expr<'tcx> {
96 let Expr { temp_lifetime, .. } = expr;
97
98 let mut adjust_span = |expr: &mut Expr<'tcx>| {
109 if let ExprKind::Block { block } = expr.kind
110 && let Some(last_expr) = self.thir[block].expr
111 {
112 span = self.thir[last_expr].span;
113 expr.span = span;
114 }
115 };
116
117 let kind = match adjustment.kind {
118 Adjust::Pointer(cast) => {
119 if cast == PointerCoercion::Unsize {
120 adjust_span(&mut expr);
121 }
122
123 let is_from_as_cast = if let hir::Node::Expr(hir::Expr {
124 kind: hir::ExprKind::Cast(..),
125 span: cast_span,
126 ..
127 }) = self.tcx.parent_hir_node(hir_expr.hir_id)
128 {
129 span = *cast_span;
131 true
132 } else {
133 false
134 };
135 ExprKind::PointerCoercion {
136 cast,
137 source: self.thir.exprs.push(expr),
138 is_from_as_cast,
139 }
140 }
141 Adjust::NeverToAny if adjustment.target.is_never() => return expr,
142 Adjust::NeverToAny => ExprKind::NeverToAny { source: self.thir.exprs.push(expr) },
143 Adjust::Deref(None) => {
144 adjust_span(&mut expr);
145 ExprKind::Deref { arg: self.thir.exprs.push(expr) }
146 }
147 Adjust::Deref(Some(deref)) => {
148 let call_def_id = deref.method_call(self.tcx);
151 let overloaded_callee =
152 Ty::new_fn_def(self.tcx, call_def_id, self.tcx.mk_args(&[expr.ty.into()]));
153
154 expr = Expr {
155 temp_lifetime,
156 ty: Ty::new_ref(self.tcx, self.tcx.lifetimes.re_erased, expr.ty, deref.mutbl),
157 span,
158 kind: ExprKind::Borrow {
159 borrow_kind: deref.mutbl.to_borrow_kind(),
160 arg: self.thir.exprs.push(expr),
161 },
162 };
163
164 let expr = Box::new([self.thir.exprs.push(expr)]);
165
166 self.overloaded_place(
167 hir_expr,
168 adjustment.target,
169 Some(overloaded_callee),
170 expr,
171 deref.span,
172 )
173 }
174 Adjust::Borrow(AutoBorrow::Ref(m)) => ExprKind::Borrow {
175 borrow_kind: m.to_borrow_kind(),
176 arg: self.thir.exprs.push(expr),
177 },
178 Adjust::Borrow(AutoBorrow::RawPtr(mutability)) => {
179 ExprKind::RawBorrow { mutability, arg: self.thir.exprs.push(expr) }
180 }
181 Adjust::ReborrowPin(mutbl) => {
182 debug!("apply ReborrowPin adjustment");
183 let pin_ty_args = match expr.ty.kind() {
187 ty::Adt(_, args) => args,
188 _ => bug!("ReborrowPin with non-Pin type"),
189 };
190 let pin_ty = pin_ty_args.iter().next().unwrap().expect_ty();
191 let ptr_target_ty = match pin_ty.kind() {
192 ty::Ref(_, ty, _) => *ty,
193 _ => bug!("ReborrowPin with non-Ref type"),
194 };
195
196 let pointer_target = ExprKind::Field {
198 lhs: self.thir.exprs.push(expr),
199 variant_index: FIRST_VARIANT,
200 name: FieldIdx::ZERO,
201 };
202 let arg = Expr { temp_lifetime, ty: pin_ty, span, kind: pointer_target };
203 let arg = self.thir.exprs.push(arg);
204
205 let expr = ExprKind::Deref { arg };
207 let arg = self.thir.exprs.push(Expr {
208 temp_lifetime,
209 ty: ptr_target_ty,
210 span,
211 kind: expr,
212 });
213
214 let borrow_kind = match mutbl {
216 hir::Mutability::Mut => BorrowKind::Mut { kind: mir::MutBorrowKind::Default },
217 hir::Mutability::Not => BorrowKind::Shared,
218 };
219 let new_pin_target =
220 Ty::new_ref(self.tcx, self.tcx.lifetimes.re_erased, ptr_target_ty, mutbl);
221 let expr = self.thir.exprs.push(Expr {
222 temp_lifetime,
223 ty: new_pin_target,
224 span,
225 kind: ExprKind::Borrow { borrow_kind, arg },
226 });
227
228 let pin_did = self.tcx.require_lang_item(rustc_hir::LangItem::Pin, span);
230 let args = self.tcx.mk_args(&[new_pin_target.into()]);
231 let kind = ExprKind::Adt(Box::new(AdtExpr {
232 adt_def: self.tcx.adt_def(pin_did),
233 variant_index: FIRST_VARIANT,
234 args,
235 fields: Box::new([FieldExpr { name: FieldIdx::ZERO, expr }]),
236 user_ty: None,
237 base: AdtExprBase::None,
238 }));
239
240 debug!(?kind);
241 kind
242 }
243 };
244
245 Expr { temp_lifetime, ty: adjustment.target, span, kind }
246 }
247
248 fn mirror_expr_cast(
252 &mut self,
253 source: &'tcx hir::Expr<'tcx>,
254 temp_lifetime: TempLifetime,
255 span: Span,
256 ) -> ExprKind<'tcx> {
257 let tcx = self.tcx;
258
259 if self.typeck_results.is_coercion_cast(source.hir_id) {
262 ExprKind::Use { source: self.mirror_expr(source) }
264 } else if self.typeck_results.expr_ty(source).is_ref() {
265 ExprKind::PointerCoercion {
269 source: self.mirror_expr(source),
270 cast: PointerCoercion::ArrayToPointer,
271 is_from_as_cast: true,
272 }
273 } else if let hir::ExprKind::Path(ref qpath) = source.kind
274 && let res = self.typeck_results.qpath_res(qpath, source.hir_id)
275 && let ty = self.typeck_results.node_type(source.hir_id)
276 && let ty::Adt(adt_def, args) = ty.kind()
277 && let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res
278 {
279 let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id);
292 let (discr_did, discr_offset) = adt_def.discriminant_def_for_variant(idx);
293
294 use rustc_middle::ty::util::IntTypeExt;
295 let ty = adt_def.repr().discr_type();
296 let discr_ty = ty.to_ty(tcx);
297
298 let size = tcx
299 .layout_of(self.typing_env.as_query_input(discr_ty))
300 .unwrap_or_else(|e| panic!("could not compute layout for {discr_ty:?}: {e:?}"))
301 .size;
302
303 let (lit, overflowing) = ScalarInt::truncate_from_uint(discr_offset as u128, size);
304 if overflowing {
305 self.tcx.dcx().span_delayed_bug(
307 source.span,
308 "overflowing enum wasn't rejected by hir analysis",
309 );
310 }
311 let kind = ExprKind::NonHirLiteral { lit, user_ty: None };
312 let offset = self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind });
313
314 let source = match discr_did {
315 Some(did) => {
318 let kind = ExprKind::NamedConst { def_id: did, args, user_ty: None };
319 let lhs =
320 self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind });
321 let bin = ExprKind::Binary { op: BinOp::Add, lhs, rhs: offset };
322 self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind: bin })
323 }
324 None => offset,
325 };
326
327 ExprKind::Cast { source }
328 } else {
329 ExprKind::Cast { source: self.mirror_expr(source) }
332 }
333 }
334
335 #[instrument(level = "debug", skip(self), ret)]
336 fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
337 let tcx = self.tcx;
338 let expr_ty = self.typeck_results.expr_ty(expr);
339 let (temp_lifetime, backwards_incompatible) =
340 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
341
342 let kind = match expr.kind {
343 hir::ExprKind::MethodCall(segment, receiver, args, fn_span) => {
345 let expr = self.method_callee(expr, segment.ident.span, None);
347 info!("Using method span: {:?}", expr.span);
348 let args = std::iter::once(receiver)
349 .chain(args.iter())
350 .map(|expr| self.mirror_expr(expr))
351 .collect();
352 ExprKind::Call {
353 ty: expr.ty,
354 fun: self.thir.exprs.push(expr),
355 args,
356 from_hir_call: true,
357 fn_span,
358 }
359 }
360
361 hir::ExprKind::Call(fun, ref args) => {
362 if self.typeck_results.is_method_call(expr) {
363 let method = self.method_callee(expr, fun.span, None);
371
372 let arg_tys = args.iter().map(|e| self.typeck_results.expr_ty_adjusted(e));
373 let tupled_args = Expr {
374 ty: Ty::new_tup_from_iter(tcx, arg_tys),
375 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
376 span: expr.span,
377 kind: ExprKind::Tuple { fields: self.mirror_exprs(args) },
378 };
379 let tupled_args = self.thir.exprs.push(tupled_args);
380
381 ExprKind::Call {
382 ty: method.ty,
383 fun: self.thir.exprs.push(method),
384 args: Box::new([self.mirror_expr(fun), tupled_args]),
385 from_hir_call: true,
386 fn_span: expr.span,
387 }
388 } else if let ty::FnDef(def_id, _) = self.typeck_results.expr_ty(fun).kind()
389 && let Some(intrinsic) = self.tcx.intrinsic(def_id)
390 && intrinsic.name == sym::box_new
391 {
392 if !matches!(fun.kind, hir::ExprKind::Path(_)) {
394 span_bug!(
395 expr.span,
396 "`box_new` intrinsic can only be called via path expression"
397 );
398 }
399 let value = &args[0];
400 return Expr {
401 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
402 ty: expr_ty,
403 span: expr.span,
404 kind: ExprKind::Box { value: self.mirror_expr(value) },
405 };
406 } else {
407 let adt_data = if let hir::ExprKind::Path(ref qpath) = fun.kind
409 && let Some(adt_def) = expr_ty.ty_adt_def()
410 {
411 match qpath {
412 hir::QPath::Resolved(_, path) => match path.res {
413 Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_id) => {
414 Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
415 }
416 Res::SelfCtor(..) => Some((adt_def, FIRST_VARIANT)),
417 _ => None,
418 },
419 hir::QPath::TypeRelative(_ty, _) => {
420 if let Some((DefKind::Ctor(_, CtorKind::Fn), ctor_id)) =
421 self.typeck_results.type_dependent_def(fun.hir_id)
422 {
423 Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
424 } else {
425 None
426 }
427 }
428 _ => None,
429 }
430 } else {
431 None
432 };
433 if let Some((adt_def, index)) = adt_data {
434 let node_args = self.typeck_results.node_args(fun.hir_id);
435 let user_provided_types = self.typeck_results.user_provided_types();
436 let user_ty =
437 user_provided_types.get(fun.hir_id).copied().map(|mut u_ty| {
438 if let ty::UserTypeKind::TypeOf(did, _) = &mut u_ty.value.kind {
439 *did = adt_def.did();
440 }
441 Box::new(u_ty)
442 });
443 debug!("make_mirror_unadjusted: (call) user_ty={:?}", user_ty);
444
445 let field_refs = args
446 .iter()
447 .enumerate()
448 .map(|(idx, e)| FieldExpr {
449 name: FieldIdx::new(idx),
450 expr: self.mirror_expr(e),
451 })
452 .collect();
453 ExprKind::Adt(Box::new(AdtExpr {
454 adt_def,
455 args: node_args,
456 variant_index: index,
457 fields: field_refs,
458 user_ty,
459 base: AdtExprBase::None,
460 }))
461 } else {
462 ExprKind::Call {
463 ty: self.typeck_results.node_type(fun.hir_id),
464 fun: self.mirror_expr(fun),
465 args: self.mirror_exprs(args),
466 from_hir_call: true,
467 fn_span: expr.span,
468 }
469 }
470 }
471 }
472
473 hir::ExprKind::Use(expr, span) => {
474 ExprKind::ByUse { expr: self.mirror_expr(expr), span }
475 }
476
477 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mutbl, arg) => {
478 ExprKind::Borrow { borrow_kind: mutbl.to_borrow_kind(), arg: self.mirror_expr(arg) }
479 }
480
481 hir::ExprKind::AddrOf(hir::BorrowKind::Raw, mutability, arg) => {
482 ExprKind::RawBorrow { mutability, arg: self.mirror_expr(arg) }
483 }
484
485 hir::ExprKind::AddrOf(hir::BorrowKind::Pin, mutbl, arg_expr) => match expr_ty.kind() {
488 &ty::Adt(adt_def, args) if tcx.is_lang_item(adt_def.did(), hir::LangItem::Pin) => {
489 let ty = args.type_at(0);
490 let arg_ty = self.typeck_results.expr_ty(arg_expr);
491 let mut arg = self.mirror_expr(arg_expr);
492 if mutbl.is_mut() && !arg_ty.is_unpin(self.tcx, self.typing_env) {
495 let block = self.thir.blocks.push(Block {
496 targeted_by_break: false,
497 region_scope: region::Scope {
498 local_id: arg_expr.hir_id.local_id,
499 data: region::ScopeData::Node,
500 },
501 span: arg_expr.span,
502 stmts: Box::new([]),
503 expr: Some(arg),
504 safety_mode: BlockSafety::Safe,
505 });
506 let (temp_lifetime, backwards_incompatible) = self
507 .rvalue_scopes
508 .temporary_scope(self.region_scope_tree, arg_expr.hir_id.local_id);
509 arg = self.thir.exprs.push(Expr {
510 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
511 ty: arg_ty,
512 span: arg_expr.span,
513 kind: ExprKind::Block { block },
514 });
515 }
516 let expr = self.thir.exprs.push(Expr {
517 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
518 ty,
519 span: expr.span,
520 kind: ExprKind::Borrow { borrow_kind: mutbl.to_borrow_kind(), arg },
521 });
522 ExprKind::Adt(Box::new(AdtExpr {
523 adt_def,
524 variant_index: FIRST_VARIANT,
525 args,
526 fields: Box::new([FieldExpr { name: FieldIdx::from(0u32), expr }]),
527 user_ty: None,
528 base: AdtExprBase::None,
529 }))
530 }
531 _ => span_bug!(expr.span, "unexpected type for pinned borrow: {:?}", expr_ty),
532 },
533
534 hir::ExprKind::Block(blk, _) => ExprKind::Block { block: self.mirror_block(blk) },
535
536 hir::ExprKind::Assign(lhs, rhs, _) => {
537 ExprKind::Assign { lhs: self.mirror_expr(lhs), rhs: self.mirror_expr(rhs) }
538 }
539
540 hir::ExprKind::AssignOp(op, lhs, rhs) => {
541 if self.typeck_results.is_method_call(expr) {
542 let lhs = self.mirror_expr(lhs);
543 let rhs = self.mirror_expr(rhs);
544 self.overloaded_operator(expr, Box::new([lhs, rhs]))
545 } else {
546 ExprKind::AssignOp {
547 op: assign_op(op.node),
548 lhs: self.mirror_expr(lhs),
549 rhs: self.mirror_expr(rhs),
550 }
551 }
552 }
553
554 hir::ExprKind::Lit(lit) => ExprKind::Literal { lit, neg: false },
555
556 hir::ExprKind::Binary(op, lhs, rhs) => {
557 if self.typeck_results.is_method_call(expr) {
558 let lhs = self.mirror_expr(lhs);
559 let rhs = self.mirror_expr(rhs);
560 self.overloaded_operator(expr, Box::new([lhs, rhs]))
561 } else {
562 match op.node {
563 hir::BinOpKind::And => ExprKind::LogicalOp {
564 op: LogicalOp::And,
565 lhs: self.mirror_expr(lhs),
566 rhs: self.mirror_expr(rhs),
567 },
568 hir::BinOpKind::Or => ExprKind::LogicalOp {
569 op: LogicalOp::Or,
570 lhs: self.mirror_expr(lhs),
571 rhs: self.mirror_expr(rhs),
572 },
573 _ => {
574 let op = bin_op(op.node);
575 ExprKind::Binary {
576 op,
577 lhs: self.mirror_expr(lhs),
578 rhs: self.mirror_expr(rhs),
579 }
580 }
581 }
582 }
583 }
584
585 hir::ExprKind::Index(lhs, index, brackets_span) => {
586 if self.typeck_results.is_method_call(expr) {
587 let lhs = self.mirror_expr(lhs);
588 let index = self.mirror_expr(index);
589 self.overloaded_place(
590 expr,
591 expr_ty,
592 None,
593 Box::new([lhs, index]),
594 brackets_span,
595 )
596 } else {
597 ExprKind::Index { lhs: self.mirror_expr(lhs), index: self.mirror_expr(index) }
598 }
599 }
600
601 hir::ExprKind::Unary(hir::UnOp::Deref, arg) => {
602 if self.typeck_results.is_method_call(expr) {
603 let arg = self.mirror_expr(arg);
604 self.overloaded_place(expr, expr_ty, None, Box::new([arg]), expr.span)
605 } else {
606 ExprKind::Deref { arg: self.mirror_expr(arg) }
607 }
608 }
609
610 hir::ExprKind::Unary(hir::UnOp::Not, arg) => {
611 if self.typeck_results.is_method_call(expr) {
612 let arg = self.mirror_expr(arg);
613 self.overloaded_operator(expr, Box::new([arg]))
614 } else {
615 ExprKind::Unary { op: UnOp::Not, arg: self.mirror_expr(arg) }
616 }
617 }
618
619 hir::ExprKind::Unary(hir::UnOp::Neg, arg) => {
620 if self.typeck_results.is_method_call(expr) {
621 let arg = self.mirror_expr(arg);
622 self.overloaded_operator(expr, Box::new([arg]))
623 } else if let hir::ExprKind::Lit(lit) = arg.kind {
624 ExprKind::Literal { lit, neg: true }
625 } else {
626 ExprKind::Unary { op: UnOp::Neg, arg: self.mirror_expr(arg) }
627 }
628 }
629
630 hir::ExprKind::Struct(qpath, fields, ref base) => match expr_ty.kind() {
631 ty::Adt(adt, args) => match adt.adt_kind() {
632 AdtKind::Struct | AdtKind::Union => {
633 let user_provided_types = self.typeck_results.user_provided_types();
634 let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
635 debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty);
636 ExprKind::Adt(Box::new(AdtExpr {
637 adt_def: *adt,
638 variant_index: FIRST_VARIANT,
639 args,
640 user_ty,
641 fields: self.field_refs(fields),
642 base: match base {
643 hir::StructTailExpr::Base(base) => AdtExprBase::Base(FruInfo {
644 base: self.mirror_expr(base),
645 field_types: self.typeck_results.fru_field_types()[expr.hir_id]
646 .iter()
647 .copied()
648 .collect(),
649 }),
650 hir::StructTailExpr::DefaultFields(_) => {
651 AdtExprBase::DefaultFields(
652 self.typeck_results.fru_field_types()[expr.hir_id]
653 .iter()
654 .copied()
655 .collect(),
656 )
657 }
658 hir::StructTailExpr::None => AdtExprBase::None,
659 },
660 }))
661 }
662 AdtKind::Enum => {
663 let res = self.typeck_results.qpath_res(qpath, expr.hir_id);
664 match res {
665 Res::Def(DefKind::Variant, variant_id) => {
666 assert!(matches!(
667 base,
668 hir::StructTailExpr::None
669 | hir::StructTailExpr::DefaultFields(_)
670 ));
671
672 let index = adt.variant_index_with_id(variant_id);
673 let user_provided_types = self.typeck_results.user_provided_types();
674 let user_ty =
675 user_provided_types.get(expr.hir_id).copied().map(Box::new);
676 debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty);
677 ExprKind::Adt(Box::new(AdtExpr {
678 adt_def: *adt,
679 variant_index: index,
680 args,
681 user_ty,
682 fields: self.field_refs(fields),
683 base: match base {
684 hir::StructTailExpr::DefaultFields(_) => {
685 AdtExprBase::DefaultFields(
686 self.typeck_results.fru_field_types()[expr.hir_id]
687 .iter()
688 .copied()
689 .collect(),
690 )
691 }
692 hir::StructTailExpr::Base(base) => {
693 span_bug!(base.span, "unexpected res: {:?}", res);
694 }
695 hir::StructTailExpr::None => AdtExprBase::None,
696 },
697 }))
698 }
699 _ => {
700 span_bug!(expr.span, "unexpected res: {:?}", res);
701 }
702 }
703 }
704 },
705 _ => {
706 span_bug!(expr.span, "unexpected type for struct literal: {:?}", expr_ty);
707 }
708 },
709
710 hir::ExprKind::Closure(hir::Closure { .. }) => {
711 let closure_ty = self.typeck_results.expr_ty(expr);
712 let (def_id, args, movability) = match *closure_ty.kind() {
713 ty::Closure(def_id, args) => (def_id, UpvarArgs::Closure(args), None),
714 ty::Coroutine(def_id, args) => {
715 (def_id, UpvarArgs::Coroutine(args), Some(tcx.coroutine_movability(def_id)))
716 }
717 ty::CoroutineClosure(def_id, args) => {
718 (def_id, UpvarArgs::CoroutineClosure(args), None)
719 }
720 _ => {
721 span_bug!(expr.span, "closure expr w/o closure type: {:?}", closure_ty);
722 }
723 };
724 let def_id = def_id.expect_local();
725
726 let upvars = self
727 .tcx
728 .closure_captures(def_id)
729 .iter()
730 .zip_eq(args.upvar_tys())
731 .map(|(captured_place, ty)| {
732 let upvars = self.capture_upvar(expr, captured_place, ty);
733 self.thir.exprs.push(upvars)
734 })
735 .collect();
736
737 let fake_reads = match self.typeck_results.closure_fake_reads.get(&def_id) {
739 Some(fake_reads) => fake_reads
740 .iter()
741 .map(|(place, cause, hir_id)| {
742 let expr = self.convert_captured_hir_place(expr, place.clone());
743 (self.thir.exprs.push(expr), *cause, *hir_id)
744 })
745 .collect(),
746 None => Vec::new(),
747 };
748
749 ExprKind::Closure(Box::new(ClosureExpr {
750 closure_id: def_id,
751 args,
752 upvars,
753 movability,
754 fake_reads,
755 }))
756 }
757
758 hir::ExprKind::Path(ref qpath) => {
759 let res = self.typeck_results.qpath_res(qpath, expr.hir_id);
760 self.convert_path_expr(expr, res)
761 }
762
763 hir::ExprKind::InlineAsm(asm) => ExprKind::InlineAsm(Box::new(InlineAsmExpr {
764 asm_macro: asm.asm_macro,
765 template: asm.template,
766 operands: asm
767 .operands
768 .iter()
769 .map(|(op, _op_sp)| match *op {
770 hir::InlineAsmOperand::In { reg, expr } => {
771 InlineAsmOperand::In { reg, expr: self.mirror_expr(expr) }
772 }
773 hir::InlineAsmOperand::Out { reg, late, ref expr } => {
774 InlineAsmOperand::Out {
775 reg,
776 late,
777 expr: expr.map(|expr| self.mirror_expr(expr)),
778 }
779 }
780 hir::InlineAsmOperand::InOut { reg, late, expr } => {
781 InlineAsmOperand::InOut { reg, late, expr: self.mirror_expr(expr) }
782 }
783 hir::InlineAsmOperand::SplitInOut { reg, late, in_expr, ref out_expr } => {
784 InlineAsmOperand::SplitInOut {
785 reg,
786 late,
787 in_expr: self.mirror_expr(in_expr),
788 out_expr: out_expr.map(|expr| self.mirror_expr(expr)),
789 }
790 }
791 hir::InlineAsmOperand::Const { ref anon_const } => {
792 let ty = self.typeck_results.node_type(anon_const.hir_id);
793 let did = anon_const.def_id.to_def_id();
794 let typeck_root_def_id = tcx.typeck_root_def_id(did);
795 let parent_args = tcx.erase_and_anonymize_regions(
796 GenericArgs::identity_for_item(tcx, typeck_root_def_id),
797 );
798 let args =
799 InlineConstArgs::new(tcx, InlineConstArgsParts { parent_args, ty })
800 .args;
801
802 let uneval = mir::UnevaluatedConst::new(did, args);
803 let value = mir::Const::Unevaluated(uneval, ty);
804 InlineAsmOperand::Const { value, span: tcx.def_span(did) }
805 }
806 hir::InlineAsmOperand::SymFn { expr } => {
807 InlineAsmOperand::SymFn { value: self.mirror_expr(expr) }
808 }
809 hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
810 InlineAsmOperand::SymStatic { def_id }
811 }
812 hir::InlineAsmOperand::Label { block } => {
813 InlineAsmOperand::Label { block: self.mirror_block(block) }
814 }
815 })
816 .collect(),
817 options: asm.options,
818 line_spans: asm.line_spans,
819 })),
820
821 hir::ExprKind::OffsetOf(_, _) => {
822 let data = self.typeck_results.offset_of_data();
823 let &(container, ref indices) = data.get(expr.hir_id).unwrap();
824 let fields = tcx.mk_offset_of_from_iter(indices.iter().copied());
825
826 ExprKind::OffsetOf { container, fields }
827 }
828
829 hir::ExprKind::ConstBlock(ref anon_const) => {
830 let ty = self.typeck_results.node_type(anon_const.hir_id);
831 let did = anon_const.def_id.to_def_id();
832 let typeck_root_def_id = tcx.typeck_root_def_id(did);
833 let parent_args = tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(
834 tcx,
835 typeck_root_def_id,
836 ));
837 let args = InlineConstArgs::new(tcx, InlineConstArgsParts { parent_args, ty }).args;
838
839 ExprKind::ConstBlock { did, args }
840 }
841 hir::ExprKind::Repeat(v, _) => {
843 let ty = self.typeck_results.expr_ty(expr);
844 let ty::Array(_, count) = ty.kind() else {
845 span_bug!(expr.span, "unexpected repeat expr ty: {:?}", ty);
846 };
847
848 ExprKind::Repeat { value: self.mirror_expr(v), count: *count }
849 }
850 hir::ExprKind::Ret(v) => ExprKind::Return { value: v.map(|v| self.mirror_expr(v)) },
851 hir::ExprKind::Become(call) => ExprKind::Become { value: self.mirror_expr(call) },
852 hir::ExprKind::Break(dest, ref value) => {
853 if find_attr!(self.tcx.hir_attrs(expr.hir_id), AttributeKind::ConstContinue(_)) {
854 match dest.target_id {
855 Ok(target_id) => {
856 let (Some(value), Some(_)) = (value, dest.label) else {
857 let span = expr.span;
858 self.tcx.dcx().emit_fatal(ConstContinueMissingLabelOrValue { span })
859 };
860
861 ExprKind::ConstContinue {
862 label: region::Scope {
863 local_id: target_id.local_id,
864 data: region::ScopeData::Node,
865 },
866 value: self.mirror_expr(value),
867 }
868 }
869 Err(err) => bug!("invalid loop id for break: {}", err),
870 }
871 } else {
872 match dest.target_id {
873 Ok(target_id) => ExprKind::Break {
874 label: region::Scope {
875 local_id: target_id.local_id,
876 data: region::ScopeData::Node,
877 },
878 value: value.map(|value| self.mirror_expr(value)),
879 },
880 Err(err) => bug!("invalid loop id for break: {}", err),
881 }
882 }
883 }
884 hir::ExprKind::Continue(dest) => match dest.target_id {
885 Ok(loop_id) => ExprKind::Continue {
886 label: region::Scope {
887 local_id: loop_id.local_id,
888 data: region::ScopeData::Node,
889 },
890 },
891 Err(err) => bug!("invalid loop id for continue: {}", err),
892 },
893 hir::ExprKind::Let(let_expr) => ExprKind::Let {
894 expr: self.mirror_expr(let_expr.init),
895 pat: self.pattern_from_hir(let_expr.pat),
896 },
897 hir::ExprKind::If(cond, then, else_opt) => ExprKind::If {
898 if_then_scope: region::Scope {
899 local_id: then.hir_id.local_id,
900 data: {
901 if expr.span.at_least_rust_2024() {
902 region::ScopeData::IfThenRescope
903 } else {
904 region::ScopeData::IfThen
905 }
906 },
907 },
908 cond: self.mirror_expr(cond),
909 then: self.mirror_expr(then),
910 else_opt: else_opt.map(|el| self.mirror_expr(el)),
911 },
912 hir::ExprKind::Match(discr, arms, match_source) => ExprKind::Match {
913 scrutinee: self.mirror_expr(discr),
914 arms: arms.iter().map(|a| self.convert_arm(a)).collect(),
915 match_source,
916 },
917 hir::ExprKind::Loop(body, ..) => {
918 if find_attr!(self.tcx.hir_attrs(expr.hir_id), AttributeKind::LoopMatch(_)) {
919 let dcx = self.tcx.dcx();
920
921 let loop_body_expr = match body.stmts {
923 [] => match body.expr {
924 Some(expr) => expr,
925 None => dcx.emit_fatal(LoopMatchMissingAssignment { span: body.span }),
926 },
927 [single] if body.expr.is_none() => match single.kind {
928 hir::StmtKind::Expr(expr) | hir::StmtKind::Semi(expr) => expr,
929 _ => dcx.emit_fatal(LoopMatchMissingAssignment { span: body.span }),
930 },
931 [first @ last] | [first, .., last] => dcx
932 .emit_fatal(LoopMatchBadStatements { span: first.span.to(last.span) }),
933 };
934
935 let hir::ExprKind::Assign(state, rhs_expr, _) = loop_body_expr.kind else {
936 dcx.emit_fatal(LoopMatchMissingAssignment { span: loop_body_expr.span })
937 };
938
939 let hir::ExprKind::Block(block_body, _) = rhs_expr.kind else {
940 dcx.emit_fatal(LoopMatchBadRhs { span: rhs_expr.span })
941 };
942
943 for stmt in block_body.stmts {
946 if !matches!(stmt.kind, rustc_hir::StmtKind::Item(_)) {
947 dcx.emit_fatal(LoopMatchBadStatements { span: stmt.span })
948 }
949 }
950
951 let Some(block_body_expr) = block_body.expr else {
952 dcx.emit_fatal(LoopMatchBadRhs { span: block_body.span })
953 };
954
955 let hir::ExprKind::Match(scrutinee, arms, _match_source) = block_body_expr.kind
956 else {
957 dcx.emit_fatal(LoopMatchBadRhs { span: block_body_expr.span })
958 };
959
960 fn local(
961 cx: &mut ThirBuildCx<'_>,
962 expr: &rustc_hir::Expr<'_>,
963 ) -> Option<hir::HirId> {
964 if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind
965 && let Res::Local(hir_id) = path.res
966 && !cx.is_upvar(hir_id)
967 {
968 return Some(hir_id);
969 }
970
971 None
972 }
973
974 let Some(scrutinee_hir_id) = local(self, scrutinee) else {
975 dcx.emit_fatal(LoopMatchInvalidMatch { span: scrutinee.span })
976 };
977
978 if local(self, state) != Some(scrutinee_hir_id) {
979 dcx.emit_fatal(LoopMatchInvalidUpdate {
980 scrutinee: scrutinee.span,
981 lhs: state.span,
982 })
983 }
984
985 ExprKind::LoopMatch {
986 state: self.mirror_expr(state),
987 region_scope: region::Scope {
988 local_id: block_body.hir_id.local_id,
989 data: region::ScopeData::Node,
990 },
991
992 match_data: Box::new(LoopMatchMatchData {
993 scrutinee: self.mirror_expr(scrutinee),
994 arms: arms.iter().map(|a| self.convert_arm(a)).collect(),
995 span: block_body_expr.span,
996 }),
997 }
998 } else {
999 let block_ty = self.typeck_results.node_type(body.hir_id);
1000 let (temp_lifetime, backwards_incompatible) = self
1001 .rvalue_scopes
1002 .temporary_scope(self.region_scope_tree, body.hir_id.local_id);
1003 let block = self.mirror_block(body);
1004 let body = self.thir.exprs.push(Expr {
1005 ty: block_ty,
1006 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1007 span: self.thir[block].span,
1008 kind: ExprKind::Block { block },
1009 });
1010 ExprKind::Loop { body }
1011 }
1012 }
1013 hir::ExprKind::Field(source, ..) => ExprKind::Field {
1014 lhs: self.mirror_expr(source),
1015 variant_index: FIRST_VARIANT,
1016 name: self.typeck_results.field_index(expr.hir_id),
1017 },
1018 hir::ExprKind::Cast(source, cast_ty) => {
1019 let user_provided_types = self.typeck_results.user_provided_types();
1021 let user_ty = user_provided_types.get(cast_ty.hir_id);
1022
1023 debug!(
1024 "cast({:?}) has ty w/ hir_id {:?} and user provided ty {:?}",
1025 expr, cast_ty.hir_id, user_ty,
1026 );
1027
1028 let cast = self.mirror_expr_cast(
1029 source,
1030 TempLifetime { temp_lifetime, backwards_incompatible },
1031 expr.span,
1032 );
1033
1034 if let Some(user_ty) = user_ty {
1035 let cast_expr = self.thir.exprs.push(Expr {
1038 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1039 ty: expr_ty,
1040 span: expr.span,
1041 kind: cast,
1042 });
1043 debug!("make_mirror_unadjusted: (cast) user_ty={:?}", user_ty);
1044
1045 ExprKind::ValueTypeAscription {
1046 source: cast_expr,
1047 user_ty: Some(Box::new(*user_ty)),
1048 user_ty_span: cast_ty.span,
1049 }
1050 } else {
1051 cast
1052 }
1053 }
1054 hir::ExprKind::Type(source, ty) => {
1055 let user_provided_types = self.typeck_results.user_provided_types();
1056 let user_ty = user_provided_types.get(ty.hir_id).copied().map(Box::new);
1057 debug!("make_mirror_unadjusted: (type) user_ty={:?}", user_ty);
1058 let mirrored = self.mirror_expr(source);
1059 if source.is_syntactic_place_expr() {
1060 ExprKind::PlaceTypeAscription {
1061 source: mirrored,
1062 user_ty,
1063 user_ty_span: ty.span,
1064 }
1065 } else {
1066 ExprKind::ValueTypeAscription {
1067 source: mirrored,
1068 user_ty,
1069 user_ty_span: ty.span,
1070 }
1071 }
1072 }
1073
1074 hir::ExprKind::UnsafeBinderCast(UnsafeBinderCastKind::Unwrap, source, _ty) => {
1075 let mirrored = self.mirror_expr(source);
1077 if source.is_syntactic_place_expr() {
1078 ExprKind::PlaceUnwrapUnsafeBinder { source: mirrored }
1079 } else {
1080 ExprKind::ValueUnwrapUnsafeBinder { source: mirrored }
1081 }
1082 }
1083 hir::ExprKind::UnsafeBinderCast(UnsafeBinderCastKind::Wrap, source, _ty) => {
1084 let mirrored = self.mirror_expr(source);
1086 ExprKind::WrapUnsafeBinder { source: mirrored }
1087 }
1088
1089 hir::ExprKind::DropTemps(source) => ExprKind::Use { source: self.mirror_expr(source) },
1090 hir::ExprKind::Array(fields) => ExprKind::Array { fields: self.mirror_exprs(fields) },
1091 hir::ExprKind::Tup(fields) => ExprKind::Tuple { fields: self.mirror_exprs(fields) },
1092
1093 hir::ExprKind::Yield(v, _) => ExprKind::Yield { value: self.mirror_expr(v) },
1094 hir::ExprKind::Err(_) => unreachable!("cannot lower a `hir::ExprKind::Err` to THIR"),
1095 };
1096
1097 Expr {
1098 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1099 ty: expr_ty,
1100 span: expr.span,
1101 kind,
1102 }
1103 }
1104
1105 fn user_args_applied_to_res(
1106 &mut self,
1107 hir_id: hir::HirId,
1108 res: Res,
1109 ) -> Option<Box<ty::CanonicalUserType<'tcx>>> {
1110 debug!("user_args_applied_to_res: res={:?}", res);
1111 let user_provided_type = match res {
1112 Res::Def(DefKind::Fn, _)
1116 | Res::Def(DefKind::AssocFn, _)
1117 | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
1118 | Res::Def(DefKind::Const, _)
1119 | Res::Def(DefKind::AssocConst, _) => {
1120 self.typeck_results.user_provided_types().get(hir_id).copied().map(Box::new)
1121 }
1122
1123 Res::Def(DefKind::Ctor(_, CtorKind::Const), _) => {
1128 self.user_args_applied_to_ty_of_hir_id(hir_id).map(Box::new)
1129 }
1130
1131 Res::SelfCtor(_) => self.user_args_applied_to_ty_of_hir_id(hir_id).map(Box::new),
1133
1134 _ => bug!("user_args_applied_to_res: unexpected res {:?} at {:?}", res, hir_id),
1135 };
1136 debug!("user_args_applied_to_res: user_provided_type={:?}", user_provided_type);
1137 user_provided_type
1138 }
1139
1140 fn method_callee(
1141 &mut self,
1142 expr: &hir::Expr<'_>,
1143 span: Span,
1144 overloaded_callee: Option<Ty<'tcx>>,
1145 ) -> Expr<'tcx> {
1146 let (temp_lifetime, backwards_incompatible) =
1147 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
1148 let (ty, user_ty) = match overloaded_callee {
1149 Some(fn_def) => (fn_def, None),
1150 None => {
1151 let (kind, def_id) =
1152 self.typeck_results.type_dependent_def(expr.hir_id).unwrap_or_else(|| {
1153 span_bug!(expr.span, "no type-dependent def for method callee")
1154 });
1155 let user_ty = self.user_args_applied_to_res(expr.hir_id, Res::Def(kind, def_id));
1156 debug!("method_callee: user_ty={:?}", user_ty);
1157 (
1158 Ty::new_fn_def(self.tcx, def_id, self.typeck_results.node_args(expr.hir_id)),
1159 user_ty,
1160 )
1161 }
1162 };
1163 Expr {
1164 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1165 ty,
1166 span,
1167 kind: ExprKind::ZstLiteral { user_ty },
1168 }
1169 }
1170
1171 fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
1172 let arm = Arm {
1173 pattern: self.pattern_from_hir(&arm.pat),
1174 guard: arm.guard.as_ref().map(|g| self.mirror_expr(g)),
1175 body: self.mirror_expr(arm.body),
1176 lint_level: LintLevel::Explicit(arm.hir_id),
1177 scope: region::Scope { local_id: arm.hir_id.local_id, data: region::ScopeData::Node },
1178 span: arm.span,
1179 };
1180 self.thir.arms.push(arm)
1181 }
1182
1183 fn convert_path_expr(&mut self, expr: &'tcx hir::Expr<'tcx>, res: Res) -> ExprKind<'tcx> {
1184 let args = self.typeck_results.node_args(expr.hir_id);
1185 match res {
1186 Res::Def(DefKind::Fn, _)
1188 | Res::Def(DefKind::AssocFn, _)
1189 | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
1190 | Res::SelfCtor(_) => {
1191 let user_ty = self.user_args_applied_to_res(expr.hir_id, res);
1192 ExprKind::ZstLiteral { user_ty }
1193 }
1194
1195 Res::Def(DefKind::ConstParam, def_id) => {
1196 let hir_id = self.tcx.local_def_id_to_hir_id(def_id.expect_local());
1197 let generics = self.tcx.generics_of(hir_id.owner);
1198 let Some(&index) = generics.param_def_id_to_index.get(&def_id) else {
1199 span_bug!(
1200 expr.span,
1201 "Should have already errored about late bound consts: {def_id:?}"
1202 );
1203 };
1204 let name = self.tcx.hir_name(hir_id);
1205 let param = ty::ParamConst::new(index, name);
1206
1207 ExprKind::ConstParam { param, def_id }
1208 }
1209
1210 Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
1211 let user_ty = self.user_args_applied_to_res(expr.hir_id, res);
1212 ExprKind::NamedConst { def_id, args, user_ty }
1213 }
1214
1215 Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id) => {
1216 let user_provided_types = self.typeck_results.user_provided_types();
1217 let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
1218 debug!("convert_path_expr: user_ty={:?}", user_ty);
1219 let ty = self.typeck_results.node_type(expr.hir_id);
1220 match ty.kind() {
1221 ty::Adt(adt_def, args) => ExprKind::Adt(Box::new(AdtExpr {
1224 adt_def: *adt_def,
1225 variant_index: adt_def.variant_index_with_ctor_id(def_id),
1226 args,
1227 user_ty,
1228 fields: Box::new([]),
1229 base: AdtExprBase::None,
1230 })),
1231 _ => bug!("unexpected ty: {:?}", ty),
1232 }
1233 }
1234
1235 Res::Def(DefKind::Static { .. }, id) => {
1239 let ty = self.tcx.static_ptr_ty(id, self.typing_env);
1241 let (temp_lifetime, backwards_incompatible) = self
1242 .rvalue_scopes
1243 .temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
1244 let kind = if self.tcx.is_thread_local_static(id) {
1245 ExprKind::ThreadLocalRef(id)
1246 } else {
1247 let alloc_id = self.tcx.reserve_and_set_static_alloc(id);
1248 ExprKind::StaticRef { alloc_id, ty, def_id: id }
1249 };
1250 ExprKind::Deref {
1251 arg: self.thir.exprs.push(Expr {
1252 ty,
1253 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1254 span: expr.span,
1255 kind,
1256 }),
1257 }
1258 }
1259
1260 Res::Local(var_hir_id) => self.convert_var(var_hir_id),
1261
1262 _ => span_bug!(expr.span, "res `{:?}` not yet implemented", res),
1263 }
1264 }
1265
1266 fn convert_var(&mut self, var_hir_id: hir::HirId) -> ExprKind<'tcx> {
1267 let is_upvar = self.is_upvar(var_hir_id);
1270
1271 debug!(
1272 "convert_var({:?}): is_upvar={}, body_owner={:?}",
1273 var_hir_id, is_upvar, self.body_owner
1274 );
1275
1276 if is_upvar {
1277 ExprKind::UpvarRef {
1278 closure_def_id: self.body_owner,
1279 var_hir_id: LocalVarId(var_hir_id),
1280 }
1281 } else {
1282 ExprKind::VarRef { id: LocalVarId(var_hir_id) }
1283 }
1284 }
1285
1286 fn overloaded_operator(
1287 &mut self,
1288 expr: &'tcx hir::Expr<'tcx>,
1289 args: Box<[ExprId]>,
1290 ) -> ExprKind<'tcx> {
1291 let fun = self.method_callee(expr, expr.span, None);
1292 let fun = self.thir.exprs.push(fun);
1293 ExprKind::Call {
1294 ty: self.thir[fun].ty,
1295 fun,
1296 args,
1297 from_hir_call: false,
1298 fn_span: expr.span,
1299 }
1300 }
1301
1302 fn overloaded_place(
1303 &mut self,
1304 expr: &'tcx hir::Expr<'tcx>,
1305 place_ty: Ty<'tcx>,
1306 overloaded_callee: Option<Ty<'tcx>>,
1307 args: Box<[ExprId]>,
1308 span: Span,
1309 ) -> ExprKind<'tcx> {
1310 let ty::Ref(region, _, mutbl) = *self.thir[args[0]].ty.kind() else {
1318 span_bug!(span, "overloaded_place: receiver is not a reference");
1319 };
1320 let ref_ty = Ty::new_ref(self.tcx, region, place_ty, mutbl);
1321
1322 let (temp_lifetime, backwards_incompatible) =
1325 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
1326 let fun = self.method_callee(expr, span, overloaded_callee);
1327 let fun = self.thir.exprs.push(fun);
1328 let fun_ty = self.thir[fun].ty;
1329 let ref_expr = self.thir.exprs.push(Expr {
1330 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1331 ty: ref_ty,
1332 span,
1333 kind: ExprKind::Call { ty: fun_ty, fun, args, from_hir_call: false, fn_span: span },
1334 });
1335
1336 ExprKind::Deref { arg: ref_expr }
1338 }
1339
1340 fn convert_captured_hir_place(
1341 &mut self,
1342 closure_expr: &'tcx hir::Expr<'tcx>,
1343 place: HirPlace<'tcx>,
1344 ) -> Expr<'tcx> {
1345 let (temp_lifetime, backwards_incompatible) = self
1346 .rvalue_scopes
1347 .temporary_scope(self.region_scope_tree, closure_expr.hir_id.local_id);
1348 let var_ty = place.base_ty;
1349
1350 let var_hir_id = match place.base {
1356 HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
1357 base => bug!("Expected an upvar, found {:?}", base),
1358 };
1359
1360 let mut captured_place_expr = Expr {
1361 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1362 ty: var_ty,
1363 span: closure_expr.span,
1364 kind: self.convert_var(var_hir_id),
1365 };
1366
1367 for proj in place.projections.iter() {
1368 let kind = match proj.kind {
1369 HirProjectionKind::Deref => {
1370 ExprKind::Deref { arg: self.thir.exprs.push(captured_place_expr) }
1371 }
1372 HirProjectionKind::Field(field, variant_index) => ExprKind::Field {
1373 lhs: self.thir.exprs.push(captured_place_expr),
1374 variant_index,
1375 name: field,
1376 },
1377 HirProjectionKind::OpaqueCast => {
1378 ExprKind::Use { source: self.thir.exprs.push(captured_place_expr) }
1379 }
1380 HirProjectionKind::UnwrapUnsafeBinder => ExprKind::PlaceUnwrapUnsafeBinder {
1381 source: self.thir.exprs.push(captured_place_expr),
1382 },
1383 HirProjectionKind::Index | HirProjectionKind::Subslice => {
1384 continue;
1386 }
1387 };
1388
1389 captured_place_expr = Expr {
1390 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1391 ty: proj.ty,
1392 span: closure_expr.span,
1393 kind,
1394 };
1395 }
1396
1397 captured_place_expr
1398 }
1399
1400 fn capture_upvar(
1401 &mut self,
1402 closure_expr: &'tcx hir::Expr<'tcx>,
1403 captured_place: &'tcx ty::CapturedPlace<'tcx>,
1404 upvar_ty: Ty<'tcx>,
1405 ) -> Expr<'tcx> {
1406 let upvar_capture = captured_place.info.capture_kind;
1407 let captured_place_expr =
1408 self.convert_captured_hir_place(closure_expr, captured_place.place.clone());
1409 let (temp_lifetime, backwards_incompatible) = self
1410 .rvalue_scopes
1411 .temporary_scope(self.region_scope_tree, closure_expr.hir_id.local_id);
1412
1413 match upvar_capture {
1414 ty::UpvarCapture::ByValue => captured_place_expr,
1415 ty::UpvarCapture::ByUse => {
1416 let span = captured_place_expr.span;
1417 let expr_id = self.thir.exprs.push(captured_place_expr);
1418
1419 Expr {
1420 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1421 ty: upvar_ty,
1422 span: closure_expr.span,
1423 kind: ExprKind::ByUse { expr: expr_id, span },
1424 }
1425 }
1426 ty::UpvarCapture::ByRef(upvar_borrow) => {
1427 let borrow_kind = match upvar_borrow {
1428 ty::BorrowKind::Immutable => BorrowKind::Shared,
1429 ty::BorrowKind::UniqueImmutable => {
1430 BorrowKind::Mut { kind: mir::MutBorrowKind::ClosureCapture }
1431 }
1432 ty::BorrowKind::Mutable => {
1433 BorrowKind::Mut { kind: mir::MutBorrowKind::Default }
1434 }
1435 };
1436 Expr {
1437 temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1438 ty: upvar_ty,
1439 span: closure_expr.span,
1440 kind: ExprKind::Borrow {
1441 borrow_kind,
1442 arg: self.thir.exprs.push(captured_place_expr),
1443 },
1444 }
1445 }
1446 }
1447 }
1448
1449 fn is_upvar(&mut self, var_hir_id: hir::HirId) -> bool {
1450 self.tcx
1451 .upvars_mentioned(self.body_owner)
1452 .is_some_and(|upvars| upvars.contains_key(&var_hir_id))
1453 }
1454
1455 fn field_refs(&mut self, fields: &'tcx [hir::ExprField<'tcx>]) -> Box<[FieldExpr]> {
1457 fields
1458 .iter()
1459 .map(|field| FieldExpr {
1460 name: self.typeck_results.field_index(field.hir_id),
1461 expr: self.mirror_expr(field.expr),
1462 })
1463 .collect()
1464 }
1465}
1466
1467trait ToBorrowKind {
1468 fn to_borrow_kind(&self) -> BorrowKind;
1469}
1470
1471impl ToBorrowKind for AutoBorrowMutability {
1472 fn to_borrow_kind(&self) -> BorrowKind {
1473 use rustc_middle::ty::adjustment::AllowTwoPhase;
1474 match *self {
1475 AutoBorrowMutability::Mut { allow_two_phase_borrow } => BorrowKind::Mut {
1476 kind: match allow_two_phase_borrow {
1477 AllowTwoPhase::Yes => mir::MutBorrowKind::TwoPhaseBorrow,
1478 AllowTwoPhase::No => mir::MutBorrowKind::Default,
1479 },
1480 },
1481 AutoBorrowMutability::Not => BorrowKind::Shared,
1482 }
1483 }
1484}
1485
1486impl ToBorrowKind for hir::Mutability {
1487 fn to_borrow_kind(&self) -> BorrowKind {
1488 match *self {
1489 hir::Mutability::Mut => BorrowKind::Mut { kind: mir::MutBorrowKind::Default },
1490 hir::Mutability::Not => BorrowKind::Shared,
1491 }
1492 }
1493}
1494
1495fn bin_op(op: hir::BinOpKind) -> BinOp {
1496 match op {
1497 hir::BinOpKind::Add => BinOp::Add,
1498 hir::BinOpKind::Sub => BinOp::Sub,
1499 hir::BinOpKind::Mul => BinOp::Mul,
1500 hir::BinOpKind::Div => BinOp::Div,
1501 hir::BinOpKind::Rem => BinOp::Rem,
1502 hir::BinOpKind::BitXor => BinOp::BitXor,
1503 hir::BinOpKind::BitAnd => BinOp::BitAnd,
1504 hir::BinOpKind::BitOr => BinOp::BitOr,
1505 hir::BinOpKind::Shl => BinOp::Shl,
1506 hir::BinOpKind::Shr => BinOp::Shr,
1507 hir::BinOpKind::Eq => BinOp::Eq,
1508 hir::BinOpKind::Lt => BinOp::Lt,
1509 hir::BinOpKind::Le => BinOp::Le,
1510 hir::BinOpKind::Ne => BinOp::Ne,
1511 hir::BinOpKind::Ge => BinOp::Ge,
1512 hir::BinOpKind::Gt => BinOp::Gt,
1513 _ => bug!("no equivalent for ast binop {:?}", op),
1514 }
1515}
1516
1517fn assign_op(op: hir::AssignOpKind) -> AssignOp {
1518 match op {
1519 hir::AssignOpKind::AddAssign => AssignOp::AddAssign,
1520 hir::AssignOpKind::SubAssign => AssignOp::SubAssign,
1521 hir::AssignOpKind::MulAssign => AssignOp::MulAssign,
1522 hir::AssignOpKind::DivAssign => AssignOp::DivAssign,
1523 hir::AssignOpKind::RemAssign => AssignOp::RemAssign,
1524 hir::AssignOpKind::BitXorAssign => AssignOp::BitXorAssign,
1525 hir::AssignOpKind::BitAndAssign => AssignOp::BitAndAssign,
1526 hir::AssignOpKind::BitOrAssign => AssignOp::BitOrAssign,
1527 hir::AssignOpKind::ShlAssign => AssignOp::ShlAssign,
1528 hir::AssignOpKind::ShrAssign => AssignOp::ShrAssign,
1529 }
1530}