Skip to main content

rapx/util/
mir_utils.rs

1use crate::analysis::dataflow::call_summary::{effects::has_registered_effect, function_var::supports_result};
2use crate::compat::{FxHashMap, GenericArgsRef};
3use crate::def_ids::{
4    diagnostic_api, intrinsic_api,
5    lang_api::{self, LangItem},
6    type_ids,
7};
8use rustc_hir::{
9    ItemKind,
10    def::DefKind,
11    def_id::{DefId, LocalDefId},
12};
13use rustc_middle::ty::ShimKind;
14#[cfg(rapx_const_ext)]
15use rustc_middle::ty::consts::ConstExt;
16#[cfg(rapx_alias_const_inherent_self)]
17use rustc_middle::ty::AliasConstKind;
18use rustc_middle::{
19    mir::interpret::{AllocId, GlobalAlloc},
20    mir::{
21        BasicBlock, Body, ConstValue, Local, Operand, Place, Rvalue, StatementKind, SwitchTargets,
22        TerminatorKind,
23    },
24    ty::{
25        Const, ConstKind, FieldDef, GenericArgKind, GenericArgs, GenericParamDefKind,
26        Instance, InstanceKind, PseudoCanonicalInput, ScalarInt, Ty, TyCtxt, TyKind, TypeFlags,
27        TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingEnv,
28    },
29};
30use rustc_span::{DUMMY_SP, Symbol};
31
32use std::{collections::HashSet, ops::ControlFlow};
33
34use super::def_use::PlaceKey;
35
36pub(crate) fn pointee_ty<'tcx>(ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
37    match ty.kind() {
38        TyKind::RawPtr(ty, _) | TyKind::Ref(_, ty, _) => Some(*ty),
39        // `NonNull<T>` is a raw-pointer wrapper; unwrap it so pointer-arith
40        // effects (`NonNull::add`/`sub`/`offset`) keep their provenance.
41        TyKind::Adt(adt, args) if type_ids::is_std_nonnull(adt.did()) => {
42            args.types().next()
43        }
44        _ => None,
45    }
46}
47
48pub(crate) fn dep_callee_def_id(func: &Operand<'_>) -> Option<DefId> {
49    let Operand::Constant(c) = func else {
50        return None;
51    };
52    let TyKind::FnDef(def_id, _) = c.const_.ty().kind() else {
53        return None;
54    };
55    Some(*def_id)
56}
57
58/// Whether `def_id` is `core::ptr::drop_in_place`.
59pub(crate) fn is_drop_in_place(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
60    diagnostic_api::is_drop_in_place(tcx, def_id)
61}
62
63/// Whether `def_id` is a diverging call target: a `panic*` lang item or the
64/// `unreachable`/`abort` intrinsics.
65fn is_diverging_call(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
66    crate::def_ids::lang_api::is_panic_family(tcx, def_id)
67        || intrinsic_api::intrinsic_name(tcx, Some(def_id)).is_some_and(|name| {
68            name == rustc_span::sym::unreachable || name == rustc_span::sym::abort
69        })
70}
71
72/// Whether a `SwitchInt`'s non-`otherwise` targets all lead straight to
73/// `panic`/`unreachable` (a `debug_assert!`/`assert!` dispatch).  Such a switch
74/// is dead on the normal path and can be inlined by following only the
75/// `otherwise` edge.
76pub(crate) fn switch_targets_unreachable<'tcx>(
77    tcx: TyCtxt<'tcx>,
78    body: &Body<'tcx>,
79    targets: &SwitchTargets,
80) -> bool {
81    targets.iter().all(|(_, target)| {
82        let mut cur = target;
83        let mut seen = HashSet::new();
84        loop {
85            if !seen.insert(cur) {
86                return false;
87            }
88            let bb = &body.basic_blocks[cur];
89            let term = bb.terminator();
90            match &term.kind {
91                TerminatorKind::Unreachable => return true,
92                TerminatorKind::Call { func, .. } => {
93                    let Some(callee) = dep_callee_def_id(func) else {
94                        return false;
95                    };
96                    return is_diverging_call(tcx, callee);
97                }
98                TerminatorKind::Goto { target: next, .. } => {
99                    cur = *next;
100                }
101                // A bare `return` with no statements is a drop-flag skip (dead
102                // on the normal path); a `return` preceded by real statements
103                // computes a different value, so it is a semantic branch and
104                // must not be ignored.
105                TerminatorKind::Return => return bb.statements.is_empty(),
106                _ => return false,
107            }
108        }
109    })
110}
111
112/// Resolve a `cfg!`-style runtime-check flag (`UbChecks`, `ContractChecks`,
113/// `OverflowChecks`) to a constant `u64`. We fold to the *no-check* edge (`0`):
114/// the check only panics on a violated precondition, and its branchy body would
115/// otherwise corrupt field/Typed propagation during inlining. Rustc lowers
116/// these to `Operand::RuntimeChecks`.
117pub(crate) fn rvalue_runtime_checks_value(rvalue: &Rvalue<'_>) -> Option<u64> {
118    if let Rvalue::Use(Operand::RuntimeChecks(_), ..) = rvalue {
119        return Some(0);
120    }
121    None
122}
123
124/// The concrete `core::ops::Range*` struct a `DefId` denotes.
125pub(crate) enum RangeKind {
126    RangeTo,
127    RangeFrom,
128    Range,
129    RangeInclusive,
130    Other,
131}
132
133/// Classify a `DefId` as one of the `core::ops::Range*` structs.
134pub(crate) fn range_kind(tcx: TyCtxt<'_>, def_id: DefId) -> RangeKind {
135    if lang_api::matches(tcx, def_id, LangItem::RangeTo) {
136        RangeKind::RangeTo
137    } else if lang_api::matches(tcx, def_id, LangItem::RangeFrom) {
138        RangeKind::RangeFrom
139    } else if lang_api::matches(tcx, def_id, LangItem::RangeInclusiveStruct) {
140        RangeKind::RangeInclusive
141    } else if lang_api::matches(tcx, def_id, LangItem::Range) {
142        RangeKind::Range
143    } else {
144        RangeKind::Other
145    }
146}
147
148/// Resolve a `FnDef(def_id, args)` (already `skip_binder`d by the caller) to
149/// its monomorphized [`Instance`] in `caller`'s post-analysis typing env.
150pub(crate) fn resolve_instance<'tcx>(
151    tcx: TyCtxt<'tcx>,
152    caller: DefId,
153    def_id: DefId,
154    args: GenericArgsRef<'tcx>,
155) -> Option<Instance<'tcx>> {
156    if has_unresolved_const_args(tcx, caller, args) {
157        return None;
158    }
159    let typing_env = TypingEnv::post_analysis(tcx, caller);
160    Instance::try_resolve(tcx, typing_env, def_id, args)
161        .ok()
162        .flatten()
163}
164
165pub(crate) fn has_unresolved_const_args<'tcx>(
166    tcx: TyCtxt<'tcx>,
167    caller: DefId,
168    args: GenericArgsRef<'tcx>,
169) -> bool {
170    if !args.has_type_flags(TypeFlags::HAS_CT_PARAM) {
171        return false;
172    }
173    struct UnboundConstParam<'tcx> {
174        tcx: TyCtxt<'tcx>,
175        caller: DefId,
176    }
177    impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UnboundConstParam<'tcx> {
178        type Result = ControlFlow<()>;
179
180        fn visit_const(&mut self, ct: Const<'tcx>) -> Self::Result {
181            if let ConstKind::Param(param) = ct.kind() {
182                let generics = self.tcx.generics_of(self.caller);
183                let index = param.index as usize;
184                if index >= generics.count() {
185                    return ControlFlow::Break(());
186                }
187                let definition = generics.param_at(index, self.tcx);
188                if !matches!(definition.kind, GenericParamDefKind::Const { .. })
189                    || definition.name != param.name
190                {
191                    return ControlFlow::Break(());
192                }
193            }
194            ct.super_visit_with(self)
195        }
196    }
197    args.visit_with(&mut UnboundConstParam { tcx, caller })
198        .is_break()
199}
200
201/// Map a resolved [`Instance`] to its concrete target `DefId` and whether the
202/// dispatch is virtual. `None` for kinds with no single target (e.g. a closure's
203/// `Clone` shim).
204pub(crate) fn instance_def_id<'tcx>(
205    tcx: TyCtxt<'tcx>,
206    instance: Instance<'tcx>,
207) -> (Option<DefId>, bool) {
208    let mut is_virtual = false;
209    let def_id = match instance.def {
210        InstanceKind::Item(def_id) => Some(def_id),
211        InstanceKind::Intrinsic(def_id) => Some(def_id),
212        InstanceKind::Virtual(def_id, _) => {
213            is_virtual = true;
214            Some(def_id)
215        }
216        InstanceKind::Shim(shim_kind) => match shim_kind {
217            ShimKind::VTable(def_id) => Some(def_id),
218            ShimKind::Reify(def_id, _) => Some(def_id),
219            ShimKind::FnPtr(def_id, _) => Some(def_id),
220            ShimKind::ClosureOnce { call_once, .. } => Some(call_once),
221            ShimKind::ConstructCoroutineInClosure {
222                coroutine_closure_def_id,
223                ..
224            } => Some(coroutine_closure_def_id),
225            ShimKind::ThreadLocal(def_id) => Some(def_id),
226            ShimKind::DropGlue(def_id, _) => Some(def_id),
227            #[cfg(rapx_has_fnaddr_asptr)]
228            ShimKind::FnAddrAsPtr(def_id, _) => Some(def_id),
229            #[cfg(all(not(rapx_has_fnaddr_asptr), rapx_has_fnptr_asptr))]
230            ShimKind::FnPtrAsPtr(def_id, _) => Some(def_id),
231            #[cfg(all(not(rapx_has_fnaddr_asptr), not(rapx_has_fnptr_asptr)))]
232            ShimKind::FnPtrAddr(def_id, _) => Some(def_id),
233            ShimKind::AsyncDropGlueCtor(def_id, _) => Some(def_id),
234            ShimKind::Clone(def_id, _) => {
235                if !tcx.is_closure_like(def_id) {
236                    Some(def_id)
237                } else {
238                    None
239                }
240            }
241            _ => todo!(),
242        },
243        #[allow(unreachable_patterns)]
244        _ => None,
245    };
246    (def_id, is_virtual)
247}
248
249/// Enumerate the concrete impl methods that could be dispatched for a trait
250/// method (the set of targets behind a trait-object / virtual call). Each
251/// returned `DefId` is an impl's override of `method_def_id`.
252pub(crate) fn trait_impl_candidates(tcx: TyCtxt<'_>, method_def_id: DefId) -> Vec<DefId> {
253    let mut candidates = Vec::new();
254    if let Some(trait_def_id) = tcx.trait_of_assoc(method_def_id) {
255        for impl_id in tcx.all_impls(trait_def_id) {
256            let impl_map = tcx.impl_item_implementor_ids(impl_id);
257            if let Some(candidate_def_id) = impl_map.get(&method_def_id) {
258                candidates.push(*candidate_def_id);
259            }
260        }
261    }
262    candidates
263}
264
265/// Build a partially-monomorphized [`Instance`] for `def_id`, replacing one
266/// type parameter and leaving the others as parameters.
267pub(crate) fn monomorphize_instance<'tcx>(
268    tcx: TyCtxt<'tcx>,
269    def_id: DefId,
270    param_id: DefId,
271    concrete_ty: Ty<'tcx>,
272) -> Instance<'tcx> {
273    let args = GenericArgs::for_item(tcx, def_id, |param, _| {
274        if param.def_id == param_id {
275            concrete_ty.into()
276        } else {
277            tcx.mk_param_from_def(param)
278        }
279    });
280    Instance::new_raw(def_id, args)
281}
282
283/// Enumerate the concrete self types implementing a type parameter's trait
284/// bounds. Chooses the bound with the fewest concrete (non-generic) impls —
285/// the "small" helper trait that usually pins the supported types (e.g.
286/// `GetDisjointMutIndex` over the generic `SliceIndex`/`Clone` bounds) — and
287/// returns its concrete impl self types.
288pub(crate) fn type_param_impl_tys<'tcx>(
289    tcx: TyCtxt<'tcx>,
290    def_id: DefId,
291    param_ty: Ty<'tcx>,
292) -> Vec<Ty<'tcx>> {
293    if !matches!(param_ty.kind(), TyKind::Param(_)) {
294        return Vec::new();
295    }
296    #[cfg(not(rapx_caller_bounds_accessor))]
297    let param_env = tcx.param_env(def_id);
298    let mut bounds = Vec::new();
299    #[cfg(rapx_caller_bounds_accessor)]
300    let clauses = std::iter::successors(Some(def_id), |&did| tcx.clauses_of(did).parent)
301        .flat_map(|did| tcx.clauses_of(did).clauses.iter().map(|(clause, _)| clause));
302    #[cfg(not(rapx_caller_bounds_accessor))]
303    let clauses = param_env.caller_bounds();
304    for clause in clauses {
305        let Some(trait_clause) = clause.as_trait_clause() else {
306            continue;
307        };
308        let self_ty = trait_clause.self_ty().skip_binder();
309        if self_ty != param_ty || tcx.trait_is_auto(trait_clause.def_id()) {
310            continue;
311        }
312        let requested_args = trait_clause.skip_binder().trait_ref.args;
313        let mut impl_tys: Vec<Ty<'tcx>> = tcx
314            .all_impls(trait_clause.def_id())
315            .filter(|impl_def_id| {
316                tcx.impl_polarity(*impl_def_id) == rustc_middle::ty::ImplPolarity::Positive
317                    && requested_args
318                        .iter()
319                        .skip(1)
320                        .zip(tcx.impl_trait_ref(*impl_def_id).skip_binder().args.iter().skip(1))
321                        .all(|(requested, implemented)| match implemented.kind() {
322                            GenericArgKind::Type(ty) if matches!(ty.kind(), TyKind::Param(_)) => true,
323                            GenericArgKind::Const(ct) if matches!(ct.kind(), ConstKind::Param(_)) => true,
324                            _ => requested == implemented,
325                        })
326            })
327            .map(|impl_def_id| tcx.type_of(impl_def_id).skip_binder())
328            // Only concrete self types are usable as monomorphization candidates
329            // (a generic `impl<T: Clone> Clone for Range<T>` must not count).
330            .filter(|ty| !ty_has_type_param(*ty))
331            .collect();
332        let mut seen = HashSet::new();
333        impl_tys.retain(|ty| seen.insert(*ty));
334        if impl_tys.is_empty() {
335            continue;
336        }
337        bounds.push(impl_tys);
338    }
339    let Some(best) = bounds.iter().min_by_key(|types| types.len()) else {
340        return Vec::new();
341    };
342    best.iter()
343        .copied()
344        .filter(|ty| bounds.iter().all(|types| types.contains(ty)))
345        .collect()
346}
347
348/// Enumerate up to `limit` instances of a generic function using concrete
349/// implementors of one type parameter bound, including containing impl bounds.
350pub(crate) fn enumerate_mono_instances<'tcx>(
351    tcx: TyCtxt<'tcx>,
352    def_id: DefId,
353    limit: usize,
354) -> Vec<Instance<'tcx>> {
355    let candidate = std::iter::successors(Some(def_id), |id| tcx.generics_of(*id).parent)
356        .flat_map(|id| tcx.generics_of(id).own_params.iter())
357        .filter(|param| matches!(param.kind, GenericParamDefKind::Type { .. }))
358        .filter_map(|param| {
359            let param_ty = tcx.mk_param_from_def(param).expect_ty();
360            let impl_tys = type_param_impl_tys(tcx, def_id, param_ty);
361            (!impl_tys.is_empty()).then_some((param.def_id, impl_tys))
362        })
363        .next();
364    let Some((param_id, impl_tys)) = candidate else {
365        return Vec::new();
366    };
367    impl_tys
368        .into_iter()
369        .take(limit)
370        .map(|ty| monomorphize_instance(tcx, def_id, param_id, ty))
371        .collect()
372}
373
374/// Whether `def_id`'s body (or any callee it would inline) contains a
375/// trait-method call with no executable MIR (a still-generic `T: Bound` or
376/// virtual dispatch) *and* no symbolic model — i.e. a call whose opaque
377/// fallback would lose dataflow and therefore benefits from caller
378/// monomorphization.
379pub(crate) fn has_unmodeled_generic_call<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
380    let mut visited = HashSet::default();
381    has_unmodeled_generic_call_rec(tcx, def_id, &mut visited)
382}
383
384pub(crate) fn has_generic_const_call_argument(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
385    if !tcx.is_mir_available(def_id) {
386        return false;
387    }
388    tcx.optimized_mir(def_id).basic_blocks.iter().any(|bb| {
389        let TerminatorKind::Call { args, .. } = &bb.terminator().kind else {
390            return false;
391        };
392        args.iter().any(|arg| {
393            let Operand::Constant(constant) = &arg.node else {
394                return false;
395            };
396            match constant.const_ {
397                rustc_middle::mir::Const::Ty(_, value) => value.has_param(),
398                rustc_middle::mir::Const::Unevaluated(value, _) => value.args.has_param(),
399                _ => false,
400            }
401        })
402    })
403}
404
405/// A callee may need its own MIR instantiated when an immediate trait call
406/// can change target after substituting the caller's type arguments.
407pub(crate) fn has_direct_unmodeled_generic_call<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
408    if !tcx.is_mir_available(def_id) {
409        return false;
410    }
411    let body = tcx.optimized_mir(def_id);
412    body.basic_blocks.iter().any(|bb| {
413        let TerminatorKind::Call {
414            func, destination, ..
415        } = &bb.terminator().kind
416        else {
417            return false;
418        };
419        let Some(callee) = dep_callee_def_id(func) else {
420            return false;
421        };
422        tcx.opt_associated_item(callee)
423            .and_then(|item| item.trait_container(tcx))
424            .is_some()
425            && resolve_callee_def_id(tcx, def_id, func)
426                .is_none_or(|target| !tcx.is_mir_available(target))
427            && !has_function_var_effect(tcx, callee, body.local_decls[destination.local].ty)
428            && !has_registered_effect(tcx, Some(callee))
429    })
430}
431
432fn has_unmodeled_generic_call_rec<'tcx>(
433    tcx: TyCtxt<'tcx>,
434    def_id: DefId,
435    visited: &mut HashSet<DefId>,
436) -> bool {
437    if !visited.insert(def_id) || !tcx.is_mir_available(def_id) {
438        return false;
439    }
440    let body = tcx.optimized_mir(def_id);
441    for bb in body.basic_blocks.iter() {
442        let Some(term) = &bb.terminator else {
443            continue;
444        };
445        let TerminatorKind::Call {
446            func, destination, ..
447        } = &term.kind
448        else {
449            continue;
450        };
451        let Some(callee) = dep_callee_def_id(func) else {
452            continue;
453        };
454        let is_trait_method = tcx
455            .opt_associated_item(callee)
456            .and_then(|a| a.trait_container(tcx))
457            .is_some();
458        if is_trait_method {
459            // A resolved `Instance` may still be virtual or name the trait
460            // method itself. What matters is whether the actual target has
461            // executable MIR after resolving this call.
462            let target = resolve_callee_def_id(tcx, def_id, func);
463            if target.is_none_or(|target| !tcx.is_mir_available(target))
464                && !has_function_var_effect(tcx, callee, body.local_decls[destination.local].ty)
465                && !has_registered_effect(tcx, Some(callee))
466            {
467                return true;
468            }
469            if target.is_some_and(|target| has_unmodeled_generic_call_rec(tcx, target, visited)) {
470                return true;
471            }
472            continue;
473        }
474        // A concrete callee gets inlined, so recurse to surface its own
475        // unmodeled generic trait calls.
476        if has_unmodeled_generic_call_rec(tcx, callee, visited) {
477            return true;
478        }
479    }
480    false
481}
482
483fn has_function_var_effect(tcx: TyCtxt<'_>, callee: DefId, result_ty: Ty<'_>) -> bool {
484    tcx.trait_of_assoc(callee)
485        .is_some_and(|trait_id| lang_api::is_fn_trait(tcx, trait_id))
486        && supports_result(result_ty)
487}
488
489/// Resolve a call `func` operand to its monomorphized [`Instance`].
490pub(crate) fn resolve_callee_instance<'tcx>(
491    tcx: TyCtxt<'tcx>,
492    caller: DefId,
493    func: &Operand<'tcx>,
494) -> Option<Instance<'tcx>> {
495    let Operand::Constant(c) = func else {
496        return None;
497    };
498    let TyKind::FnDef(def_id, callee_args) = c.const_.ty().kind() else {
499        return None;
500    };
501    let callee_args = callee_args.skip_binder();
502    resolve_instance(tcx, caller, *def_id, callee_args)
503}
504
505/// Return the call's `DefId`, using the selected impl method's `DefId` when
506/// available. Otherwise return the `DefId` from the operand.
507pub(crate) fn resolve_callee_def_id<'tcx>(
508    tcx: TyCtxt<'tcx>,
509    caller: DefId,
510    func: &Operand<'tcx>,
511) -> Option<DefId> {
512    let Operand::Constant(c) = func else {
513        return None;
514    };
515    let TyKind::FnDef(def_id, callee_args) = c.const_.ty().kind() else {
516        return None;
517    };
518    let callee_def_id = *def_id;
519    let callee_args = callee_args.skip_binder();
520    let resolved = tcx
521        .opt_associated_item(callee_def_id)
522        .and_then(|item| item.trait_container(tcx))
523        .and_then(|_| resolve_instance(tcx, caller, callee_def_id, callee_args))
524        .and_then(|instance| match instance.def {
525            InstanceKind::Item(def_id) => Some(def_id),
526            _ => None,
527        });
528    Some(resolved.unwrap_or(callee_def_id))
529}
530
531/// Collect all return basic block indices for a function body.
532pub fn collect_return_block_indices(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<BasicBlock> {
533    let mut blocks = Vec::new();
534    if !tcx.is_mir_available(def_id) {
535        return blocks;
536    }
537    let body = tcx.optimized_mir(def_id);
538    for (bb, data) in body.basic_blocks.iter_enumerated() {
539        if matches!(data.terminator().kind, TerminatorKind::Return) {
540            blocks.push(bb);
541        }
542    }
543    blocks
544}
545
546/// Whether `block` ends in a `Return` terminator (a normal exit point).
547pub fn is_return_block(tcx: TyCtxt<'_>, def_id: DefId, block: BasicBlock) -> bool {
548    if !tcx.is_mir_available(def_id) {
549        return false;
550    }
551    let body = tcx.optimized_mir(def_id);
552    body.basic_blocks
553        .get(block)
554        .is_some_and(|data| matches!(data.terminator().kind, TerminatorKind::Return))
555}
556
557/// Return the callee argument index represented by a MIR local.
558///
559/// Contract annotations written with parameter names are parsed in the callee's
560/// local namespace.  MIR local `_0` is the return place and argument locals are
561/// `_1..=_arg_count`, so callee local `_1` denotes checkpoint argument `0`.
562pub fn callee_param_index_for_local(tcx: TyCtxt<'_>, callee: DefId, local: usize) -> Option<usize> {
563    let arg_count = if tcx.is_mir_available(callee) {
564        tcx.optimized_mir(callee).arg_count
565    } else {
566        tcx.fn_sig(callee)
567            .skip_binder()
568            .inputs()
569            .skip_binder()
570            .len()
571    };
572    arg_of_local(Local::from_usize(local), arg_count)
573}
574
575pub fn is_std_crate_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
576    matches!(
577        tcx.crate_name(def_id.krate).as_str(),
578        "core" | "std" | "alloc"
579    )
580}
581
582pub fn is_trait_unsafe(tcx: TyCtxt<'_>, trait_def_id: DefId) -> bool {
583    let Some(local_id) = trait_def_id.as_local() else {
584        return false;
585    };
586    let item = tcx.hir_expect_item(local_id);
587
588    if let ItemKind::Trait { safety, .. } = &item.kind {
589        return matches!(safety, rustc_hir::Safety::Unsafe);
590    }
591
592    false
593}
594
595pub fn resolve_impl_self_ty_def_id(item: &rustc_hir::Item<'_>) -> Option<DefId> {
596    let ItemKind::Impl(rustc_hir::Impl { self_ty, .. }) = &item.kind else {
597        return None;
598    };
599    match &self_ty.kind {
600        rustc_hir::TyKind::Path(rustc_hir::QPath::Resolved(_, path)) => match path.res {
601            rustc_hir::def::Res::Def(
602                rustc_hir::def::DefKind::Struct
603                | rustc_hir::def::DefKind::Enum
604                | rustc_hir::def::DefKind::Union,
605                def_id,
606            ) => Some(def_id),
607            _ => None,
608        },
609        _ => None,
610    }
611}
612
613/// Whether a local item carries a `#[rapx::<name>(...)]` attribute.
614pub(crate) fn has_rapx_attr(tcx: TyCtxt<'_>, def_id: LocalDefId, name: Symbol) -> bool {
615    let hir_id = tcx.local_def_id_to_hir_id(def_id);
616
617    let rapx = Symbol::intern("rapx");
618    let attrs = tcx.hir_attrs(hir_id);
619
620    attrs.iter().any(|attr| crate::compat::attribute_path_matches(tcx, attr, &[rapx, name]))
621}
622
623pub fn has_rapx_verify_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
624    has_rapx_attr(tcx, def_id, Symbol::intern("verify"))
625}
626
627/// True when a type transitively contains a const-generic parameter or
628/// an associated type alias (which may be layout-ambiguous).
629fn ty_has_param_const(ty: Ty<'_>) -> bool {
630    for arg in ty.walk() {
631        match arg.kind() {
632            GenericArgKind::Const(c) if matches!(c.kind(), ConstKind::Param(_)) => return true,
633            GenericArgKind::Type(inner_ty) if matches!(inner_ty.kind(), TyKind::Alias(..)) => {
634                return true;
635            }
636            _ => {}
637        }
638    }
639    false
640}
641
642/// Run `f` inside `catch_unwind`, returning either the result or the
643/// downcasted panic message.
644pub(crate) fn catch_panic<T>(f: impl FnOnce() -> T) -> Result<T, String> {
645    std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).map_err(|e| {
646        e.downcast_ref::<String>()
647            .cloned()
648            .or_else(|| e.downcast_ref::<&str>().map(|s| s.to_string()))
649            .unwrap_or_else(|| "<rustc ICE>".to_string())
650    })
651}
652
653/// Return a stable, human-readable name for a MIR call operand.
654pub fn call_name(tcx: TyCtxt<'_>, func: &Operand<'_>) -> String {
655    dep_callee_def_id(func)
656        .map(|def_id| tcx.def_path_str(def_id))
657        .unwrap_or_else(|| format!("{func:?}"))
658}
659
660/// Return the zero-based argument index of `local`, if it is a MIR argument.
661///
662/// MIR local `_0` is the return place; argument locals start at `_1`.
663pub fn arg_of_local(local: Local, arg_count: usize) -> Option<usize> {
664    let i = local.as_usize();
665    if i >= 1 && i <= arg_count {
666        Some(i - 1)
667    } else {
668        None
669    }
670}
671
672pub fn has_crate(tcx: TyCtxt<'_>, name: &str) -> bool {
673    for num in tcx.crates(()) {
674        if tcx.crate_name(*num) == Symbol::intern(name) {
675            return true;
676        }
677    }
678    false
679}
680
681/// Extracts the source `Place` from an rvalue for simple forwarding operations
682/// (copy, move, cast, reference, raw-pointer, copy-for-deref).
683pub fn rvalue_source_place<'a, 'tcx>(
684    rvalue: &'a Rvalue<'tcx>,
685) -> Option<&'a rustc_middle::mir::Place<'tcx>> {
686    match rvalue {
687        Rvalue::Use(Operand::Copy(place), ..)
688        | Rvalue::Use(Operand::Move(place), ..)
689        | Rvalue::Cast(_, Operand::Copy(place), _)
690        | Rvalue::Cast(_, Operand::Move(place), _)
691        | Rvalue::Ref(_, _, place)
692        | Rvalue::RawPtr(_, place)
693        | Rvalue::CopyForDeref(place) => Some(place),
694        _ => None,
695    }
696}
697
698// ── PlaceKey / operand utilities ─────────────────────────────────
699
700/// Extract a PlaceKey from a MIR operand.
701pub fn operand_place(operand: &Operand<'_>) -> Option<PlaceKey> {
702    operand_mir_place(operand).map(PlaceKey::from_mir_place)
703}
704
705/// Extract the MIR Place from an operand.
706pub fn operand_mir_place<'a, 'tcx>(operand: &'a Operand<'tcx>) -> Option<&'a Place<'tcx>> {
707    match operand {
708        Operand::Copy(place) | Operand::Move(place) => Some(place),
709        _ => None,
710    }
711}
712
713// ── Place resolution utilities ───────────────────────────────────
714
715// ── Block reachability ───────────────────────────────────────────
716
717/// Collect all basic blocks reachable after a call block's normal return, or —
718/// for a non-call block such as a raw-pointer deref statement — after the block
719/// itself (all of its successors).
720pub fn blocks_reachable_after_call(
721    tcx: TyCtxt<'_>,
722    caller: DefId,
723    call_block: BasicBlock,
724) -> HashSet<BasicBlock> {
725    let body = tcx.optimized_mir(caller);
726    let mut starts = Vec::new();
727    if let TerminatorKind::Call { target, .. } = &body.basic_blocks[call_block].terminator().kind
728        && let Some(target) = target
729    {
730        starts.push(*target);
731    } else {
732        starts.extend(body.basic_blocks[call_block].terminator().successors());
733    }
734
735    let mut seen = HashSet::new();
736    let mut stack = starts;
737    while let Some(block) = stack.pop() {
738        if !seen.insert(block) {
739            continue;
740        }
741        let terminator = body.basic_blocks[block].terminator();
742        for successor in terminator.successors() {
743            stack.push(successor);
744        }
745    }
746    seen
747}
748
749// ── MIR place alias mapping ──────────────────────────────────────
750
751// ── Rvalue place scanning ────────────────────────────────────────
752
753/// Check whether any MIR place used in an rvalue matches a predicate.
754pub fn rvalue_any_place_matching<'tcx>(
755    rvalue: &Rvalue<'tcx>,
756    pred: &mut impl FnMut(&Place<'tcx>) -> bool,
757) -> bool {
758    match rvalue {
759        Rvalue::Aggregate(_, operands) => operands.iter().any(|operand| match operand {
760            Operand::Copy(place) | Operand::Move(place) => pred(place),
761            Operand::Constant(_) => false,
762            Operand::RuntimeChecks(_) => false,
763        }),
764        _ => rvalue_source_place(rvalue).is_some_and(pred),
765    }
766}
767
768// ── Pointer arithmetic origin tracing ────────────────────────────
769
770/// Resolve a `const` item (e.g. `const CAPACITY: usize = 2 * B - 1`) by name in
771/// the local crate to its evaluated unsigned-integer value, for use in the
772/// contract DSL (`ValidNum(len <= CAPACITY)`).
773pub(crate) fn resolve_const_item_value<'tcx>(tcx: TyCtxt<'tcx>, name: &str) -> Option<u128> {
774    for item_id in tcx.hir_crate_items(()).free_items() {
775        let def_id = item_id.owner_id.to_def_id();
776        let Some(item_name) = tcx.opt_item_name(def_id) else {
777            continue;
778        };
779        if item_name.as_str() != name {
780            continue;
781        }
782        #[cfg(rapx_defkind_const_struct)]
783        if !matches!(tcx.def_kind(def_id), rustc_hir::def::DefKind::Const { .. }) {
784            continue;
785        }
786        #[cfg(not(rapx_defkind_const_struct))]
787        if !matches!(tcx.def_kind(def_id), rustc_hir::def::DefKind::Const) {
788            continue;
789        }
790        let instance = rustc_middle::ty::Instance::mono(tcx, def_id);
791        let cid = rustc_middle::mir::interpret::GlobalId {
792            instance,
793            promoted: None,
794        };
795        let Ok(val) = tcx.const_eval_global_id(TypingEnv::fully_monomorphized(), cid, DUMMY_SP)
796        else {
797            continue;
798        };
799        let Some(scalar) = val.try_to_scalar_int() else {
800            continue;
801        };
802        return Some(scalar.to_bits(scalar.size()));
803    }
804    None
805}
806
807/// Extract raw bytes from a `ConstValue`, following reference indirection.
808fn const_value_bytes<'tcx>(tcx: TyCtxt<'tcx>, value: ConstValue, depth: usize) -> Option<Vec<u8>> {
809    if depth > 4 {
810        return None;
811    }
812    match value {
813        ConstValue::Slice { alloc_id, .. } => alloc_id_bytes(tcx, alloc_id, depth),
814        ConstValue::Scalar(scalar) => {
815            #[cfg(rapx_scalar_to_pointer_interp_result)]
816            let ptr = scalar.to_pointer(&tcx).discard_err()?;
817            #[cfg(not(rapx_scalar_to_pointer_interp_result))]
818            let ptr = scalar.to_pointer(&tcx);
819            let alloc_id = ptr.provenance?.alloc_id();
820            alloc_id_bytes(tcx, alloc_id, depth)
821        }
822        ConstValue::Indirect { alloc_id, .. } => alloc_id_bytes(tcx, alloc_id, depth),
823        _ => None,
824    }
825}
826
827/// Read bytes from a global allocation.
828fn alloc_id_bytes<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, depth: usize) -> Option<Vec<u8>> {
829    if depth > 4 {
830        return None;
831    }
832    let alloc = match tcx.global_alloc(alloc_id) {
833        GlobalAlloc::Memory(alloc) => alloc,
834        GlobalAlloc::Static(def_id) => tcx.eval_static_initializer(def_id).ok()?,
835        _ => return None,
836    };
837    let alloc = alloc.inner();
838    let provenance = alloc.provenance().ptrs();
839    if let Some((_, prov)) = provenance.iter().next() {
840        return alloc_id_bytes(tcx, prov.alloc_id(), depth + 1);
841    }
842    Some(
843        alloc
844            .inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len())
845            .to_vec(),
846    )
847}
848
849// ── Type layout helpers ───────────────────────────────────────────
850
851/// If `constant` is a promoted `offset_of!(Container, field)` constant (an
852/// unevaluated `Const` whose body is a call to the `offset_of` intrinsic),
853/// return the container type.
854///
855/// Used by the verifier to recognise `byte_add(offset_of!(Container, ..))` and
856/// prove the resulting pointer stays within the container allocation.
857pub(crate) fn offset_of_container<'tcx>(
858    tcx: TyCtxt<'tcx>,
859    constant: &rustc_middle::mir::Const<'tcx>,
860) -> Option<Ty<'tcx>> {
861    let rustc_middle::mir::Const::Unevaluated(uneval, _) = constant else {
862        return None;
863    };
864    // `mir_for_ctfe` only accepts const-like defs; unevaluated consts may also
865    // reference plain functions, so gate on the def kind first.
866    // `mir_for_ctfe` panics for cross-crate constants (e.g. `char::MAX` from
867    // `core`), since it only serves local, CTFE-able definitions. `offset_of!`
868    // always expands to a local `AnonConst`, so rejecting external defs loses
869    // nothing but avoids the ICE.
870    if !ctfe_mir_available(tcx, uneval.def) {
871        return None;
872    }
873    let body = tcx.mir_for_ctfe(uneval.def);
874    for bb in body.basic_blocks.iter() {
875        if let Some(term) = &bb.terminator
876            && let TerminatorKind::Call { func, .. } = &term.kind
877            && let Some(ty) = offset_of_ty_from_func(tcx, func)
878        {
879            return Some(ty);
880        }
881    }
882    None
883}
884
885/// Whether `def` is a `SizedTypeProperties::<ASSOC>` associated const — a
886/// compiler-internal layout constant (`SIZE`, `ALIGN`, `IS_ZST`) that has no
887/// lang/diagnostic item, so it is matched by its def-path suffix.
888pub(crate) fn is_sized_type_property(tcx: TyCtxt<'_>, def: DefId, assoc: &str) -> bool {
889    tcx.def_path_str(def)
890        .ends_with(&format!("SizedTypeProperties::{assoc}"))
891}
892
893/// Whether `mir_for_ctfe` can query this locally available const-like definition.
894pub(crate) fn ctfe_mir_available(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
895    is_const_def_kind(tcx, def_id) && def_id.is_local() && tcx.is_mir_available(def_id)
896}
897
898fn is_const_def_kind(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
899    #[cfg(rapx_defkind_const_struct)]
900    let base = matches!(
901        tcx.def_kind(def_id),
902        DefKind::Const { .. }
903            | DefKind::Static { .. }
904            | DefKind::AssocConst { .. }
905            | DefKind::AnonConst
906    );
907    #[cfg(not(rapx_defkind_const_struct))]
908    let base = matches!(
909        tcx.def_kind(def_id),
910        DefKind::Const | DefKind::Static { .. } | DefKind::AssocConst | DefKind::AnonConst
911    );
912    base
913}
914
915/// Extract the first `Type` generic argument of an `FnDef` call operand.
916///
917/// Many monomorphized std APIs have the interesting type (the receiver or
918/// container element) as their first generic argument.
919pub(crate) fn fn_def_first_type_arg<'tcx>(func: &Operand<'tcx>) -> Option<Ty<'tcx>> {
920    let Operand::Constant(c) = func else {
921        return None;
922    };
923    let TyKind::FnDef(_, args) = c.const_.ty().kind() else {
924        return None;
925    };
926    args.iter().find_map(|a| {
927        let a = a.skip_binder();
928        match a.kind() {
929            GenericArgKind::Type(t) => Some(t),
930            _ => None,
931        }
932    })
933}
934
935fn offset_of_ty_from_func<'tcx>(tcx: TyCtxt<'tcx>, func: &Operand<'tcx>) -> Option<Ty<'tcx>> {
936    let Operand::Constant(c) = func else {
937        return None;
938    };
939    let TyKind::FnDef(def_id, _) = c.const_.ty().kind() else {
940        return None;
941    };
942    if !lang_api::matches(tcx, *def_id, LangItem::OffsetOf) {
943        return None;
944    }
945    fn_def_first_type_arg(func)
946}
947
948pub fn type_layout<'tcx>(tcx: TyCtxt<'tcx>, caller: DefId, ty: Ty<'tcx>) -> Option<(u64, u64)> {
949    if ty_has_param_const(ty) {
950        return None;
951    }
952    match layout_of_ty(tcx, caller, ty) {
953        Some(l) => Some((layout_align_bytes(&l), l.size.bytes())),
954        None if matches!(ty.kind(), TyKind::Param(_)) => Some((0, 0)),
955        None => None,
956    }
957}
958
959/// Compute the full type layout, catching rustc panics and layout errors.
960/// Shared by `type_layout` and the symbolic VM's size/align/field-offset
961/// queries so the `layout_of` call and its panic-guard live in one place.
962pub fn layout_of_ty<'tcx>(
963    tcx: TyCtxt<'tcx>,
964    caller: DefId,
965    ty: Ty<'tcx>,
966) -> Option<rustc_abi::TyAndLayout<'tcx, Ty<'tcx>>> {
967    let env = TypingEnv::post_analysis(tcx, caller);
968    catch_panic(|| {
969        tcx.layout_of(PseudoCanonicalInput {
970            typing_env: env,
971            value: ty,
972        })
973    })
974    .ok()
975    .and_then(|r| r.ok())
976}
977
978/// ABI alignment in bytes across rustc layout API versions.
979pub fn layout_align_bytes(layout: &rustc_abi::TyAndLayout<'_, Ty<'_>>) -> u64 {
980    #[cfg(rapx_layout_align_abi)]
981    {
982        layout.align.abi.bytes()
983    }
984    #[cfg(not(rapx_layout_align_abi))]
985    {
986        layout.align.bytes()
987    }
988}
989
990/// Byte offset of a struct field within its container type (0 on failure).
991pub fn field_offset_in_bytes<'tcx>(
992    tcx: TyCtxt<'tcx>,
993    caller: DefId,
994    ty: Ty<'tcx>,
995    field_idx: usize,
996) -> u64 {
997    let Some(layout) = layout_of_ty(tcx, caller, ty) else {
998        return 0;
999    };
1000    if let rustc_abi::FieldsShape::Arbitrary { ref offsets, .. } = layout.fields {
1001        let idx = rustc_abi::FieldIdx::from_usize(field_idx);
1002        if idx.as_usize() < offsets.len() {
1003            return offsets[idx].bytes();
1004        }
1005    }
1006    0
1007}
1008
1009/// Whether `ty` mentions a (type) generic parameter anywhere in its structure,
1010/// e.g. `T`, `*mut T`, `Option<T>`.
1011pub(crate) fn ty_has_type_param(ty: Ty<'_>) -> bool {
1012    ty.walk().any(|t| {
1013        matches!(
1014            t.kind(),
1015            GenericArgKind::Type(inner) if matches!(inner.kind(), TyKind::Param(_))
1016        )
1017    })
1018}
1019
1020// ── Constant scalar / byte-string extraction ───────────────────
1021
1022/// Parse an integer from a MIR constant's `Debug` text. Handles decimal,
1023/// `0x` hex, and `Value(...)` forms.
1024pub fn const_int_from_debug(text: &str) -> Option<u64> {
1025    if let Ok(v) = text.parse::<u64>() {
1026        return Some(v);
1027    }
1028    if let Some(start) = text.find("0x") {
1029        let hex_part = &text[start..];
1030        let end = hex_part
1031            .find(|c: char| !c.is_ascii_hexdigit() && c != 'x')
1032            .unwrap_or(hex_part.len());
1033        u64::from_str_radix(&hex_part[2..end], 16).ok()
1034    } else if let Some(start) = text.find("Value(") {
1035        let inner = &text[start + 6..];
1036        if let Some(end) = inner.find(')') {
1037            inner[..end].parse::<u64>().ok()
1038        } else {
1039            None
1040        }
1041    } else {
1042        None
1043    }
1044}
1045
1046/// Resolve a MIR constant to a concrete integer, falling back from the cheap
1047/// debug-text parse to full const evaluation.
1048///
1049/// Layout constants (`offset_of!(Container, field)`) and the `T::{BITS,MAX,MIN}`
1050/// associated constants of *small* integer types (`u8`..`u32`, `i8`..`i32`) are
1051/// evaluated here.  Arbitrary unevaluated consts — and the wide bounds
1052/// `usize::MAX` / `u64::MAX` / `u128::MAX` — are deliberately left symbolic:
1053/// forcing them to a concrete `u64` would overflow downstream size arithmetic.
1054pub fn eval_const_scalar_int<'tcx>(
1055    tcx: TyCtxt<'tcx>,
1056    constant: &rustc_middle::mir::Const<'tcx>,
1057    text: &str,
1058) -> Option<i128> {
1059    if let Some(v) = const_int_from_debug(text) {
1060        return Some(v as i128);
1061    }
1062    // Resolve `T::{BITS,MAX,MIN}` associated constants of small integer types,
1063    // used in numeric bounds (`u32::MAX`) and shift-width masks (`u32::BITS`).
1064    let is_num_bound = text.contains("::BITS") || text.contains("::MAX") || text.contains("::MIN");
1065    // An unevaluated `const` item (e.g. `const CAPACITY: usize = 2 * B - 1`)
1066    // must be const-evaluated to its scalar value, so comparisons like
1067    // `idx < CAPACITY` are modeled as `idx < 11` rather than an opaque symbol.
1068    let is_unevaluated = matches!(*constant, rustc_middle::mir::Const::Unevaluated(..));
1069    if !is_num_bound && !is_unevaluated && offset_of_container(tcx, constant).is_none() {
1070        return None;
1071    }
1072    let typing_env = TypingEnv::fully_monomorphized();
1073    let val = constant.eval(tcx, typing_env, rustc_span::DUMMY_SP).ok()?;
1074    let scalar = val.try_to_scalar_int()?;
1075    // Keep wide bounds (`u64::MAX`, `usize::MAX`, `u128::MAX`) symbolic so
1076    // they don't overflow downstream size arithmetic.
1077    if scalar.to_bits(scalar.size()) > u32::MAX as u128 {
1078        return None;
1079    }
1080    Some(scalar_int_value(scalar, constant.ty()))
1081}
1082
1083pub(crate) fn const_destructure<'tcx>(
1084    tcx: TyCtxt<'tcx>,
1085    constant: &rustc_middle::mir::Const<'tcx>,
1086) -> Option<(Option<u64>, Vec<(ConstValue, Ty<'tcx>)>)> {
1087    let ty = constant.ty();
1088    match ty.kind() {
1089        TyKind::Tuple(_) => {}
1090        TyKind::Adt(adt_def, _) if !adt_def.is_union() => {}
1091        _ => return None,
1092    }
1093    let val = constant
1094        .eval(tcx, TypingEnv::fully_monomorphized(), DUMMY_SP)
1095        .ok()?;
1096    let destructured = tcx.try_destructure_mir_constant_for_user_output(val, ty)?;
1097    Some((
1098        destructured.variant.map(|v| v.as_u32() as u64),
1099        destructured.fields.to_vec(),
1100    ))
1101}
1102
1103pub(crate) fn scalar_int_value<'tcx>(scalar: ScalarInt, ty: Ty<'tcx>) -> i128 {
1104    let bits = scalar.size().bits() as u32;
1105    let raw = scalar.to_bits(scalar.size()) as i128;
1106    if let TyKind::Int(_) = ty.kind() {
1107        let sign = 1i128 << (bits - 1);
1108        if raw >= sign {
1109            raw - (1i128 << bits)
1110        } else {
1111            raw
1112        }
1113    } else {
1114        raw
1115    }
1116}
1117
1118/// Resolve an array length const (`ty::Const`) to a concrete `u64`. Unlike
1119/// `try_to_target_usize`, this also evaluates unevaluated const expressions
1120/// (e.g. `[MaybeUninit<K>; CAPACITY]` where `CAPACITY = 2 * B - 1`), so the
1121/// VM can allocate the array with its true element count instead of a
1122/// collapsed zero-size fallback.
1123pub(crate) fn eval_array_len<'tcx>(
1124    tcx: TyCtxt<'tcx>,
1125    c: &rustc_middle::ty::Const<'tcx>,
1126) -> Option<u64> {
1127    if let Some(v) = c.try_to_target_usize(tcx) {
1128        return Some(v);
1129    }
1130    // An unevaluated array length (`[MaybeUninit<K>; CAPACITY]` where
1131    // `CAPACITY = 2 * B - 1`) is an anonymous const represented as an alias.
1132    let def_id = {
1133        let ConstKind::Alias(_, alias_const) = c.kind() else {
1134            return None;
1135        };
1136        #[cfg(not(rapx_alias_const_inherent_self))]
1137        let def_id = alias_const.kind.opt_def_id()?;
1138        #[cfg(rapx_alias_const_inherent_self)]
1139        let def_id = {
1140            match alias_const.kind {
1141                AliasConstKind::Projection { def_id } => def_id,
1142                AliasConstKind::InherentSelf { def_id } => def_id,
1143                AliasConstKind::InherentImpl { def_id } => def_id,
1144                AliasConstKind::Free { def_id } => def_id,
1145                AliasConstKind::Anon { def_id } => def_id,
1146            }
1147        };
1148        def_id
1149    };
1150    let instance = rustc_middle::ty::Instance::mono(tcx, def_id);
1151    let cid = rustc_middle::mir::interpret::GlobalId {
1152        instance,
1153        promoted: None,
1154    };
1155    if let Ok(val) = tcx.const_eval_global_id(TypingEnv::fully_monomorphized(), cid, DUMMY_SP)
1156        && let Some(scalar) = val.try_to_scalar_int()
1157    {
1158        return Some(scalar.to_target_usize(tcx));
1159    }
1160    None
1161}
1162
1163/// Try to extract raw bytes from a MIR constant operand that is a reference
1164/// to a byte array/slice (e.g. `b"hello\0"`). Returns the byte values.
1165/// Used by the VM to populate byte-level tracking for constant C strings.
1166pub fn const_operand_bytes<'tcx>(tcx: TyCtxt<'tcx>, operand: &Operand<'tcx>) -> Option<Vec<u8>> {
1167    let constant = match operand {
1168        Operand::Constant(c) => c,
1169        _ => return None,
1170    };
1171    let ty = constant.const_.ty();
1172    let inner_ty = match ty.kind() {
1173        TyKind::Ref(_, inner, _) => *inner,
1174        _ => return None,
1175    };
1176    // Peel through nested references (e.g. &&[u8])
1177    let inner_ty = if let TyKind::Ref(_, innermost, _) = inner_ty.kind() {
1178        *innermost
1179    } else {
1180        inner_ty
1181    };
1182    if !matches!(inner_ty.kind(), TyKind::Array(..) | TyKind::Slice(..)) {
1183        return None;
1184    }
1185
1186    // Evaluate the MIR constant to get a ConstValue
1187    let typing_env = TypingEnv::fully_monomorphized();
1188    let value = constant
1189        .const_
1190        .eval(tcx, typing_env, rustc_span::DUMMY_SP)
1191        .ok()?;
1192
1193    const_value_bytes(tcx, value, 0)
1194}
1195
1196/// Extract the bare local from a Copy/Move operand with no projection.
1197pub fn extract_local(operand: &Operand<'_>) -> Option<Local> {
1198    operand_mir_place(operand)
1199        .filter(|place| place.projection.is_empty())
1200        .map(|place| place.local)
1201}
1202
1203/// Extract a constant u64 value from an operand, if it's a known constant.
1204pub fn operand_const_u64(operand: &Operand<'_>) -> Option<u64> {
1205    operand_scalar_int(operand).and_then(|v| u64::try_from(v).ok())
1206}
1207
1208/// Extract a scalar constant's full bit pattern for typed interpretation.
1209pub fn operand_const_u128(operand: &Operand<'_>) -> Option<u128> {
1210    operand_scalar_int(operand)
1211}
1212
1213/// Whether a type is a `u8` array (`[u8; N]`) or `u8` slice (`[u8]`).
1214pub fn is_u8_array_or_slice(ty: Ty<'_>) -> bool {
1215    match ty.kind() {
1216        TyKind::Array(elem_ty, _) => {
1217            matches!(elem_ty.kind(), TyKind::Uint(rustc_middle::ty::UintTy::U8))
1218        }
1219        TyKind::Slice(elem_ty) => {
1220            matches!(elem_ty.kind(), TyKind::Uint(rustc_middle::ty::UintTy::U8))
1221        }
1222        _ => false,
1223    }
1224}
1225
1226/// Whether a type transitively contains a reference.
1227///
1228/// This is a shallow check: it recurses only through `Adt` generic arguments,
1229/// not through tuple elements or `Adt` fields. See [`type_contains_raw_ptr`]
1230/// for a deeper check that also matches raw pointers.
1231pub fn type_contains_reference(ty: Ty<'_>) -> bool {
1232    match ty.kind() {
1233        TyKind::Ref(..) => true,
1234        TyKind::Adt(_, substs) => substs.types().any(type_contains_reference),
1235        _ => false,
1236    }
1237}
1238
1239/// Whether a type transitively contains a raw pointer, recursing through tuple
1240/// elements and `Adt` fields. Unlike [`type_contains_reference`], this does not
1241/// match references — a method returning `&*self.raw` re-borrows the pointee
1242/// (safe) rather than leaking the raw pointer value itself (unsafe).
1243pub fn type_contains_raw_ptr<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
1244    match ty.kind() {
1245        TyKind::RawPtr(_, _) => true,
1246        TyKind::Tuple(elems) => elems.iter().any(|t| type_contains_raw_ptr(tcx, t)),
1247        TyKind::Adt(def, args) => {
1248            if args.iter().any(|arg| {
1249                if let Some(t) = arg.as_type() {
1250                    type_contains_raw_ptr(tcx, t)
1251                } else {
1252                    false
1253                }
1254            }) {
1255                return true;
1256            }
1257            let adt = tcx.adt_def(def.did());
1258            adt.all_fields()
1259                .any(|field| type_contains_raw_ptr(tcx, field_ty(tcx, field, args)))
1260        }
1261        _ => false,
1262    }
1263}
1264
1265/// Resolve a struct field's type, normalizing where the rustc version requires it.
1266///
1267/// On newer rustc `field.ty(tcx, args)` returns an `Unnormalized<Ty>` that must
1268/// be `.skip_norm_wip()`-ed; on older toolchains the `SkipNormWip` shim makes
1269/// the same call a no-op, so this is version-independent.
1270#[cfg(not(rapx_generic_args_value))]
1271pub fn field_ty<'tcx>(tcx: TyCtxt<'tcx>, field: &FieldDef, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
1272    field.ty(tcx, args).skip_norm_wip()
1273}
1274
1275#[cfg(rapx_generic_args_value)]
1276pub fn field_ty<'tcx>(
1277    tcx: TyCtxt<'tcx>,
1278    field: &FieldDef,
1279    args: impl std::borrow::Borrow<GenericArgsRef<'tcx>>,
1280) -> Ty<'tcx> {
1281    field.ty(tcx, *args.borrow()).skip_norm_wip()
1282}
1283
1284/// Resolve a field-index path against its root type, including `Some`/`Ok` wrappers.
1285pub(crate) fn projected_field_ty<'tcx>(tcx: TyCtxt<'tcx>, mut ty: Ty<'tcx>, path: &[usize]) -> Option<Ty<'tcx>> {
1286    for &index in path {
1287        ty = match ty.peel_refs().kind() {
1288            TyKind::Adt(adt, args) => {
1289                let variant = if adt.is_enum() {
1290                    adt.variants().iter().find(|variant| {
1291                        matches!(variant.name.as_str(), "Some" | "Ok") && variant.fields.len() > index
1292                    })?
1293                } else {
1294                    adt.non_enum_variant()
1295                };
1296                field_ty(tcx, variant.fields.get(rustc_abi::FieldIdx::from_usize(index))?, args)
1297            }
1298            TyKind::Tuple(fields) => *fields.get(index)?,
1299            _ => return None,
1300        };
1301    }
1302    Some(ty)
1303}
1304
1305/// Whether `def_id` is a single-field struct wrapping a raw pointer (i.e.
1306/// `NonNull`-shaped).  Used by alias/ownership reasoning to recognize pointer
1307/// wrappers — including local re-implementations — by their structure rather
1308/// than by a std `DefId`.
1309pub fn is_raw_ptr_wrapper<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
1310    let adt = tcx.adt_def(def_id);
1311    let variant = adt.non_enum_variant();
1312    if variant.fields.len() != 1 {
1313        return false;
1314    }
1315    let args = rustc_middle::ty::GenericArgs::identity_for_item(tcx, def_id);
1316    let field = variant.fields.iter().next().unwrap();
1317    let field_ty = field_ty(tcx, field, args);
1318    matches!(field_ty.kind(), TyKind::RawPtr(..))
1319}
1320
1321/// Collect the layouts of every concrete implementor of a generic type
1322/// parameter's trait bounds (empty for non-param types).
1323fn generic_param_impl_layouts<'tcx>(
1324    tcx: TyCtxt<'tcx>,
1325    caller: DefId,
1326    ty: Ty<'tcx>,
1327) -> Vec<rustc_abi::TyAndLayout<'tcx, Ty<'tcx>>> {
1328    if !matches!(ty.kind(), TyKind::Param(_)) {
1329        return Vec::new();
1330    }
1331    #[cfg(not(rapx_caller_bounds_accessor))]
1332    let param_env = tcx.param_env(caller);
1333    let typing_env = TypingEnv::post_analysis(tcx, caller);
1334    #[cfg(rapx_caller_bounds_accessor)]
1335    let clauses = std::iter::successors(Some(caller), |&did| tcx.clauses_of(did).parent)
1336        .flat_map(|did| tcx.clauses_of(did).clauses.iter().map(|(clause, _)| clause));
1337    #[cfg(not(rapx_caller_bounds_accessor))]
1338    let clauses = param_env.caller_bounds();
1339    for clause in clauses {
1340        let Some(trait_clause) = clause.as_trait_clause() else {
1341            continue;
1342        };
1343        let self_ty = trait_clause.self_ty().skip_binder();
1344        if self_ty != ty {
1345            continue;
1346        }
1347        let mut layouts = Vec::new();
1348        for impl_def_id in tcx.all_impls(trait_clause.def_id()) {
1349            let impl_ty = tcx.type_of(impl_def_id).skip_binder();
1350            if ty_has_param_const(impl_ty) {
1351                continue;
1352            }
1353            let Ok(Ok(layout)) = catch_panic(|| {
1354                tcx.layout_of(PseudoCanonicalInput {
1355                    typing_env,
1356                    value: impl_ty,
1357                })
1358            }) else {
1359                continue;
1360            };
1361            layouts.push(layout);
1362        }
1363        if !layouts.is_empty() {
1364            return layouts;
1365        }
1366    }
1367    Vec::new()
1368}
1369
1370/// Max `size_of` over all implementors of a generic type parameter's trait
1371/// bounds (0 for non-param types).
1372pub fn size_of_generic_param<'tcx>(tcx: TyCtxt<'tcx>, caller: DefId, ty: Ty<'tcx>) -> u64 {
1373    generic_param_impl_layouts(tcx, caller, ty)
1374        .into_iter()
1375        .map(|l| l.size.bytes())
1376        .max()
1377        .unwrap_or(0)
1378}
1379
1380/// Min `align_of` over all implementors of a generic type parameter's trait
1381/// bounds (0 for non-param types).
1382pub fn min_align_of_generic_param<'tcx>(tcx: TyCtxt<'tcx>, caller: DefId, ty: Ty<'tcx>) -> u64 {
1383    generic_param_impl_layouts(tcx, caller, ty)
1384        .into_iter()
1385        .map(|l| layout_align_bytes(&l))
1386        .min()
1387        .unwrap_or(0)
1388}
1389
1390/// Max `align_of` over all implementors of a generic type parameter's trait
1391/// bounds (0 for non-param types or a parameter without a bounded set of
1392/// implementors).
1393pub fn max_align_of_generic_param<'tcx>(tcx: TyCtxt<'tcx>, caller: DefId, ty: Ty<'tcx>) -> u64 {
1394    generic_param_impl_layouts(tcx, caller, ty)
1395        .into_iter()
1396        .map(|l| layout_align_bytes(&l))
1397        .max()
1398        .unwrap_or(0)
1399}
1400
1401/// Follow a `parents` map (built by `verify::property_checker::cstr`'s
1402/// `body_parents`) from `start` to its root local, guarding against cycles.
1403pub fn follow_parents(parents: &FxHashMap<Local, Local>, start: Local) -> Local {
1404    let mut current = start;
1405    let mut seen = std::collections::HashSet::new();
1406    while seen.insert(current) {
1407        let Some(next) = parents.get(&current) else {
1408            break;
1409        };
1410        current = *next;
1411    }
1412    current
1413}
1414
1415/// Resolve a local through `Cast` assignments back to its non-cast source.
1416pub fn resolve_through_casts<'tcx>(body: &Body<'tcx>, local: Local) -> Local {
1417    let mut current = local;
1418    let mut seen = std::collections::HashSet::new();
1419    while seen.insert(current) {
1420        let found = body.basic_blocks.iter().any(|data| {
1421            data.statements.iter().any(|stmt| {
1422                let StatementKind::Assign(assign) = &stmt.kind else {
1423                    return false;
1424                };
1425                let (target, rvalue) = assign.as_ref();
1426                if target.local != current || !target.projection.is_empty() {
1427                    return false;
1428                }
1429                if let Rvalue::Cast(_, operand, _) = rvalue {
1430                    #[allow(unreachable_patterns)]
1431                    match operand {
1432                        Operand::Copy(p) | Operand::Move(p) if p.projection.is_empty() => {
1433                            current = p.local;
1434                            return true;
1435                        }
1436                        _ => {}
1437                    }
1438                }
1439                false
1440            })
1441        });
1442        if !found {
1443            break;
1444        }
1445    }
1446    current
1447}
1448
1449// ── Constant byte recovery from MIR ─────────────────────────────
1450
1451fn operand_scalar_int(operand: &Operand<'_>) -> Option<u128> {
1452    let constant = match operand {
1453        Operand::Constant(c) => c,
1454        _ => return None,
1455    };
1456    constant
1457        .const_
1458        .try_to_scalar_int()
1459        .map(|s| s.to_uint(s.size()))
1460        .or_else(|| const_int_from_debug(&format!("{:?}", constant.const_)).map(|v| v as u128))
1461}
1462
1463fn rvalue_const_bytes<'tcx>(tcx: TyCtxt<'tcx>, rvalue: &Rvalue<'tcx>) -> Option<Vec<u8>> {
1464    let constant = match rvalue {
1465        Rvalue::Use(Operand::Constant(constant), ..)
1466        | Rvalue::Cast(_, Operand::Constant(constant), _) => constant,
1467        _ => return None,
1468    };
1469    let value = constant
1470        .const_
1471        .eval(tcx, TypingEnv::fully_monomorphized(), DUMMY_SP)
1472        .ok()?;
1473    const_value_bytes(tcx, value, 0)
1474}
1475
1476pub fn collect_all_const_bytes_worklist<'tcx>(
1477    tcx: TyCtxt<'tcx>,
1478    body: &Body<'tcx>,
1479    root: Local,
1480) -> Vec<Vec<u8>> {
1481    let mut results: Vec<Vec<u8>> = Vec::new();
1482    let mut worklist: Vec<Local> = vec![root];
1483    let mut visited: std::collections::HashSet<Local> = std::collections::HashSet::new();
1484
1485    while let Some(local) = worklist.pop() {
1486        if !visited.insert(local) {
1487            continue;
1488        }
1489
1490        for data in body.basic_blocks.iter() {
1491            for statement in &data.statements {
1492                let StatementKind::Assign(assign) = &statement.kind else {
1493                    continue;
1494                };
1495                let (target, rvalue) = assign.as_ref();
1496                if target.local != local || !target.projection.is_empty() {
1497                    continue;
1498                }
1499
1500                if let Rvalue::Ref(_, _, place) = rvalue {
1501                    if let Some(bytes) = const_bytes_for_local(tcx, body, place.local) {
1502                        results.push(bytes);
1503                    }
1504                    continue;
1505                }
1506
1507                if let Rvalue::Use(operand, ..) = rvalue {
1508                    #[allow(unreachable_patterns)]
1509                    match operand {
1510                        Operand::Copy(p) | Operand::Move(p) => {
1511                            worklist.push(p.local);
1512                            if let Some(bytes) = const_bytes_for_local(tcx, body, p.local) {
1513                                results.push(bytes);
1514                            }
1515                            continue;
1516                        }
1517                        Operand::Constant(_) => {}
1518                        _ => continue,
1519                    }
1520                }
1521
1522                if let Some(bytes) = rvalue_const_bytes(tcx, rvalue) {
1523                    results.push(bytes);
1524                }
1525            }
1526        }
1527
1528        for data in body.basic_blocks.iter() {
1529            if let Some(terminator) = &data.terminator
1530                && let TerminatorKind::Call {
1531                    destination,
1532                    func,
1533                    args,
1534                    ..
1535                } = &terminator.kind
1536            {
1537                let dlocal = destination.local;
1538                if dlocal != local {
1539                    continue;
1540                }
1541                if !destination.projection.is_empty() {
1542                    continue;
1543                }
1544                if dep_callee_def_id(func).is_some_and(|d| {
1545                    tcx.is_diagnostic_item(Symbol::intern("box_assume_init_into_vec_unsafe"), d)
1546                })
1547                    && let Some(box_op) = args.first()
1548                    && let Operand::Copy(p) | Operand::Move(p) = &box_op.node
1549                    && p.projection.is_empty()
1550                {
1551                    worklist.push(p.local);
1552                }
1553            }
1554        }
1555    }
1556
1557    collect_aggregate_const_bytes(tcx, body, &mut results);
1558
1559    results
1560}
1561
1562fn collect_aggregate_const_bytes<'tcx>(
1563    tcx: TyCtxt<'tcx>,
1564    body: &Body<'tcx>,
1565    results: &mut Vec<Vec<u8>>,
1566) {
1567    for data in body.basic_blocks.iter() {
1568        for statement in &data.statements {
1569            let StatementKind::Assign(assign) = &statement.kind else {
1570                continue;
1571            };
1572            let (_, rvalue) = assign.as_ref();
1573            let Rvalue::Aggregate(_, operands) = rvalue else {
1574                continue;
1575            };
1576            if operands.len() < 2 {
1577                continue;
1578            }
1579            let last_op = operands.iter().last().unwrap();
1580            if !is_constant_zero(last_op) {
1581                continue;
1582            }
1583            let mut all_nonzero = true;
1584            for op in operands.iter().take(operands.len() - 1) {
1585                if !aggregate_op_is_nonzero(tcx, body, op) {
1586                    all_nonzero = false;
1587                    break;
1588                }
1589            }
1590            if all_nonzero {
1591                let len = operands.len();
1592                let mut bytes = vec![b'x'; len - 1];
1593                bytes.push(0);
1594                results.push(bytes);
1595            }
1596        }
1597    }
1598}
1599
1600pub fn const_bytes_for_local<'tcx>(
1601    tcx: TyCtxt<'tcx>,
1602    body: &Body<'tcx>,
1603    root: Local,
1604) -> Option<Vec<u8>> {
1605    for data in body.basic_blocks.iter() {
1606        for statement in &data.statements {
1607            let StatementKind::Assign(assign) = &statement.kind else {
1608                continue;
1609            };
1610            let (target, rvalue) = assign.as_ref();
1611            if target.local != root || !target.projection.is_empty() {
1612                continue;
1613            }
1614            if let Rvalue::Ref(_, _, place) = rvalue {
1615                let deref_local = place.local;
1616                if let Some(bytes) = const_bytes_for_local(tcx, body, deref_local) {
1617                    return Some(bytes);
1618                }
1619                continue;
1620            }
1621            if let Rvalue::Use(operand, ..) = rvalue {
1622                #[allow(unreachable_patterns)]
1623                match operand {
1624                    Operand::Copy(p) | Operand::Move(p) => {
1625                        if let Some(bytes) = const_bytes_for_local(tcx, body, p.local) {
1626                            return Some(bytes);
1627                        }
1628                        continue;
1629                    }
1630                    Operand::Constant(_) => {}
1631                    _ => continue,
1632                }
1633            }
1634            if let Rvalue::Cast(_, operand, _) = rvalue {
1635                if let Operand::Copy(p) | Operand::Move(p) = operand
1636                    && p.projection.is_empty()
1637                    && let Some(bytes) = const_bytes_for_local(tcx, body, p.local)
1638                {
1639                    return Some(bytes);
1640                }
1641                continue;
1642            }
1643            return rvalue_const_bytes(tcx, rvalue);
1644        }
1645    }
1646    None
1647}
1648
1649fn aggregate_op_is_nonzero<'tcx>(
1650    tcx: TyCtxt<'tcx>,
1651    body: &Body<'tcx>,
1652    operand: &Operand<'tcx>,
1653) -> bool {
1654    if is_constant_zero(operand) {
1655        return false;
1656    }
1657    if const_operand_bytes(tcx, operand).is_some() {
1658        return true;
1659    }
1660    match operand {
1661        Operand::Copy(p) | Operand::Move(p) if p.projection.is_empty() => {
1662            for data in body.basic_blocks.iter() {
1663                if let Some(terminator) = &data.terminator
1664                    && let TerminatorKind::Call {
1665                        destination, func, ..
1666                    } = &terminator.kind
1667                    && destination.local == p.local
1668                    && destination.projection.is_empty()
1669                {
1670                    return fn_always_returns_nonzero(tcx, func);
1671                }
1672            }
1673            false
1674        }
1675        Operand::Constant(_) => operand_scalar_int(operand).is_some_and(|v| v != 0),
1676        _ => false,
1677    }
1678}
1679
1680fn is_constant_zero(operand: &Operand<'_>) -> bool {
1681    operand_scalar_int(operand) == Some(0)
1682}
1683
1684fn fn_always_returns_nonzero<'tcx>(tcx: TyCtxt<'tcx>, func: &Operand<'tcx>) -> bool {
1685    let Some(fn_def_id) = dep_callee_def_id(func) else {
1686        return false;
1687    };
1688    if !tcx.is_mir_available(fn_def_id) {
1689        return false;
1690    }
1691    let callee_body = tcx.optimized_mir(fn_def_id);
1692
1693    let mut has_return = false;
1694    for bb_data in callee_body.basic_blocks.iter() {
1695        if let Some(terminator) = &bb_data.terminator
1696            && matches!(terminator.kind, TerminatorKind::Return)
1697        {
1698            has_return = true;
1699        }
1700        for stmt in &bb_data.statements {
1701            let StatementKind::Assign(assign) = &stmt.kind else {
1702                continue;
1703            };
1704            let (target, rvalue) = assign.as_ref();
1705            if target.local != Local::from_usize(0) || !target.projection.is_empty() {
1706                continue;
1707            }
1708            if !rvalue_is_nonzero(rvalue) {
1709                return false;
1710            }
1711        }
1712    }
1713
1714    has_return
1715}
1716
1717fn rvalue_is_nonzero(rvalue: &Rvalue<'_>) -> bool {
1718    match rvalue {
1719        #[allow(unreachable_patterns)]
1720        Rvalue::Use(operand, ..) => match operand {
1721            Operand::Constant(_) => operand_scalar_int(operand).is_some_and(|v| v != 0),
1722            Operand::Copy(_) | Operand::Move(_) => true,
1723            _ => false,
1724        },
1725        _ => false,
1726    }
1727}