rapx/analysis/api_dependency/
utils.rs1#![allow(warnings, unused)]
2use super::fuzzable;
3#[cfg(not(rapx_has_skip_norm_wip))]
4use crate::compat::SkipNormWip;
5
6#[cfg(not(rapx_ge_100))]
7use rustc_hir::LangItem;
8#[cfg(rapx_ge_100)]
9use rustc_hir::attrs::lang_items::LangItem;
10use rustc_hir::def_id::DefId;
11use rustc_middle::ty::{self, FnSig, GenericArgKind, GenericArgsRef, Ty, TyCtxt, TyKind};
12use rustc_span::sym;
13
14pub fn is_def_id_public(fn_def_id: impl Into<DefId>, tcx: TyCtxt<'_>) -> bool {
15 let fn_def_id: DefId = fn_def_id.into();
16 let local_id = fn_def_id.expect_local();
17 rap_trace!(
18 "vis: {:?} (path: {}) => {:?}",
19 fn_def_id,
20 tcx.def_path_str(fn_def_id),
21 tcx.effective_visibilities(()).effective_vis(local_id)
22 );
23
24 tcx.effective_visibilities(()).is_directly_public(local_id)
25 || tcx.effective_visibilities(()).is_exported(local_id)
26}
27
28pub fn is_fuzzable_ty<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
29 rap_trace!("check fuzzable ty: {}.", ty);
30 let is_fuzzable = fuzzable::is_fuzzable_ty(ty, tcx, 0);
31 rap_trace!("is_fuzzable({}) = {}.", ty, is_fuzzable);
32 is_fuzzable
33}
34
35pub fn is_fuzzable_api<'tcx>(fn_did: DefId, args: GenericArgsRef<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
36 let fn_sig = fn_sig_with_generic_args(fn_did, args, tcx);
37 fn_sig
38 .inputs()
39 .iter()
40 .copied()
41 .all(|ty| is_fuzzable_ty(ty, tcx))
42}
43
44pub fn fn_sig_with_generic_args<'tcx>(
45 fn_did: DefId,
46 args: &[ty::GenericArg<'tcx>],
47 tcx: TyCtxt<'tcx>,
48) -> FnSig<'tcx> {
49 let early_fn_sig = tcx.fn_sig(fn_did);
50 let binder_fn_sig = early_fn_sig.instantiate(tcx, args);
51 #[cfg(rapx_ge_99)]
52 let binder_fn_sig = binder_fn_sig.skip_norm_wip();
53 let result = tcx.liberate_late_bound_regions(fn_did, binder_fn_sig);
54 result
55}
56
57pub fn fn_requires_monomorphization<'tcx>(fn_did: DefId, tcx: TyCtxt<'_>) -> bool {
58 tcx.generics_of(fn_did).requires_monomorphization(tcx)
59}
60
61pub fn is_ty_eq<'tcx>(ty1: Ty<'tcx>, ty2: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
62 let ty1 = tcx.erase_and_anonymize_regions(ty1);
63 let ty2 = tcx.erase_and_anonymize_regions(ty2);
64 return ty1 == ty2;
65}
66
67pub fn ty_complexity<'tcx>(ty: Ty<'tcx>) -> usize {
68 match ty.kind() {
69 TyKind::Ref(_, inner_ty, _) | TyKind::Array(inner_ty, _) | TyKind::Slice(inner_ty) => {
71 ty_complexity(*inner_ty) + 1
72 }
73
74 TyKind::Tuple(tys) => tys.iter().fold(0, |ans, ty| ans.max(ty_complexity(ty))) + 1,
76
77 TyKind::Adt(_, args) => {
79 args.iter().fold(0, |ans, arg| {
80 if let GenericArgKind::Type(ty) = arg.kind() {
81 ans.max(ty_complexity(ty))
82 } else {
83 ans
84 }
85 }) + 1
86 }
87
88 _ => 1,
90 }
91}
92
93pub fn is_ty_unstable<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
94 ty == tcx.types.f16
95}