Skip to main content

rapx/
def_ids.rs

1//! Known API and type identities.
2//!
3//! # Agent Rules
4//!
5//! Keep this module limited to identity submodules and shared identity helpers.
6//! Do not depend on other RAPx modules.
7
8use rustc_hir::def_id::DefId;
9use rustc_middle::ty::{Ty, TyCtxt, TyKind};
10
11pub(crate) mod diagnostic_api;
12pub(crate) mod intrinsic_api;
13pub(crate) mod lang_api;
14pub(crate) mod nomir_api;
15pub(crate) mod other_api;
16pub(crate) mod type_ids;
17
18fn is_std_api(tcx: TyCtxt<'_>, did: DefId) -> bool {
19    matches!(tcx.crate_name(did.krate).as_str(), "core" | "alloc" | "std")
20}
21
22/// Whether `callee` explicitly drops the value stored in `ManuallyDrop<T>`.
23pub(crate) fn is_manually_drop_drop(tcx: TyCtxt<'_>, callee: Option<DefId>) -> bool {
24    callee.is_some_and(|did| other_api::is_manually_drop(tcx, did))
25}
26
27/// Whether `callee` is the `ptr_offset_from` or `ptr_offset_from_unsigned` intrinsic.
28pub(crate) fn is_intrinsic_ptr_offset_from(tcx: TyCtxt<'_>, callee: Option<DefId>) -> bool {
29    intrinsic_api::intrinsic_name(tcx, callee).is_some_and(|name| {
30        matches!(name.as_str(), "ptr_offset_from" | "ptr_offset_from_unsigned")
31    })
32}
33
34/// Whether `callee` is a diagnostic-item pointer write API.
35pub(crate) fn is_ptr_write(tcx: TyCtxt<'_>, callee: Option<DefId>) -> bool {
36    callee.is_some_and(|did| diagnostic_api::is_ptr_write(tcx, did))
37}
38
39/// Whether stripping references, raw pointers, slices, and arrays from `ty`
40/// reaches the standard `MaybeUninit<T>` type.
41pub(crate) fn strips_to_maybe_uninit(ty: Ty<'_>) -> bool {
42    let mut ty = ty;
43    loop {
44        match ty.kind() {
45            TyKind::Slice(element) | TyKind::Array(element, _) => ty = *element,
46            TyKind::RawPtr(element, _) | TyKind::Ref(_, element, _) => ty = *element,
47            TyKind::Adt(adt, _) => return type_ids::is_maybe_uninit_type(adt.did()),
48            _ => return false,
49        }
50    }
51}