2019-10-25 09:27:36 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
|
2020-10-21 12:24:35 +00:00
|
|
|
use std::ops::ControlFlow;
|
2019-10-25 09:27:36 +00:00
|
|
|
|
|
|
|
/// This method traverses the structure of `ty`, trying to find an
|
2020-03-24 18:52:22 +00:00
|
|
|
/// instance of an ADT (i.e. struct or enum) that doesn't implement
|
|
|
|
/// the structural-match traits, or a generic type parameter
|
|
|
|
/// (which cannot be determined to be structural-match).
|
2019-10-25 09:27:36 +00:00
|
|
|
///
|
|
|
|
/// The "structure of a type" includes all components that would be
|
|
|
|
/// considered when doing a pattern match on a constant of that
|
|
|
|
/// type.
|
|
|
|
///
|
|
|
|
/// * This means this method descends into fields of structs/enums,
|
|
|
|
/// and also descends into the inner type `T` of `&T` and `&mut T`
|
|
|
|
///
|
|
|
|
/// * The traversal doesn't dereference unsafe pointers (`*const T`,
|
|
|
|
/// `*mut T`), and it does not visit the type arguments of an
|
|
|
|
/// instantiated generic like `PhantomData<T>`.
|
|
|
|
///
|
|
|
|
/// The reason we do this search is Rust currently require all ADTs
|
2020-03-24 18:52:22 +00:00
|
|
|
/// reachable from a constant's type to implement the
|
|
|
|
/// structural-match traits, which essentially say that
|
2019-10-25 09:27:36 +00:00
|
|
|
/// the implementation of `PartialEq::eq` behaves *equivalently* to a
|
|
|
|
/// comparison against the unfolded structure.
|
|
|
|
///
|
|
|
|
/// For more background on why Rust has this requirement, and issues
|
|
|
|
/// that arose when the requirement was not enforced completely, see
|
|
|
|
/// Rust RFC 1445, rust-lang/rust#61188, and rust-lang/rust#62307.
|
|
|
|
pub fn search_for_structural_match_violation<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
2022-07-23 23:10:08 +00:00
|
|
|
) -> Option<Ty<'tcx>> {
|
2024-02-14 04:17:15 +00:00
|
|
|
ty.visit_with(&mut Search { tcx, seen: FxHashSet::default() }).break_value()
|
2019-10-17 08:54:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 12:47:04 +00:00
|
|
|
/// This implements the traversal over the structure of a given type to try to
|
|
|
|
/// find instances of ADTs (specifically structs or enums) that do not implement
|
2023-09-26 07:39:41 +00:00
|
|
|
/// `StructuralPartialEq`.
|
2022-06-29 00:43:47 +00:00
|
|
|
struct Search<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-10-25 09:27:36 +00:00
|
|
|
|
2019-10-25 12:47:04 +00:00
|
|
|
/// Tracks ADTs previously encountered during search, so that
|
|
|
|
/// we will not recur on them again.
|
2019-10-17 08:54:37 +00:00
|
|
|
seen: FxHashSet<hir::def_id::DefId>,
|
|
|
|
}
|
|
|
|
|
2022-06-29 00:43:47 +00:00
|
|
|
impl<'tcx> Search<'tcx> {
|
2019-10-17 08:54:37 +00:00
|
|
|
fn type_marked_structural(&self, adt_ty: Ty<'tcx>) -> bool {
|
2022-06-29 00:43:47 +00:00
|
|
|
adt_ty.is_structural_eq_shallow(self.tcx)
|
2019-10-17 08:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 19:38:07 +00:00
|
|
|
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for Search<'tcx> {
|
2024-02-24 22:22:28 +00:00
|
|
|
type Result = ControlFlow<Ty<'tcx>>;
|
2020-11-05 18:11:42 +00:00
|
|
|
|
2024-02-24 22:22:28 +00:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
|
2019-10-17 08:54:37 +00:00
|
|
|
debug!("Search visiting ty: {:?}", ty);
|
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
let (adt_def, args) = match *ty.kind() {
|
|
|
|
ty::Adt(adt_def, args) => (adt_def, args),
|
2019-10-17 08:54:37 +00:00
|
|
|
ty::Param(_) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2019-10-25 09:27:36 +00:00
|
|
|
}
|
2020-04-11 19:02:49 +00:00
|
|
|
ty::Dynamic(..) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2020-04-11 19:02:49 +00:00
|
|
|
}
|
2020-05-12 20:18:55 +00:00
|
|
|
ty::Foreign(_) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2020-05-12 20:18:55 +00:00
|
|
|
}
|
2022-11-27 17:52:17 +00:00
|
|
|
ty::Alias(..) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2020-05-12 22:40:28 +00:00
|
|
|
}
|
2021-10-18 00:00:00 +00:00
|
|
|
ty::Closure(..) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2021-10-18 00:00:00 +00:00
|
|
|
}
|
2024-01-24 18:01:56 +00:00
|
|
|
ty::CoroutineClosure(..) => {
|
|
|
|
return ControlFlow::Break(ty);
|
|
|
|
}
|
2023-10-19 16:06:43 +00:00
|
|
|
ty::Coroutine(..) | ty::CoroutineWitness(..) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2020-05-12 22:40:28 +00:00
|
|
|
}
|
2022-07-23 20:09:52 +00:00
|
|
|
ty::FnDef(..) => {
|
2020-05-12 20:18:55 +00:00
|
|
|
// Types of formals and return in `fn(_) -> _` are also irrelevant;
|
2019-10-25 12:47:04 +00:00
|
|
|
// so we do not recur into them via `super_visit_with`
|
2023-01-18 07:17:13 +00:00
|
|
|
return ControlFlow::Continue(());
|
2019-10-17 08:54:37 +00:00
|
|
|
}
|
|
|
|
ty::Array(_, n)
|
2023-02-14 08:51:19 +00:00
|
|
|
if { n.try_eval_target_usize(self.tcx, ty::ParamEnv::reveal_all()) == Some(0) } =>
|
2019-10-17 08:54:37 +00:00
|
|
|
{
|
|
|
|
// rust-lang/rust#62336: ignore type of contents
|
|
|
|
// for empty array.
|
2023-01-18 07:17:13 +00:00
|
|
|
return ControlFlow::Continue(());
|
2019-10-25 09:27:36 +00:00
|
|
|
}
|
2022-07-04 23:44:41 +00:00
|
|
|
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Str | ty::Never => {
|
2020-05-12 20:18:55 +00:00
|
|
|
// These primitive types are always structural match.
|
|
|
|
//
|
|
|
|
// `Never` is kind of special here, but as it is not inhabitable, this should be fine.
|
2023-01-18 07:17:13 +00:00
|
|
|
return ControlFlow::Continue(());
|
2020-05-12 20:18:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 20:09:52 +00:00
|
|
|
ty::FnPtr(..) => {
|
2023-05-17 18:51:45 +00:00
|
|
|
return ControlFlow::Continue(());
|
2022-07-23 20:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::RawPtr(..) => {
|
2023-05-17 18:51:45 +00:00
|
|
|
// structural-match ignores substructure of
|
|
|
|
// `*const _`/`*mut _`, so skip `super_visit_with`.
|
|
|
|
//
|
|
|
|
// For example, if you have:
|
|
|
|
// ```
|
|
|
|
// struct NonStructural;
|
|
|
|
// #[derive(PartialEq, Eq)]
|
|
|
|
// struct T(*const NonStructural);
|
|
|
|
// const C: T = T(std::ptr::null());
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Even though `NonStructural` does not implement `PartialEq`,
|
|
|
|
// structural equality on `T` does not recur into the raw
|
|
|
|
// pointer. Therefore, one can still use `C` in a pattern.
|
|
|
|
return ControlFlow::Continue(());
|
2022-07-23 20:09:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 23:44:41 +00:00
|
|
|
ty::Float(_) => {
|
2023-05-17 18:51:45 +00:00
|
|
|
return ControlFlow::Continue(());
|
2022-07-04 23:44:41 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 13:57:36 +00:00
|
|
|
ty::Pat(..) | ty::Array(..) | ty::Slice(_) | ty::Ref(..) | ty::Tuple(..) => {
|
2020-05-13 09:52:22 +00:00
|
|
|
// First check all contained types and then tell the caller to continue searching.
|
2020-11-05 18:11:42 +00:00
|
|
|
return ty.super_visit_with(self);
|
2019-10-17 08:54:37 +00:00
|
|
|
}
|
2021-10-18 00:00:00 +00:00
|
|
|
ty::Infer(_) | ty::Placeholder(_) | ty::Bound(..) => {
|
2020-05-12 21:35:29 +00:00
|
|
|
bug!("unexpected type during structural-match checking: {:?}", ty);
|
|
|
|
}
|
2020-05-06 04:02:09 +00:00
|
|
|
ty::Error(_) => {
|
2020-05-12 21:35:29 +00:00
|
|
|
// We still want to check other types after encountering an error,
|
|
|
|
// as this may still emit relevant errors.
|
2023-01-18 07:17:13 +00:00
|
|
|
return ControlFlow::Continue(());
|
2020-05-12 21:35:29 +00:00
|
|
|
}
|
2019-10-17 08:54:37 +00:00
|
|
|
};
|
2019-10-25 09:27:36 +00:00
|
|
|
|
2022-03-04 20:28:41 +00:00
|
|
|
if !self.seen.insert(adt_def.did()) {
|
2019-10-17 08:54:37 +00:00
|
|
|
debug!("Search already seen adt_def: {:?}", adt_def);
|
2023-01-18 07:17:13 +00:00
|
|
|
return ControlFlow::Continue(());
|
2019-10-25 09:27:36 +00:00
|
|
|
}
|
2019-10-17 08:54:37 +00:00
|
|
|
|
|
|
|
if !self.type_marked_structural(ty) {
|
|
|
|
debug!("Search found ty: {:?}", ty);
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2019-10-17 08:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// structural-match does not care about the
|
|
|
|
// instantiation of the generics in an ADT (it
|
|
|
|
// instead looks directly at its fields outside
|
|
|
|
// this match), so we skip super_visit_with.
|
|
|
|
//
|
2023-07-11 21:35:29 +00:00
|
|
|
// (Must not recur on args for `PhantomData<T>` cf
|
2019-10-17 08:54:37 +00:00
|
|
|
// rust-lang/rust#55028 and rust-lang/rust#55837; but also
|
2023-07-11 21:35:29 +00:00
|
|
|
// want to skip args when only uses of generic are
|
2019-10-17 08:54:37 +00:00
|
|
|
// behind unsafe pointers `*const T`/`*mut T`.)
|
|
|
|
|
|
|
|
// even though we skip super_visit_with, we must recur on
|
|
|
|
// fields of ADT.
|
2022-06-29 00:43:47 +00:00
|
|
|
let tcx = self.tcx;
|
2023-07-11 21:35:29 +00:00
|
|
|
adt_def.all_fields().map(|field| field.ty(tcx, args)).try_for_each(|field_ty| {
|
2022-06-29 00:43:47 +00:00
|
|
|
let ty = self.tcx.normalize_erasing_regions(ty::ParamEnv::empty(), field_ty);
|
2020-06-01 22:23:47 +00:00
|
|
|
debug!("structural-match ADT: field_ty={:?}, ty={:?}", field_ty, ty);
|
2020-11-05 18:11:42 +00:00
|
|
|
ty.visit_with(self)
|
|
|
|
})
|
2019-10-25 09:27:36 +00:00
|
|
|
}
|
|
|
|
}
|