Skip to main content

charon_driver/translate/
translate_bodies.rs

1//! Translate functions from the rust compiler MIR to our internal representation.
2//! Our internal representation is very close to MIR, but is more convenient for
3//! us to handle, and easier to maintain - rustc's representation can evolve
4//! independently.
5
6use itertools::Itertools;
7use rustc_hash::FxHashMap as HashMap;
8use rustc_hir::attrs::LangItem;
9use std::borrow::Cow;
10use std::collections::VecDeque;
11use std::mem;
12use std::ops::Deref;
13use std::ops::DerefMut;
14use std::panic;
15use std::rc::Rc;
16
17use crate::hax;
18use rustc_middle::mir;
19use rustc_middle::ty;
20use rustc_span::{Symbol, sym};
21
22use super::translate_crate::*;
23use super::translate_ctx::*;
24use charon_lib::formatter::{FmtCtx, IntoFormatter, compute_local_names};
25use charon_lib::name_matcher::NamePattern;
26use charon_lib::options::TranslateOptions;
27use charon_lib::pretty::FmtWithCtx;
28use charon_lib::transform::ctx::BodyTransformCtx;
29use charon_lib::ullbc_ast::*;
30
31/// A translation context for function bodies.
32pub(crate) struct BodyTransCtx<'tcx, 'tctx, 'ictx> {
33    /// The translation context for the item.
34    pub i_ctx: &'ictx mut ItemTransCtx<'tcx, 'tctx>,
35    /// List of body locals.
36    pub local_decls: &'ictx rustc_index::IndexVec<mir::Local, mir::LocalDecl<'tcx>>,
37    /// Types supplied explicitly by the user.
38    pub user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
39
40    /// What kind of drops we get in this body.
41    pub drop_kind: DropKind,
42    /// The (regular) variables in the current function body.
43    pub locals: Locals,
44    /// The map from rust variable indices to translated variables indices.
45    pub locals_map: HashMap<usize, LocalId>,
46    /// The translated blocks.
47    pub blocks: IndexMap<BlockId, BlockData>,
48    /// The map from rust blocks to translated blocks.
49    /// Note that when translating terminators like DropAndReplace, we might have
50    /// to introduce new blocks which don't appear in the original MIR.
51    pub blocks_map: HashMap<mir::BasicBlock, BlockId>,
52    /// We register the blocks to translate in a stack, so as to avoid
53    /// writing the translation functions as recursive functions. We do
54    /// so because we had stack overflows in the past.
55    pub blocks_stack: VecDeque<mir::BasicBlock>,
56}
57
58impl<'tcx, 'tctx, 'ictx> BodyTransCtx<'tcx, 'tctx, 'ictx> {
59    pub(crate) fn new(
60        i_ctx: &'ictx mut ItemTransCtx<'tcx, 'tctx>,
61        body: &'ictx Rc<mir::Body<'tcx>>,
62        drop_kind: DropKind,
63    ) -> Self {
64        i_ctx.lifetime_freshener = (!i_ctx.options.erase_body_lifetimes).then(IndexMap::new);
65        let mut user_type_annotations = body.user_type_annotations.clone();
66        if let RustcItem::Mono(item) = &i_ctx.item_src.item {
67            // `CanonicalUserTypeAnnotation::user_ty` is deliberately not folded when rustc
68            // instantiates a MIR body, so do the item substitution explicitly.
69            let item = item.clone();
70            let args = item.rustc_args(i_ctx.hax_state_with_id());
71            for annotation in &mut user_type_annotations {
72                annotation.user_ty.value = hax::substitute(
73                    i_ctx.tcx,
74                    hax::UnderOwnerState::typing_env(&i_ctx.hax_state),
75                    Some(args),
76                    annotation.user_ty.value,
77                );
78            }
79        }
80        BodyTransCtx {
81            i_ctx,
82            local_decls: &body.local_decls,
83            user_type_annotations,
84            drop_kind,
85            locals: Default::default(),
86            locals_map: Default::default(),
87            blocks: Default::default(),
88            blocks_map: Default::default(),
89            blocks_stack: Default::default(),
90        }
91    }
92}
93
94impl<'tcx, 'tctx, 'ictx> Deref for BodyTransCtx<'tcx, 'tctx, 'ictx> {
95    type Target = ItemTransCtx<'tcx, 'tctx>;
96    fn deref(&self) -> &Self::Target {
97        self.i_ctx
98    }
99}
100impl<'tcx, 'tctx, 'ictx> DerefMut for BodyTransCtx<'tcx, 'tctx, 'ictx> {
101    fn deref_mut(&mut self) -> &mut Self::Target {
102        self.i_ctx
103    }
104}
105
106/// A translation context for function blocks.
107pub(crate) struct BlockTransCtx<'tcx, 'tctx, 'ictx, 'bctx> {
108    /// The translation context for the item.
109    pub b_ctx: &'bctx mut BodyTransCtx<'tcx, 'tctx, 'ictx>,
110    /// Block onto which we're adding statements.
111    pub current_block: BlockId,
112    /// The kind of the current block.
113    pub kind: UnwindKind,
114    /// Span of the statement or terminator currently being translated.
115    pub span: Span,
116    /// List of currently translated statements
117    pub statements: Vec<Statement>,
118}
119
120impl<'tcx, 'tctx, 'ictx, 'bctx> BlockTransCtx<'tcx, 'tctx, 'ictx, 'bctx> {
121    pub(crate) fn new(
122        b_ctx: &'bctx mut BodyTransCtx<'tcx, 'tctx, 'ictx>,
123        current_block: BlockId,
124        kind: UnwindKind,
125    ) -> Self {
126        BlockTransCtx {
127            b_ctx,
128            current_block,
129            kind,
130            span: Span::dummy(),
131            statements: Vec::new(),
132        }
133    }
134
135    fn finish_current_block(self, terminator: Terminator) {
136        let block = BlockData {
137            statements: self.statements,
138            terminator,
139            kind: self.kind,
140        };
141        self.b_ctx.blocks.set_slot(self.current_block, block);
142    }
143
144    /// Used for non-diverging intrinsics.
145    fn push_nounwind_call(&mut self, span: Span, dest: Place, call: Call) {
146        let target = self.blocks.reserve_slot();
147        let unwind_kind = self.kind.further_unwind_kind();
148        let on_unwind = self
149            .blocks
150            .push(Terminator::new(span, TerminatorKind::UndefinedBehavior).into_block(unwind_kind));
151        let block = BlockData {
152            statements: mem::take(&mut self.statements),
153            terminator: Terminator::new(
154                span,
155                TerminatorKind::Call {
156                    call,
157                    dest,
158                    target,
159                    on_unwind,
160                },
161            ),
162            kind: self.kind,
163        };
164        let current_block = mem::replace(&mut self.current_block, target);
165        self.blocks.set_slot(current_block, block);
166    }
167}
168
169impl<'tcx, 'tctx, 'ictx, 'bctx> Deref for BlockTransCtx<'tcx, 'tctx, 'ictx, 'bctx> {
170    type Target = BodyTransCtx<'tcx, 'tctx, 'ictx>;
171    fn deref(&self) -> &Self::Target {
172        self.b_ctx
173    }
174}
175impl<'tcx, 'tctx, 'ictx, 'bctx> DerefMut for BlockTransCtx<'tcx, 'tctx, 'ictx, 'bctx> {
176    fn deref_mut(&mut self) -> &mut Self::Target {
177        self.b_ctx
178    }
179}
180
181impl<'tcx> TranslateCtx<'tcx> {
182    pub fn translate_variant_id(&self, id: hax::VariantIdx) -> VariantId {
183        VariantId::new(id.as_usize())
184    }
185
186    pub fn translate_field_id(&self, id: hax::FieldIdx) -> FieldId {
187        FieldId::new(id.index())
188    }
189
190    pub fn translate_metadata_path(&self, path: &[hax::FieldIdx]) -> MetadataPath {
191        path.iter().map(|id| self.translate_field_id(*id)).collect()
192    }
193
194    fn translate_borrow_kind(
195        &self,
196        span: Span,
197        borrow_kind: mir::BorrowKind,
198    ) -> Result<BorrowKind, Error> {
199        Ok(match borrow_kind {
200            mir::BorrowKind::Shared => BorrowKind::Shared,
201            mir::BorrowKind::Mut { kind } => match kind {
202                mir::MutBorrowKind::Default => BorrowKind::Mut,
203                mir::MutBorrowKind::TwoPhaseBorrow => BorrowKind::TwoPhaseMut,
204                mir::MutBorrowKind::ClosureCapture => BorrowKind::UniqueImmutable,
205            },
206            mir::BorrowKind::Fake(mir::FakeBorrowKind::Shallow) => BorrowKind::Shallow,
207            // This one is used only in deref patterns.
208            mir::BorrowKind::Fake(mir::FakeBorrowKind::Deep) => {
209                raise_error!(self, span, "deref patterns are not supported")
210            }
211        })
212    }
213}
214
215impl<'tcx> ItemTransCtx<'tcx, '_> {
216    /// Translate the names of the arguments of this definition, if they are available,
217    /// otherwise naming arguments `arg0`, `arg1`, etc.
218    /// Note that the names of the arguments are not always available, even when
219    /// we can retrieve the MIR body, in which case we also fall back to `argN`.
220    pub fn translate_argument_names(
221        &mut self,
222        span: Span,
223        def: &hax::FullDef<'tcx>,
224        n_args: usize,
225    ) -> Vec<Option<String>> {
226        let Ok(Some(body)) = self.get_mir(def.this(), span) else {
227            return vec![None; n_args];
228        };
229        body.local_decls
230            .iter_enumerated()
231            .skip(1)
232            .take(body.arg_count)
233            .map(|(index, _)| hax::name_of_local(index, &body.var_debug_info))
234            .collect()
235    }
236
237    /// Translate the MIR body of this definition if it has one. Catches any error and returns
238    /// `Body::Error` instead
239    pub fn translate_def_body(&mut self, span: Span, def: &hax::FullDef<'tcx>) -> Body {
240        match self.translate_def_body_inner(span, def) {
241            Ok(body) => body,
242            Err(e) => Body::Error(e),
243        }
244    }
245
246    fn translate_def_body_inner(
247        &mut self,
248        span: Span,
249        def: &hax::FullDef<'tcx>,
250    ) -> Result<Body, Error> {
251        // Retrieve the body
252        if let Some(body) = self.get_mir(def.this(), span)? {
253            Ok(self.translate_body(span, body, &def.source_text))
254        } else if let Some(value) =
255            def.evaluate(self.hax_state_with_id(), hax::ReadMode::Structured)
256        {
257            // For globals without MIR, generate a body by evaluating the global. This is how we
258            // get the value of statics (which have no cross-crate MIR at all) and of "trivial"
259            // consts (whose value rustc stores directly instead of encoding MIR for it).
260            let c = self.translate_constant_expr(span, &value)?;
261            let mut bb = BodyBuilder::new(span, 0);
262            let ret = bb.new_var(None, c.ty().clone());
263            bb.push_statement(StatementKind::Assign(
264                ret,
265                Rvalue::Use(Operand::Const(c), WithRetag::No),
266            ));
267            Ok(Body::Unstructured(bb.build()))
268        } else {
269            Ok(Body::Missing)
270        }
271    }
272
273    /// Translate a function body. Catches errors and returns `Body::Error` instead.
274    /// That's the entrypoint of this module.
275    pub fn translate_body(
276        &mut self,
277        span: Span,
278        body: mir::Body<'tcx>,
279        source_text: &Option<String>,
280    ) -> Body {
281        let _guard = charon_lib::timing::scope("translate-body");
282        let drop_kind = match body.phase {
283            mir::MirPhase::Built | mir::MirPhase::Analysis(..) => DropKind::Conditional,
284            mir::MirPhase::Runtime(..) => DropKind::Precise,
285        };
286        let mut ctx = panic::AssertUnwindSafe(&mut *self);
287        let body = panic::AssertUnwindSafe(body);
288        // Stopgap measure because there are still many panics in charon and hax.
289        let res = panic::catch_unwind(move || {
290            let body = Rc::new({ body }.0);
291            let ctx = BodyTransCtx::new(*ctx, &body, drop_kind);
292            ctx.translate_body(&body, source_text)
293        });
294        match res {
295            Ok(Ok(body)) => body,
296            // Translation error
297            Ok(Err(e)) => Body::Error(e),
298            // Panic
299            Err(_) => {
300                let e = register_error!(self, span, "Thread panicked when extracting body.");
301                Body::Error(e)
302            }
303        }
304    }
305
306    pub(crate) fn translate_unsizing_metadata(
307        &mut self,
308        span: Span,
309        meta: &hax::UnsizingMetadata<'tcx>,
310    ) -> Result<UnsizingMetadata, Error> {
311        Ok(match meta {
312            hax::UnsizingMetadata::Length(len) => {
313                let len = self.translate_constant_expr(span, len)?;
314                UnsizingMetadata::Length(len)
315            }
316            hax::UnsizingMetadata::DirectVTable(trait_proof) => {
317                let tref = self.translate_trait_proof(span, trait_proof)?;
318                let vtable = self.translate_vtable_instance_const(span, trait_proof)?;
319                UnsizingMetadata::VTable(tref, vtable)
320            }
321            hax::UnsizingMetadata::NestedVTable(dyn_trait_proof) => {
322                // This binds a fake `T: SrcTrait` variable.
323                let binder =
324                    self.translate_dyn_binder(span, dyn_trait_proof, |ctx, _, trait_proof| {
325                        ctx.translate_trait_proof(span, trait_proof)
326                    })?;
327
328                // Compute the supertrait path from the source tref to the target
329                // tref.
330                let target_proof = binder.skip_binder.clone().erase();
331                let mut target_tref = &target_proof;
332                let mut clause_path: Vec<(TraitDeclId, TraitClauseId)> = vec![];
333                while let TraitRefKind::ParentClause(tref, id, _) = &target_tref.kind {
334                    clause_path.push((tref.trait_id(), *id));
335                    target_tref = tref;
336                }
337
338                let mut field_path = vec![];
339                for &(trait_id, clause_id) in &clause_path {
340                    if let Ok(ItemRef::TraitDecl(tdecl)) = self.get_or_translate(trait_id.into())
341                        && let vtable_decl_id = tdecl.vtable.as_ref().unwrap().id
342                        && let Ok(ItemRef::Type(vtable_decl)) =
343                            self.get_or_translate(vtable_decl_id.into())
344                    {
345                        let TypeSource::VTable { supertrait_map, .. } = &vtable_decl.src else {
346                            unreachable!()
347                        };
348                        field_path.push(supertrait_map[clause_id].unwrap());
349                    } else {
350                        break;
351                    }
352                }
353
354                if field_path.len() == clause_path.len() {
355                    UnsizingMetadata::VTableUpcast(field_path)
356                } else {
357                    UnsizingMetadata::Unknown
358                }
359            }
360            hax::UnsizingMetadata::Unknown => UnsizingMetadata::Unknown,
361        })
362    }
363
364    /// Generate a fake function body for ADT constructors.
365    pub(crate) fn build_ctor_body(
366        &mut self,
367        span: Span,
368        def: &hax::FullDef<'tcx>,
369    ) -> Result<Body, Error> {
370        let hax::FullDefKind::Ctor(ctor) = def.kind() else {
371            unreachable!()
372        };
373        let tref = self.translate_type_decl_ref(
374            span,
375            &def.this().with_def_id(self.hax_state(), ctor.adt_def_id()),
376        )?;
377        let output_ty = self.translate_ty(span, ctor.output_ty())?;
378
379        let mut builder = BodyBuilder::new(span, ctor.fields().len());
380        let return_place = builder.new_var(None, output_ty);
381        let args: Vec<_> = ctor
382            .fields()
383            .iter()
384            .map(|field| -> Result<Operand, Error> {
385                let ty = self.translate_ty(span, &field.ty)?;
386                let place = builder.new_var(None, ty);
387                Ok(Operand::Move(place))
388            })
389            .try_collect()?;
390        let variant = match ctor.ctor_of() {
391            hax::CtorOf::Struct => None,
392            hax::CtorOf::Variant => Some(self.translate_variant_id(ctor.variant_id())),
393        };
394        builder.push_statement(StatementKind::Assign(
395            return_place,
396            Rvalue::Aggregate(AggregateKind::Adt(tref, variant, None), args),
397        ));
398        Ok(Body::Unstructured(builder.build()))
399    }
400
401    /// FIXME(#865): Generate a function body for the `box_assume_init_into_vec_unsafe` function,
402    /// because the MIR we get for it is too optimized to be usable.
403    pub(crate) fn build_box_assume_init_into_vec_unsafe(
404        &mut self,
405        span: Span,
406        def: &hax::FullDef<'tcx>,
407    ) -> Result<Body, Error> {
408        // pub fn box_assume_init_into_vec_unsafe<T, const N: usize>(
409        //     b: Box<MaybeUninit<[T; N]>>,
410        // ) -> Vec<T> {
411        //     let x: Box<[T; N]> = unsafe { Box::assume_init(b) };
412        //     let y = x as Box<[T]>;
413        //     core::slice::into_vec(y)
414        // }
415        let tcx = self.tcx;
416        let hax::FullDefKind::Fn(f) = def.kind() else {
417            unreachable!()
418        };
419        let hax_sig = f.sig().hax_skip_binder_ref();
420        let sig = self.translate_fun_sig(span, hax_sig)?;
421
422        // Get the `[T; N]` and `A` parameters.
423        let (array_rust_ty, alloc_rust_ty) = {
424            let input_box_rust_args = {
425                let hax::TyKind::Adt(input_box) = hax_sig.inputs[0].kind() else {
426                    raise_error!(self, span, "expected a boxed input in the hax signature");
427                };
428                input_box.item.rustc_args(self.hax_state_with_id())
429            };
430            let maybe_uninit_array_rust_ty = input_box_rust_args[0].as_type().unwrap();
431            let alloc_rust_ty = input_box_rust_args[1].as_type().unwrap();
432            let ty::Adt(_, maybe_uninit_rust_args) = maybe_uninit_array_rust_ty.kind() else {
433                raise_error!(
434                    self,
435                    span,
436                    "expected `MaybeUninit<[T; N]>` in the hax signature"
437                );
438            };
439            let Some(array_rust_ty) = maybe_uninit_rust_args[0].as_type() else {
440                raise_error!(
441                    self,
442                    span,
443                    "expected the first `MaybeUninit` parameter to be a type"
444                );
445            };
446            (array_rust_ty, alloc_rust_ty)
447        };
448        // `T`
449        let elem_rust_ty = array_rust_ty.builtin_index().unwrap();
450        // `[T]`
451        let slice_rust_ty = ty::Ty::new_slice(tcx, elem_rust_ty);
452        // `Box<[T; N]>`
453        let box_array_rust_ty = ty::Ty::new_box(tcx, array_rust_ty);
454        let box_array_ty = self.translate_rustc_ty(span, &box_array_rust_ty)?;
455        // `Box<[T]>`
456        let box_slice_rust_ty = ty::Ty::new_box(tcx, slice_rust_ty);
457        let box_slice_ty = self.translate_rustc_ty(span, &box_slice_rust_ty)?;
458
459        if !self.monomorphize() {
460            // Make `Box::new` and `Box::write` available to a later construction pass.
461            let path = NamePattern::parse(names::BOX_NEW).unwrap();
462            let box_new_def_id = self.resolve_single_path(span, &path)?;
463            let box_new_args = tcx.mk_args(&[array_rust_ty.into()]);
464            let box_new_item =
465                hax::ItemRef::translate(self.hax_state_with_id(), box_new_def_id, box_new_args);
466            let _ = self.translate_fn_ptr(span, &box_new_item, TransItemSourceKind::Fun)?;
467
468            let path = NamePattern::parse(names::BOX_WRITE).unwrap();
469            let box_write_def_id = self.resolve_single_path(span, &path)?;
470            let box_write_args = tcx.mk_args(&[array_rust_ty.into(), alloc_rust_ty.into()]);
471            let box_write_item =
472                hax::ItemRef::translate(self.hax_state_with_id(), box_write_def_id, box_write_args);
473            let _ = self.translate_fn_ptr(span, &box_write_item, TransItemSourceKind::Fun)?;
474        }
475
476        let body = {
477            let mut builder = BodyBuilder::new(span, sig.inputs.len());
478            let return_place = builder.new_var(Some("ret".to_string()), sig.output.clone());
479            let input = builder.new_var(Some("b".to_string()), sig.inputs[0].clone());
480            let initialized_box = builder.new_var(Some("x".to_string()), box_array_ty.clone());
481            let box_slice = builder.new_var(Some("y".to_string()), box_slice_ty.clone());
482
483            let call = {
484                let assume_init_fn = {
485                    let path = NamePattern::parse("alloc::boxed::Box::assume_init").unwrap();
486                    let assume_init_def_id = self
487                        .resolve_path(span, &path, true)?
488                        .into_iter()
489                        // There's `assume_init` on `Box<MU<T>>` and `Box<[MU<T>]>`, we want the former.
490                        .filter(|&def_id| {
491                            let sig = self.tcx.fn_sig(def_id);
492                            !sig.skip_binder().inputs().skip_binder()[0]
493                                .expect_boxed_ty()
494                                .is_slice()
495                        })
496                        .exactly_one()
497                        .unwrap();
498                    let assume_init_args =
499                        tcx.mk_args(&[array_rust_ty.into(), alloc_rust_ty.into()]);
500                    let assume_init_item = hax::ItemRef::translate(
501                        self.hax_state_with_id(),
502                        assume_init_def_id,
503                        assume_init_args,
504                    );
505                    self.translate_fn_ptr(span, &assume_init_item, TransItemSourceKind::Fun)?
506                };
507                Call {
508                    func: FnOperand::Regular(assume_init_fn),
509                    args: vec![Operand::Move(input)],
510                    safety: CallSafety::Inherit,
511                }
512            };
513            builder.call(initialized_box.clone(), call);
514
515            builder.push_statement({
516                let (path, meta) = hax::compute_unsizing_metadata(
517                    &self.hax_state,
518                    box_array_rust_ty,
519                    box_slice_rust_ty,
520                );
521                let meta = self.translate_unsizing_metadata(span, &meta)?;
522                let path = self.translate_metadata_path(&path);
523                StatementKind::Assign(
524                    box_slice.clone(),
525                    Rvalue::UnaryOp(
526                        UnOp::Cast(CastKind::Unsize(box_slice_ty, meta, path)),
527                        Operand::Move(initialized_box),
528                    ),
529                )
530            });
531
532            let call = {
533                let into_vec_fn = {
534                    let path = NamePattern::parse("slice::into_vec").unwrap();
535                    let into_vec_def_id = self.resolve_single_path(span, &path)?;
536                    let into_vec_args = tcx.mk_args(&[elem_rust_ty.into(), alloc_rust_ty.into()]);
537                    let into_vec_item = hax::ItemRef::translate(
538                        self.hax_state_with_id(),
539                        into_vec_def_id,
540                        into_vec_args,
541                    );
542                    self.translate_fn_ptr(span, &into_vec_item, TransItemSourceKind::Fun)?
543                };
544                Call {
545                    func: FnOperand::Regular(into_vec_fn),
546                    args: vec![Operand::Move(box_slice)],
547                    safety: CallSafety::Inherit,
548                }
549            };
550            builder.call(return_place, call);
551            builder.build()
552        };
553
554        Ok(Body::Unstructured(body))
555    }
556
557    /// Generate a function body for `core::intrinsics::type_id`.
558    pub(crate) fn build_type_id_body(
559        &mut self,
560        span: Span,
561        signature: &FunSig,
562    ) -> Result<Body, Error> {
563        let generics = self.outermost_generics().identity_args();
564        let type_id_ty = generics.types[0].clone();
565
566        let mut builder = BodyBuilder::new(span, signature.inputs.len());
567        let return_place = builder.new_var(Some("ret".to_string()), signature.output.clone());
568        let type_id = ConstantExpr::new(
569            ConstantExprKind::TypeId(type_id_ty),
570            signature.output.clone(),
571        );
572        builder.push_statement(StatementKind::Assign(
573            return_place,
574            Rvalue::Use(Operand::Const(type_id), WithRetag::No),
575        ));
576        Ok(Body::Unstructured(builder.build()))
577    }
578
579    /// Generate a function body for `core::ptr::drop_glue`.
580    pub(crate) fn build_drop_glue_body(
581        &mut self,
582        span: Span,
583        def: &hax::FullDef<'tcx>,
584        signature: &FunSig,
585    ) -> Result<Body, Error> {
586        let hax::FullDefKind::Fn(_) = def.kind() else {
587            unreachable!()
588        };
589        let def_id = def.def_id().as_real_def_id().unwrap();
590        let rustc_args = def.this().rustc_args(self.hax_state_with_id());
591        let rustc_sig = self.tcx.fn_sig(def_id).instantiate(self.tcx, rustc_args);
592        // `skip_binder` is ok because we have that lifetime in scope.
593        let input_ty = rustc_sig.skip_binder().inputs()[0];
594        let pointee_ty = input_ty
595            .builtin_deref(true)
596            .expect("`drop_glue` argument is not a pointer");
597        let fn_ptr = self.translate_drop_glue_method_call(span, pointee_ty)?;
598
599        let mut builder = BodyBuilder::new(span, signature.inputs.len());
600        let _return_place = builder.new_var(Some("ret".to_string()), signature.output.clone());
601        let input = builder.new_var(None, signature.inputs[0].clone());
602        builder.insert_drop(input.deref(), fn_ptr);
603        Ok(Body::Unstructured(builder.build()))
604    }
605}
606
607impl<'tcx> BodyTransCtx<'tcx, '_, '_> {
608    pub(crate) fn translate_local(&self, local: &mir::Local) -> Option<LocalId> {
609        self.locals_map.get(&local.index()).copied()
610    }
611
612    pub(crate) fn push_var(&mut self, rid: mir::Local, ty: Ty, name: Option<String>, span: Span) {
613        let local_id = self.locals.locals.push_with(|index| Local {
614            index,
615            name,
616            span,
617            ty,
618            drop_flag_for: None,
619        });
620        self.locals_map.insert(rid.as_usize(), local_id);
621    }
622
623    /// Translate a function's local variables by adding them in the environment.
624    fn translate_body_locals(&mut self, body: &mir::Body<'tcx>) -> Result<(), Error> {
625        // Translate the parameters
626        for (index, var) in body.local_decls.iter_enumerated() {
627            // Find the name of the variable
628            let name: Option<String> = hax::name_of_local(index, &body.var_debug_info);
629
630            // Translate the type
631            let span = self.translate_span(&var.source_info.span);
632            let ty = self.translate_rustc_ty(span, &var.ty)?;
633
634            // Add the variable to the environment
635            self.push_var(index, ty, name, span);
636        }
637
638        Ok(())
639    }
640
641    /// Translate a basic block id and register it, if it hasn't been done.
642    fn translate_basic_block_id(&mut self, block_id: mir::BasicBlock) -> BlockId {
643        match self.blocks_map.get(&block_id) {
644            Some(id) => *id,
645            // Generate a fresh id - this also registers the block
646            None => {
647                // Push to the stack of blocks awaiting translation
648                self.blocks_stack.push_back(block_id);
649                let id = self.blocks.reserve_slot();
650                // Insert in the map
651                self.blocks_map.insert(block_id, id);
652                id
653            }
654        }
655    }
656
657    fn translate_basic_block(
658        &mut self,
659        block_id: BlockId,
660        source_scopes: &rustc_index::IndexVec<mir::SourceScope, mir::SourceScopeData>,
661        block: &mir::BasicBlockData<'tcx>,
662    ) -> Result<(), Error> {
663        // Translate the statements
664        let kind = if block.is_cleanup {
665            UnwindKind::Cleanup
666        } else {
667            UnwindKind::Regular
668        };
669        let mut block_ctx = BlockTransCtx::new(self, block_id, kind);
670        for statement in &block.statements {
671            trace!("statement: {:?}", statement);
672            block_ctx.translate_statement(source_scopes, statement)?;
673        }
674
675        // Translate the terminator
676        let terminator = block.terminator.as_ref().unwrap();
677        block_ctx.translate_terminator(source_scopes, terminator)?;
678
679        Ok(())
680    }
681
682    /// Gather all the lines that start with `//` inside the given span.
683    fn translate_body_comments(
684        &mut self,
685        source_text: &Option<String>,
686        charon_span: Span,
687    ) -> Vec<(u32, Vec<String>)> {
688        if let Some(body_text) = source_text {
689            let mut comments = body_text
690                .lines()
691                // Iter through the lines of this body in reverse order.
692                .rev()
693                .enumerate()
694                // Compute the absolute line number
695                .filter_map(|(i, line)| {
696                    Some(((charon_span.data().end.line).checked_sub(i as u32)?, line))
697                })
698                // Extract the comment if this line starts with `//`
699                .map(|(line_nbr, line)| (line_nbr, line.trim_start().strip_prefix("//")))
700                .peekable()
701                .batching(|iter| {
702                    // Get the next line. This is not a comment: it's either the last line of the
703                    // body or a line that wasn't consumed by `peeking_take_while`.
704                    let (line_nbr, _first) = iter.next()?;
705                    // Collect all the comments before this line.
706                    let mut comments = iter
707                        // `peeking_take_while` ensures we don't consume a line that returns
708                        // `false`. It will be consumed by the next round of `batching`.
709                        .peeking_take_while(|(_, opt_comment)| opt_comment.is_some())
710                        .map(|(_, opt_comment)| opt_comment.unwrap())
711                        .map(|s| s.strip_prefix(" ").unwrap_or(s))
712                        .map(str::to_owned)
713                        .collect_vec();
714                    comments.reverse();
715                    Some((line_nbr, comments))
716                })
717                .filter(|(_, comments)| !comments.is_empty())
718                .collect_vec();
719            comments.reverse();
720            comments
721        } else {
722            Vec::new()
723        }
724    }
725
726    fn translate_body(
727        mut self,
728        mir_body: &mir::Body<'tcx>,
729        source_text: &Option<String>,
730    ) -> Result<Body, Error> {
731        // Compute the span information
732        let span = self.translate_span(&mir_body.span);
733
734        // Initialize the local variables
735        trace!("Translating the body locals");
736        self.locals.arg_count = mir_body.arg_count;
737        self.translate_body_locals(mir_body)?;
738
739        // Translate the expression body
740        trace!("Translating the expression body");
741
742        // Register the start block
743        let id = self.translate_basic_block_id(rustc_index::Idx::new(mir::START_BLOCK.as_usize()));
744        assert!(id == START_BLOCK_ID);
745
746        // For as long as there are blocks in the stack, translate them
747        while let Some(mir_block_id) = self.blocks_stack.pop_front() {
748            let mir_block = mir_body.basic_blocks.get(mir_block_id).unwrap();
749            let block_id = self.translate_basic_block_id(mir_block_id);
750            self.translate_basic_block(block_id, &mir_body.source_scopes, mir_block)?;
751        }
752
753        // Create the body
754        let comments = self.translate_body_comments(source_text, span);
755        Ok(Body::Unstructured(ExprBody {
756            span,
757            locals: self.locals,
758            bound_body_regions: self
759                .i_ctx
760                .lifetime_freshener
761                .take()
762                .map_or(0, |v| v.slot_count()),
763            body: self.blocks.make_contiguous(),
764            comments,
765        }))
766    }
767}
768
769impl BodyTransformCtx for BlockTransCtx<'_, '_, '_, '_> {
770    fn get_crate(&self) -> &TranslatedCrate {
771        &self.translated
772    }
773
774    fn get_options(&self) -> &TranslateOptions {
775        &self.options
776    }
777
778    fn get_params(&self) -> &GenericParams {
779        self.outermost_generics()
780    }
781
782    fn get_locals_mut(&mut self) -> &mut Locals {
783        &mut self.locals
784    }
785
786    fn insert_storage_live_stmt(&mut self, local: LocalId) {
787        self.statements
788            .push(Statement::new(self.span, StatementKind::StorageLive(local)));
789    }
790
791    fn insert_storage_dead_stmt(&mut self, local: LocalId) {
792        self.statements
793            .push(Statement::new(self.span, StatementKind::StorageDead(local)));
794    }
795
796    fn insert_assn_stmt(&mut self, place: Place, rvalue: Rvalue) {
797        self.statements.push(Statement::new(
798            self.span,
799            StatementKind::Assign(place, rvalue),
800        ));
801    }
802}
803
804impl<'tcx> BlockTransCtx<'tcx, '_, '_, '_> {
805    fn missing_ptr_metadata() -> Operand {
806        Operand::Const(ConstantExpr::new(
807            ConstantExprKind::Opaque("Missing metadata".to_string()),
808            Ty::mk_unit(),
809        ))
810    }
811
812    fn apply_user_type_projection(
813        &mut self,
814        span: Span,
815        mut ty: Ty,
816        projections: &[mir::ProjectionElem<(), ()>],
817    ) -> Result<Ty, Error> {
818        let mut downcast = None;
819        for projection in projections {
820            let projection = match projection {
821                mir::ProjectionElem::Deref => ProjectionElem::Deref,
822                mir::ProjectionElem::PhantomDeref => {
823                    raise_error!(
824                        self,
825                        span,
826                        "unsupported phantom dereference in user type projection"
827                    );
828                }
829                mir::ProjectionElem::Field(field, ()) => {
830                    let field = self.translate_field_id(*field);
831                    let TyKind::Adt(type_ref) = ty.kind() else {
832                        raise_error!(self, span, "field projection on unexpected type");
833                    };
834                    match type_ref.as_builtin() {
835                        None => ProjectionElem::Field(downcast.take(), field),
836                        Some(BuiltinAdt::Tuple) => ProjectionElem::Field(None, field),
837                        Some(BuiltinAdt::Box) if field == FieldId::ZERO => ProjectionElem::Deref,
838                        _ => raise_error!(self, span, "field projection on unexpected type"),
839                    }
840                }
841                mir::ProjectionElem::Index(()) => ProjectionElem::Index {
842                    offset: Box::new(Operand::mk_const_unit()),
843                    from_end: false,
844                },
845                mir::ProjectionElem::ConstantIndex { from_end, .. } => ProjectionElem::Index {
846                    offset: Box::new(Operand::mk_const_unit()),
847                    from_end: *from_end,
848                },
849                mir::ProjectionElem::Subslice { from_end, .. } => ProjectionElem::Subslice {
850                    from: Box::new(Operand::mk_const_unit()),
851                    to: Box::new(Operand::mk_const_unit()),
852                    from_end: *from_end,
853                },
854                mir::ProjectionElem::Downcast(_, variant) => {
855                    downcast = Some(self.translate_variant_id(*variant));
856                    continue;
857                }
858                mir::ProjectionElem::OpaqueCast(()) => {
859                    raise_error!(self, span, "unexpected opaque cast in user type projection");
860                }
861                mir::ProjectionElem::UnwrapUnsafeBinder(()) => {
862                    raise_error!(
863                        self,
864                        span,
865                        "unsupported unsafe binder in user type projection"
866                    );
867                }
868            };
869            let Some(next_ty) = projection.project_type(&self.translated, &ty) else {
870                raise_error!(self, span, "invalid user type projection");
871            };
872            ty = next_ty;
873        }
874        Ok(ty)
875    }
876
877    fn translate_user_type_projection(
878        &mut self,
879        span: Span,
880        user_ty: &mir::UserTypeProjection,
881    ) -> Result<(Ty, Vec<BorrowckStatement>), Error> {
882        use rustc_trait_selection::infer::canonical::CanonicalExt;
883
884        let annotation = self.user_type_annotations[user_ty.base].clone();
885        let canonical = *annotation.user_ty;
886
887        let mut facts = Vec::new();
888        if !canonical.value.bounds.is_empty() {
889            let user_ty_before_inference = match canonical.value.kind {
890                ty::UserTypeKind::Ty(ty) => ty,
891                ty::UserTypeKind::TypeOf(def_id, user_args)
892                    if user_args.args.len() == self.tcx.generics_of(def_id).count() =>
893                {
894                    self.tcx
895                        .type_of(def_id)
896                        .instantiate(self.tcx, user_args.args)
897                        .skip_normalization()
898                }
899                // Inherent associated type consts use a special argument format; rustc reconstructs
900                // their impl arguments with inference. Their resulting type is already recorded here.
901                ty::UserTypeKind::TypeOf(..) => annotation.inferred_ty,
902            };
903
904            // Rustc discards the original canonicalization values when it stores this annotation.
905            // Recover just enough of that mapping to instantiate the explicit user bounds. The
906            // place relation below deliberately uses `inferred_ty` directly.
907            let Some(var_values) = hax::rustc::match_canonical_var_values(
908                self.tcx,
909                canonical.var_kinds,
910                user_ty_before_inference,
911                annotation.inferred_ty,
912            ) else {
913                raise_error!(
914                    self,
915                    span,
916                    "could not match a user type annotation with its inferred type"
917                )
918            };
919            let instantiated_user_ty = canonical.instantiate(self.tcx, &var_values);
920
921            for clause in instantiated_user_ty.bounds {
922                if let Some(trait_predicate) = clause.as_trait_clause() {
923                    if trait_predicate.skip_binder().polarity != ty::ClausePolarity::Positive {
924                        raise_error!(self, span, "negative trait bound in a user type annotation")
925                    }
926                    let proof = hax::solve_trait(
927                        &self.hax_state,
928                        trait_predicate.map_bound(|predicate| predicate.trait_ref),
929                    );
930                    facts.push(BorrowckStatement::PredicateHolds(
931                        self.translate_trait_proof(span, &proof)?,
932                    ));
933                } else if let Some(outlives) = clause.as_type_outlives_clause() {
934                    let Some(ty::OutlivesClause(outlived_ty, region)) = outlives.no_bound_vars()
935                    else {
936                        raise_error!(self, span, "higher-ranked outlives user type bound")
937                    };
938                    let outlived_ty = self.translate_rustc_ty(span, &outlived_ty)?;
939                    let region = self.catch_sinto(span, &region)?;
940                    let region = self.translate_region(span, &region)?;
941                    facts.push(BorrowckStatement::SetOutlives(outlived_ty, region));
942                }
943            }
944        }
945
946        // This is the type rustc itself relates the MIR place against. In particular, it has
947        // already revealed local `impl Trait` types and performed type normalization.
948        let ty = self.translate_rustc_ty(span, &annotation.inferred_ty)?;
949        let ty = self.apply_user_type_projection(span, ty, &user_ty.projs)?;
950
951        Ok((ty, facts))
952    }
953
954    fn translate_thread_local_ref(
955        &mut self,
956        span: Span,
957        def_id: rustc_hir::def_id::DefId,
958    ) -> Result<Rvalue, Error> {
959        let args = ty::GenericArgs::empty();
960        let item = hax::translate_item_ref(&self.hax_state, def_id, args);
961        let global_ref = self.translate_global_decl_ref(span, &item)?;
962
963        let ptr_ty = self.tcx.thread_local_ptr_ty(def_id);
964        let ty = ptr_ty.builtin_deref(true).unwrap();
965        let ty = self.translate_rustc_ty(span, &ty)?;
966        let place = Place::new_global(global_ref, ty);
967        match ptr_ty.kind() {
968            ty::TyKind::Ref(_, _, mutability) => {
969                let kind = if mutability.is_mut() {
970                    BorrowKind::Mut
971                } else {
972                    BorrowKind::Shared
973                };
974                Ok(Rvalue::Ref {
975                    place,
976                    kind,
977                    // Will be fixed by the cleanup pass `insert_ptr_metadata`.
978                    ptr_metadata: Self::missing_ptr_metadata(),
979                })
980            }
981            ty::TyKind::RawPtr(_, mutability) => {
982                let kind = if mutability.is_mut() {
983                    RefKind::Mut
984                } else {
985                    RefKind::Shared
986                };
987                Ok(Rvalue::RawPtr {
988                    place,
989                    kind,
990                    // Will be fixed by the cleanup pass `insert_ptr_metadata`.
991                    ptr_metadata: Self::missing_ptr_metadata(),
992                })
993            }
994            _ => raise_error!(
995                self,
996                span,
997                "unexpected type for thread-local reference: {ptr_ty:?}"
998            ),
999        }
1000    }
1001
1002    fn translate_binaryop_kind(&mut self, _span: Span, binop: mir::BinOp) -> Result<BinOp, Error> {
1003        Ok(match binop {
1004            mir::BinOp::BitXor => BinOp::BitXor,
1005            mir::BinOp::BitAnd => BinOp::BitAnd,
1006            mir::BinOp::BitOr => BinOp::BitOr,
1007            mir::BinOp::Eq => BinOp::Eq,
1008            mir::BinOp::Lt => BinOp::Lt,
1009            mir::BinOp::Le => BinOp::Le,
1010            mir::BinOp::Ne => BinOp::Ne,
1011            mir::BinOp::Ge => BinOp::Ge,
1012            mir::BinOp::Gt => BinOp::Gt,
1013            mir::BinOp::Add => BinOp::Add(OverflowMode::Wrap),
1014            mir::BinOp::AddUnchecked => BinOp::Add(OverflowMode::UB),
1015            mir::BinOp::Sub => BinOp::Sub(OverflowMode::Wrap),
1016            mir::BinOp::SubUnchecked => BinOp::Sub(OverflowMode::UB),
1017            mir::BinOp::Mul => BinOp::Mul(OverflowMode::Wrap),
1018            mir::BinOp::MulUnchecked => BinOp::Mul(OverflowMode::UB),
1019            mir::BinOp::Div => BinOp::Div(OverflowMode::UB),
1020            mir::BinOp::Rem => BinOp::Rem(OverflowMode::UB),
1021            mir::BinOp::AddWithOverflow
1022            | mir::BinOp::SubWithOverflow
1023            | mir::BinOp::MulWithOverflow => unreachable!("handled in `translate_statement`"),
1024            mir::BinOp::Shl => BinOp::Shl(OverflowMode::Wrap),
1025            mir::BinOp::ShlUnchecked => BinOp::Shl(OverflowMode::UB),
1026            mir::BinOp::Shr => BinOp::Shr(OverflowMode::Wrap),
1027            mir::BinOp::ShrUnchecked => BinOp::Shr(OverflowMode::UB),
1028            mir::BinOp::Cmp => BinOp::Cmp,
1029            mir::BinOp::Offset => unreachable!("handled in `translate_mir_rvalue`"),
1030        })
1031    }
1032
1033    fn translate_place(
1034        &mut self,
1035        span: Span,
1036        mir_place: &mir::Place<'tcx>,
1037    ) -> Result<Place, Error> {
1038        use crate::hax::{BaseState, SInto};
1039        use rustc_middle::ty;
1040
1041        let tcx = self.hax_state.base().tcx;
1042        let local_decls = self.local_decls;
1043        let mut place_ty: mir::PlaceTy = mir::Place::from(mir_place.local).ty(local_decls, tcx);
1044        let var_id = self.translate_local(&mir_place.local).unwrap();
1045        let mut place = self.locals.place_for_var(var_id);
1046        for elem in mir_place.projection.as_slice() {
1047            use mir::ProjectionElem::*;
1048            if let TyKind::Error(msg) = place.ty().kind() {
1049                return Err(Error {
1050                    span,
1051                    msg: msg.clone(),
1052                });
1053            }
1054            let projected_place_ty = place_ty.projection_ty(tcx, *elem);
1055            let next_place_ty = projected_place_ty.ty.sinto(&self.hax_state);
1056            let next_place_ty = self.translate_ty(span, &next_place_ty)?;
1057            let proj_elem = match elem {
1058                Deref => ProjectionElem::Deref,
1059                PhantomDeref => {
1060                    raise_error!(self, span, "unsupported phantom dereference in MIR place");
1061                }
1062                Field(index, _) => {
1063                    let TyKind::Adt(tref) = place.ty().kind() else {
1064                        raise_error!(
1065                            self,
1066                            span,
1067                            "found unexpected type in field projection: {}",
1068                            next_place_ty.with_ctx(&self.into_fmt())
1069                        )
1070                    };
1071                    let field_id = self.translate_field_id(*index);
1072                    match place_ty.ty.kind() {
1073                        ty::Adt(adt_def, _) => {
1074                            let variant = place_ty.variant_index;
1075                            let variant_id = variant.map(|id| self.translate_variant_id(id));
1076                            let generics = &tref.generics;
1077                            match tref.as_builtin() {
1078                                None => {
1079                                    assert!(
1080                                        ((adt_def.is_struct() || adt_def.is_union())
1081                                            && variant.is_none())
1082                                            || (adt_def.is_enum() && variant.is_some())
1083                                    );
1084                                    ProjectionElem::Field(variant_id, field_id)
1085                                }
1086                                Some(BuiltinAdt::Tuple) => {
1087                                    assert!(generics.regions.is_empty());
1088                                    assert!(variant.is_none());
1089                                    assert!(generics.const_generics.is_empty());
1090                                    ProjectionElem::Field(None, field_id)
1091                                }
1092                                Some(BuiltinAdt::Box)
1093                                    if self.t_ctx.options.treat_box_as_builtin =>
1094                                {
1095                                    // Some sanity checks
1096                                    assert!(generics.regions.is_empty());
1097                                    assert!(generics.types.len() == 2);
1098                                    assert!(generics.const_generics.is_empty());
1099                                    if field_id == FieldId::ZERO {
1100                                        // We pretend the pointee field is a deref.
1101                                        ProjectionElem::Deref
1102                                    } else {
1103                                        raise_error!(
1104                                            self,
1105                                            span,
1106                                            "trying to access the allocator field from Box, \
1107                                            but it is being treated as a builtin (without allocator)"
1108                                        )
1109                                    }
1110                                }
1111                                Some(BuiltinAdt::Box) => ProjectionElem::Field(None, field_id),
1112                                Some(_) => {
1113                                    raise_error!(self, span, "Unexpected field projection")
1114                                }
1115                            }
1116                        }
1117                        ty::Tuple(_types) => ProjectionElem::Field(None, field_id),
1118                        // We get there when we access one of the fields of the state captured by a
1119                        // closure.
1120                        ty::Closure(..) => ProjectionElem::Field(None, field_id),
1121                        _ => panic!(),
1122                    }
1123                }
1124                Index(local) => {
1125                    let var_id = self.translate_local(local).unwrap();
1126                    let local = self.locals.place_for_var(var_id);
1127                    let offset = Operand::Copy(local);
1128                    ProjectionElem::Index {
1129                        offset: Box::new(offset),
1130                        from_end: false,
1131                    }
1132                }
1133                &ConstantIndex {
1134                    offset, from_end, ..
1135                } => {
1136                    let offset =
1137                        Operand::Const(IntegerValue::mk_usize(offset as u128).to_constant());
1138                    ProjectionElem::Index {
1139                        offset: Box::new(offset),
1140                        from_end,
1141                    }
1142                }
1143                &Subslice { from, to, from_end } => {
1144                    let from = Operand::Const(IntegerValue::mk_usize(from as u128).to_constant());
1145                    let to = Operand::Const(IntegerValue::mk_usize(to as u128).to_constant());
1146                    ProjectionElem::Subslice {
1147                        from: Box::new(from),
1148                        to: Box::new(to),
1149                        from_end,
1150                    }
1151                }
1152                OpaqueCast(..) => {
1153                    raise_error!(self, span, "Unexpected ProjectionElem::OpaqueCast");
1154                }
1155                Downcast { .. } => {
1156                    // We keep the same `Place`, the variant is tracked in the `PlaceTy` and we can
1157                    // access it next loop iteration.
1158                    place_ty = projected_place_ty;
1159                    continue;
1160                }
1161                UnwrapUnsafeBinder { .. } => {
1162                    raise_error!(self, span, "unsupported feature: unsafe binders");
1163                }
1164            };
1165            place = place.project(proj_elem, next_place_ty);
1166            place_ty = projected_place_ty;
1167        }
1168        Ok(place)
1169    }
1170
1171    /// Translate an operand
1172    fn translate_operand(
1173        &mut self,
1174        span: Span,
1175        operand: &mir::Operand<'tcx>,
1176    ) -> Result<Operand, Error> {
1177        Ok(match operand {
1178            mir::Operand::Copy(place) => {
1179                let p = self.translate_place(span, place)?;
1180                Operand::Copy(p)
1181            }
1182            mir::Operand::Move(place) => {
1183                let p = self.translate_place(span, place)?;
1184                Operand::Move(p)
1185            }
1186            mir::Operand::Constant(const_op) => {
1187                let const_op = self.catch_sinto(span, &const_op)?;
1188                match &const_op.kind {
1189                    hax::ConstOperandKind::Value(constant) => {
1190                        let constant = self.translate_constant_expr(span, constant)?;
1191                        Operand::Const(constant)
1192                    }
1193                    hax::ConstOperandKind::Promoted(item) => {
1194                        // A promoted constant that could not be evaluated.
1195                        let global_ref = self.translate_global_decl_ref(span, item)?;
1196                        let constant = ConstantExpr::new(
1197                            ConstantExprKind::Global(global_ref),
1198                            self.translate_ty(span, &const_op.ty)?,
1199                        );
1200                        Operand::Const(constant)
1201                    }
1202                }
1203            }
1204            mir::Operand::RuntimeChecks(check) => {
1205                let op = match check {
1206                    mir::RuntimeChecks::UbChecks => NullOp::UbChecks,
1207                    mir::RuntimeChecks::OverflowChecks => NullOp::OverflowChecks,
1208                    mir::RuntimeChecks::ContractChecks => NullOp::ContractChecks,
1209                };
1210                let local = self.fresh_var(None, Ty::mk_bool());
1211                self.insert_assn_stmt(local.clone(), Rvalue::NullaryOp(op));
1212                Operand::Move(local)
1213            }
1214        })
1215    }
1216
1217    /// Translate an rvalue
1218    fn translate_mir_rvalue(
1219        &mut self,
1220        span: Span,
1221        rvalue: &mir::Rvalue<'tcx>,
1222        tgt_ty: &Ty,
1223    ) -> Result<Rvalue, Error> {
1224        match rvalue {
1225            mir::Rvalue::Use(operand, retag) => {
1226                let retag = match retag {
1227                    mir::WithRetag::Yes => WithRetag::Yes,
1228                    mir::WithRetag::No => WithRetag::No,
1229                };
1230                Ok(Rvalue::Use(self.translate_operand(span, operand)?, retag))
1231            }
1232            mir::Rvalue::CopyForDeref(place) => {
1233                // According to the documentation, it seems to be an optimisation
1234                // for drop elaboration. We treat it as a regular copy.
1235                let place = self.translate_place(span, place)?;
1236                Ok(Rvalue::Use(Operand::Copy(place), WithRetag::No))
1237            }
1238            mir::Rvalue::Repeat(operand, _) => {
1239                let copy_proof = {
1240                    let rust_ty = operand.ty(self.local_decls, self.tcx);
1241                    hax::solve_copy(&self.hax_state, rust_ty)
1242                };
1243                let operand = self.translate_operand(span, operand)?;
1244                let TyKind::Array(elem_ty, len, ty_is_sized) = tgt_ty.kind() else {
1245                    raise_error!(self, span, "array repeat has non-array type")
1246                };
1247                if let Some(copy_proof) = copy_proof {
1248                    let elem_is_copy = self
1249                        .translate_trait_proof(span, &copy_proof)?
1250                        .no_bound_vars();
1251                    Ok(Rvalue::Repeat {
1252                        operand,
1253                        count: len.clone(),
1254                        elem_is_copy,
1255                        elem_is_sized: ty_is_sized.clone(),
1256                    })
1257                } else if let Operand::Const(value) = operand {
1258                    let value = ConstantExpr::new(
1259                        ConstantExprKind::Repeat {
1260                            value,
1261                            count: len.clone(),
1262                            value_is_sized: ty_is_sized.clone(),
1263                        },
1264                        tgt_ty.clone(),
1265                    );
1266                    Ok(Rvalue::Use(Operand::Const(value), WithRetag::Yes))
1267                } else if len.as_usize_literal() == Some(1) {
1268                    // A single element can be moved into the array without being `Copy`.
1269                    Ok(Rvalue::Aggregate(
1270                        AggregateKind::Array(elem_ty.clone(), len.clone(), ty_is_sized.clone()),
1271                        vec![operand],
1272                    ))
1273                } else {
1274                    raise_error!(
1275                        self,
1276                        span,
1277                        "Rvalue::Repeat found with an operand that is neither constant nor `Copy`"
1278                    )
1279                }
1280            }
1281            mir::Rvalue::Ref(_region, borrow_kind, place) => {
1282                let place = self.translate_place(span, place)?;
1283                let borrow_kind = self.translate_borrow_kind(span, *borrow_kind)?;
1284                Ok(Rvalue::Ref {
1285                    place,
1286                    kind: borrow_kind,
1287                    // Will be fixed by the cleanup pass `insert_ptr_metadata`.
1288                    ptr_metadata: Self::missing_ptr_metadata(),
1289                })
1290            }
1291            mir::Rvalue::RawPtr(mtbl, place) => {
1292                let mtbl = match mtbl {
1293                    mir::RawPtrKind::Mut => RefKind::Mut,
1294                    mir::RawPtrKind::Const => RefKind::Shared,
1295                    mir::RawPtrKind::FakeForPtrMetadata => RefKind::Shared,
1296                };
1297                let place = self.translate_place(span, place)?;
1298                Ok(Rvalue::RawPtr {
1299                    place,
1300                    kind: mtbl,
1301                    // Will be fixed by the cleanup pass `insert_ptr_metadata`.
1302                    ptr_metadata: Self::missing_ptr_metadata(),
1303                })
1304            }
1305            mir::Rvalue::Cast(cast_kind, mir_operand, rust_tgt_ty) => {
1306                let op_ty = mir_operand.ty(self.local_decls, self.tcx);
1307                let tgt_ty = self.translate_rustc_ty(span, rust_tgt_ty)?;
1308
1309                // Translate the operand
1310                let mut operand = self.translate_operand(span, mir_operand)?;
1311
1312                let mut cast_kind = match cast_kind {
1313                    mir::CastKind::IntToInt
1314                    | mir::CastKind::IntToFloat
1315                    | mir::CastKind::FloatToInt
1316                    | mir::CastKind::FloatToFloat => {
1317                        let tgt_ty = *tgt_ty.kind().as_scalar().unwrap();
1318                        CastKind::Scalar(tgt_ty)
1319                    }
1320                    mir::CastKind::PtrToPtr
1321                    | mir::CastKind::PointerCoercion(
1322                        ty::adjustment::PointerCoercion::MutToConstPointer,
1323                        ..,
1324                    )
1325                    | mir::CastKind::PointerCoercion(
1326                        ty::adjustment::PointerCoercion::ArrayToPointer,
1327                        ..,
1328                    )
1329                    | mir::CastKind::FnPtrToPtr => CastKind::RawPtr(tgt_ty.clone()),
1330
1331                    mir::CastKind::PointerExposeProvenance => {
1332                        let tgt_ty = *tgt_ty.kind().as_scalar().unwrap().as_integer().unwrap();
1333                        CastKind::PtrExposeProvenance(tgt_ty)
1334                    }
1335                    mir::CastKind::PointerWithExposedProvenance => {
1336                        CastKind::PtrWithExposedProvenance(tgt_ty.clone())
1337                    }
1338                    mir::CastKind::PointerCoercion(
1339                        ty::adjustment::PointerCoercion::ClosureFnPointer(_),
1340                        ..,
1341                    ) => {
1342                        let hax_op_ty: hax::Ty = self.catch_sinto(span, &op_ty)?;
1343                        // We model casts of closures to function pointers by generating a new
1344                        // function item without the closure's state, that calls the actual closure.
1345                        let hax::TyKind::Closure(closure) = hax_op_ty.kind() else {
1346                            unreachable!("Non-closure type in PointerCoercion::ClosureFnPointer");
1347                        };
1348                        let fn_ref: RegionBinder<FunDeclRef> =
1349                            self.translate_stateless_closure_as_fn_ref(span, closure)?;
1350                        let fn_ptr_bound: RegionBinder<FnPtr> = fn_ref.map(FunDeclRef::into);
1351                        let fn_ptr: FnPtr = self.erase_region_binder(fn_ptr_bound.clone());
1352                        let src_ty = TyKind::FnDef(fn_ptr_bound).into_ty();
1353                        operand = Operand::Const(ConstantExpr::new(
1354                            ConstantExprKind::FnDef(fn_ptr),
1355                            src_ty.clone(),
1356                        ));
1357                        CastKind::FnDefToFnPtr
1358                    }
1359                    mir::CastKind::PointerCoercion(
1360                        ty::adjustment::PointerCoercion::ReifyFnPointer(_),
1361                        ..,
1362                    ) => CastKind::FnDefToFnPtr,
1363                    mir::CastKind::PointerCoercion(
1364                        ty::adjustment::PointerCoercion::UnsafeFnPointer,
1365                        ..,
1366                    ) => CastKind::FnPtrToUnsafe,
1367                    mir::CastKind::Transmute | mir::CastKind::BoxDerefTransmute => {
1368                        CastKind::Transmute(tgt_ty.clone())
1369                    }
1370                    mir::CastKind::Subtype => CastKind::Subtype(tgt_ty.clone()),
1371                    mir::CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, ..) => {
1372                        let (path, meta) =
1373                            hax::compute_unsizing_metadata(&self.hax_state, op_ty, *rust_tgt_ty);
1374                        let meta = self.translate_unsizing_metadata(span, &meta)?;
1375                        CastKind::Unsize(tgt_ty.clone(), meta, self.translate_metadata_path(&path))
1376                    }
1377                };
1378
1379                // We're more precise than rustc: we separate "cast to fn ptr" and "safety cast",
1380                // so we reconstruct the distinction here.
1381                if let CastKind::FnDefToFnPtr = cast_kind {
1382                    let rustc_sig = match op_ty.kind() {
1383                        ty::TyKind::FnDef(..) => op_ty.fn_sig(self.tcx),
1384                        ty::TyKind::Closure(_, args) => {
1385                            hax::closure_sig(self.tcx, args.as_closure())
1386                        }
1387                        _ => unreachable!("Expected a function definition or closure"),
1388                    };
1389                    let rustc_fn_ptr = ty::Ty::new_fn_ptr(self.tcx, rustc_sig);
1390                    let intermediate_ty = self.translate_rustc_ty(span, &rustc_fn_ptr)?;
1391                    let (TyKind::FnPtr(interm), TyKind::FnPtr(target)) =
1392                        (intermediate_ty.kind(), tgt_ty.kind())
1393                    else {
1394                        raise_error!(self, span, "Expected function pointers in fn pointer cast")
1395                    };
1396                    if interm.skip_binder.is_unsafe != target.skip_binder.is_unsafe {
1397                        let local = self.rval_to_place(
1398                            Rvalue::UnaryOp(UnOp::Cast(CastKind::FnDefToFnPtr), operand),
1399                            intermediate_ty.clone(),
1400                        );
1401                        operand = Operand::Move(local);
1402                        cast_kind = CastKind::FnPtrToUnsafe;
1403                    }
1404                }
1405
1406                Ok(Rvalue::UnaryOp(UnOp::Cast(cast_kind), operand))
1407            }
1408            // `ptr.offset(n)` becomes `&raw (*ptr).offset(n)`.
1409            mir::Rvalue::BinaryOp(mir::BinOp::Offset, (ptr, n)) => {
1410                let TyKind::RawPtr(pointee, kind) = tgt_ty.kind() else {
1411                    raise_error!(self, span, "`Offset` on a non-raw-pointer type")
1412                };
1413                let rust_pointee = ptr
1414                    .ty(self.local_decls, self.tcx)
1415                    .builtin_deref(true)
1416                    .unwrap();
1417                let pointee_is_sized = self.translate_sized_proof(span, rust_pointee)?;
1418                let ptr = match self.translate_operand(span, ptr)? {
1419                    Operand::Copy(p) | Operand::Move(p) => p,
1420                    op @ Operand::Const(_) => {
1421                        let ty = op.ty().clone();
1422                        self.rval_to_place(Rvalue::Use(op, WithRetag::No), ty)
1423                    }
1424                };
1425                let n = self.translate_operand(span, n)?;
1426                // MIR's `Offset` is UB if it goes out of bounds.
1427                let offset = self.offset_projection(
1428                    n,
1429                    Some(pointee),
1430                    true,
1431                    pointee.clone(),
1432                    pointee_is_sized,
1433                );
1434                let place = ptr.deref().project(offset, pointee.clone());
1435                Ok(Rvalue::RawPtr {
1436                    place,
1437                    kind: *kind,
1438                    ptr_metadata: Operand::mk_const_unit(),
1439                })
1440            }
1441            mir::Rvalue::BinaryOp(binop, (left, right)) => Ok(Rvalue::BinaryOp(
1442                self.translate_binaryop_kind(span, *binop)?,
1443                self.translate_operand(span, left)?,
1444                self.translate_operand(span, right)?,
1445            )),
1446            mir::Rvalue::UnaryOp(unop, operand) => {
1447                let operand = self.translate_operand(span, operand)?;
1448                let unop = match unop {
1449                    mir::UnOp::Not => UnOp::Not,
1450                    mir::UnOp::Neg => UnOp::Neg(OverflowMode::Wrap),
1451                    mir::UnOp::PtrMetadata => match operand {
1452                        Operand::Copy(p) | Operand::Move(p) => {
1453                            return Ok(Rvalue::Use(
1454                                Operand::Copy(
1455                                    p.project(ProjectionElem::PtrMetadata, tgt_ty.clone()),
1456                                ),
1457                                WithRetag::No,
1458                            ));
1459                        }
1460                        Operand::Const(_) => {
1461                            panic!("unexpected metadata operand")
1462                        }
1463                    },
1464                };
1465                Ok(Rvalue::UnaryOp(unop, operand))
1466            }
1467            mir::Rvalue::Discriminant(place) => {
1468                let place = self.translate_place(span, place)?;
1469                Ok(Rvalue::Discriminant(place))
1470            }
1471            mir::Rvalue::Aggregate(aggregate_kind, operands) => {
1472                // It seems this instruction is not present in certain passes:
1473                // for example, it seems it is not used in optimized MIR, where
1474                // ADT initialization is split into several instructions, for
1475                // instance:
1476                // ```
1477                // p = Pair { x:xv, y:yv };
1478                // ```
1479                // Might become:
1480                // ```
1481                // p.x = x;
1482                // p.y = yv;
1483                // ```
1484
1485                // First translate the operands
1486                let operands_t: Vec<Operand> = operands
1487                    .iter()
1488                    .map(|op| self.translate_operand(span, op))
1489                    .try_collect()?;
1490                match aggregate_kind {
1491                    mir::AggregateKind::Array(ty) => {
1492                        let t_ty = self.translate_rustc_ty(span, ty)?;
1493                        let len = ConstantExpr::mk_usize(operands_t.len() as u128);
1494                        let TyKind::Array(_, _, ty_is_sized) = tgt_ty.kind() else {
1495                            raise_error!(self, span, "array aggregate has non-array type")
1496                        };
1497                        if operands_t.len() >= 2 && operands_t.iter().all(Operand::is_const) {
1498                            let fields =
1499                                operands_t.iter().map(|operand| operand.as_const().unwrap());
1500                            if let Ok(field) = fields.dedup().exactly_one()
1501                                && let Some(copy_proof) = hax::solve_copy(&self.hax_state, *ty)
1502                            {
1503                                let ty_is_copy = self
1504                                    .translate_trait_proof(span, &copy_proof)?
1505                                    .no_bound_vars();
1506                                return Ok(Rvalue::Repeat {
1507                                    operand: Operand::Const(field.clone()),
1508                                    count: len,
1509                                    elem_is_sized: ty_is_sized.clone(),
1510                                    elem_is_copy: ty_is_copy,
1511                                });
1512                            }
1513                        }
1514                        Ok(Rvalue::Aggregate(
1515                            AggregateKind::Array(t_ty, len, ty_is_sized.clone()),
1516                            operands_t,
1517                        ))
1518                    }
1519                    mir::AggregateKind::Tuple => {
1520                        let tys = operands.iter().map(|op| op.ty(self.local_decls, self.tcx));
1521                        let ty = ty::Ty::new_tup_from_iter(self.tcx, tys);
1522                        let ty = self.translate_rustc_ty(span, &ty)?;
1523                        let tref = ty.as_adt().unwrap().clone();
1524                        Ok(Rvalue::Aggregate(
1525                            AggregateKind::Adt(tref, None, None),
1526                            operands_t,
1527                        ))
1528                    }
1529                    mir::AggregateKind::Adt(def_id, variant_idx, generics, _, field_index) => {
1530                        use ty::AdtKind;
1531                        trace!("{:?}", rvalue);
1532
1533                        let adt_kind = self.tcx.adt_def(*def_id).adt_kind();
1534                        let item = hax::translate_item_ref(&self.hax_state, *def_id, generics);
1535                        let tref = self.translate_type_decl_ref(span, &item)?;
1536                        let variant_id = match adt_kind {
1537                            AdtKind::Struct | AdtKind::Union => None,
1538                            AdtKind::Enum => Some(self.translate_variant_id(*variant_idx)),
1539                        };
1540                        let field_id = match adt_kind {
1541                            AdtKind::Struct | AdtKind::Enum => None,
1542                            AdtKind::Union => Some(self.translate_field_id(field_index.unwrap())),
1543                        };
1544
1545                        let akind = AggregateKind::Adt(tref, variant_id, field_id);
1546                        Ok(Rvalue::Aggregate(akind, operands_t))
1547                    }
1548                    mir::AggregateKind::Closure(def_id, generics) => {
1549                        let item = hax::translate_item_ref(&self.hax_state, *def_id, generics);
1550                        let tref = self.translate_closure_type_ref(span, &item)?;
1551                        let akind = AggregateKind::Adt(tref, None, None);
1552                        Ok(Rvalue::Aggregate(akind, operands_t))
1553                    }
1554                    mir::AggregateKind::RawPtr(ty, mutability) => {
1555                        let t_ty = self.translate_rustc_ty(span, ty)?;
1556                        let mutability = if mutability.is_mut() {
1557                            RefKind::Mut
1558                        } else {
1559                            RefKind::Shared
1560                        };
1561
1562                        let akind = AggregateKind::RawPtr(t_ty, mutability);
1563
1564                        Ok(Rvalue::Aggregate(akind, operands_t))
1565                    }
1566                    mir::AggregateKind::Coroutine(..)
1567                    | mir::AggregateKind::CoroutineClosure(..) => {
1568                        raise_error!(self, span, "Coroutines are not supported");
1569                    }
1570                }
1571            }
1572            mir::Rvalue::ThreadLocalRef(def_id) => self.translate_thread_local_ref(span, *def_id),
1573            mir::Rvalue::WrapUnsafeBinder { .. } => {
1574                raise_error!(
1575                    self,
1576                    span,
1577                    "charon does not support unsafe lifetime binders"
1578                );
1579            }
1580            mir::Rvalue::Reborrow(..) => {
1581                raise_error!(
1582                    self,
1583                    span,
1584                    "charon does not support reborrow rvalues (for Reborrow traits)"
1585                );
1586            }
1587        }
1588    }
1589
1590    /// Translate a statement.
1591    fn translate_statement(
1592        &mut self,
1593        source_scopes: &rustc_index::IndexVec<mir::SourceScope, mir::SourceScopeData>,
1594        statement: &mir::Statement<'tcx>,
1595    ) -> Result<(), Error> {
1596        trace!("About to translate statement (MIR) {:?}", statement);
1597        let span = self.translate_span_from_source_info(source_scopes, &statement.source_info);
1598
1599        self.span = span;
1600        let kind: Option<StatementKind> = match &statement.kind {
1601            mir::StatementKind::Assign((
1602                place,
1603                mir::Rvalue::BinaryOp(
1604                    binop @ (mir::BinOp::AddWithOverflow
1605                    | mir::BinOp::SubWithOverflow
1606                    | mir::BinOp::MulWithOverflow),
1607                    (left, right),
1608                ),
1609            )) => {
1610                // Translate these binops to intrinsic calls to make binops easier to typecheck:
1611                // their return type is a tuple, which we monomorphize hence it's hard to compute
1612                // ourselves.
1613                let name = match binop {
1614                    mir::BinOp::AddWithOverflow => sym::add_with_overflow,
1615                    mir::BinOp::SubWithOverflow => sym::sub_with_overflow,
1616                    mir::BinOp::MulWithOverflow => sym::mul_with_overflow,
1617                    _ => unreachable!(),
1618                };
1619                let ty = left.ty(self.local_decls, self.tcx);
1620                let generic_args = self.tcx.mk_args(&[ty.into()]);
1621                let dest = self.translate_place(span, place)?;
1622                let left = self.translate_operand(span, left)?;
1623                let right = self.translate_operand(span, right)?;
1624                self.translate_intrinsic_call(span, dest, name, generic_args, vec![left, right])?;
1625                None
1626            }
1627            mir::StatementKind::Assign((place, rvalue)) => {
1628                let t_place = self.translate_place(span, place)?;
1629                let t_rvalue = self.translate_mir_rvalue(span, rvalue, t_place.ty())?;
1630                Some(StatementKind::Assign(t_place, t_rvalue))
1631            }
1632            mir::StatementKind::SetDiscriminant {
1633                place,
1634                variant_index,
1635            } => {
1636                let t_place = self.translate_place(span, place)?;
1637                let variant_id = self.translate_variant_id(*variant_index);
1638                Some(StatementKind::SetDiscriminant(t_place, variant_id))
1639            }
1640            mir::StatementKind::StorageLive(local) => {
1641                let var_id = self.translate_local(local).unwrap();
1642                Some(StatementKind::StorageLive(var_id))
1643            }
1644            mir::StatementKind::StorageDead(local) => {
1645                let var_id = self.translate_local(local).unwrap();
1646                Some(StatementKind::StorageDead(var_id))
1647            }
1648            mir::StatementKind::Intrinsic(mir::NonDivergingIntrinsic::Assume(op)) => {
1649                let op = self.translate_operand(span, op)?;
1650                let dest = self.locals.new_var(None, Ty::mk_unit());
1651                self.translate_intrinsic_call(
1652                    span,
1653                    dest,
1654                    sym::assume,
1655                    ty::GenericArgs::empty(),
1656                    vec![op],
1657                )?;
1658                None
1659            }
1660            mir::StatementKind::Intrinsic(mir::NonDivergingIntrinsic::CopyNonOverlapping(
1661                mir::CopyNonOverlapping { src, dst, count },
1662            )) => {
1663                let pointee_ty = src
1664                    .ty(self.local_decls, self.tcx)
1665                    .builtin_deref(true)
1666                    .unwrap();
1667                let generic_args = self.tcx.mk_args(&[pointee_ty.into()]);
1668                let src = self.translate_operand(span, src)?;
1669                let dst = self.translate_operand(span, dst)?;
1670                let count = self.translate_operand(span, count)?;
1671                let dest = self.locals.new_var(None, Ty::mk_unit());
1672                self.translate_intrinsic_call(
1673                    span,
1674                    dest,
1675                    sym::copy_nonoverlapping,
1676                    generic_args,
1677                    vec![src, dst, count],
1678                )?;
1679                None
1680            }
1681            mir::StatementKind::PlaceMention(place) => {
1682                let place = self.translate_place(span, place)?;
1683                // We only translate this for places with projections, as
1684                // no UB can arise from simply mentioning a local variable.
1685                if place.is_local() {
1686                    None
1687                } else {
1688                    Some(StatementKind::PlaceMention(place))
1689                }
1690            }
1691            mir::StatementKind::FakeRead((_, place)) => {
1692                let place = self.translate_place(span, place)?;
1693                Some(StatementKind::Borrowck(BorrowckStatement::FakeRead(place)))
1694            }
1695            mir::StatementKind::AscribeUserType((place, user_ty), variance) => {
1696                let variance = match variance {
1697                    ty::Variance::Covariant => Variance::Covariant,
1698                    ty::Variance::Invariant => Variance::Invariant,
1699                    ty::Variance::Contravariant => Variance::Contravariant,
1700                    // Does nothing so we discard it.
1701                    ty::Variance::Bivariant => return Ok(()),
1702                };
1703                let place = self.translate_place(span, place)?;
1704                let (ty, facts) = self.translate_user_type_projection(span, user_ty)?;
1705                self.statements.push(Statement::new(
1706                    span,
1707                    StatementKind::Borrowck(BorrowckStatement::SetType {
1708                        place,
1709                        ty,
1710                        variance,
1711                    }),
1712                ));
1713                self.statements.extend(
1714                    facts
1715                        .into_iter()
1716                        .map(|fact| Statement::new(span, StatementKind::Borrowck(fact))),
1717                );
1718                None
1719            }
1720            // Used for coverage instrumentation.
1721            mir::StatementKind::Coverage(_) => None,
1722            // Used in the interpreter to check that const code doesn't run for too long or even
1723            // indefinitely.
1724            mir::StatementKind::ConstEvalCounter => None,
1725            // Semantically equivalent to `Nop`, used only for rustc lints.
1726            mir::StatementKind::BackwardIncompatibleDropHint { .. } => None,
1727            mir::StatementKind::Nop => None,
1728        };
1729
1730        let Some(kind) = kind else {
1731            return Ok(());
1732        };
1733        self.statements.push(Statement::new(span, kind));
1734        Ok(())
1735    }
1736
1737    /// Translate a call to a non-diverging intrinsic.
1738    fn translate_intrinsic_call(
1739        &mut self,
1740        span: Span,
1741        dest: Place,
1742        name: Symbol,
1743        generic_args: ty::GenericArgsRef<'tcx>,
1744        args: Vec<Operand>,
1745    ) -> Result<(), Error> {
1746        // Sadly rustc doesn't expose a Symbol -> DefId map for intrinsics.
1747        let path = NamePattern::parse(&format!("core::intrinsics::{name}")).unwrap();
1748        let def_id = self.resolve_single_path(span, &path)?;
1749        assert!(self.tcx.is_intrinsic(def_id, name));
1750        let item = hax::ItemRef::translate(self.hax_state_with_id(), def_id, generic_args);
1751        let func =
1752            FnOperand::Regular(self.translate_fn_ptr(span, &item, TransItemSourceKind::Fun)?);
1753        self.push_nounwind_call(
1754            span,
1755            dest,
1756            Call {
1757                func,
1758                args,
1759                safety: CallSafety::Inherit,
1760            },
1761        );
1762        Ok(())
1763    }
1764
1765    /// Translate a terminator
1766    fn translate_terminator(
1767        mut self,
1768        source_scopes: &rustc_index::IndexVec<mir::SourceScope, mir::SourceScopeData>,
1769        terminator: &mir::Terminator<'tcx>,
1770    ) -> Result<(), Error> {
1771        trace!("About to translate terminator (MIR) {:?}", terminator);
1772        let span = self.translate_span_from_source_info(source_scopes, &terminator.source_info);
1773
1774        // Translate the terminator
1775        self.span = span;
1776        use mir::TerminatorKind;
1777        let kind: ullbc_ast::TerminatorKind = match &terminator.kind {
1778            TerminatorKind::Goto { target } => {
1779                let target = self.translate_basic_block_id(*target);
1780                ullbc_ast::TerminatorKind::Goto { target }
1781            }
1782            TerminatorKind::SwitchInt { discr, targets, .. } => {
1783                let discr = self.translate_operand(span, discr)?;
1784                let (data, branches) = self.translate_switch_targets(span, discr, targets)?;
1785                ullbc_ast::TerminatorKind::Switch { data, branches }
1786            }
1787            TerminatorKind::UnwindResume => ullbc_ast::TerminatorKind::UnwindResume,
1788            TerminatorKind::UnwindTerminate { .. } => ullbc_ast::TerminatorKind::UnwindTerminate,
1789            TerminatorKind::Return => ullbc_ast::TerminatorKind::Return,
1790            // A MIR `Unreachable` terminator indicates undefined behavior of the rust abstract
1791            // machine.
1792            TerminatorKind::Unreachable => ullbc_ast::TerminatorKind::UndefinedBehavior,
1793            TerminatorKind::Drop {
1794                place,
1795                target,
1796                unwind,
1797                ..
1798            } => self.translate_drop(span, place, target, unwind)?,
1799            TerminatorKind::Call {
1800                func,
1801                args,
1802                destination,
1803                target,
1804                unwind,
1805                ..
1806            } => self.translate_function_call(span, func, args, destination, target, unwind)?,
1807            TerminatorKind::Assert {
1808                cond,
1809                expected,
1810                msg,
1811                target,
1812                unwind,
1813            } => {
1814                let on_unwind = self.translate_unwind_action(span, unwind);
1815                let kind = self.translate_assert_kind(span, msg)?;
1816                let assert = Assert {
1817                    cond: self.translate_operand(span, cond)?,
1818                    expected: *expected,
1819                    check_kind: Some(kind),
1820                };
1821                let target = self.translate_basic_block_id(*target);
1822                ullbc_ast::TerminatorKind::Assert {
1823                    assert,
1824                    target,
1825                    on_unwind,
1826                }
1827            }
1828            TerminatorKind::FalseEdge {
1829                real_target,
1830                imaginary_target: _,
1831            } => {
1832                // False edges are used to make the borrow checker a bit conservative.
1833                // We translate them as Gotos.
1834                // Also note that they are used in some passes, and not in some others
1835                // (they are present in mir_promoted, but not mir_optimized).
1836                let target = self.translate_basic_block_id(*real_target);
1837                ullbc_ast::TerminatorKind::Goto { target }
1838            }
1839            TerminatorKind::FalseUnwind {
1840                real_target,
1841                unwind: _,
1842            } => {
1843                // We consider this to be a goto
1844                let target = self.translate_basic_block_id(*real_target);
1845                ullbc_ast::TerminatorKind::Goto { target }
1846            }
1847            TerminatorKind::InlineAsm {
1848                asm_macro,
1849                template,
1850                operands,
1851                options,
1852                targets,
1853                unwind,
1854                ..
1855            } => {
1856                use rustc_ast::ast::InlineAsmOptions as O;
1857                let kind = match asm_macro {
1858                    mir::InlineAsmMacro::Asm => AsmKind::Asm,
1859                    mir::InlineAsmMacro::NakedAsm => AsmKind::NakedAsm,
1860                };
1861                let options = AsmOptions {
1862                    pure: options.contains(O::PURE),
1863                    nomem: options.contains(O::NOMEM),
1864                    readonly: options.contains(O::READONLY),
1865                    preserves_flags: options.contains(O::PRESERVES_FLAGS),
1866                    noreturn: options.contains(O::NORETURN),
1867                    nostack: options.contains(O::NOSTACK),
1868                    att_syntax: options.contains(O::ATT_SYNTAX),
1869                    may_unwind: options.contains(O::MAY_UNWIND),
1870                };
1871                let template = template
1872                    .iter()
1873                    .map(|piece| match piece {
1874                        rustc_ast::ast::InlineAsmTemplatePiece::String(text) => {
1875                            AsmTemplatePiece::Text(text.to_string().into())
1876                        }
1877                        rustc_ast::ast::InlineAsmTemplatePiece::Placeholder {
1878                            operand_idx,
1879                            modifier,
1880                            span,
1881                        } => AsmTemplatePiece::Placeholder {
1882                            operand_id: AsmOperandId::from_usize(*operand_idx),
1883                            modifier: *modifier,
1884                            span: self.translate_span(span),
1885                        },
1886                    })
1887                    .collect();
1888                let has_fallthrough = matches!(kind, AsmKind::Asm) && !options.noreturn;
1889                let translate_reg = |reg: &rustc_target::asm::InlineAsmRegOrRegClass| match reg {
1890                    rustc_target::asm::InlineAsmRegOrRegClass::Reg(reg) => {
1891                        AsmRegister::Explicit(reg.name().to_string().into())
1892                    }
1893                    rustc_target::asm::InlineAsmRegOrRegClass::RegClass(class) => {
1894                        AsmRegister::Class(class.name().to_string().into())
1895                    }
1896                };
1897                let operands = operands
1898                    .iter()
1899                    .map(|operand| {
1900                        Ok(match operand {
1901                            mir::InlineAsmOperand::In { reg, value } => AsmOperand::In {
1902                                reg: translate_reg(reg),
1903                                value: self.translate_operand(span, value)?,
1904                            },
1905                            mir::InlineAsmOperand::Out { reg, late, place } => AsmOperand::Out {
1906                                reg: translate_reg(reg),
1907                                late: *late,
1908                                place: place
1909                                    .as_ref()
1910                                    .map(|place| self.translate_place(span, place))
1911                                    .transpose()?,
1912                            },
1913                            mir::InlineAsmOperand::InOut {
1914                                reg,
1915                                late,
1916                                in_value,
1917                                out_place,
1918                            } => AsmOperand::InOut {
1919                                reg: translate_reg(reg),
1920                                late: *late,
1921                                in_value: self.translate_operand(span, in_value)?,
1922                                out_place: out_place
1923                                    .as_ref()
1924                                    .map(|place| self.translate_place(span, place))
1925                                    .transpose()?,
1926                            },
1927                            mir::InlineAsmOperand::Const { value } => {
1928                                let Operand::Const(value) = self.translate_operand(
1929                                    span,
1930                                    &mir::Operand::Constant(value.clone()),
1931                                )?
1932                                else {
1933                                    unreachable!("inline asm const operand is not a constant")
1934                                };
1935                                AsmOperand::Const(value)
1936                            }
1937                            mir::InlineAsmOperand::SymFn { value } => {
1938                                let Operand::Const(value) = self.translate_operand(
1939                                    span,
1940                                    &mir::Operand::Constant(value.clone()),
1941                                )?
1942                                else {
1943                                    unreachable!("inline asm function symbol is not a constant")
1944                                };
1945                                let ConstantExprKind::FnDef(fn_ptr) = value.kind() else {
1946                                    unreachable!("inline asm function symbol is not a function")
1947                                };
1948                                AsmOperand::SymFn(fn_ptr.clone())
1949                            }
1950                            mir::InlineAsmOperand::SymStatic { def_id } => {
1951                                let item = hax::translate_item_ref(
1952                                    &self.hax_state,
1953                                    *def_id,
1954                                    ty::GenericArgs::empty(),
1955                                );
1956                                AsmOperand::SymStatic(self.translate_global_decl_ref(span, &item)?)
1957                            }
1958                            mir::InlineAsmOperand::Label { target_index } => AsmOperand::Label(
1959                                BranchId::from_usize(*target_index - usize::from(has_fallthrough)),
1960                            ),
1961                        })
1962                    })
1963                    .collect::<Result<IndexVec<AsmOperandId, _>, Error>>()?;
1964                let fallthrough =
1965                    has_fallthrough.then(|| self.translate_basic_block_id(targets[0]));
1966                let labels = targets
1967                    .iter()
1968                    .skip(usize::from(has_fallthrough))
1969                    .map(|target| self.translate_basic_block_id(*target))
1970                    .collect();
1971                let on_unwind = self.translate_unwind_action(span, unwind);
1972                let asm = InlineAsm {
1973                    kind,
1974                    template,
1975                    operands,
1976                    options,
1977                };
1978                ullbc_ast::TerminatorKind::InlineAsm {
1979                    asm,
1980                    fallthrough,
1981                    labels,
1982                    on_unwind,
1983                }
1984            }
1985            TerminatorKind::CoroutineDrop
1986            | TerminatorKind::TailCall { .. }
1987            | TerminatorKind::Yield { .. } => {
1988                raise_error!(self, span, "Unsupported terminator: {:?}", terminator.kind);
1989            }
1990        };
1991
1992        self.finish_current_block(Terminator::new(span, kind));
1993        Ok(())
1994    }
1995
1996    /// Translate switch targets
1997    fn translate_switch_targets(
1998        &mut self,
1999        span: Span,
2000        discr: Operand,
2001        targets: &mir::SwitchTargets,
2002    ) -> Result<(SwitchData, IndexVec<BranchId, BlockId>), Error> {
2003        // Convert all the test values to the proper values.
2004        let otherwise = targets.otherwise();
2005        let switch_ty = discr.ty();
2006        let switch_scalar_ty = *switch_ty.as_scalar().unwrap();
2007        let mut branch_targets: IndexVec<BranchId, BlockId> = IndexVec::new();
2008        let mut target_to_branch: SeqHashMap<BlockId, BranchId> = SeqHashMap::new();
2009        let mut switch_branches = Vec::with_capacity(targets.iter().count());
2010
2011        // Keep the historical true-then-false traversal order for boolean switches.
2012        let bool_fallback = (switch_scalar_ty == ScalarTy::Bool).then(|| {
2013            let target = self.translate_basic_block_id(otherwise);
2014            *target_to_branch
2015                .entry(target)
2016                .or_insert_with(|| branch_targets.push(target))
2017        });
2018
2019        for (bits, target) in targets.iter() {
2020            let Some(kind) = ConstantExprKind::from_bits(&switch_scalar_ty, bits) else {
2021                raise_error!(self, span, "Can't match on type {switch_scalar_ty}")
2022            };
2023            let target = self.translate_basic_block_id(target);
2024            let branch_id = *target_to_branch
2025                .entry(target)
2026                .or_insert_with(|| branch_targets.push(target));
2027            let value = ConstantExpr::new(kind, switch_ty.clone());
2028            switch_branches.push((value, branch_id));
2029        }
2030
2031        let fallback = bool_fallback.unwrap_or_else(|| {
2032            let target = self.translate_basic_block_id(otherwise);
2033            *target_to_branch
2034                .entry(target)
2035                .or_insert_with(|| branch_targets.push(target))
2036        });
2037        let data = SwitchData {
2038            scrutinee: SwitchScrutinee::Value(discr),
2039            branches: switch_branches,
2040            fallback: Some(fallback),
2041        };
2042        Ok((data, branch_targets))
2043    }
2044
2045    /// Translate a function call statement.
2046    /// Note that `body` is the body of the function being translated, not of the
2047    /// function referenced in the function call: we need it in order to translate
2048    /// the blocks we go to after the function call returns.
2049    #[allow(clippy::too_many_arguments)]
2050    fn translate_function_call(
2051        &mut self,
2052        span: Span,
2053        func: &mir::Operand<'tcx>,
2054        args: &[hax::Spanned<mir::Operand<'tcx>>],
2055        destination: &mir::Place<'tcx>,
2056        target: &Option<mir::BasicBlock>,
2057        unwind: &mir::UnwindAction,
2058    ) -> Result<TerminatorKind, Error> {
2059        let tcx = self.tcx;
2060        let op_ty = func.ty(self.local_decls, tcx);
2061        // There are two cases, depending on whether this is a "regular"
2062        // call to a top-level function identified by its id, or if we
2063        // are using a local function pointer (i.e., the operand is a "move").
2064        let lval = self.translate_place(span, destination)?;
2065        let on_unwind = self.translate_unwind_action(span, unwind);
2066        // Translate the function operand.
2067        let fn_operand = match op_ty.kind() {
2068            ty::TyKind::FnDef(def_id, generics) => {
2069                // The type of the value is one of the singleton types that corresponds to each function,
2070                // which is enough information.
2071                //
2072                // note: loss of precision, we erase the bound vars.
2073                let generics = hax::erase_free_regions(tcx, generics.skip_binder());
2074                let item = &hax::translate_item_ref(&self.hax_state, *def_id, generics);
2075                trace!("func: {:?}", item.def_id);
2076                let fn_ptr = self.translate_fn_ptr(span, item, TransItemSourceKind::Fun)?;
2077                FnOperand::Regular(fn_ptr)
2078            }
2079            _ => {
2080                // Call to a function pointer.
2081                let op = self.translate_operand(span, func)?;
2082                let regions = match op.ty().kind() {
2083                    TyKind::FnPtr(sig) => RegionArgs::new(
2084                        sig.regions
2085                            .map_ref_indexed(|_, _| self.translate_erased_region()),
2086                    ),
2087                    _ => raise_error!(
2088                        self,
2089                        span,
2090                        "expected a function pointer for call operand, got {}",
2091                        op.ty().with_ctx(&self.into_fmt())
2092                    ),
2093                };
2094                FnOperand::Dynamic(op, regions)
2095            }
2096        };
2097        let args = self.translate_arguments(span, args)?;
2098
2099        let safety = match op_ty.kind() {
2100            ty::TyKind::FnDef(def_id, generics) => 'safety_fndef: {
2101                let sig_is_unsafe = tcx.fn_sig(*def_id).skip_binder().safety().is_unsafe();
2102
2103                // Safe `#[target_feature]` functions still get an unsafe signature, but calling them is safe if
2104                // the caller enables the same (or implying) features (safety.unsafe-target-feature-call).
2105                // We also get the inverse: `#[unsafe(force_target_feature)]` functions have a safe signature, but calling
2106                // them is unsafe if the caller does not enable the feature.
2107                let attrs = tcx.codegen_fn_attrs(*def_id);
2108                let caller = self.item_src.def_id().as_real_or_promoted();
2109                let caller_features = caller
2110                    .map(|caller| tcx.typeck_root_def_id(caller))
2111                    .filter(|caller| tcx.def_kind(*caller).has_codegen_attrs())
2112                    .map_or(&[][..], |caller| {
2113                        &tcx.codegen_fn_attrs(caller).target_features[..]
2114                    });
2115                let has_feature =
2116                    tcx.is_target_feature_call_safe(&attrs.target_features, caller_features);
2117                if sig_is_unsafe && attrs.safe_target_features && has_feature {
2118                    break 'safety_fndef CallSafety::Safe;
2119                } else if !has_feature {
2120                    break 'safety_fndef CallSafety::Unsafe;
2121                }
2122
2123                // Explicitly calling a `Drop` method is unsafe.
2124                if let Some(trait_id) = tcx.trait_of_assoc(*def_id)
2125                    && tcx.is_lang_item(trait_id, LangItem::Drop)
2126                {
2127                    break 'safety_fndef CallSafety::Unsafe;
2128                }
2129
2130                // FIXME(#856) In mono mode, trait decls have no methods, so we track the safety of the function here,
2131                // and `transform_dyn_trait_calls` moves it into the signature of the called function pointer.
2132                let mono_dyn_call = self.monomorphize()
2133                    && tcx.trait_of_assoc(*def_id).is_some()
2134                    && generics.skip_binder().type_at(0).is_trait();
2135                if mono_dyn_call {
2136                    break 'safety_fndef if sig_is_unsafe {
2137                        CallSafety::Unsafe
2138                    } else {
2139                        CallSafety::Safe
2140                    };
2141                }
2142
2143                CallSafety::Inherit
2144            }
2145            _ => CallSafety::Inherit,
2146        };
2147
2148        let call = Call {
2149            func: fn_operand,
2150            args,
2151            safety,
2152        };
2153
2154        let target = match target {
2155            Some(target) => self.translate_basic_block_id(*target),
2156            None => {
2157                let abort = Terminator::new(span, TerminatorKind::UndefinedBehavior);
2158                let kind = self.kind;
2159                self.blocks.push(abort.into_block(kind))
2160            }
2161        };
2162
2163        Ok(TerminatorKind::Call {
2164            call,
2165            dest: lval,
2166            target,
2167            on_unwind,
2168        })
2169    }
2170
2171    /// Translate a drop terminator
2172    #[allow(clippy::too_many_arguments)]
2173    fn translate_drop(
2174        &mut self,
2175        span: Span,
2176        place: &mir::Place<'tcx>,
2177        target: &mir::BasicBlock,
2178        unwind: &mir::UnwindAction,
2179    ) -> Result<TerminatorKind, Error> {
2180        let place_ty = place.ty(self.local_decls, self.tcx).ty;
2181        let fn_ptr = self.translate_drop_glue_method_call(span, place_ty)?;
2182        let place = self.translate_place(span, place)?;
2183        let target = self.translate_basic_block_id(*target);
2184        let on_unwind = self.translate_unwind_action(span, unwind);
2185
2186        Ok(TerminatorKind::Drop {
2187            kind: self.drop_kind,
2188            place,
2189            fn_ptr,
2190            target,
2191            on_unwind,
2192        })
2193    }
2194
2195    // construct unwind block for the terminators
2196    fn translate_unwind_action(&mut self, span: Span, unwind: &mir::UnwindAction) -> BlockId {
2197        let unwind_kind = self.kind.further_unwind_kind();
2198        match unwind {
2199            mir::UnwindAction::Continue => {
2200                assert_eq!(unwind_kind, UnwindKind::Cleanup);
2201                let unwind_continue = Terminator::new(span, TerminatorKind::UnwindResume);
2202                self.blocks.push(unwind_continue.into_block(unwind_kind))
2203            }
2204            mir::UnwindAction::Unreachable => {
2205                let abort = Terminator::new(span, TerminatorKind::UndefinedBehavior);
2206                self.blocks.push(abort.into_block(unwind_kind))
2207            }
2208            mir::UnwindAction::Terminate(..) => {
2209                let abort = Terminator::new(span, TerminatorKind::UnwindTerminate);
2210                self.blocks.push(abort.into_block(unwind_kind))
2211            }
2212            mir::UnwindAction::Cleanup(bb) => self.translate_basic_block_id(*bb),
2213        }
2214    }
2215
2216    fn translate_assert_kind(
2217        &mut self,
2218        span: Span,
2219        kind: &mir::AssertKind<mir::Operand<'tcx>>,
2220    ) -> Result<BuiltinAssertKind, Error> {
2221        match kind {
2222            mir::AssertKind::BoundsCheck { len, index } => {
2223                let len = self.translate_operand(span, len)?;
2224                let index = self.translate_operand(span, index)?;
2225                Ok(BuiltinAssertKind::BoundsCheck { len, index })
2226            }
2227            mir::AssertKind::Overflow(binop, left, right) => {
2228                let binop = self.translate_binaryop_kind(span, *binop)?;
2229                let left = self.translate_operand(span, left)?;
2230                let right = self.translate_operand(span, right)?;
2231                Ok(BuiltinAssertKind::Overflow(binop, left, right))
2232            }
2233            mir::AssertKind::OverflowNeg(operand) => {
2234                let operand = self.translate_operand(span, operand)?;
2235                Ok(BuiltinAssertKind::OverflowNeg(operand))
2236            }
2237            mir::AssertKind::DivisionByZero(operand) => {
2238                let operand = self.translate_operand(span, operand)?;
2239                Ok(BuiltinAssertKind::DivisionByZero(operand))
2240            }
2241            mir::AssertKind::RemainderByZero(operand) => {
2242                let operand = self.translate_operand(span, operand)?;
2243                Ok(BuiltinAssertKind::RemainderByZero(operand))
2244            }
2245            mir::AssertKind::MisalignedPointerDereference { required, found } => {
2246                let required = self.translate_operand(span, required)?;
2247                let found = self.translate_operand(span, found)?;
2248                Ok(BuiltinAssertKind::MisalignedPointerDereference { required, found })
2249            }
2250            mir::AssertKind::NullPointerDereference => {
2251                Ok(BuiltinAssertKind::NullPointerDereference)
2252            }
2253            mir::AssertKind::NullReferenceConstructed => {
2254                Ok(BuiltinAssertKind::NullReferenceCreated)
2255            }
2256            mir::AssertKind::InvalidEnumConstruction(operand) => {
2257                let operand = self.translate_operand(span, operand)?;
2258                Ok(BuiltinAssertKind::InvalidEnumConstruction(operand))
2259            }
2260            mir::AssertKind::ResumedAfterDrop(..)
2261            | mir::AssertKind::ResumedAfterPanic(..)
2262            | mir::AssertKind::ResumedAfterReturn(..) => {
2263                raise_error!(self, span, "Coroutines are not supported");
2264            }
2265        }
2266    }
2267
2268    /// Evaluate function arguments in a context, and return the list of computed
2269    /// values.
2270    fn translate_arguments(
2271        &mut self,
2272        span: Span,
2273        args: &[hax::Spanned<mir::Operand<'tcx>>],
2274    ) -> Result<Vec<Operand>, Error> {
2275        let mut t_args: Vec<Operand> = Vec::new();
2276        for arg in args.iter().map(|x| &x.node) {
2277            // Translate
2278            let op = self.translate_operand(span, arg)?;
2279            t_args.push(op);
2280        }
2281        Ok(t_args)
2282    }
2283}
2284
2285impl<'a> IntoFormatter for &'a BodyTransCtx<'_, '_, '_> {
2286    type C = FmtCtx<'a>;
2287    fn into_fmt(self) -> Self::C {
2288        FmtCtx {
2289            local_names: Some(Cow::Owned(compute_local_names(&self.locals))),
2290            ..self.i_ctx.into_fmt()
2291        }
2292    }
2293}