2019-10-25 11:27:36 +02:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2022-07-23 23:10:08 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2020-10-21 14:24:35 +02:00
|
|
|
use std::ops::ControlFlow;
|
2019-10-25 11:27:36 +02:00
|
|
|
|
|
|
|
/// This method traverses the structure of `ty`, trying to find an
|
2020-03-24 21:52:22 +03: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 11:27:36 +02: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 21:52:22 +03:00
|
|
|
/// reachable from a constant's type to implement the
|
|
|
|
/// structural-match traits, which essentially say that
|
2019-10-25 11:27:36 +02: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>(
|
2019-10-17 10:54:37 +02:00
|
|
|
span: Span,
|
2019-10-25 11:27:36 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
2022-07-23 23:10:08 +00:00
|
|
|
) -> Option<Ty<'tcx>> {
|
2022-07-24 20:44:19 +00:00
|
|
|
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), adt_const_param: false })
|
|
|
|
.break_value()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method traverses the structure of `ty`, trying to find any
|
|
|
|
/// types that are not allowed to be used in a const generic.
|
|
|
|
///
|
|
|
|
/// This is either because the type does not implement `StructuralEq`
|
|
|
|
/// and `StructuralPartialEq`, or because the type is intentionally
|
|
|
|
/// not supported in const generics (such as floats and raw pointers,
|
|
|
|
/// which are allowed in match blocks).
|
|
|
|
pub fn search_for_adt_const_param_violation<'tcx>(
|
|
|
|
span: Span,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Option<Ty<'tcx>> {
|
|
|
|
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), adt_const_param: true })
|
2022-07-04 23:44:41 +00:00
|
|
|
.break_value()
|
2019-10-17 10:54:37 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 14:47:04 +02: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
|
|
|
|
/// the structural-match traits (`StructuralPartialEq` and `StructuralEq`).
|
2022-06-29 10:43:47 +10:00
|
|
|
struct Search<'tcx> {
|
2019-10-17 10:54:37 +02:00
|
|
|
span: Span,
|
2019-10-25 11:27:36 +02:00
|
|
|
|
2022-06-29 10:43:47 +10:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-10-25 11:27:36 +02:00
|
|
|
|
2019-10-25 14:47:04 +02:00
|
|
|
/// Tracks ADTs previously encountered during search, so that
|
|
|
|
/// we will not recur on them again.
|
2019-10-17 10:54:37 +02:00
|
|
|
seen: FxHashSet<hir::def_id::DefId>,
|
2022-07-04 23:44:41 +00:00
|
|
|
|
2022-07-23 20:09:52 +00:00
|
|
|
// Additionally deny things that have been allowed in patterns,
|
2022-07-24 20:44:19 +00:00
|
|
|
// but are not allowed in adt const params, such as floats and
|
|
|
|
// fn ptrs.
|
|
|
|
adt_const_param: bool,
|
2019-10-17 10:54:37 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 10:43:47 +10:00
|
|
|
impl<'tcx> Search<'tcx> {
|
2019-10-17 10:54:37 +02:00
|
|
|
fn type_marked_structural(&self, adt_ty: Ty<'tcx>) -> bool {
|
2022-06-29 10:43:47 +10:00
|
|
|
adt_ty.is_structural_eq_shallow(self.tcx)
|
2019-10-17 10:54:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:43:47 +10:00
|
|
|
impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
|
2022-07-23 23:10:08 +00:00
|
|
|
type BreakTy = Ty<'tcx>;
|
2020-11-05 19:11:42 +01:00
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2019-10-17 10:54:37 +02:00
|
|
|
debug!("Search visiting ty: {:?}", ty);
|
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
let (adt_def, substs) = match *ty.kind() {
|
2019-10-17 10:54:37 +02:00
|
|
|
ty::Adt(adt_def, substs) => (adt_def, substs),
|
|
|
|
ty::Param(_) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2019-10-25 11:27:36 +02:00
|
|
|
}
|
2020-04-11 21:02:49 +02:00
|
|
|
ty::Dynamic(..) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2020-04-11 21:02:49 +02:00
|
|
|
}
|
2020-05-12 22:18:55 +02:00
|
|
|
ty::Foreign(_) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2020-05-12 22:18:55 +02: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-13 00:40:28 +02: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
|
|
|
}
|
2020-05-13 00:40:28 +02:00
|
|
|
ty::Generator(..) | ty::GeneratorWitness(..) => {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2020-05-13 00:40:28 +02:00
|
|
|
}
|
2022-07-23 20:09:52 +00:00
|
|
|
ty::FnDef(..) => {
|
2020-05-12 22:18:55 +02:00
|
|
|
// Types of formals and return in `fn(_) -> _` are also irrelevant;
|
2019-10-25 14:47:04 +02:00
|
|
|
// so we do not recur into them via `super_visit_with`
|
2023-01-17 23:17:13 -08:00
|
|
|
return ControlFlow::Continue(());
|
2019-10-17 10:54:37 +02:00
|
|
|
}
|
|
|
|
ty::Array(_, n)
|
2022-06-29 10:43:47 +10:00
|
|
|
if { n.try_eval_usize(self.tcx, ty::ParamEnv::reveal_all()) == Some(0) } =>
|
2019-10-17 10:54:37 +02:00
|
|
|
{
|
|
|
|
// rust-lang/rust#62336: ignore type of contents
|
|
|
|
// for empty array.
|
2023-01-17 23:17:13 -08:00
|
|
|
return ControlFlow::Continue(());
|
2019-10-25 11:27:36 +02:00
|
|
|
}
|
2022-07-04 23:44:41 +00:00
|
|
|
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Str | ty::Never => {
|
2020-05-12 22:18:55 +02: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-17 23:17:13 -08:00
|
|
|
return ControlFlow::Continue(());
|
2020-05-12 22:18:55 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 20:09:52 +00:00
|
|
|
ty::FnPtr(..) => {
|
2022-07-24 20:44:19 +00:00
|
|
|
if !self.adt_const_param {
|
2023-01-17 23:17:13 -08:00
|
|
|
return ControlFlow::Continue(());
|
2022-07-23 20:09:52 +00:00
|
|
|
} else {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2022-07-23 20:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::RawPtr(..) => {
|
2022-07-24 20:44:19 +00:00
|
|
|
if !self.adt_const_param {
|
2022-07-23 20:09:52 +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.
|
2023-01-17 23:17:13 -08:00
|
|
|
return ControlFlow::Continue(());
|
2022-07-23 20:09:52 +00:00
|
|
|
} else {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2022-07-23 20:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 23:44:41 +00:00
|
|
|
ty::Float(_) => {
|
2022-07-24 20:44:19 +00:00
|
|
|
if !self.adt_const_param {
|
2023-01-17 23:17:13 -08:00
|
|
|
return ControlFlow::Continue(());
|
2022-07-04 23:44:41 +00:00
|
|
|
} else {
|
2022-07-23 23:10:08 +00:00
|
|
|
return ControlFlow::Break(ty);
|
2022-07-04 23:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 00:40:28 +02:00
|
|
|
ty::Array(..) | ty::Slice(_) | ty::Ref(..) | ty::Tuple(..) => {
|
2020-05-13 11:52:22 +02:00
|
|
|
// First check all contained types and then tell the caller to continue searching.
|
2020-11-05 19:11:42 +01:00
|
|
|
return ty.super_visit_with(self);
|
2019-10-17 10:54:37 +02:00
|
|
|
}
|
2021-10-18 00:00:00 +00:00
|
|
|
ty::Infer(_) | ty::Placeholder(_) | ty::Bound(..) => {
|
2020-05-12 23:35:29 +02:00
|
|
|
bug!("unexpected type during structural-match checking: {:?}", ty);
|
|
|
|
}
|
2020-05-05 23:02:09 -05:00
|
|
|
ty::Error(_) => {
|
2022-06-29 10:43:47 +10:00
|
|
|
self.tcx.sess.delay_span_bug(self.span, "ty::Error in structural-match check");
|
2020-05-12 23:35:29 +02:00
|
|
|
// We still want to check other types after encountering an error,
|
|
|
|
// as this may still emit relevant errors.
|
2023-01-17 23:17:13 -08:00
|
|
|
return ControlFlow::Continue(());
|
2020-05-12 23:35:29 +02:00
|
|
|
}
|
2019-10-17 10:54:37 +02:00
|
|
|
};
|
2019-10-25 11:27:36 +02:00
|
|
|
|
2022-03-05 07:28:41 +11:00
|
|
|
if !self.seen.insert(adt_def.did()) {
|
2019-10-17 10:54:37 +02:00
|
|
|
debug!("Search already seen adt_def: {:?}", adt_def);
|
2023-01-17 23:17:13 -08:00
|
|
|
return ControlFlow::Continue(());
|
2019-10-25 11:27:36 +02:00
|
|
|
}
|
2019-10-17 10:54:37 +02: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 10:54:37 +02: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.
|
|
|
|
//
|
|
|
|
// (Must not recur on substs for `PhantomData<T>` cf
|
|
|
|
// rust-lang/rust#55028 and rust-lang/rust#55837; but also
|
|
|
|
// want to skip substs when only uses of generic are
|
|
|
|
// behind unsafe pointers `*const T`/`*mut T`.)
|
|
|
|
|
|
|
|
// even though we skip super_visit_with, we must recur on
|
|
|
|
// fields of ADT.
|
2022-06-29 10:43:47 +10:00
|
|
|
let tcx = self.tcx;
|
2020-11-05 19:11:42 +01:00
|
|
|
adt_def.all_fields().map(|field| field.ty(tcx, substs)).try_for_each(|field_ty| {
|
2022-06-29 10:43:47 +10:00
|
|
|
let ty = self.tcx.normalize_erasing_regions(ty::ParamEnv::empty(), field_ty);
|
2020-06-02 00:23:47 +02:00
|
|
|
debug!("structural-match ADT: field_ty={:?}, ty={:?}", field_ty, ty);
|
2020-11-05 19:11:42 +01:00
|
|
|
ty.visit_with(self)
|
|
|
|
})
|
2019-10-25 11:27:36 +02:00
|
|
|
}
|
|
|
|
}
|