Skip to main content

rapx/def_ids/
nomir_api.rs

1//! Known standard-library APIs without MIR.
2//!
3//! # Agent Rules
4//!
5//! Keep this module limited to identifying standard-library APIs when their
6//! MIR is unavailable. Do not depend on other RAPx modules.
7
8use rustc_hir::def_id::DefId;
9use rustc_middle::ty::TyCtxt;
10
11use super::is_std_api;
12
13fn matches_path(tcx: TyCtxt<'_>, did: DefId, paths: &[&str]) -> bool {
14    if !is_std_api(tcx, did) {
15        return false;
16    }
17    let name = tcx.def_path_str(did);
18    paths.iter().any(|path| {
19        name == *path
20            || path
21                .split_once("::")
22                .is_some_and(|(_, suffix)| name == suffix)
23    })
24}
25
26fn has_no_mir(tcx: TyCtxt<'_>, did: DefId) -> bool {
27    !tcx.is_mir_available(did)
28}
29
30pub fn is_convert_vec_to_vec(tcx: TyCtxt<'_>, callee: Option<DefId>) -> bool {
31    callee.is_some_and(|did| {
32        has_no_mir(tcx, did)
33            && matches_path(
34                tcx,
35                did,
36                &[
37                    "std::slice::<impl [T]>::to_vec_in::ConvertVec::to_vec",
38                    "alloc::slice::<impl [T]>::to_vec_in::ConvertVec::to_vec",
39                ],
40            )
41    })
42}
43
44pub fn is_global_alloc_shim(tcx: TyCtxt<'_>, callee: Option<DefId>) -> bool {
45    callee.is_some_and(|did| {
46        has_no_mir(tcx, did)
47            && matches_path(
48                tcx,
49                did,
50                &["alloc::alloc::__rust_alloc", "std::alloc::__rust_alloc"],
51            )
52    })
53}
54
55pub fn is_alloc_shim_marker(tcx: TyCtxt<'_>, callee: Option<DefId>) -> bool {
56    callee.is_some_and(|did| {
57        has_no_mir(tcx, did)
58            && matches_path(
59                tcx,
60                did,
61                &[
62                    "alloc::alloc::__rust_no_alloc_shim_is_unstable_v2",
63                    "std::alloc::__rust_no_alloc_shim_is_unstable_v2",
64                ],
65            )
66    })
67}
68
69pub fn is_global_alloc_zeroed_shim(tcx: TyCtxt<'_>, callee: Option<DefId>) -> bool {
70    callee.is_some_and(|did| {
71        has_no_mir(tcx, did)
72            && matches_path(
73                tcx,
74                did,
75                &[
76                    "alloc::alloc::__rust_alloc_zeroed",
77                    "std::alloc::__rust_alloc_zeroed",
78                ],
79            )
80    })
81}