2021-03-25 13:29:11 -05:00
|
|
|
//! Util methods for [`rustc_middle::ty`]
|
|
|
|
|
|
|
|
#![allow(clippy::module_name_repetitions)]
|
|
|
|
|
|
|
|
use rustc_ast::ast::Mutability;
|
2021-05-20 05:30:31 -05:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2021-03-25 13:29:11 -05:00
|
|
|
use rustc_hir as hir;
|
2022-01-27 08:12:45 -06:00
|
|
|
use rustc_hir::def::{CtorKind, DefKind, Res};
|
2021-03-25 13:29:11 -05:00
|
|
|
use rustc_hir::def_id::DefId;
|
2022-01-27 08:12:45 -06:00
|
|
|
use rustc_hir::{Expr, TyKind, Unsafety};
|
2021-03-25 13:29:11 -05:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
|
|
|
use rustc_lint::LateContext;
|
2022-02-18 08:10:48 -06:00
|
|
|
use rustc_middle::mir::interpret::{ConstValue, Scalar};
|
2022-01-27 08:12:45 -06:00
|
|
|
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
|
|
|
|
use rustc_middle::ty::{
|
2022-02-18 08:10:48 -06:00
|
|
|
self, AdtDef, Binder, FnSig, IntTy, Predicate, PredicateKind, Ty, TyCtxt, TypeFoldable, UintTy, VariantDiscr,
|
2022-01-27 08:12:45 -06:00
|
|
|
};
|
2021-12-06 05:33:31 -06:00
|
|
|
use rustc_span::symbol::Ident;
|
|
|
|
use rustc_span::{sym, Span, Symbol, DUMMY_SP};
|
2022-02-18 08:10:48 -06:00
|
|
|
use rustc_target::abi::{Size, VariantIdx};
|
2021-07-06 04:38:15 -05:00
|
|
|
use rustc_trait_selection::infer::InferCtxtExt;
|
2021-07-15 03:44:10 -05:00
|
|
|
use rustc_trait_selection::traits::query::normalize::AtExt;
|
2021-12-06 05:33:31 -06:00
|
|
|
use std::iter;
|
2021-03-25 13:29:11 -05:00
|
|
|
|
2022-01-06 09:35:25 -06:00
|
|
|
use crate::{match_def_path, must_use_attr, path_res};
|
2021-03-25 13:29:11 -05:00
|
|
|
|
2021-12-06 05:33:31 -06:00
|
|
|
// Checks if the given type implements copy.
|
2021-03-25 13:29:11 -05:00
|
|
|
pub fn is_copy<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
ty.is_copy_modulo_regions(cx.tcx.at(DUMMY_SP), cx.param_env)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether a type can be partially moved.
|
2022-01-13 06:18:19 -06:00
|
|
|
pub fn can_partially_move_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
2021-03-25 13:29:11 -05:00
|
|
|
if has_drop(cx, ty) || is_copy(cx, ty) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Param(_) => false,
|
|
|
|
ty::Adt(def, subs) => def.all_fields().any(|f| !is_copy(cx, f.ty(cx.tcx, subs))),
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Walks into `ty` and returns `true` if any inner type is the same as `other_ty`
|
2022-01-11 21:19:52 -06:00
|
|
|
pub fn contains_ty(ty: Ty<'_>, other_ty: Ty<'_>) -> bool {
|
|
|
|
ty.walk().any(|inner| match inner.unpack() {
|
2022-01-25 01:42:52 -06:00
|
|
|
GenericArgKind::Type(inner_ty) => other_ty == inner_ty,
|
2021-03-25 13:29:11 -05:00
|
|
|
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Walks into `ty` and returns `true` if any inner type is an instance of the given adt
|
|
|
|
/// constructor.
|
2022-01-11 21:19:52 -06:00
|
|
|
pub fn contains_adt_constructor(ty: Ty<'_>, adt: &AdtDef) -> bool {
|
|
|
|
ty.walk().any(|inner| match inner.unpack() {
|
2021-03-25 13:29:11 -05:00
|
|
|
GenericArgKind::Type(inner_ty) => inner_ty.ty_adt_def() == Some(adt),
|
|
|
|
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-22 04:31:13 -05:00
|
|
|
/// Resolves `<T as Iterator>::Item` for `T`
|
|
|
|
/// Do not invoke without first verifying that the type implements `Iterator`
|
|
|
|
pub fn get_iterator_item_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
|
|
|
cx.tcx
|
|
|
|
.get_diagnostic_item(sym::Iterator)
|
2021-12-17 06:40:22 -06:00
|
|
|
.and_then(|iter_did| get_associated_type(cx, ty, iter_did, "Item"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the associated type `name` for `ty` as an implementation of `trait_id`.
|
|
|
|
/// Do not invoke without first verifying that the type implements the trait.
|
|
|
|
pub fn get_associated_type<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
trait_id: DefId,
|
|
|
|
name: &str,
|
|
|
|
) -> Option<Ty<'tcx>> {
|
|
|
|
cx.tcx
|
|
|
|
.associated_items(trait_id)
|
|
|
|
.find_by_name_and_kind(cx.tcx, Ident::from_str(name), ty::AssocKind::Type, trait_id)
|
2021-04-22 04:31:13 -05:00
|
|
|
.map(|assoc| {
|
|
|
|
let proj = cx.tcx.mk_projection(assoc.def_id, cx.tcx.mk_substs_trait(ty, &[]));
|
|
|
|
cx.tcx.normalize_erasing_regions(cx.param_env, proj)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
/// Returns true if ty has `iter` or `iter_mut` methods
|
|
|
|
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<Symbol> {
|
|
|
|
// FIXME: instead of this hard-coded list, we should check if `<adt>::iter`
|
|
|
|
// exists and has the desired signature. Unfortunately FnCtxt is not exported
|
|
|
|
// so we can't use its `lookup_method` method.
|
|
|
|
let into_iter_collections: &[Symbol] = &[
|
2021-10-02 18:51:01 -05:00
|
|
|
sym::Vec,
|
|
|
|
sym::Option,
|
|
|
|
sym::Result,
|
2021-03-25 13:29:11 -05:00
|
|
|
sym::BTreeMap,
|
|
|
|
sym::BTreeSet,
|
2021-10-02 18:51:01 -05:00
|
|
|
sym::VecDeque,
|
2021-03-25 13:29:11 -05:00
|
|
|
sym::LinkedList,
|
|
|
|
sym::BinaryHeap,
|
2021-10-02 18:51:01 -05:00
|
|
|
sym::HashSet,
|
|
|
|
sym::HashMap,
|
2021-03-25 13:29:11 -05:00
|
|
|
sym::PathBuf,
|
|
|
|
sym::Path,
|
|
|
|
sym::Receiver,
|
|
|
|
];
|
|
|
|
|
|
|
|
let ty_to_check = match probably_ref_ty.kind() {
|
|
|
|
ty::Ref(_, ty_to_check, _) => ty_to_check,
|
|
|
|
_ => probably_ref_ty,
|
|
|
|
};
|
|
|
|
|
|
|
|
let def_id = match ty_to_check.kind() {
|
|
|
|
ty::Array(..) => return Some(sym::array),
|
|
|
|
ty::Slice(..) => return Some(sym::slice),
|
|
|
|
ty::Adt(adt, _) => adt.did,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
for &name in into_iter_collections {
|
|
|
|
if cx.tcx.is_diagnostic_item(name, def_id) {
|
|
|
|
return Some(cx.tcx.item_name(def_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether a type implements a trait.
|
2021-07-06 04:38:15 -05:00
|
|
|
/// The function returns false in case the type contains an inference variable.
|
2021-12-06 05:33:31 -06:00
|
|
|
///
|
|
|
|
/// See:
|
|
|
|
/// * [`get_trait_def_id`](super::get_trait_def_id) to get a trait [`DefId`].
|
|
|
|
/// * [Common tools for writing lints] for an example how to use this function and other options.
|
|
|
|
///
|
|
|
|
/// [Common tools for writing lints]: https://github.com/rust-lang/rust-clippy/blob/master/doc/common_tools_writing_lints.md#checking-if-a-type-implements-a-specific-trait
|
2021-03-25 13:29:11 -05:00
|
|
|
pub fn implements_trait<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
trait_id: DefId,
|
|
|
|
ty_params: &[GenericArg<'tcx>],
|
|
|
|
) -> bool {
|
2021-07-06 04:38:15 -05:00
|
|
|
// Clippy shouldn't have infer types
|
|
|
|
assert!(!ty.needs_infer());
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
let ty = cx.tcx.erase_regions(ty);
|
|
|
|
if ty.has_escaping_bound_vars() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let ty_params = cx.tcx.mk_substs(ty_params.iter());
|
2021-07-15 03:44:10 -05:00
|
|
|
cx.tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
infcx
|
|
|
|
.type_implements_trait(trait_id, ty, ty_params, cx.param_env)
|
|
|
|
.must_apply_modulo_regions()
|
|
|
|
})
|
2021-03-25 13:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether this type implements `Drop`.
|
|
|
|
pub fn has_drop<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
match ty.ty_adt_def() {
|
|
|
|
Some(def) => def.has_dtor(cx.tcx),
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns whether the type has #[must_use] attribute
|
|
|
|
pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
match ty.kind() {
|
2021-06-03 01:41:37 -05:00
|
|
|
ty::Adt(adt, _) => must_use_attr(cx.tcx.get_attrs(adt.did)).is_some(),
|
|
|
|
ty::Foreign(ref did) => must_use_attr(cx.tcx.get_attrs(*did)).is_some(),
|
2021-07-04 11:50:41 -05:00
|
|
|
ty::Slice(ty) | ty::Array(ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) | ty::Ref(_, ty, _) => {
|
2021-03-25 13:29:11 -05:00
|
|
|
// for the Array case we don't need to care for the len == 0 case
|
|
|
|
// because we don't want to lint functions returning empty arrays
|
|
|
|
is_must_use_ty(cx, *ty)
|
2021-07-04 11:50:41 -05:00
|
|
|
},
|
2021-06-03 01:41:37 -05:00
|
|
|
ty::Tuple(substs) => substs.types().any(|ty| is_must_use_ty(cx, ty)),
|
2021-03-25 13:29:11 -05:00
|
|
|
ty::Opaque(ref def_id, _) => {
|
|
|
|
for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) {
|
2021-07-22 08:56:07 -05:00
|
|
|
if let ty::PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() {
|
2021-06-03 01:41:37 -05:00
|
|
|
if must_use_attr(cx.tcx.get_attrs(trait_predicate.trait_ref.def_id)).is_some() {
|
2021-03-25 13:29:11 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
2021-07-04 11:50:41 -05:00
|
|
|
},
|
2021-03-25 13:29:11 -05:00
|
|
|
ty::Dynamic(binder, _) => {
|
|
|
|
for predicate in binder.iter() {
|
|
|
|
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
|
2021-06-03 01:41:37 -05:00
|
|
|
if must_use_attr(cx.tcx.get_attrs(trait_ref.def_id)).is_some() {
|
2021-03-25 13:29:11 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
2021-07-04 11:50:41 -05:00
|
|
|
},
|
2021-03-25 13:29:11 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Per https://doc.rust-lang.org/nightly/nightly-rustc/rustc_trait_selection/infer/at/struct.At.html#method.normalize
|
2021-04-26 13:19:23 -05:00
|
|
|
// this function can be removed once the `normalize` method does not panic when normalization does
|
2021-03-25 13:29:11 -05:00
|
|
|
// not succeed
|
|
|
|
/// Checks if `Ty` is normalizable. This function is useful
|
|
|
|
/// to avoid crashes on `layout_of`.
|
2021-07-04 11:50:41 -05:00
|
|
|
pub fn is_normalizable<'tcx>(cx: &LateContext<'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> bool {
|
2021-05-20 05:30:31 -05:00
|
|
|
is_normalizable_helper(cx, param_env, ty, &mut FxHashMap::default())
|
2021-03-25 13:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_normalizable_helper<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
2021-05-20 05:30:31 -05:00
|
|
|
cache: &mut FxHashMap<Ty<'tcx>, bool>,
|
2021-03-25 13:29:11 -05:00
|
|
|
) -> bool {
|
|
|
|
if let Some(&cached_result) = cache.get(ty) {
|
|
|
|
return cached_result;
|
|
|
|
}
|
|
|
|
// prevent recursive loops, false-negative is better than endless loop leading to stack overflow
|
|
|
|
cache.insert(ty, false);
|
|
|
|
let result = cx.tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
let cause = rustc_middle::traits::ObligationCause::dummy();
|
|
|
|
if infcx.at(&cause, param_env).normalize(ty).is_ok() {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Adt(def, substs) => def.variants.iter().all(|variant| {
|
2021-07-04 11:50:41 -05:00
|
|
|
variant
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.all(|field| is_normalizable_helper(cx, param_env, field.ty(cx.tcx, substs), cache))
|
2021-03-25 13:29:11 -05:00
|
|
|
}),
|
2022-01-11 21:19:52 -06:00
|
|
|
_ => ty.walk().all(|generic_arg| match generic_arg.unpack() {
|
2021-03-25 13:29:11 -05:00
|
|
|
GenericArgKind::Type(inner_ty) if inner_ty != ty => {
|
|
|
|
is_normalizable_helper(cx, param_env, inner_ty, cache)
|
2021-07-04 11:50:41 -05:00
|
|
|
},
|
2021-03-25 13:29:11 -05:00
|
|
|
_ => true, // if inner_ty == ty, we've already checked it
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
cache.insert(ty, result);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2021-10-07 04:21:30 -05:00
|
|
|
/// Returns `true` if the given type is a non aggregate primitive (a `bool` or `char`, any
|
|
|
|
/// integer or floating-point number type). For checking aggregation of primitive types (e.g.
|
|
|
|
/// tuples and slices of primitive type) see `is_recursively_primitive_type`
|
2021-09-08 09:31:47 -05:00
|
|
|
pub fn is_non_aggregate_primitive_type(ty: Ty<'_>) -> bool {
|
|
|
|
matches!(ty.kind(), ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_))
|
|
|
|
}
|
|
|
|
|
2021-10-07 04:21:30 -05:00
|
|
|
/// Returns `true` if the given type is a primitive (a `bool` or `char`, any integer or
|
|
|
|
/// floating-point number type, a `str`, or an array, slice, or tuple of those types).
|
2021-03-25 13:29:11 -05:00
|
|
|
pub fn is_recursively_primitive_type(ty: Ty<'_>) -> bool {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => true,
|
|
|
|
ty::Ref(_, inner, _) if *inner.kind() == ty::Str => true,
|
2021-07-04 11:50:41 -05:00
|
|
|
ty::Array(inner_type, _) | ty::Slice(inner_type) => is_recursively_primitive_type(inner_type),
|
2021-03-25 13:29:11 -05:00
|
|
|
ty::Tuple(inner_types) => inner_types.types().all(is_recursively_primitive_type),
|
|
|
|
_ => false,
|
2021-07-15 03:44:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if the type is a reference equals to a diagnostic item
|
|
|
|
pub fn is_type_ref_to_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Ref(_, ref_ty, _) => match ref_ty.kind() {
|
|
|
|
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did),
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
2021-03-25 13:29:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:33:31 -06:00
|
|
|
/// Checks if the type is equal to a diagnostic item. To check if a type implements a
|
|
|
|
/// trait marked with a diagnostic item use [`implements_trait`].
|
|
|
|
///
|
|
|
|
/// For a further exploitation what diagnostic items are see [diagnostic items] in
|
|
|
|
/// rustc-dev-guide.
|
|
|
|
///
|
|
|
|
/// ---
|
2021-03-25 13:29:11 -05:00
|
|
|
///
|
|
|
|
/// If you change the signature, remember to update the internal lint `MatchTypeOnDiagItem`
|
2021-12-06 05:33:31 -06:00
|
|
|
///
|
|
|
|
/// [Diagnostic Items]: https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html
|
2021-03-25 13:29:11 -05:00
|
|
|
pub fn is_type_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:52:05 -05:00
|
|
|
/// Checks if the type is equal to a lang item.
|
|
|
|
///
|
|
|
|
/// Returns `false` if the `LangItem` is not defined.
|
2021-03-25 13:29:11 -05:00
|
|
|
pub fn is_type_lang_item(cx: &LateContext<'_>, ty: Ty<'_>, lang_item: hir::LangItem) -> bool {
|
|
|
|
match ty.kind() {
|
2021-07-19 04:52:05 -05:00
|
|
|
ty::Adt(adt, _) => cx.tcx.lang_items().require(lang_item).map_or(false, |li| li == adt.did),
|
2021-03-25 13:29:11 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return `true` if the passed `typ` is `isize` or `usize`.
|
|
|
|
pub fn is_isize_or_usize(typ: Ty<'_>) -> bool {
|
|
|
|
matches!(typ.kind(), ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if type is struct, enum or union type with the given def path.
|
|
|
|
///
|
|
|
|
/// If the type is a diagnostic item, use `is_type_diagnostic_item` instead.
|
|
|
|
/// If you change the signature, remember to update the internal lint `MatchTypeOnDiagItem`
|
|
|
|
pub fn match_type(cx: &LateContext<'_>, ty: Ty<'_>, path: &[&str]) -> bool {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Adt(adt, _) => match_def_path(cx, adt.did, path),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Peels off all references on the type. Returns the underlying type and the number of references
|
|
|
|
/// removed.
|
|
|
|
pub fn peel_mid_ty_refs(ty: Ty<'_>) -> (Ty<'_>, usize) {
|
|
|
|
fn peel(ty: Ty<'_>, count: usize) -> (Ty<'_>, usize) {
|
2021-07-04 11:50:41 -05:00
|
|
|
if let ty::Ref(_, ty, _) = ty.kind() {
|
|
|
|
peel(ty, count + 1)
|
|
|
|
} else {
|
|
|
|
(ty, count)
|
|
|
|
}
|
2021-03-25 13:29:11 -05:00
|
|
|
}
|
|
|
|
peel(ty, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Peels off all references on the type.Returns the underlying type, the number of references
|
|
|
|
/// removed, and whether the pointer is ultimately mutable or not.
|
|
|
|
pub fn peel_mid_ty_refs_is_mutable(ty: Ty<'_>) -> (Ty<'_>, usize, Mutability) {
|
|
|
|
fn f(ty: Ty<'_>, count: usize, mutability: Mutability) -> (Ty<'_>, usize, Mutability) {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Ref(_, ty, Mutability::Mut) => f(ty, count + 1, mutability),
|
|
|
|
ty::Ref(_, ty, Mutability::Not) => f(ty, count + 1, Mutability::Not),
|
|
|
|
_ => (ty, count, mutability),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(ty, 0, Mutability::Mut)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the given type is an `unsafe` function.
|
|
|
|
pub fn type_is_unsafe_function<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::FnDef(..) | ty::FnPtr(_) => ty.fn_sig(cx.tcx).unsafety() == Unsafety::Unsafe,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the base type for HIR references and pointers.
|
|
|
|
pub fn walk_ptrs_hir_ty<'tcx>(ty: &'tcx hir::Ty<'tcx>) -> &'tcx hir::Ty<'tcx> {
|
|
|
|
match ty.kind {
|
2021-06-03 01:41:37 -05:00
|
|
|
TyKind::Ptr(ref mut_ty) | TyKind::Rptr(_, ref mut_ty) => walk_ptrs_hir_ty(mut_ty.ty),
|
2021-03-25 13:29:11 -05:00
|
|
|
_ => ty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the base type for references and raw pointers, and count reference
|
|
|
|
/// depth.
|
|
|
|
pub fn walk_ptrs_ty_depth(ty: Ty<'_>) -> (Ty<'_>, usize) {
|
|
|
|
fn inner(ty: Ty<'_>, depth: usize) -> (Ty<'_>, usize) {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Ref(_, ty, _) => inner(ty, depth + 1),
|
|
|
|
_ => (ty, depth),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inner(ty, 0)
|
|
|
|
}
|
2021-05-20 05:30:31 -05:00
|
|
|
|
|
|
|
/// Returns `true` if types `a` and `b` are same types having same `Const` generic args,
|
|
|
|
/// otherwise returns `false`
|
2022-01-13 06:18:19 -06:00
|
|
|
pub fn same_type_and_consts<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
|
2021-05-20 05:30:31 -05:00
|
|
|
match (&a.kind(), &b.kind()) {
|
|
|
|
(&ty::Adt(did_a, substs_a), &ty::Adt(did_b, substs_b)) => {
|
|
|
|
if did_a != did_b {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:50:41 -05:00
|
|
|
substs_a
|
|
|
|
.iter()
|
|
|
|
.zip(substs_b.iter())
|
|
|
|
.all(|(arg_a, arg_b)| match (arg_a.unpack(), arg_b.unpack()) {
|
|
|
|
(GenericArgKind::Const(inner_a), GenericArgKind::Const(inner_b)) => inner_a == inner_b,
|
2021-05-20 05:30:31 -05:00
|
|
|
(GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => {
|
|
|
|
same_type_and_consts(type_a, type_b)
|
2021-07-04 11:50:41 -05:00
|
|
|
},
|
2021-05-20 05:30:31 -05:00
|
|
|
_ => true,
|
2021-07-04 11:50:41 -05:00
|
|
|
})
|
|
|
|
},
|
2021-05-20 05:30:31 -05:00
|
|
|
_ => a == b,
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 06:11:36 -05:00
|
|
|
|
|
|
|
/// Checks if a given type looks safe to be uninitialized.
|
|
|
|
pub fn is_uninit_value_valid_for_ty(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Array(component, _) => is_uninit_value_valid_for_ty(cx, component),
|
|
|
|
ty::Tuple(types) => types.types().all(|ty| is_uninit_value_valid_for_ty(cx, ty)),
|
|
|
|
ty::Adt(adt, _) => cx.tcx.lang_items().maybe_uninit() == Some(adt.did),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 05:33:31 -06:00
|
|
|
|
|
|
|
/// Gets an iterator over all predicates which apply to the given item.
|
|
|
|
pub fn all_predicates_of(tcx: TyCtxt<'_>, id: DefId) -> impl Iterator<Item = &(Predicate<'_>, Span)> {
|
|
|
|
let mut next_id = Some(id);
|
|
|
|
iter::from_fn(move || {
|
|
|
|
next_id.take().map(|id| {
|
|
|
|
let preds = tcx.predicates_of(id);
|
|
|
|
next_id = preds.parent;
|
|
|
|
preds.predicates.iter()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
}
|
2022-01-27 08:12:45 -06:00
|
|
|
|
|
|
|
/// A signature for a function like type.
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum ExprFnSig<'tcx> {
|
|
|
|
Sig(Binder<'tcx, FnSig<'tcx>>),
|
|
|
|
Closure(Binder<'tcx, FnSig<'tcx>>),
|
|
|
|
Trait(Binder<'tcx, Ty<'tcx>>, Option<Binder<'tcx, Ty<'tcx>>>),
|
|
|
|
}
|
|
|
|
impl<'tcx> ExprFnSig<'tcx> {
|
|
|
|
/// Gets the argument type at the given offset.
|
|
|
|
pub fn input(self, i: usize) -> Binder<'tcx, Ty<'tcx>> {
|
|
|
|
match self {
|
|
|
|
Self::Sig(sig) => sig.input(i),
|
|
|
|
Self::Closure(sig) => sig.input(0).map_bound(|ty| ty.tuple_element_ty(i).unwrap()),
|
|
|
|
Self::Trait(inputs, _) => inputs.map_bound(|ty| ty.tuple_element_ty(i).unwrap()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the result type, if one could be found. Note that the result type of a trait may not be
|
|
|
|
/// specified.
|
|
|
|
pub fn output(self) -> Option<Binder<'tcx, Ty<'tcx>>> {
|
|
|
|
match self {
|
|
|
|
Self::Sig(sig) | Self::Closure(sig) => Some(sig.output()),
|
|
|
|
Self::Trait(_, output) => output,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the expression is function like, get the signature for it.
|
|
|
|
pub fn expr_sig<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option<ExprFnSig<'tcx>> {
|
2022-01-06 09:35:25 -06:00
|
|
|
if let Res::Def(DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::AssocFn, id) = path_res(cx, expr) {
|
2022-01-27 08:12:45 -06:00
|
|
|
Some(ExprFnSig::Sig(cx.tcx.fn_sig(id)))
|
|
|
|
} else {
|
|
|
|
let ty = cx.typeck_results().expr_ty_adjusted(expr).peel_refs();
|
|
|
|
match *ty.kind() {
|
|
|
|
ty::Closure(_, subs) => Some(ExprFnSig::Closure(subs.as_closure().sig())),
|
|
|
|
ty::FnDef(id, subs) => Some(ExprFnSig::Sig(cx.tcx.fn_sig(id).subst(cx.tcx, subs))),
|
|
|
|
ty::FnPtr(sig) => Some(ExprFnSig::Sig(sig)),
|
|
|
|
ty::Dynamic(bounds, _) => {
|
|
|
|
let lang_items = cx.tcx.lang_items();
|
|
|
|
match bounds.principal() {
|
|
|
|
Some(bound)
|
|
|
|
if Some(bound.def_id()) == lang_items.fn_trait()
|
|
|
|
|| Some(bound.def_id()) == lang_items.fn_once_trait()
|
|
|
|
|| Some(bound.def_id()) == lang_items.fn_mut_trait() =>
|
|
|
|
{
|
|
|
|
let output = bounds
|
|
|
|
.projection_bounds()
|
|
|
|
.find(|p| lang_items.fn_once_output().map_or(false, |id| id == p.item_def_id()))
|
|
|
|
.map(|p| p.map_bound(|p| p.term.ty().expect("return type was a const")));
|
|
|
|
Some(ExprFnSig::Trait(bound.map_bound(|b| b.substs.type_at(0)), output))
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ty::Param(_) | ty::Projection(..) => {
|
|
|
|
let mut inputs = None;
|
|
|
|
let mut output = None;
|
|
|
|
let lang_items = cx.tcx.lang_items();
|
|
|
|
|
|
|
|
for (pred, _) in all_predicates_of(cx.tcx, cx.typeck_results().hir_owner.to_def_id()) {
|
|
|
|
let mut is_input = false;
|
|
|
|
if let Some(ty) = pred
|
|
|
|
.kind()
|
|
|
|
.map_bound(|pred| match pred {
|
|
|
|
PredicateKind::Trait(p)
|
|
|
|
if (lang_items.fn_trait() == Some(p.def_id())
|
|
|
|
|| lang_items.fn_mut_trait() == Some(p.def_id())
|
|
|
|
|| lang_items.fn_once_trait() == Some(p.def_id()))
|
|
|
|
&& p.self_ty() == ty =>
|
|
|
|
{
|
|
|
|
is_input = true;
|
|
|
|
Some(p.trait_ref.substs.type_at(1))
|
|
|
|
},
|
|
|
|
PredicateKind::Projection(p)
|
|
|
|
if Some(p.projection_ty.item_def_id) == lang_items.fn_once_output()
|
|
|
|
&& p.projection_ty.self_ty() == ty =>
|
|
|
|
{
|
|
|
|
is_input = false;
|
|
|
|
p.term.ty()
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
{
|
|
|
|
if is_input && inputs.is_none() {
|
|
|
|
inputs = Some(ty);
|
|
|
|
} else if !is_input && output.is_none() {
|
|
|
|
output = Some(ty);
|
|
|
|
} else {
|
|
|
|
// Multiple different fn trait impls. Is this even allowed?
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inputs.map(|ty| ExprFnSig::Trait(ty, output))
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 08:10:48 -06:00
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum EnumValue {
|
|
|
|
Unsigned(u128),
|
|
|
|
Signed(i128),
|
|
|
|
}
|
|
|
|
impl core::ops::Add<u32> for EnumValue {
|
|
|
|
type Output = Self;
|
|
|
|
fn add(self, n: u32) -> Self::Output {
|
|
|
|
match self {
|
|
|
|
Self::Unsigned(x) => Self::Unsigned(x + u128::from(n)),
|
|
|
|
Self::Signed(x) => Self::Signed(x + i128::from(n)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to read the given constant as though it were an an enum value.
|
|
|
|
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
|
|
|
|
pub fn read_explicit_enum_value(tcx: TyCtxt<'_>, id: DefId) -> Option<EnumValue> {
|
|
|
|
if let Ok(ConstValue::Scalar(Scalar::Int(value))) = tcx.const_eval_poly(id) {
|
|
|
|
match tcx.type_of(id).kind() {
|
|
|
|
ty::Int(_) => Some(EnumValue::Signed(match value.size().bytes() {
|
|
|
|
1 => i128::from(value.assert_bits(Size::from_bytes(1)) as u8 as i8),
|
|
|
|
2 => i128::from(value.assert_bits(Size::from_bytes(2)) as u16 as i16),
|
|
|
|
4 => i128::from(value.assert_bits(Size::from_bytes(4)) as u32 as i32),
|
|
|
|
8 => i128::from(value.assert_bits(Size::from_bytes(8)) as u64 as i64),
|
|
|
|
16 => value.assert_bits(Size::from_bytes(16)) as i128,
|
|
|
|
_ => return None,
|
|
|
|
})),
|
|
|
|
ty::Uint(_) => Some(EnumValue::Unsigned(match value.size().bytes() {
|
|
|
|
1 => value.assert_bits(Size::from_bytes(1)),
|
|
|
|
2 => value.assert_bits(Size::from_bytes(2)),
|
|
|
|
4 => value.assert_bits(Size::from_bytes(4)),
|
|
|
|
8 => value.assert_bits(Size::from_bytes(8)),
|
|
|
|
16 => value.assert_bits(Size::from_bytes(16)),
|
|
|
|
_ => return None,
|
|
|
|
})),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the value of the given variant.
|
|
|
|
pub fn get_discriminant_value(tcx: TyCtxt<'_>, adt: &'_ AdtDef, i: VariantIdx) -> EnumValue {
|
|
|
|
let variant = &adt.variants[i];
|
|
|
|
match variant.discr {
|
|
|
|
VariantDiscr::Explicit(id) => read_explicit_enum_value(tcx, id).unwrap(),
|
|
|
|
VariantDiscr::Relative(x) => match adt.variants[(i.as_usize() - x as usize).into()].discr {
|
|
|
|
VariantDiscr::Explicit(id) => read_explicit_enum_value(tcx, id).unwrap() + x,
|
|
|
|
VariantDiscr::Relative(_) => EnumValue::Unsigned(x.into()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|