Skip to main content

rapx/def_ids/
diagnostic_api.rs

1//! Standard-library API identities registered as rustc diagnostic items.
2//!
3//! A diagnostic item is a definition marked with `#[rustc_diagnostic_item = "..."]`
4//! in the standard library. rustc records its marker name and `DefId`, allowing
5//! callers to recognize the definition without matching its module path.
6//!
7//! # Agent Rules
8//!
9//! Keep this module limited to identifying diagnostic items by `DefId`.
10//! Do not depend on other RAPx modules.
11
12use rustc_hir::def_id::DefId;
13use rustc_middle::ty::TyCtxt;
14use rustc_span::Symbol;
15
16/// Whether `did` is `mem::size_of` or `mem::align_of`.
17pub fn is_mem_size_or_align_of(tcx: TyCtxt<'_>, did: DefId) -> bool {
18    tcx.is_diagnostic_item(Symbol::intern("mem_size_of"), did)
19        || tcx.is_diagnostic_item(Symbol::intern("mem_align_of"), did)
20}
21
22pub fn is_ptr_read(tcx: TyCtxt<'_>, did: DefId) -> bool {
23    tcx.get_diagnostic_name(did).is_some_and(|name| {
24        matches!(name.as_str(), "ptr_read" | "ptr_read_unaligned" | "ptr_read_volatile")
25    })
26}
27
28pub fn is_ptr_write(tcx: TyCtxt<'_>, did: DefId) -> bool {
29    tcx.get_diagnostic_name(did).is_some_and(|name| {
30        matches!(name.as_str(), "ptr_write" | "ptr_write_unaligned" | "ptr_write_volatile" | "ptr_write_bytes")
31    })
32}
33
34pub fn is_ptr_write_to_uninit(tcx: TyCtxt<'_>, did: DefId) -> bool {
35    tcx.get_diagnostic_name(did)
36        .is_some_and(|name| matches!(name.as_str(), "ptr_write" | "ptr_write_bytes"))
37}
38
39pub fn is_ptr_copy(tcx: TyCtxt<'_>, did: DefId) -> bool {
40    tcx.get_diagnostic_name(did)
41        .is_some_and(|name| matches!(name.as_str(), "ptr_copy" | "ptr_copy_nonoverlapping"))
42}
43
44pub fn is_drop(tcx: TyCtxt<'_>, did: DefId) -> bool {
45    tcx.is_diagnostic_item(Symbol::intern("mem_drop"), did)
46}
47
48pub fn is_drop_in_place(tcx: TyCtxt<'_>, did: DefId) -> bool {
49    tcx.is_diagnostic_item(Symbol::intern("ptr_drop_in_place"), did)
50}