1use itertools::Itertools;
24use rustc_abi::{ExternAbi, FieldIdx};
25use rustc_apfloat::Float;
26use rustc_apfloat::ieee::{Double, Half, Quad, Single};
27use rustc_ast::attr;
28use rustc_data_structures::fx::FxHashMap;
29use rustc_data_structures::sorted_map::SortedIndexMultiMap;
30use rustc_errors::ErrorGuaranteed;
31use rustc_hir::attrs::AttributeKind;
32use rustc_hir::def::DefKind;
33use rustc_hir::def_id::{DefId, LocalDefId};
34use rustc_hir::{self as hir, BindingMode, ByRef, HirId, ItemLocalId, Node, find_attr};
35use rustc_index::bit_set::GrowableBitSet;
36use rustc_index::{Idx, IndexSlice, IndexVec};
37use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
38use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
39use rustc_middle::middle::region;
40use rustc_middle::mir::*;
41use rustc_middle::thir::{self, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir};
42use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt, TypeVisitableExt, TypingMode};
43use rustc_middle::{bug, span_bug};
44use rustc_session::lint;
45use rustc_span::{Span, Symbol, sym};
46
47use crate::builder::expr::as_place::PlaceBuilder;
48use crate::builder::scope::DropKind;
49use crate::errors;
50
51pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
52 tcx: TyCtxt<'tcx>,
53 def_id: LocalDefId,
54) -> IndexVec<FieldIdx, Symbol> {
55 tcx.closure_captures(def_id)
56 .iter()
57 .map(|captured_place| {
58 let name = captured_place.to_symbol();
59 match captured_place.info.capture_kind {
60 ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => name,
61 ty::UpvarCapture::ByRef(..) => Symbol::intern(&format!("_ref__{name}")),
62 }
63 })
64 .collect()
65}
66
67pub fn build_mir<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
70 tcx.ensure_done().thir_abstract_const(def);
71 if let Err(e) = tcx.ensure_ok().check_match(def) {
72 return construct_error(tcx, def, e);
73 }
74
75 if let Err(err) = tcx.ensure_ok().check_tail_calls(def) {
76 return construct_error(tcx, def, err);
77 }
78
79 let body = match tcx.thir_body(def) {
80 Err(error_reported) => construct_error(tcx, def, error_reported),
81 Ok((thir, expr)) => {
82 let build_mir = |thir: &Thir<'tcx>| match thir.body_type {
83 thir::BodyTy::Fn(fn_sig) => construct_fn(tcx, def, thir, expr, fn_sig),
84 thir::BodyTy::Const(ty) | thir::BodyTy::GlobalAsm(ty) => {
85 construct_const(tcx, def, thir, expr, ty)
86 }
87 };
88
89 build_mir(&thir.borrow())
93 }
94 };
95
96 debug_assert!(
101 !(body.local_decls.has_free_regions()
102 || body.basic_blocks.has_free_regions()
103 || body.var_debug_info.has_free_regions()
104 || body.yield_ty().has_free_regions()),
105 "Unexpected free regions in MIR: {body:?}",
106 );
107
108 body
109}
110
111#[derive(Debug, PartialEq, Eq)]
115enum BlockFrame {
116 Statement {
123 ignores_expr_result: bool,
126 },
127
128 TailExpr { info: BlockTailInfo },
132
133 SubExpr,
138}
139
140impl BlockFrame {
141 fn is_tail_expr(&self) -> bool {
142 match *self {
143 BlockFrame::TailExpr { .. } => true,
144
145 BlockFrame::Statement { .. } | BlockFrame::SubExpr => false,
146 }
147 }
148 fn is_statement(&self) -> bool {
149 match *self {
150 BlockFrame::Statement { .. } => true,
151
152 BlockFrame::TailExpr { .. } | BlockFrame::SubExpr => false,
153 }
154 }
155}
156
157#[derive(Debug)]
158struct BlockContext(Vec<BlockFrame>);
159
160struct Builder<'a, 'tcx> {
161 tcx: TyCtxt<'tcx>,
162 infcx: InferCtxt<'tcx>,
167 region_scope_tree: &'tcx region::ScopeTree,
168 param_env: ty::ParamEnv<'tcx>,
169
170 thir: &'a Thir<'tcx>,
171 cfg: CFG<'tcx>,
172
173 def_id: LocalDefId,
174 hir_id: HirId,
175 parent_module: DefId,
176 check_overflow: bool,
177 fn_span: Span,
178 arg_count: usize,
179 coroutine: Option<Box<CoroutineInfo<'tcx>>>,
180
181 scopes: scope::Scopes<'tcx>,
184
185 block_context: BlockContext,
198
199 source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
202 source_scope: SourceScope,
203
204 guard_context: Vec<GuardFrame>,
208
209 fixed_temps: FxHashMap<ExprId, Local>,
212 fixed_temps_scope: Option<region::Scope>,
214
215 var_indices: FxHashMap<LocalVarId, LocalsForNode>,
218 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
219 canonical_user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
220 upvars: CaptureMap<'tcx>,
221 unit_temp: Option<Place<'tcx>>,
222
223 var_debug_info: Vec<VarDebugInfo<'tcx>>,
224
225 lint_level_roots_cache: GrowableBitSet<hir::ItemLocalId>,
232
233 coverage_info: Option<coverageinfo::CoverageInfoBuilder>,
236}
237
238type CaptureMap<'tcx> = SortedIndexMultiMap<usize, ItemLocalId, Capture<'tcx>>;
239
240#[derive(Debug)]
241struct Capture<'tcx> {
242 captured_place: &'tcx ty::CapturedPlace<'tcx>,
243 use_place: Place<'tcx>,
244 mutability: Mutability,
245}
246
247impl<'a, 'tcx> Builder<'a, 'tcx> {
248 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
249 self.infcx.typing_env(self.param_env)
250 }
251
252 fn is_bound_var_in_guard(&self, id: LocalVarId) -> bool {
253 self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id))
254 }
255
256 fn var_local_id(&self, id: LocalVarId, for_guard: ForGuard) -> Local {
257 self.var_indices[&id].local_id(for_guard)
258 }
259}
260
261impl BlockContext {
262 fn new() -> Self {
263 BlockContext(vec![])
264 }
265 fn push(&mut self, bf: BlockFrame) {
266 self.0.push(bf);
267 }
268 fn pop(&mut self) -> Option<BlockFrame> {
269 self.0.pop()
270 }
271
272 fn currently_in_block_tail(&self) -> Option<BlockTailInfo> {
283 for bf in self.0.iter().rev() {
284 match bf {
285 BlockFrame::SubExpr => continue,
286 BlockFrame::Statement { .. } => break,
287 &BlockFrame::TailExpr { info } => return Some(info),
288 }
289 }
290
291 None
292 }
293
294 fn currently_ignores_tail_results(&self) -> bool {
301 match self.0.last() {
302 None => false,
304
305 Some(BlockFrame::SubExpr) => false,
307
308 Some(
310 BlockFrame::TailExpr { info: BlockTailInfo { tail_result_is_ignored: ign, .. } }
311 | BlockFrame::Statement { ignores_expr_result: ign },
312 ) => *ign,
313 }
314 }
315}
316
317#[derive(Debug)]
318enum LocalsForNode {
319 One(Local),
322
323 ForGuard { ref_for_guard: Local, for_arm_body: Local },
334}
335
336#[derive(Debug)]
337struct GuardFrameLocal {
338 id: LocalVarId,
339}
340
341impl GuardFrameLocal {
342 fn new(id: LocalVarId) -> Self {
343 GuardFrameLocal { id }
344 }
345}
346
347#[derive(Debug)]
348struct GuardFrame {
349 locals: Vec<GuardFrameLocal>,
361}
362
363#[derive(Copy, Clone, Debug, PartialEq, Eq)]
368enum ForGuard {
369 RefWithinGuard,
370 OutsideGuard,
371}
372
373impl LocalsForNode {
374 fn local_id(&self, for_guard: ForGuard) -> Local {
375 match (self, for_guard) {
376 (&LocalsForNode::One(local_id), ForGuard::OutsideGuard)
377 | (
378 &LocalsForNode::ForGuard { ref_for_guard: local_id, .. },
379 ForGuard::RefWithinGuard,
380 )
381 | (&LocalsForNode::ForGuard { for_arm_body: local_id, .. }, ForGuard::OutsideGuard) => {
382 local_id
383 }
384
385 (&LocalsForNode::One(_), ForGuard::RefWithinGuard) => {
386 bug!("anything with one local should never be within a guard.")
387 }
388 }
389 }
390}
391
392struct CFG<'tcx> {
393 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
394}
395
396rustc_index::newtype_index! {
397 struct ScopeId {}
398}
399
400#[derive(Debug)]
401enum NeedsTemporary {
402 No,
407 Maybe,
410}
411
412#[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
417struct BlockAnd<T>(BasicBlock, T);
418
419impl BlockAnd<()> {
420 #[must_use]
422 fn into_block(self) -> BasicBlock {
423 let Self(block, ()) = self;
424 block
425 }
426}
427
428trait BlockAndExtension {
429 fn and<T>(self, v: T) -> BlockAnd<T>;
430 fn unit(self) -> BlockAnd<()>;
431}
432
433impl BlockAndExtension for BasicBlock {
434 fn and<T>(self, v: T) -> BlockAnd<T> {
435 BlockAnd(self, v)
436 }
437
438 fn unit(self) -> BlockAnd<()> {
439 BlockAnd(self, ())
440 }
441}
442
443macro_rules! unpack {
446 ($x:ident = $c:expr) => {{
447 let BlockAnd(b, v) = $c;
448 $x = b;
449 v
450 }};
451}
452
453fn construct_fn<'tcx>(
455 tcx: TyCtxt<'tcx>,
456 fn_def: LocalDefId,
457 thir: &Thir<'tcx>,
458 expr: ExprId,
459 fn_sig: ty::FnSig<'tcx>,
460) -> Body<'tcx> {
461 let span = tcx.def_span(fn_def);
462 let fn_id = tcx.local_def_id_to_hir_id(fn_def);
463
464 let body = tcx.hir_body_owned_by(fn_def);
466 let span_with_body = tcx.hir_span_with_body(fn_id);
467 let return_ty_span = tcx
468 .hir_fn_decl_by_hir_id(fn_id)
469 .unwrap_or_else(|| span_bug!(span, "can't build MIR for {:?}", fn_def))
470 .output
471 .span();
472
473 let mut abi = fn_sig.abi;
474 if let DefKind::Closure = tcx.def_kind(fn_def) {
475 abi = ExternAbi::Rust;
478 }
479
480 let arguments = &thir.params;
481
482 let return_ty = fn_sig.output();
483 let coroutine = match tcx.type_of(fn_def).instantiate_identity().kind() {
484 ty::Coroutine(_, args) => Some(Box::new(CoroutineInfo::initial(
485 tcx.coroutine_kind(fn_def).unwrap(),
486 args.as_coroutine().yield_ty(),
487 args.as_coroutine().resume_ty(),
488 ))),
489 ty::Closure(..) | ty::CoroutineClosure(..) | ty::FnDef(..) => None,
490 ty => span_bug!(span_with_body, "unexpected type of body: {ty:?}"),
491 };
492
493 if let Some((dialect, phase)) = find_attr!(tcx.hir_attrs(fn_id), AttributeKind::CustomMir(dialect, phase, _) => (dialect, phase))
494 {
495 return custom::build_custom_mir(
496 tcx,
497 fn_def.to_def_id(),
498 fn_id,
499 thir,
500 expr,
501 arguments,
502 return_ty,
503 return_ty_span,
504 span_with_body,
505 dialect.as_ref().map(|(d, _)| *d),
506 phase.as_ref().map(|(p, _)| *p),
507 );
508 }
509
510 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
513 let mut builder = Builder::new(
514 thir,
515 infcx,
516 fn_def,
517 fn_id,
518 span_with_body,
519 arguments.len(),
520 return_ty,
521 return_ty_span,
522 coroutine,
523 );
524
525 let call_site_scope =
526 region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::CallSite };
527 let arg_scope =
528 region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
529 let source_info = builder.source_info(span);
530 let call_site_s = (call_site_scope, source_info);
531 let _: BlockAnd<()> = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
532 let arg_scope_s = (arg_scope, source_info);
533 let fn_end = span_with_body.shrink_to_hi();
535 let return_block = builder
536 .in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
537 Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
538 builder.args_and_body(START_BLOCK, arguments, arg_scope, expr)
539 }))
540 })
541 .into_block();
542 let source_info = builder.source_info(fn_end);
543 builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
544 builder.build_drop_trees();
545 return_block.unit()
546 });
547
548 builder.lint_and_remove_uninhabited();
549 let mut body = builder.finish();
550
551 body.spread_arg = if abi == ExternAbi::RustCall {
552 Some(Local::new(arguments.len()))
554 } else {
555 None
556 };
557
558 body
559}
560
561fn construct_const<'a, 'tcx>(
562 tcx: TyCtxt<'tcx>,
563 def: LocalDefId,
564 thir: &'a Thir<'tcx>,
565 expr: ExprId,
566 const_ty: Ty<'tcx>,
567) -> Body<'tcx> {
568 let hir_id = tcx.local_def_id_to_hir_id(def);
569
570 let (span, const_ty_span) = match tcx.hir_node(hir_id) {
572 Node::Item(hir::Item {
573 kind: hir::ItemKind::Static(_, _, ty, _) | hir::ItemKind::Const(_, _, ty, _),
574 span,
575 ..
576 })
577 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), span, .. })
578 | Node::TraitItem(hir::TraitItem {
579 kind: hir::TraitItemKind::Const(ty, Some(_)),
580 span,
581 ..
582 }) => (*span, ty.span),
583 Node::AnonConst(ct) => (ct.span, ct.span),
584 Node::ConstBlock(_) => {
585 let span = tcx.def_span(def);
586 (span, span)
587 }
588 Node::Item(hir::Item { kind: hir::ItemKind::GlobalAsm { .. }, span, .. }) => (*span, *span),
589 _ => span_bug!(tcx.def_span(def), "can't build MIR for {:?}", def),
590 };
591
592 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
595 let mut builder =
596 Builder::new(thir, infcx, def, hir_id, span, 0, const_ty, const_ty_span, None);
597
598 let mut block = START_BLOCK;
599 block = builder.expr_into_dest(Place::return_place(), block, expr).into_block();
600
601 let source_info = builder.source_info(span);
602 builder.cfg.terminate(block, source_info, TerminatorKind::Return);
603
604 builder.build_drop_trees();
605
606 builder.lint_and_remove_uninhabited();
607 builder.finish()
608}
609
610fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -> Body<'_> {
615 let span = tcx.def_span(def_id);
616 let hir_id = tcx.local_def_id_to_hir_id(def_id);
617
618 let (inputs, output, coroutine) = match tcx.def_kind(def_id) {
619 DefKind::Const
620 | DefKind::AssocConst
621 | DefKind::AnonConst
622 | DefKind::InlineConst
623 | DefKind::Static { .. }
624 | DefKind::GlobalAsm => (vec![], tcx.type_of(def_id).instantiate_identity(), None),
625 DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => {
626 let sig = tcx.liberate_late_bound_regions(
627 def_id.to_def_id(),
628 tcx.fn_sig(def_id).instantiate_identity(),
629 );
630 (sig.inputs().to_vec(), sig.output(), None)
631 }
632 DefKind::Closure => {
633 let closure_ty = tcx.type_of(def_id).instantiate_identity();
634 match closure_ty.kind() {
635 ty::Closure(_, args) => {
636 let args = args.as_closure();
637 let sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), args.sig());
638 let self_ty = match args.kind() {
639 ty::ClosureKind::Fn => {
640 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
641 }
642 ty::ClosureKind::FnMut => {
643 Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
644 }
645 ty::ClosureKind::FnOnce => closure_ty,
646 };
647 (
648 [self_ty].into_iter().chain(sig.inputs()[0].tuple_fields()).collect(),
649 sig.output(),
650 None,
651 )
652 }
653 ty::Coroutine(_, args) => {
654 let args = args.as_coroutine();
655 let resume_ty = args.resume_ty();
656 let yield_ty = args.yield_ty();
657 let return_ty = args.return_ty();
658 (
659 vec![closure_ty, resume_ty],
660 return_ty,
661 Some(Box::new(CoroutineInfo::initial(
662 tcx.coroutine_kind(def_id).unwrap(),
663 yield_ty,
664 resume_ty,
665 ))),
666 )
667 }
668 ty::CoroutineClosure(did, args) => {
669 let args = args.as_coroutine_closure();
670 let sig = tcx.liberate_late_bound_regions(
671 def_id.to_def_id(),
672 args.coroutine_closure_sig(),
673 );
674 let self_ty = match args.kind() {
675 ty::ClosureKind::Fn => {
676 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
677 }
678 ty::ClosureKind::FnMut => {
679 Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
680 }
681 ty::ClosureKind::FnOnce => closure_ty,
682 };
683 (
684 [self_ty].into_iter().chain(sig.tupled_inputs_ty.tuple_fields()).collect(),
685 sig.to_coroutine(
686 tcx,
687 args.parent_args(),
688 args.kind_ty(),
689 tcx.coroutine_for_closure(*did),
690 Ty::new_error(tcx, guar),
691 ),
692 None,
693 )
694 }
695 ty::Error(_) => (vec![closure_ty, closure_ty], closure_ty, None),
696 kind => {
697 span_bug!(
698 span,
699 "expected type of closure body to be a closure or coroutine, got {kind:?}"
700 );
701 }
702 }
703 }
704 dk => span_bug!(span, "{:?} is not a body: {:?}", def_id, dk),
705 };
706
707 let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
708 let local_decls = IndexVec::from_iter(
709 [output].iter().chain(&inputs).map(|ty| LocalDecl::with_source_info(*ty, source_info)),
710 );
711 let mut cfg = CFG { basic_blocks: IndexVec::new() };
712 let mut source_scopes = IndexVec::new();
713
714 cfg.start_new_block();
715 source_scopes.push(SourceScopeData {
716 span,
717 parent_scope: None,
718 inlined: None,
719 inlined_parent_scope: None,
720 local_data: ClearCrossCrate::Set(SourceScopeLocalData { lint_root: hir_id }),
721 });
722
723 cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
724
725 Body::new(
726 MirSource::item(def_id.to_def_id()),
727 cfg.basic_blocks,
728 source_scopes,
729 local_decls,
730 IndexVec::new(),
731 inputs.len(),
732 vec![],
733 span,
734 coroutine,
735 Some(guar),
736 )
737}
738
739impl<'a, 'tcx> Builder<'a, 'tcx> {
740 fn new(
741 thir: &'a Thir<'tcx>,
742 infcx: InferCtxt<'tcx>,
743 def: LocalDefId,
744 hir_id: HirId,
745 span: Span,
746 arg_count: usize,
747 return_ty: Ty<'tcx>,
748 return_span: Span,
749 coroutine: Option<Box<CoroutineInfo<'tcx>>>,
750 ) -> Builder<'a, 'tcx> {
751 let tcx = infcx.tcx;
752 let attrs = tcx.hir_attrs(hir_id);
753 let mut check_overflow = attr::contains_name(attrs, sym::rustc_inherit_overflow_checks);
757 check_overflow |= tcx.sess.overflow_checks();
759 check_overflow |= matches!(
761 tcx.hir_body_owner_kind(def),
762 hir::BodyOwnerKind::Const { .. } | hir::BodyOwnerKind::Static(_)
763 );
764
765 let lint_level = LintLevel::Explicit(hir_id);
766 let param_env = tcx.param_env(def);
767 let mut builder = Builder {
768 thir,
769 tcx,
770 infcx,
771 region_scope_tree: tcx.region_scope_tree(def),
772 param_env,
773 def_id: def,
774 hir_id,
775 parent_module: tcx.parent_module(hir_id).to_def_id(),
776 check_overflow,
777 cfg: CFG { basic_blocks: IndexVec::new() },
778 fn_span: span,
779 arg_count,
780 coroutine,
781 scopes: scope::Scopes::new(),
782 block_context: BlockContext::new(),
783 source_scopes: IndexVec::new(),
784 source_scope: OUTERMOST_SOURCE_SCOPE,
785 guard_context: vec![],
786 fixed_temps: Default::default(),
787 fixed_temps_scope: None,
788 local_decls: IndexVec::from_elem_n(LocalDecl::new(return_ty, return_span), 1),
789 canonical_user_type_annotations: IndexVec::new(),
790 upvars: CaptureMap::new(),
791 var_indices: Default::default(),
792 unit_temp: None,
793 var_debug_info: vec![],
794 lint_level_roots_cache: GrowableBitSet::new_empty(),
795 coverage_info: coverageinfo::CoverageInfoBuilder::new_if_enabled(tcx, def),
796 };
797
798 assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
799 assert_eq!(builder.new_source_scope(span, lint_level), OUTERMOST_SOURCE_SCOPE);
800 builder.source_scopes[OUTERMOST_SOURCE_SCOPE].parent_scope = None;
801
802 builder
803 }
804
805 #[allow(dead_code)]
806 fn dump_for_debugging(&self) {
807 let mut body = Body::new(
808 MirSource::item(self.def_id.to_def_id()),
809 self.cfg.basic_blocks.clone(),
810 self.source_scopes.clone(),
811 self.local_decls.clone(),
812 self.canonical_user_type_annotations.clone(),
813 self.arg_count.clone(),
814 self.var_debug_info.clone(),
815 self.fn_span.clone(),
816 self.coroutine.clone(),
817 None,
818 );
819 body.coverage_info_hi = self.coverage_info.as_ref().map(|b| b.as_done());
820
821 let writer = pretty::MirWriter::new(self.tcx);
822 writer.write_mir_fn(&body, &mut std::io::stdout()).unwrap();
823 }
824
825 fn lint_and_remove_uninhabited(&mut self) {
826 let mut lints = vec![];
827
828 for bbdata in self.cfg.basic_blocks.iter_mut() {
829 let term = bbdata.terminator_mut();
830 let TerminatorKind::Call { ref mut target, destination, .. } = term.kind else {
831 continue;
832 };
833 let Some(target_bb) = *target else { continue };
834
835 let ty = destination.ty(&self.local_decls, self.tcx).ty;
836 let ty_is_inhabited = ty.is_inhabited_from(
837 self.tcx,
838 self.parent_module,
839 self.infcx.typing_env(self.param_env),
840 );
841 if !ty_is_inhabited {
842 if !ty.is_never()
852 && matches!(self.tcx.def_kind(self.def_id), DefKind::Fn | DefKind::AssocFn)
853 && self
857 .tcx
858 .fn_sig(self.def_id)
859 .instantiate_identity()
860 .skip_binder()
861 .output()
862 .is_inhabited_from(
863 self.tcx,
864 self.parent_module,
865 self.infcx.typing_env(self.param_env),
866 )
867 {
868 lints.push((target_bb, ty, term.source_info.span));
869 }
870
871 *target = None;
876 }
877 }
878
879 fn find_unreachable_code_from(
881 bb: BasicBlock,
882 bbs: &IndexVec<BasicBlock, BasicBlockData<'_>>,
883 ) -> Option<(SourceInfo, &'static str)> {
884 let bb = &bbs[bb];
885 for stmt in &bb.statements {
886 match &stmt.kind {
887 StatementKind::Assign(box (_, Rvalue::Use(Operand::Constant(const_))))
889 if const_.ty().is_unit() =>
890 {
891 continue;
892 }
893 StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => {
894 continue;
895 }
896 StatementKind::FakeRead(..) => return Some((stmt.source_info, "definition")),
897 _ => return Some((stmt.source_info, "expression")),
898 }
899 }
900
901 let term = bb.terminator();
902 match term.kind {
903 TerminatorKind::Goto { .. } | TerminatorKind::Return => None,
905 _ => Some((term.source_info, "expression")),
906 }
907 }
908
909 for (target_bb, orig_ty, orig_span) in lints {
910 if orig_span.in_external_macro(self.tcx.sess.source_map()) {
911 continue;
912 }
913
914 let Some((target_loc, descr)) =
915 find_unreachable_code_from(target_bb, &self.cfg.basic_blocks)
916 else {
917 continue;
918 };
919 let lint_root = self.source_scopes[target_loc.scope]
920 .local_data
921 .as_ref()
922 .unwrap_crate_local()
923 .lint_root;
924 self.tcx.emit_node_span_lint(
925 lint::builtin::UNREACHABLE_CODE,
926 lint_root,
927 target_loc.span,
928 errors::UnreachableDueToUninhabited {
929 expr: target_loc.span,
930 orig: orig_span,
931 descr,
932 ty: orig_ty,
933 },
934 );
935 }
936 }
937
938 fn finish(self) -> Body<'tcx> {
939 let mut body = Body::new(
940 MirSource::item(self.def_id.to_def_id()),
941 self.cfg.basic_blocks,
942 self.source_scopes,
943 self.local_decls,
944 self.canonical_user_type_annotations,
945 self.arg_count,
946 self.var_debug_info,
947 self.fn_span,
948 self.coroutine,
949 None,
950 );
951 body.coverage_info_hi = self.coverage_info.map(|b| b.into_done());
952
953 let writer = pretty::MirWriter::new(self.tcx);
954 for (index, block) in body.basic_blocks.iter().enumerate() {
955 if block.terminator.is_none() {
956 writer.write_mir_fn(&body, &mut std::io::stdout()).unwrap();
957 span_bug!(self.fn_span, "no terminator on block {:?}", index);
958 }
959 }
960
961 body
962 }
963
964 fn insert_upvar_arg(&mut self) {
965 let Some(closure_arg) = self.local_decls.get(ty::CAPTURE_STRUCT_LOCAL) else { return };
966
967 let mut closure_ty = closure_arg.ty;
968 let mut closure_env_projs = vec![];
969 if let ty::Ref(_, ty, _) = closure_ty.kind() {
970 closure_env_projs.push(ProjectionElem::Deref);
971 closure_ty = *ty;
972 }
973
974 let upvar_args = match closure_ty.kind() {
975 ty::Closure(_, args) => ty::UpvarArgs::Closure(args),
976 ty::Coroutine(_, args) => ty::UpvarArgs::Coroutine(args),
977 ty::CoroutineClosure(_, args) => ty::UpvarArgs::CoroutineClosure(args),
978 _ => return,
979 };
980
981 let capture_tys = upvar_args.upvar_tys();
987
988 let tcx = self.tcx;
989 let mut upvar_owner = None;
990 self.upvars = tcx
991 .closure_captures(self.def_id)
992 .iter()
993 .zip_eq(capture_tys)
994 .enumerate()
995 .map(|(i, (captured_place, ty))| {
996 let name = captured_place.to_symbol();
997
998 let capture = captured_place.info.capture_kind;
999 let var_id = match captured_place.place.base {
1000 HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
1001 _ => bug!("Expected an upvar"),
1002 };
1003 let upvar_base = upvar_owner.get_or_insert(var_id.owner);
1004 assert_eq!(*upvar_base, var_id.owner);
1005 let var_id = var_id.local_id;
1006
1007 let mutability = captured_place.mutability;
1008
1009 let mut projs = closure_env_projs.clone();
1010 projs.push(ProjectionElem::Field(FieldIdx::new(i), ty));
1011 match capture {
1012 ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {}
1013 ty::UpvarCapture::ByRef(..) => {
1014 projs.push(ProjectionElem::Deref);
1015 }
1016 };
1017
1018 let use_place = Place {
1019 local: ty::CAPTURE_STRUCT_LOCAL,
1020 projection: tcx.mk_place_elems(&projs),
1021 };
1022 self.var_debug_info.push(VarDebugInfo {
1023 name,
1024 source_info: SourceInfo::outermost(captured_place.var_ident.span),
1025 value: VarDebugInfoContents::Place(use_place),
1026 composite: None,
1027 argument_index: None,
1028 });
1029
1030 let capture = Capture { captured_place, use_place, mutability };
1031 (var_id, capture)
1032 })
1033 .collect();
1034 }
1035
1036 fn args_and_body(
1037 &mut self,
1038 mut block: BasicBlock,
1039 arguments: &IndexSlice<ParamId, Param<'tcx>>,
1040 argument_scope: region::Scope,
1041 expr_id: ExprId,
1042 ) -> BlockAnd<()> {
1043 let expr_span = self.thir[expr_id].span;
1044 for (argument_index, param) in arguments.iter().enumerate() {
1046 let source_info =
1047 SourceInfo::outermost(param.pat.as_ref().map_or(self.fn_span, |pat| pat.span));
1048 let arg_local =
1049 self.local_decls.push(LocalDecl::with_source_info(param.ty, source_info));
1050
1051 if let Some(ref pat) = param.pat
1053 && let Some(name) = pat.simple_ident()
1054 {
1055 self.var_debug_info.push(VarDebugInfo {
1056 name,
1057 source_info,
1058 value: VarDebugInfoContents::Place(arg_local.into()),
1059 composite: None,
1060 argument_index: Some(argument_index as u16 + 1),
1061 });
1062 }
1063 }
1064
1065 self.insert_upvar_arg();
1066
1067 let mut scope = None;
1068 for (index, param) in arguments.iter().enumerate() {
1070 let local = Local::new(index + 1);
1072 let place = Place::from(local);
1073
1074 self.schedule_drop(
1076 param.pat.as_ref().map_or(expr_span, |pat| pat.span),
1077 argument_scope,
1078 local,
1079 DropKind::Value,
1080 );
1081
1082 let Some(ref pat) = param.pat else {
1083 continue;
1084 };
1085 let original_source_scope = self.source_scope;
1086 let span = pat.span;
1087 if let Some(arg_hir_id) = param.hir_id {
1088 self.set_correct_source_scope_for_arg(arg_hir_id, original_source_scope, span);
1089 }
1090 match pat.kind {
1091 PatKind::Binding {
1093 var,
1094 mode: BindingMode(ByRef::No, mutability),
1095 subpattern: None,
1096 ..
1097 } => {
1098 self.local_decls[local].mutability = mutability;
1099 self.local_decls[local].source_info.scope = self.source_scope;
1100 **self.local_decls[local].local_info.as_mut().unwrap_crate_local() =
1101 if let Some(kind) = param.self_kind {
1102 LocalInfo::User(BindingForm::ImplicitSelf(kind))
1103 } else {
1104 let binding_mode = BindingMode(ByRef::No, mutability);
1105 LocalInfo::User(BindingForm::Var(VarBindingForm {
1106 binding_mode,
1107 opt_ty_info: param.ty_span,
1108 opt_match_place: Some((None, span)),
1109 pat_span: span,
1110 introductions: vec![VarBindingIntroduction {
1111 span,
1112 is_shorthand: false,
1113 }],
1114 }))
1115 };
1116 self.var_indices.insert(var, LocalsForNode::One(local));
1117 }
1118 _ => {
1119 scope = self.declare_bindings(
1120 scope,
1121 expr_span,
1122 &pat,
1123 None,
1124 Some((Some(&place), span)),
1125 );
1126 let place_builder = PlaceBuilder::from(local);
1127 block = self.place_into_pattern(block, pat, place_builder, false).into_block();
1128 }
1129 }
1130 self.source_scope = original_source_scope;
1131 }
1132
1133 if let Some(source_scope) = scope {
1135 self.source_scope = source_scope;
1136 }
1137
1138 if self.tcx.intrinsic(self.def_id).is_some_and(|i| i.must_be_overridden)
1139 || self.tcx.is_sdylib_interface_build()
1140 {
1141 let source_info = self.source_info(rustc_span::DUMMY_SP);
1142 self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
1143 self.cfg.start_new_block().unit()
1144 } else {
1145 match self.tcx.hir_node(self.hir_id) {
1147 hir::Node::Item(hir::Item {
1148 kind: hir::ItemKind::Fn { has_body: false, .. },
1149 ..
1150 }) => {
1151 self.tcx.dcx().span_delayed_bug(
1152 expr_span,
1153 format!("fn item without body has reached MIR building: {:?}", self.def_id),
1154 );
1155 }
1156 _ => {}
1157 }
1158 self.expr_into_dest(Place::return_place(), block, expr_id)
1159 }
1160 }
1161
1162 fn set_correct_source_scope_for_arg(
1163 &mut self,
1164 arg_hir_id: HirId,
1165 original_source_scope: SourceScope,
1166 pattern_span: Span,
1167 ) {
1168 let parent_id = self.source_scopes[original_source_scope]
1169 .local_data
1170 .as_ref()
1171 .unwrap_crate_local()
1172 .lint_root;
1173 self.maybe_new_source_scope(pattern_span, arg_hir_id, parent_id);
1174 }
1175
1176 fn get_unit_temp(&mut self) -> Place<'tcx> {
1177 match self.unit_temp {
1178 Some(tmp) => tmp,
1179 None => {
1180 let ty = self.tcx.types.unit;
1181 let fn_span = self.fn_span;
1182 let tmp = self.temp(ty, fn_span);
1183 self.unit_temp = Some(tmp);
1184 tmp
1185 }
1186 }
1187 }
1188}
1189
1190fn parse_float_into_constval(num: Symbol, float_ty: ty::FloatTy, neg: bool) -> Option<ConstValue> {
1191 parse_float_into_scalar(num, float_ty, neg).map(|s| ConstValue::Scalar(s.into()))
1192}
1193
1194pub(crate) fn parse_float_into_scalar(
1195 num: Symbol,
1196 float_ty: ty::FloatTy,
1197 neg: bool,
1198) -> Option<ScalarInt> {
1199 let num = num.as_str();
1200 match float_ty {
1201 ty::FloatTy::F16 => {
1203 let mut f = num.parse::<Half>().ok()?;
1204 if neg {
1205 f = -f;
1206 }
1207 Some(ScalarInt::from(f))
1208 }
1209 ty::FloatTy::F32 => {
1210 let Ok(rust_f) = num.parse::<f32>() else { return None };
1211 let mut f = num
1212 .parse::<Single>()
1213 .unwrap_or_else(|e| panic!("apfloat::ieee::Single failed to parse `{num}`: {e:?}"));
1214
1215 assert!(
1216 u128::from(rust_f.to_bits()) == f.to_bits(),
1217 "apfloat::ieee::Single gave different result for `{}`: \
1218 {}({:#x}) vs Rust's {}({:#x})",
1219 rust_f,
1220 f,
1221 f.to_bits(),
1222 Single::from_bits(rust_f.to_bits().into()),
1223 rust_f.to_bits()
1224 );
1225
1226 if neg {
1227 f = -f;
1228 }
1229
1230 Some(ScalarInt::from(f))
1231 }
1232 ty::FloatTy::F64 => {
1233 let Ok(rust_f) = num.parse::<f64>() else { return None };
1234 let mut f = num
1235 .parse::<Double>()
1236 .unwrap_or_else(|e| panic!("apfloat::ieee::Double failed to parse `{num}`: {e:?}"));
1237
1238 assert!(
1239 u128::from(rust_f.to_bits()) == f.to_bits(),
1240 "apfloat::ieee::Double gave different result for `{}`: \
1241 {}({:#x}) vs Rust's {}({:#x})",
1242 rust_f,
1243 f,
1244 f.to_bits(),
1245 Double::from_bits(rust_f.to_bits().into()),
1246 rust_f.to_bits()
1247 );
1248
1249 if neg {
1250 f = -f;
1251 }
1252
1253 Some(ScalarInt::from(f))
1254 }
1255 ty::FloatTy::F128 => {
1257 let mut f = num.parse::<Quad>().ok()?;
1258 if neg {
1259 f = -f;
1260 }
1261 Some(ScalarInt::from(f))
1262 }
1263 }
1264}
1265
1266mod block;
1272mod cfg;
1273mod coverageinfo;
1274mod custom;
1275mod expr;
1276mod matches;
1277mod misc;
1278mod scope;
1279
1280pub(crate) use expr::category::Category as ExprCategory;