1use itertools::Itertools;
7use rustc_abi::{ExternAbi, FieldIdx};
8use rustc_apfloat::Float;
9use rustc_apfloat::ieee::{Double, Half, Quad, Single};
10use rustc_ast::attr;
11use rustc_data_structures::fx::FxHashMap;
12use rustc_data_structures::sorted_map::SortedIndexMultiMap;
13use rustc_errors::ErrorGuaranteed;
14use rustc_hir::attrs::AttributeKind;
15use rustc_hir::def::DefKind;
16use rustc_hir::def_id::{DefId, LocalDefId};
17use rustc_hir::{self as hir, BindingMode, ByRef, HirId, ItemLocalId, Node, find_attr};
18use rustc_index::bit_set::GrowableBitSet;
19use rustc_index::{Idx, IndexSlice, IndexVec};
20use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
21use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
22use rustc_middle::middle::region;
23use rustc_middle::mir::*;
24use rustc_middle::thir::{self, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir};
25use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt, TypeVisitableExt, TypingMode};
26use rustc_middle::{bug, span_bug};
27use rustc_span::{Span, Symbol, sym};
28
29use crate::builder::expr::as_place::PlaceBuilder;
30use crate::builder::scope::DropKind;
31
32pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
33 tcx: TyCtxt<'tcx>,
34 def_id: LocalDefId,
35) -> IndexVec<FieldIdx, Symbol> {
36 tcx.closure_captures(def_id)
37 .iter()
38 .map(|captured_place| {
39 let name = captured_place.to_symbol();
40 match captured_place.info.capture_kind {
41 ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => name,
42 ty::UpvarCapture::ByRef(..) => Symbol::intern(&format!("_ref__{name}")),
43 }
44 })
45 .collect()
46}
47
48pub fn build_mir<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
51 tcx.ensure_done().thir_abstract_const(def);
52 if let Err(e) = tcx.ensure_ok().check_match(def) {
53 return construct_error(tcx, def, e);
54 }
55
56 if let Err(err) = tcx.ensure_ok().check_tail_calls(def) {
57 return construct_error(tcx, def, err);
58 }
59
60 let body = match tcx.thir_body(def) {
61 Err(error_reported) => construct_error(tcx, def, error_reported),
62 Ok((thir, expr)) => {
63 let build_mir = |thir: &Thir<'tcx>| match thir.body_type {
64 thir::BodyTy::Fn(fn_sig) => construct_fn(tcx, def, thir, expr, fn_sig),
65 thir::BodyTy::Const(ty) | thir::BodyTy::GlobalAsm(ty) => {
66 construct_const(tcx, def, thir, expr, ty)
67 }
68 };
69
70 tcx.ensure_ok().check_liveness(def);
74
75 build_mir(&thir.borrow())
79 }
80 };
81
82 debug_assert!(
87 !(body.local_decls.has_free_regions()
88 || body.basic_blocks.has_free_regions()
89 || body.var_debug_info.has_free_regions()
90 || body.yield_ty().has_free_regions()),
91 "Unexpected free regions in MIR: {body:?}",
92 );
93
94 body
95}
96
97#[derive(Debug, PartialEq, Eq)]
101enum BlockFrame {
102 Statement {
109 ignores_expr_result: bool,
112 },
113
114 TailExpr { info: BlockTailInfo },
118
119 SubExpr,
124}
125
126impl BlockFrame {
127 fn is_tail_expr(&self) -> bool {
128 match *self {
129 BlockFrame::TailExpr { .. } => true,
130
131 BlockFrame::Statement { .. } | BlockFrame::SubExpr => false,
132 }
133 }
134 fn is_statement(&self) -> bool {
135 match *self {
136 BlockFrame::Statement { .. } => true,
137
138 BlockFrame::TailExpr { .. } | BlockFrame::SubExpr => false,
139 }
140 }
141}
142
143#[derive(Debug)]
144struct BlockContext(Vec<BlockFrame>);
145
146struct Builder<'a, 'tcx> {
147 tcx: TyCtxt<'tcx>,
148 infcx: InferCtxt<'tcx>,
153 region_scope_tree: &'tcx region::ScopeTree,
154 param_env: ty::ParamEnv<'tcx>,
155
156 thir: &'a Thir<'tcx>,
157 cfg: CFG<'tcx>,
158
159 def_id: LocalDefId,
160 hir_id: HirId,
161 parent_module: DefId,
162 check_overflow: bool,
163 fn_span: Span,
164 arg_count: usize,
165 coroutine: Option<Box<CoroutineInfo<'tcx>>>,
166
167 scopes: scope::Scopes<'tcx>,
170
171 block_context: BlockContext,
184
185 source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
188 source_scope: SourceScope,
189
190 guard_context: Vec<GuardFrame>,
194
195 fixed_temps: FxHashMap<ExprId, Local>,
198 fixed_temps_scope: Option<region::Scope>,
200
201 var_indices: FxHashMap<LocalVarId, LocalsForNode>,
204 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
205 canonical_user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
206 upvars: CaptureMap<'tcx>,
207 unit_temp: Option<Place<'tcx>>,
208
209 var_debug_info: Vec<VarDebugInfo<'tcx>>,
210
211 lint_level_roots_cache: GrowableBitSet<hir::ItemLocalId>,
218
219 coverage_info: Option<coverageinfo::CoverageInfoBuilder>,
222}
223
224type CaptureMap<'tcx> = SortedIndexMultiMap<usize, ItemLocalId, Capture<'tcx>>;
225
226#[derive(Debug)]
227struct Capture<'tcx> {
228 captured_place: &'tcx ty::CapturedPlace<'tcx>,
229 use_place: Place<'tcx>,
230 mutability: Mutability,
231}
232
233impl<'a, 'tcx> Builder<'a, 'tcx> {
234 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
235 self.infcx.typing_env(self.param_env)
236 }
237
238 fn is_bound_var_in_guard(&self, id: LocalVarId) -> bool {
239 self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id))
240 }
241
242 fn var_local_id(&self, id: LocalVarId, for_guard: ForGuard) -> Local {
243 self.var_indices[&id].local_id(for_guard)
244 }
245}
246
247impl BlockContext {
248 fn new() -> Self {
249 BlockContext(vec![])
250 }
251 fn push(&mut self, bf: BlockFrame) {
252 self.0.push(bf);
253 }
254 fn pop(&mut self) -> Option<BlockFrame> {
255 self.0.pop()
256 }
257
258 fn currently_in_block_tail(&self) -> Option<BlockTailInfo> {
269 for bf in self.0.iter().rev() {
270 match bf {
271 BlockFrame::SubExpr => continue,
272 BlockFrame::Statement { .. } => break,
273 &BlockFrame::TailExpr { info } => return Some(info),
274 }
275 }
276
277 None
278 }
279
280 fn currently_ignores_tail_results(&self) -> bool {
287 match self.0.last() {
288 None => false,
290
291 Some(BlockFrame::SubExpr) => false,
293
294 Some(
296 BlockFrame::TailExpr { info: BlockTailInfo { tail_result_is_ignored: ign, .. } }
297 | BlockFrame::Statement { ignores_expr_result: ign },
298 ) => *ign,
299 }
300 }
301}
302
303#[derive(Debug)]
304enum LocalsForNode {
305 One(Local),
308
309 ForGuard { ref_for_guard: Local, for_arm_body: Local },
320}
321
322#[derive(Debug)]
323struct GuardFrameLocal {
324 id: LocalVarId,
325}
326
327impl GuardFrameLocal {
328 fn new(id: LocalVarId) -> Self {
329 GuardFrameLocal { id }
330 }
331}
332
333#[derive(Debug)]
334struct GuardFrame {
335 locals: Vec<GuardFrameLocal>,
347}
348
349#[derive(Copy, Clone, Debug, PartialEq, Eq)]
354enum ForGuard {
355 RefWithinGuard,
356 OutsideGuard,
357}
358
359impl LocalsForNode {
360 fn local_id(&self, for_guard: ForGuard) -> Local {
361 match (self, for_guard) {
362 (&LocalsForNode::One(local_id), ForGuard::OutsideGuard)
363 | (
364 &LocalsForNode::ForGuard { ref_for_guard: local_id, .. },
365 ForGuard::RefWithinGuard,
366 )
367 | (&LocalsForNode::ForGuard { for_arm_body: local_id, .. }, ForGuard::OutsideGuard) => {
368 local_id
369 }
370
371 (&LocalsForNode::One(_), ForGuard::RefWithinGuard) => {
372 bug!("anything with one local should never be within a guard.")
373 }
374 }
375 }
376}
377
378struct CFG<'tcx> {
379 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
380}
381
382rustc_index::newtype_index! {
383 struct ScopeId {}
384}
385
386#[derive(Debug)]
387enum NeedsTemporary {
388 No,
393 Maybe,
396}
397
398#[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
405struct BlockAnd<T>(BasicBlock, T);
406
407impl BlockAnd<()> {
408 #[must_use]
410 fn into_block(self) -> BasicBlock {
411 let Self(block, ()) = self;
412 block
413 }
414}
415
416trait BlockAndExtension {
417 fn and<T>(self, v: T) -> BlockAnd<T>;
418 fn unit(self) -> BlockAnd<()>;
419}
420
421impl BlockAndExtension for BasicBlock {
422 fn and<T>(self, v: T) -> BlockAnd<T> {
423 BlockAnd(self, v)
424 }
425
426 fn unit(self) -> BlockAnd<()> {
427 BlockAnd(self, ())
428 }
429}
430
431macro_rules! unpack {
434 ($x:ident = $c:expr) => {{
435 let BlockAnd(b, v) = $c;
436 $x = b;
437 v
438 }};
439}
440
441fn construct_fn<'tcx>(
445 tcx: TyCtxt<'tcx>,
446 fn_def: LocalDefId,
447 thir: &Thir<'tcx>,
448 expr: ExprId,
449 fn_sig: ty::FnSig<'tcx>,
450) -> Body<'tcx> {
451 let span = tcx.def_span(fn_def);
452 let fn_id = tcx.local_def_id_to_hir_id(fn_def);
453
454 let body = tcx.hir_body_owned_by(fn_def);
456 let span_with_body = tcx.hir_span_with_body(fn_id);
457 let return_ty_span = tcx
458 .hir_fn_decl_by_hir_id(fn_id)
459 .unwrap_or_else(|| span_bug!(span, "can't build MIR for {:?}", fn_def))
460 .output
461 .span();
462
463 let mut abi = fn_sig.abi;
464 if let DefKind::Closure = tcx.def_kind(fn_def) {
465 abi = ExternAbi::Rust;
468 }
469
470 let arguments = &thir.params;
471
472 let return_ty = fn_sig.output();
473 let coroutine = match tcx.type_of(fn_def).instantiate_identity().kind() {
474 ty::Coroutine(_, args) => Some(Box::new(CoroutineInfo::initial(
475 tcx.coroutine_kind(fn_def).unwrap(),
476 args.as_coroutine().yield_ty(),
477 args.as_coroutine().resume_ty(),
478 ))),
479 ty::Closure(..) | ty::CoroutineClosure(..) | ty::FnDef(..) => None,
480 ty => span_bug!(span_with_body, "unexpected type of body: {ty:?}"),
481 };
482
483 if let Some((dialect, phase)) = find_attr!(tcx.hir_attrs(fn_id), AttributeKind::CustomMir(dialect, phase, _) => (dialect, phase))
484 {
485 return custom::build_custom_mir(
486 tcx,
487 fn_def.to_def_id(),
488 fn_id,
489 thir,
490 expr,
491 arguments,
492 return_ty,
493 return_ty_span,
494 span_with_body,
495 dialect.as_ref().map(|(d, _)| *d),
496 phase.as_ref().map(|(p, _)| *p),
497 );
498 }
499
500 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
503 let mut builder = Builder::new(
504 thir,
505 infcx,
506 fn_def,
507 fn_id,
508 span_with_body,
509 arguments.len(),
510 return_ty,
511 return_ty_span,
512 coroutine,
513 );
514
515 let call_site_scope =
516 region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::CallSite };
517 let arg_scope =
518 region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
519 let source_info = builder.source_info(span);
520 let call_site_s = (call_site_scope, source_info);
521 let _: BlockAnd<()> = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
522 let arg_scope_s = (arg_scope, source_info);
523 let fn_end = span_with_body.shrink_to_hi();
525 let return_block = builder
526 .in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
527 Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
528 builder.args_and_body(START_BLOCK, arguments, arg_scope, expr)
529 }))
530 })
531 .into_block();
532 let source_info = builder.source_info(fn_end);
533 builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
534 builder.build_drop_trees();
535 return_block.unit()
536 });
537
538 let mut body = builder.finish();
539
540 body.spread_arg = if abi == ExternAbi::RustCall {
541 Some(Local::new(arguments.len()))
543 } else {
544 None
545 };
546
547 body
548}
549
550fn construct_const<'a, 'tcx>(
551 tcx: TyCtxt<'tcx>,
552 def: LocalDefId,
553 thir: &'a Thir<'tcx>,
554 expr: ExprId,
555 const_ty: Ty<'tcx>,
556) -> Body<'tcx> {
557 let hir_id = tcx.local_def_id_to_hir_id(def);
558
559 let (span, const_ty_span) = match tcx.hir_node(hir_id) {
561 Node::Item(hir::Item {
562 kind: hir::ItemKind::Static(_, _, ty, _) | hir::ItemKind::Const(_, _, ty, _),
563 span,
564 ..
565 })
566 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), span, .. })
567 | Node::TraitItem(hir::TraitItem {
568 kind: hir::TraitItemKind::Const(ty, Some(_)),
569 span,
570 ..
571 }) => (*span, ty.span),
572 Node::AnonConst(ct) => (ct.span, ct.span),
573 Node::ConstBlock(_) => {
574 let span = tcx.def_span(def);
575 (span, span)
576 }
577 Node::Item(hir::Item { kind: hir::ItemKind::GlobalAsm { .. }, span, .. }) => (*span, *span),
578 _ => span_bug!(tcx.def_span(def), "can't build MIR for {:?}", def),
579 };
580
581 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
584 let mut builder =
585 Builder::new(thir, infcx, def, hir_id, span, 0, const_ty, const_ty_span, None);
586
587 let mut block = START_BLOCK;
588 block = builder.expr_into_dest(Place::return_place(), block, expr).into_block();
589
590 let source_info = builder.source_info(span);
591 builder.cfg.terminate(block, source_info, TerminatorKind::Return);
592
593 builder.build_drop_trees();
594
595 builder.finish()
596}
597
598fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -> Body<'_> {
603 let span = tcx.def_span(def_id);
604 let hir_id = tcx.local_def_id_to_hir_id(def_id);
605
606 let (inputs, output, coroutine) = match tcx.def_kind(def_id) {
607 DefKind::Const
608 | DefKind::AssocConst
609 | DefKind::AnonConst
610 | DefKind::InlineConst
611 | DefKind::Static { .. }
612 | DefKind::GlobalAsm => (vec![], tcx.type_of(def_id).instantiate_identity(), None),
613 DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => {
614 let sig = tcx.liberate_late_bound_regions(
615 def_id.to_def_id(),
616 tcx.fn_sig(def_id).instantiate_identity(),
617 );
618 (sig.inputs().to_vec(), sig.output(), None)
619 }
620 DefKind::Closure => {
621 let closure_ty = tcx.type_of(def_id).instantiate_identity();
622 match closure_ty.kind() {
623 ty::Closure(_, args) => {
624 let args = args.as_closure();
625 let sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), args.sig());
626 let self_ty = match args.kind() {
627 ty::ClosureKind::Fn => {
628 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
629 }
630 ty::ClosureKind::FnMut => {
631 Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
632 }
633 ty::ClosureKind::FnOnce => closure_ty,
634 };
635 (
636 [self_ty].into_iter().chain(sig.inputs()[0].tuple_fields()).collect(),
637 sig.output(),
638 None,
639 )
640 }
641 ty::Coroutine(_, args) => {
642 let args = args.as_coroutine();
643 let resume_ty = args.resume_ty();
644 let yield_ty = args.yield_ty();
645 let return_ty = args.return_ty();
646 (
647 vec![closure_ty, resume_ty],
648 return_ty,
649 Some(Box::new(CoroutineInfo::initial(
650 tcx.coroutine_kind(def_id).unwrap(),
651 yield_ty,
652 resume_ty,
653 ))),
654 )
655 }
656 ty::CoroutineClosure(did, args) => {
657 let args = args.as_coroutine_closure();
658 let sig = tcx.liberate_late_bound_regions(
659 def_id.to_def_id(),
660 args.coroutine_closure_sig(),
661 );
662 let self_ty = match args.kind() {
663 ty::ClosureKind::Fn => {
664 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
665 }
666 ty::ClosureKind::FnMut => {
667 Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
668 }
669 ty::ClosureKind::FnOnce => closure_ty,
670 };
671 (
672 [self_ty].into_iter().chain(sig.tupled_inputs_ty.tuple_fields()).collect(),
673 sig.to_coroutine(
674 tcx,
675 args.parent_args(),
676 args.kind_ty(),
677 tcx.coroutine_for_closure(*did),
678 Ty::new_error(tcx, guar),
679 ),
680 None,
681 )
682 }
683 ty::Error(_) => (vec![closure_ty, closure_ty], closure_ty, None),
684 kind => {
685 span_bug!(
686 span,
687 "expected type of closure body to be a closure or coroutine, got {kind:?}"
688 );
689 }
690 }
691 }
692 dk => span_bug!(span, "{:?} is not a body: {:?}", def_id, dk),
693 };
694
695 let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
696 let local_decls = IndexVec::from_iter(
697 [output].iter().chain(&inputs).map(|ty| LocalDecl::with_source_info(*ty, source_info)),
698 );
699 let mut cfg = CFG { basic_blocks: IndexVec::new() };
700 let mut source_scopes = IndexVec::new();
701
702 cfg.start_new_block();
703 source_scopes.push(SourceScopeData {
704 span,
705 parent_scope: None,
706 inlined: None,
707 inlined_parent_scope: None,
708 local_data: ClearCrossCrate::Set(SourceScopeLocalData { lint_root: hir_id }),
709 });
710
711 cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
712
713 Body::new(
714 MirSource::item(def_id.to_def_id()),
715 cfg.basic_blocks,
716 source_scopes,
717 local_decls,
718 IndexVec::new(),
719 inputs.len(),
720 vec![],
721 span,
722 coroutine,
723 Some(guar),
724 )
725}
726
727impl<'a, 'tcx> Builder<'a, 'tcx> {
728 fn new(
729 thir: &'a Thir<'tcx>,
730 infcx: InferCtxt<'tcx>,
731 def: LocalDefId,
732 hir_id: HirId,
733 span: Span,
734 arg_count: usize,
735 return_ty: Ty<'tcx>,
736 return_span: Span,
737 coroutine: Option<Box<CoroutineInfo<'tcx>>>,
738 ) -> Builder<'a, 'tcx> {
739 let tcx = infcx.tcx;
740 let attrs = tcx.hir_attrs(hir_id);
741 let mut check_overflow = attr::contains_name(attrs, sym::rustc_inherit_overflow_checks);
745 check_overflow |= tcx.sess.overflow_checks();
747 check_overflow |= matches!(
749 tcx.hir_body_owner_kind(def),
750 hir::BodyOwnerKind::Const { .. } | hir::BodyOwnerKind::Static(_)
751 );
752
753 let lint_level = LintLevel::Explicit(hir_id);
754 let param_env = tcx.param_env(def);
755 let mut builder = Builder {
756 thir,
757 tcx,
758 infcx,
759 region_scope_tree: tcx.region_scope_tree(def),
760 param_env,
761 def_id: def,
762 hir_id,
763 parent_module: tcx.parent_module(hir_id).to_def_id(),
764 check_overflow,
765 cfg: CFG { basic_blocks: IndexVec::new() },
766 fn_span: span,
767 arg_count,
768 coroutine,
769 scopes: scope::Scopes::new(),
770 block_context: BlockContext::new(),
771 source_scopes: IndexVec::new(),
772 source_scope: OUTERMOST_SOURCE_SCOPE,
773 guard_context: vec![],
774 fixed_temps: Default::default(),
775 fixed_temps_scope: None,
776 local_decls: IndexVec::from_elem_n(LocalDecl::new(return_ty, return_span), 1),
777 canonical_user_type_annotations: IndexVec::new(),
778 upvars: CaptureMap::new(),
779 var_indices: Default::default(),
780 unit_temp: None,
781 var_debug_info: vec![],
782 lint_level_roots_cache: GrowableBitSet::new_empty(),
783 coverage_info: coverageinfo::CoverageInfoBuilder::new_if_enabled(tcx, def),
784 };
785
786 assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
787 assert_eq!(builder.new_source_scope(span, lint_level), OUTERMOST_SOURCE_SCOPE);
788 builder.source_scopes[OUTERMOST_SOURCE_SCOPE].parent_scope = None;
789
790 builder
791 }
792
793 #[allow(dead_code)]
794 fn dump_for_debugging(&self) {
795 let mut body = Body::new(
796 MirSource::item(self.def_id.to_def_id()),
797 self.cfg.basic_blocks.clone(),
798 self.source_scopes.clone(),
799 self.local_decls.clone(),
800 self.canonical_user_type_annotations.clone(),
801 self.arg_count.clone(),
802 self.var_debug_info.clone(),
803 self.fn_span.clone(),
804 self.coroutine.clone(),
805 None,
806 );
807 body.coverage_info_hi = self.coverage_info.as_ref().map(|b| b.as_done());
808
809 use rustc_middle::mir::pretty;
810 let options = pretty::PrettyPrintMirOptions::from_cli(self.tcx);
811 pretty::write_mir_fn(self.tcx, &body, &mut |_, _| Ok(()), &mut std::io::stdout(), options)
812 .unwrap();
813 }
814
815 fn finish(self) -> Body<'tcx> {
816 let mut body = Body::new(
817 MirSource::item(self.def_id.to_def_id()),
818 self.cfg.basic_blocks,
819 self.source_scopes,
820 self.local_decls,
821 self.canonical_user_type_annotations,
822 self.arg_count,
823 self.var_debug_info,
824 self.fn_span,
825 self.coroutine,
826 None,
827 );
828 body.coverage_info_hi = self.coverage_info.map(|b| b.into_done());
829
830 for (index, block) in body.basic_blocks.iter().enumerate() {
831 if block.terminator.is_none() {
832 use rustc_middle::mir::pretty;
833 let options = pretty::PrettyPrintMirOptions::from_cli(self.tcx);
834 pretty::write_mir_fn(
835 self.tcx,
836 &body,
837 &mut |_, _| Ok(()),
838 &mut std::io::stdout(),
839 options,
840 )
841 .unwrap();
842 span_bug!(self.fn_span, "no terminator on block {:?}", index);
843 }
844 }
845
846 body
847 }
848
849 fn insert_upvar_arg(&mut self) {
850 let Some(closure_arg) = self.local_decls.get(ty::CAPTURE_STRUCT_LOCAL) else { return };
851
852 let mut closure_ty = closure_arg.ty;
853 let mut closure_env_projs = vec![];
854 if let ty::Ref(_, ty, _) = closure_ty.kind() {
855 closure_env_projs.push(ProjectionElem::Deref);
856 closure_ty = *ty;
857 }
858
859 let upvar_args = match closure_ty.kind() {
860 ty::Closure(_, args) => ty::UpvarArgs::Closure(args),
861 ty::Coroutine(_, args) => ty::UpvarArgs::Coroutine(args),
862 ty::CoroutineClosure(_, args) => ty::UpvarArgs::CoroutineClosure(args),
863 _ => return,
864 };
865
866 let capture_tys = upvar_args.upvar_tys();
872
873 let tcx = self.tcx;
874 let mut upvar_owner = None;
875 self.upvars = tcx
876 .closure_captures(self.def_id)
877 .iter()
878 .zip_eq(capture_tys)
879 .enumerate()
880 .map(|(i, (captured_place, ty))| {
881 let name = captured_place.to_symbol();
882
883 let capture = captured_place.info.capture_kind;
884 let var_id = match captured_place.place.base {
885 HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
886 _ => bug!("Expected an upvar"),
887 };
888 let upvar_base = upvar_owner.get_or_insert(var_id.owner);
889 assert_eq!(*upvar_base, var_id.owner);
890 let var_id = var_id.local_id;
891
892 let mutability = captured_place.mutability;
893
894 let mut projs = closure_env_projs.clone();
895 projs.push(ProjectionElem::Field(FieldIdx::new(i), ty));
896 match capture {
897 ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {}
898 ty::UpvarCapture::ByRef(..) => {
899 projs.push(ProjectionElem::Deref);
900 }
901 };
902
903 let use_place = Place {
904 local: ty::CAPTURE_STRUCT_LOCAL,
905 projection: tcx.mk_place_elems(&projs),
906 };
907 self.var_debug_info.push(VarDebugInfo {
908 name,
909 source_info: SourceInfo::outermost(captured_place.var_ident.span),
910 value: VarDebugInfoContents::Place(use_place),
911 composite: None,
912 argument_index: None,
913 });
914
915 let capture = Capture { captured_place, use_place, mutability };
916 (var_id, capture)
917 })
918 .collect();
919 }
920
921 fn args_and_body(
922 &mut self,
923 mut block: BasicBlock,
924 arguments: &IndexSlice<ParamId, Param<'tcx>>,
925 argument_scope: region::Scope,
926 expr_id: ExprId,
927 ) -> BlockAnd<()> {
928 let expr_span = self.thir[expr_id].span;
929 for (argument_index, param) in arguments.iter().enumerate() {
931 let source_info =
932 SourceInfo::outermost(param.pat.as_ref().map_or(self.fn_span, |pat| pat.span));
933 let arg_local =
934 self.local_decls.push(LocalDecl::with_source_info(param.ty, source_info));
935
936 if let Some(ref pat) = param.pat
938 && let Some(name) = pat.simple_ident()
939 {
940 self.var_debug_info.push(VarDebugInfo {
941 name,
942 source_info,
943 value: VarDebugInfoContents::Place(arg_local.into()),
944 composite: None,
945 argument_index: Some(argument_index as u16 + 1),
946 });
947 }
948 }
949
950 self.insert_upvar_arg();
951
952 let mut scope = None;
953 for (index, param) in arguments.iter().enumerate() {
955 let local = Local::new(index + 1);
957 let place = Place::from(local);
958
959 self.schedule_drop(
961 param.pat.as_ref().map_or(expr_span, |pat| pat.span),
962 argument_scope,
963 local,
964 DropKind::Value,
965 );
966
967 let Some(ref pat) = param.pat else {
968 continue;
969 };
970 let original_source_scope = self.source_scope;
971 let span = pat.span;
972 if let Some(arg_hir_id) = param.hir_id {
973 self.set_correct_source_scope_for_arg(arg_hir_id, original_source_scope, span);
974 }
975 match pat.kind {
976 PatKind::Binding {
978 var,
979 mode: BindingMode(ByRef::No, mutability),
980 subpattern: None,
981 ..
982 } => {
983 self.local_decls[local].mutability = mutability;
984 self.local_decls[local].source_info.scope = self.source_scope;
985 **self.local_decls[local].local_info.as_mut().unwrap_crate_local() =
986 if let Some(kind) = param.self_kind {
987 LocalInfo::User(BindingForm::ImplicitSelf(kind))
988 } else {
989 let binding_mode = BindingMode(ByRef::No, mutability);
990 LocalInfo::User(BindingForm::Var(VarBindingForm {
991 binding_mode,
992 opt_ty_info: param.ty_span,
993 opt_match_place: Some((None, span)),
994 pat_span: span,
995 }))
996 };
997 self.var_indices.insert(var, LocalsForNode::One(local));
998 }
999 _ => {
1000 scope = self.declare_bindings(
1001 scope,
1002 expr_span,
1003 &pat,
1004 None,
1005 Some((Some(&place), span)),
1006 );
1007 let place_builder = PlaceBuilder::from(local);
1008 block = self.place_into_pattern(block, pat, place_builder, false).into_block();
1009 }
1010 }
1011 self.source_scope = original_source_scope;
1012 }
1013
1014 if let Some(source_scope) = scope {
1016 self.source_scope = source_scope;
1017 }
1018
1019 if self.tcx.intrinsic(self.def_id).is_some_and(|i| i.must_be_overridden)
1020 || self.tcx.is_sdylib_interface_build()
1021 {
1022 let source_info = self.source_info(rustc_span::DUMMY_SP);
1023 self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
1024 self.cfg.start_new_block().unit()
1025 } else {
1026 match self.tcx.hir_node(self.hir_id) {
1028 hir::Node::Item(hir::Item {
1029 kind: hir::ItemKind::Fn { has_body: false, .. },
1030 ..
1031 }) => {
1032 self.tcx.dcx().span_delayed_bug(
1033 expr_span,
1034 format!("fn item without body has reached MIR building: {:?}", self.def_id),
1035 );
1036 }
1037 _ => {}
1038 }
1039 self.expr_into_dest(Place::return_place(), block, expr_id)
1040 }
1041 }
1042
1043 fn set_correct_source_scope_for_arg(
1044 &mut self,
1045 arg_hir_id: HirId,
1046 original_source_scope: SourceScope,
1047 pattern_span: Span,
1048 ) {
1049 let parent_id = self.source_scopes[original_source_scope]
1050 .local_data
1051 .as_ref()
1052 .unwrap_crate_local()
1053 .lint_root;
1054 self.maybe_new_source_scope(pattern_span, arg_hir_id, parent_id);
1055 }
1056
1057 fn get_unit_temp(&mut self) -> Place<'tcx> {
1058 match self.unit_temp {
1059 Some(tmp) => tmp,
1060 None => {
1061 let ty = self.tcx.types.unit;
1062 let fn_span = self.fn_span;
1063 let tmp = self.temp(ty, fn_span);
1064 self.unit_temp = Some(tmp);
1065 tmp
1066 }
1067 }
1068 }
1069}
1070
1071fn parse_float_into_constval(num: Symbol, float_ty: ty::FloatTy, neg: bool) -> Option<ConstValue> {
1072 parse_float_into_scalar(num, float_ty, neg).map(|s| ConstValue::Scalar(s.into()))
1073}
1074
1075pub(crate) fn parse_float_into_scalar(
1076 num: Symbol,
1077 float_ty: ty::FloatTy,
1078 neg: bool,
1079) -> Option<ScalarInt> {
1080 let num = num.as_str();
1081 match float_ty {
1082 ty::FloatTy::F16 => {
1084 let mut f = num.parse::<Half>().ok()?;
1085 if neg {
1086 f = -f;
1087 }
1088 Some(ScalarInt::from(f))
1089 }
1090 ty::FloatTy::F32 => {
1091 let Ok(rust_f) = num.parse::<f32>() else { return None };
1092 let mut f = num
1093 .parse::<Single>()
1094 .unwrap_or_else(|e| panic!("apfloat::ieee::Single failed to parse `{num}`: {e:?}"));
1095
1096 assert!(
1097 u128::from(rust_f.to_bits()) == f.to_bits(),
1098 "apfloat::ieee::Single gave different result for `{}`: \
1099 {}({:#x}) vs Rust's {}({:#x})",
1100 rust_f,
1101 f,
1102 f.to_bits(),
1103 Single::from_bits(rust_f.to_bits().into()),
1104 rust_f.to_bits()
1105 );
1106
1107 if neg {
1108 f = -f;
1109 }
1110
1111 Some(ScalarInt::from(f))
1112 }
1113 ty::FloatTy::F64 => {
1114 let Ok(rust_f) = num.parse::<f64>() else { return None };
1115 let mut f = num
1116 .parse::<Double>()
1117 .unwrap_or_else(|e| panic!("apfloat::ieee::Double failed to parse `{num}`: {e:?}"));
1118
1119 assert!(
1120 u128::from(rust_f.to_bits()) == f.to_bits(),
1121 "apfloat::ieee::Double gave different result for `{}`: \
1122 {}({:#x}) vs Rust's {}({:#x})",
1123 rust_f,
1124 f,
1125 f.to_bits(),
1126 Double::from_bits(rust_f.to_bits().into()),
1127 rust_f.to_bits()
1128 );
1129
1130 if neg {
1131 f = -f;
1132 }
1133
1134 Some(ScalarInt::from(f))
1135 }
1136 ty::FloatTy::F128 => {
1138 let mut f = num.parse::<Quad>().ok()?;
1139 if neg {
1140 f = -f;
1141 }
1142 Some(ScalarInt::from(f))
1143 }
1144 }
1145}
1146
1147mod block;
1153mod cfg;
1154mod coverageinfo;
1155mod custom;
1156mod expr;
1157mod matches;
1158mod misc;
1159mod scope;
1160
1161pub(crate) use expr::category::Category as ExprCategory;