rustc_smir/rustc_smir/
builder.rs

1//! Logic required to produce a monomorphic stable body.
2//!
3//! We first retrieve and monomorphize the rustc body representation, i.e., we generate a
4//! monomorphic body using internal representation.
5//! After that, we convert the internal representation into a stable one.
6
7use rustc_hir::def::DefKind;
8use rustc_middle::mir;
9use rustc_middle::mir::visit::MutVisitor;
10use rustc_middle::ty::{self, TyCtxt};
11
12use crate::rustc_smir::{Stable, Tables};
13
14/// Builds a monomorphic body for a given instance.
15pub(crate) struct BodyBuilder<'tcx> {
16    tcx: TyCtxt<'tcx>,
17    instance: ty::Instance<'tcx>,
18}
19
20impl<'tcx> BodyBuilder<'tcx> {
21    pub(crate) fn new(tcx: TyCtxt<'tcx>, instance: ty::Instance<'tcx>) -> Self {
22        let instance = match instance.def {
23            // To get the fallback body of an intrinsic, we need to convert it to an item.
24            ty::InstanceKind::Intrinsic(def_id) => ty::Instance::new(def_id, instance.args),
25            _ => instance,
26        };
27        BodyBuilder { tcx, instance }
28    }
29
30    /// Build a stable monomorphic body for a given instance based on the MIR body.
31    ///
32    /// All constants are also evaluated.
33    pub(crate) fn build(mut self, tables: &mut Tables<'tcx>) -> stable_mir::mir::Body {
34        let body = tables.tcx.instance_mir(self.instance.def).clone();
35        let mono_body = if !self.instance.args.is_empty()
36            // Without the `generic_const_exprs` feature gate, anon consts in signatures do not
37            // get generic parameters. Which is wrong, but also not a problem without
38            // generic_const_exprs
39            || self.tcx.def_kind(self.instance.def_id()) != DefKind::AnonConst
40        {
41            let mut mono_body = self.instance.instantiate_mir_and_normalize_erasing_regions(
42                tables.tcx,
43                ty::TypingEnv::fully_monomorphized(),
44                ty::EarlyBinder::bind(body),
45            );
46            self.visit_body(&mut mono_body);
47            mono_body
48        } else {
49            // Already monomorphic.
50            body
51        };
52        mono_body.stable(tables)
53    }
54}
55
56impl<'tcx> MutVisitor<'tcx> for BodyBuilder<'tcx> {
57    fn visit_const_operand(
58        &mut self,
59        constant: &mut mir::ConstOperand<'tcx>,
60        location: mir::Location,
61    ) {
62        let const_ = constant.const_;
63        let val = match const_.eval(self.tcx, ty::TypingEnv::fully_monomorphized(), constant.span) {
64            Ok(v) => v,
65            Err(mir::interpret::ErrorHandled::Reported(..)) => return,
66            Err(mir::interpret::ErrorHandled::TooGeneric(..)) => {
67                unreachable!("Failed to evaluate instance constant: {:?}", const_)
68            }
69        };
70        let ty = constant.ty();
71        constant.const_ = mir::Const::Val(val, ty);
72        self.super_const_operand(constant, location);
73    }
74
75    fn tcx(&self) -> TyCtxt<'tcx> {
76        self.tcx
77    }
78}