1use itertools::Itertools;
24use rustc_abi::{ExternAbi, FieldIdx};
25use rustc_apfloat::Float;
26use rustc_apfloat::ieee::{Double, Half, Quad, Single};
27use rustc_data_structures::fx::FxHashMap;
28use rustc_data_structures::sorted_map::SortedIndexMultiMap;
29use rustc_errors::ErrorGuaranteed;
30use rustc_hir::def::DefKind;
31use rustc_hir::def_id::{DefId, LocalDefId};
32use rustc_hir::{self as hir, BindingMode, ByRef, HirId, ItemLocalId, Node, find_attr};
33use rustc_index::bit_set::GrowableBitSet;
34use rustc_index::{Idx, IndexSlice, IndexVec};
35use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
36use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
37use rustc_middle::middle::region;
38use rustc_middle::mir::*;
39use rustc_middle::thir::{self, ExprId, LocalVarId, Param, ParamId, PatKind, Thir};
40use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt, TypeVisitableExt, TypingMode};
41use rustc_middle::{bug, span_bug};
42use rustc_session::lint;
43use rustc_span::{Span, Symbol};
44
45use crate::builder::expr::as_place::PlaceBuilder;
46use crate::builder::scope::{DropKind, LintLevel};
47use crate::errors;
48
49pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
50 tcx: TyCtxt<'tcx>,
51 def_id: LocalDefId,
52) -> IndexVec<FieldIdx, Symbol> {
53 tcx.closure_captures(def_id)
54 .iter()
55 .map(|captured_place| {
56 let name = captured_place.to_symbol();
57 match captured_place.info.capture_kind {
58 ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => name,
59 ty::UpvarCapture::ByRef(..) => Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("_ref__{0}", name))
})format!("_ref__{name}")),
60 }
61 })
62 .collect()
63}
64
65pub(crate) fn build_mir_inner_impl<'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 if true {
if !!(body.local_decls.has_free_regions() ||
body.basic_blocks.has_free_regions() ||
body.var_debug_info.has_free_regions() ||
body.yield_ty().has_free_regions()) {
{
::core::panicking::panic_fmt(format_args!("Unexpected free regions in MIR: {0:?}",
body));
}
};
};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(#[automatically_derived]
impl ::core::fmt::Debug for BlockFrame {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
BlockFrame::Statement { ignores_expr_result: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Statement", "ignores_expr_result", &__self_0),
BlockFrame::TailExpr { info: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"TailExpr", "info", &__self_0),
BlockFrame::SubExpr =>
::core::fmt::Formatter::write_str(f, "SubExpr"),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for BlockFrame {
#[inline]
fn eq(&self, other: &BlockFrame) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(BlockFrame::Statement { ignores_expr_result: __self_0 },
BlockFrame::Statement { ignores_expr_result: __arg1_0 }) =>
__self_0 == __arg1_0,
(BlockFrame::TailExpr { info: __self_0 },
BlockFrame::TailExpr { info: __arg1_0 }) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for BlockFrame {
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<bool>;
let _: ::core::cmp::AssertParamIsEq<BlockTailInfo>;
}
}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(#[automatically_derived]
impl ::core::fmt::Debug for BlockContext {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "BlockContext",
&&self.0)
}
}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(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for Capture<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "Capture",
"captured_place", &self.captured_place, "use_place",
&self.use_place, "mutability", &&self.mutability)
}
}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(::alloc::vec::Vec::new()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(#[automatically_derived]
impl ::core::fmt::Debug for LocalsForNode {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
LocalsForNode::One(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "One",
&__self_0),
LocalsForNode::ForGuard {
ref_for_guard: __self_0, for_arm_body: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"ForGuard", "ref_for_guard", __self_0, "for_arm_body",
&__self_1),
}
}
}Debug)]
318enum LocalsForNode {
319 One(Local),
322
323 ForGuard { ref_for_guard: Local, for_arm_body: Local },
334}
335
336#[derive(#[automatically_derived]
impl ::core::fmt::Debug for GuardFrameLocal {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"GuardFrameLocal", "id", &&self.id)
}
}Debug)]
337struct GuardFrameLocal {
338 id: LocalVarId,
339}
340
341impl GuardFrameLocal {
342 fn new(id: LocalVarId) -> Self {
343 GuardFrameLocal { id }
344 }
345}
346
347#[derive(#[automatically_derived]
impl ::core::fmt::Debug for GuardFrame {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "GuardFrame",
"locals", &&self.locals)
}
}Debug)]
348struct GuardFrame {
349 locals: Vec<GuardFrameLocal>,
361}
362
363#[derive(#[automatically_derived]
impl ::core::marker::Copy for ForGuard { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ForGuard {
#[inline]
fn clone(&self) -> ForGuard { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ForGuard {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
ForGuard::RefWithinGuard => "RefWithinGuard",
ForGuard::OutsideGuard => "OutsideGuard",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for ForGuard {
#[inline]
fn eq(&self, other: &ForGuard) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ForGuard {
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}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 ::rustc_middle::util::bug::bug_fmt(format_args!("anything with one local should never be within a guard."))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
396impl ::std::fmt::Debug for ScopeId {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
397 struct ScopeId {}
398}
399
400#[derive(#[automatically_derived]
impl ::core::fmt::Debug for NeedsTemporary {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
NeedsTemporary::No => "No",
NeedsTemporary::Maybe => "Maybe",
})
}
}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(|| ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("can\'t build MIR for {0:?}", fn_def))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 => ::rustc_middle::util::bug::span_bug_fmt(span_with_body,
format_args!("unexpected type of body: {0:?}", ty))span_bug!(span_with_body, "unexpected type of body: {ty:?}"),
491 };
492
493 if let Some((dialect, phase)) =
494 {
'done:
{
for i in tcx.hir_attrs(fn_id) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(CustomMir(dialect, phase, _)) =>
{
break 'done Some((dialect, phase));
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(tcx.hir_attrs(fn_id), CustomMir(dialect, phase, _) => (dialect, phase))
495 {
496 return custom::build_custom_mir(
497 tcx,
498 fn_def.to_def_id(),
499 fn_id,
500 thir,
501 expr,
502 arguments,
503 return_ty,
504 return_ty_span,
505 span_with_body,
506 dialect.as_ref().map(|(d, _)| *d),
507 phase.as_ref().map(|(p, _)| *p),
508 );
509 }
510
511 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
514 let mut builder = Builder::new(
515 thir,
516 infcx,
517 fn_def,
518 fn_id,
519 span_with_body,
520 arguments.len(),
521 return_ty,
522 return_ty_span,
523 coroutine,
524 );
525
526 let call_site_scope =
527 region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::CallSite };
528 let arg_scope =
529 region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
530 let source_info = builder.source_info(span);
531 let call_site_s = (call_site_scope, source_info);
532 let _: BlockAnd<()> = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
533 let arg_scope_s = (arg_scope, source_info);
534 let fn_end = span_with_body.shrink_to_hi();
536 let return_block = builder
537 .in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
538 Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
539 builder.args_and_body(START_BLOCK, arguments, arg_scope, expr)
540 }))
541 })
542 .into_block();
543 let source_info = builder.source_info(fn_end);
544 builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
545 builder.build_drop_trees();
546 return_block.unit()
547 });
548
549 builder.lint_and_remove_uninhabited();
550 let mut body = builder.finish();
551
552 body.spread_arg = if abi == ExternAbi::RustCall {
553 Some(Local::new(arguments.len()))
555 } else {
556 None
557 };
558
559 body
560}
561
562fn construct_const<'a, 'tcx>(
563 tcx: TyCtxt<'tcx>,
564 def: LocalDefId,
565 thir: &'a Thir<'tcx>,
566 expr: ExprId,
567 const_ty: Ty<'tcx>,
568) -> Body<'tcx> {
569 let hir_id = tcx.local_def_id_to_hir_id(def);
570
571 let (span, const_ty_span) = match tcx.hir_node(hir_id) {
573 Node::Item(hir::Item {
574 kind: hir::ItemKind::Static(_, _, ty, _) | hir::ItemKind::Const(_, _, ty, _),
575 span,
576 ..
577 })
578 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), span, .. })
579 | Node::TraitItem(hir::TraitItem {
580 kind: hir::TraitItemKind::Const(ty, Some(_), _),
581 span,
582 ..
583 }) => (*span, ty.span),
584 Node::AnonConst(ct) => (ct.span, ct.span),
585 Node::ConstBlock(_) => {
586 let span = tcx.def_span(def);
587 (span, span)
588 }
589 Node::Item(hir::Item { kind: hir::ItemKind::GlobalAsm { .. }, span, .. }) => (*span, *span),
590 _ => ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(def),
format_args!("can\'t build MIR for {0:?}", def))span_bug!(tcx.def_span(def), "can't build MIR for {:?}", def),
591 };
592
593 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
596 let mut builder =
597 Builder::new(thir, infcx, def, hir_id, span, 0, const_ty, const_ty_span, None);
598
599 let mut block = START_BLOCK;
600 block = builder.expr_into_dest(Place::return_place(), block, expr).into_block();
601
602 let source_info = builder.source_info(span);
603 builder.cfg.terminate(block, source_info, TerminatorKind::Return);
604
605 builder.build_drop_trees();
606
607 builder.lint_and_remove_uninhabited();
608 builder.finish()
609}
610
611fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -> Body<'_> {
616 let span = tcx.def_span(def_id);
617 let hir_id = tcx.local_def_id_to_hir_id(def_id);
618
619 let (inputs, output, coroutine) = match tcx.def_kind(def_id) {
620 DefKind::Const
621 | DefKind::AssocConst
622 | DefKind::AnonConst
623 | DefKind::InlineConst
624 | DefKind::Static { .. }
625 | DefKind::GlobalAsm => (::alloc::vec::Vec::new()vec![], tcx.type_of(def_id).instantiate_identity(), None),
626 DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => {
627 let sig = tcx.liberate_late_bound_regions(
628 def_id.to_def_id(),
629 tcx.fn_sig(def_id).instantiate_identity(),
630 );
631 (sig.inputs().to_vec(), sig.output(), None)
632 }
633 DefKind::Closure => {
634 let closure_ty = tcx.type_of(def_id).instantiate_identity();
635 match closure_ty.kind() {
636 ty::Closure(_, args) => {
637 let args = args.as_closure();
638 let sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), args.sig());
639 let self_ty = match args.kind() {
640 ty::ClosureKind::Fn => {
641 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
642 }
643 ty::ClosureKind::FnMut => {
644 Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
645 }
646 ty::ClosureKind::FnOnce => closure_ty,
647 };
648 (
649 [self_ty].into_iter().chain(sig.inputs()[0].tuple_fields()).collect(),
650 sig.output(),
651 None,
652 )
653 }
654 ty::Coroutine(_, args) => {
655 let args = args.as_coroutine();
656 let resume_ty = args.resume_ty();
657 let yield_ty = args.yield_ty();
658 let return_ty = args.return_ty();
659 (
660 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[closure_ty, resume_ty]))vec![closure_ty, resume_ty],
661 return_ty,
662 Some(Box::new(CoroutineInfo::initial(
663 tcx.coroutine_kind(def_id).unwrap(),
664 yield_ty,
665 resume_ty,
666 ))),
667 )
668 }
669 ty::CoroutineClosure(did, args) => {
670 let args = args.as_coroutine_closure();
671 let sig = tcx.liberate_late_bound_regions(
672 def_id.to_def_id(),
673 args.coroutine_closure_sig(),
674 );
675 let self_ty = match args.kind() {
676 ty::ClosureKind::Fn => {
677 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
678 }
679 ty::ClosureKind::FnMut => {
680 Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
681 }
682 ty::ClosureKind::FnOnce => closure_ty,
683 };
684 (
685 [self_ty].into_iter().chain(sig.tupled_inputs_ty.tuple_fields()).collect(),
686 sig.to_coroutine(
687 tcx,
688 args.parent_args(),
689 args.kind_ty(),
690 tcx.coroutine_for_closure(*did),
691 Ty::new_error(tcx, guar),
692 ),
693 None,
694 )
695 }
696 ty::Error(_) => (::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[closure_ty, closure_ty]))vec![closure_ty, closure_ty], closure_ty, None),
697 kind => {
698 ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("expected type of closure body to be a closure or coroutine, got {0:?}",
kind));span_bug!(
699 span,
700 "expected type of closure body to be a closure or coroutine, got {kind:?}"
701 );
702 }
703 }
704 }
705 dk => ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("{0:?} is not a body: {1:?}", def_id, dk))span_bug!(span, "{:?} is not a body: {:?}", def_id, dk),
706 };
707
708 let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
709 let local_decls = IndexVec::from_iter(
710 [output].iter().chain(&inputs).map(|ty| LocalDecl::with_source_info(*ty, source_info)),
711 );
712 let mut cfg = CFG { basic_blocks: IndexVec::new() };
713 let mut source_scopes = IndexVec::new();
714
715 cfg.start_new_block();
716 source_scopes.push(SourceScopeData {
717 span,
718 parent_scope: None,
719 inlined: None,
720 inlined_parent_scope: None,
721 local_data: ClearCrossCrate::Set(SourceScopeLocalData { lint_root: hir_id }),
722 });
723
724 cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
725
726 Body::new(
727 MirSource::item(def_id.to_def_id()),
728 cfg.basic_blocks,
729 source_scopes,
730 local_decls,
731 IndexVec::new(),
732 inputs.len(),
733 ::alloc::vec::Vec::new()vec![],
734 span,
735 coroutine,
736 Some(guar),
737 )
738}
739
740impl<'a, 'tcx> Builder<'a, 'tcx> {
741 fn new(
742 thir: &'a Thir<'tcx>,
743 infcx: InferCtxt<'tcx>,
744 def: LocalDefId,
745 hir_id: HirId,
746 span: Span,
747 arg_count: usize,
748 return_ty: Ty<'tcx>,
749 return_span: Span,
750 coroutine: Option<Box<CoroutineInfo<'tcx>>>,
751 ) -> Builder<'a, 'tcx> {
752 let tcx = infcx.tcx;
753 let mut check_overflow = {
{
'done:
{
for i in tcx.hir_attrs(hir_id) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcInheritOverflowChecks) =>
{
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(tcx.hir_attrs(hir_id), RustcInheritOverflowChecks);
757 check_overflow |= tcx.sess.overflow_checks();
759 check_overflow |= #[allow(non_exhaustive_omitted_patterns)] match tcx.hir_body_owner_kind(def) {
hir::BodyOwnerKind::Const { .. } | hir::BodyOwnerKind::Static(_) => true,
_ => false,
}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: ::alloc::vec::Vec::new()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: ::alloc::vec::Vec::new()vec![],
794 lint_level_roots_cache: GrowableBitSet::new_empty(),
795 coverage_info: coverageinfo::CoverageInfoBuilder::new_if_enabled(tcx, def),
796 };
797
798 match (&builder.cfg.start_new_block(), &START_BLOCK) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
799 match (&builder.new_source_scope(span, lint_level), &OUTERMOST_SOURCE_SCOPE) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};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 = ::alloc::vec::Vec::new()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 && #[allow(non_exhaustive_omitted_patterns)] match self.tcx.def_kind(self.def_id)
{
DefKind::Fn | DefKind::AssocFn => true,
_ => false,
}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 ::rustc_middle::util::bug::span_bug_fmt(self.fn_span,
format_args!("no terminator on block {0:?}", index));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 = ::alloc::vec::Vec::new()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 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("Expected an upvar"))bug!("Expected an upvar"),
1002 };
1003 let upvar_base = upvar_owner.get_or_insert(var_id.owner);
1004 match (&*upvar_base, &var_id.owner) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};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: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[VarBindingIntroduction { span, is_shorthand: false }]))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 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("fn item without body has reached MIR building: {0:?}",
self.def_id))
})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| {
::core::panicking::panic_fmt(format_args!("apfloat::ieee::Single failed to parse `{0}`: {1:?}",
num, e));
}panic!("apfloat::ieee::Single failed to parse `{num}`: {e:?}"));
1214
1215 if !(u128::from(rust_f.to_bits()) == f.to_bits()) {
{
::core::panicking::panic_fmt(format_args!("apfloat::ieee::Single gave different result for `{0}`: {1}({2:#x}) vs Rust\'s {3}({4:#x})",
rust_f, f, f.to_bits(),
Single::from_bits(rust_f.to_bits().into()),
rust_f.to_bits()));
}
};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| {
::core::panicking::panic_fmt(format_args!("apfloat::ieee::Double failed to parse `{0}`: {1:?}",
num, e));
}panic!("apfloat::ieee::Double failed to parse `{num}`: {e:?}"));
1237
1238 if !(u128::from(rust_f.to_bits()) == f.to_bits()) {
{
::core::panicking::panic_fmt(format_args!("apfloat::ieee::Double gave different result for `{0}`: {1}({2:#x}) vs Rust\'s {3}({4:#x})",
rust_f, f, f.to_bits(),
Double::from_bits(rust_f.to_bits().into()),
rust_f.to_bits()));
}
};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;