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