2020-01-06 20:13:24 +01:00
|
|
|
use crate::infer::{InferCtxt, TyCtxtInferExt};
|
|
|
|
use crate::traits::ObligationCause;
|
2020-05-13 13:40:22 -07:00
|
|
|
use crate::traits::{self, TraitEngine};
|
2020-01-05 02:37:57 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2020-08-18 11:47:27 +01:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2020-05-13 13:40:22 -07:00
|
|
|
use rustc_middle::ty::query::Providers;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt, TypeFoldable, 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
|
|
|
|
2019-10-17 10:54:37 +02:00
|
|
|
#[derive(Debug)]
|
2019-10-25 11:27:36 +02:00
|
|
|
pub enum NonStructuralMatchTy<'tcx> {
|
|
|
|
Adt(&'tcx AdtDef),
|
|
|
|
Param,
|
2020-04-11 21:02:49 +02:00
|
|
|
Dynamic,
|
2020-05-12 22:18:55 +02:00
|
|
|
Foreign,
|
2020-05-13 00:40:28 +02:00
|
|
|
Opaque,
|
|
|
|
Generator,
|
|
|
|
Projection,
|
2020-06-14 19:16:35 +01:00
|
|
|
Closure,
|
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>(
|
2020-05-13 13:40:22 -07:00
|
|
|
_id: hir::HirId,
|
2019-10-17 10:54:37 +02:00
|
|
|
span: Span,
|
2019-10-25 11:27:36 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Option<NonStructuralMatchTy<'tcx>> {
|
2019-10-17 10:54:37 +02:00
|
|
|
// FIXME: we should instead pass in an `infcx` from the outside.
|
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2020-05-13 13:40:22 -07:00
|
|
|
let mut search = Search { infcx, span, found: None, seen: FxHashSet::default() };
|
2019-10-17 10:54:37 +02:00
|
|
|
ty.visit_with(&mut search);
|
|
|
|
search.found
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method returns true if and only if `adt_ty` itself has been marked as
|
|
|
|
/// eligible for structural-match: namely, if it implements both
|
|
|
|
/// `StructuralPartialEq` and `StructuralEq` (which are respectively injected by
|
|
|
|
/// `#[derive(PartialEq)]` and `#[derive(Eq)]`).
|
|
|
|
///
|
|
|
|
/// Note that this does *not* recursively check if the substructure of `adt_ty`
|
|
|
|
/// implements the traits.
|
2020-06-12 08:47:26 -07:00
|
|
|
fn type_marked_structural(
|
2019-12-22 17:42:04 -05:00
|
|
|
infcx: &InferCtxt<'_, 'tcx>,
|
|
|
|
adt_ty: Ty<'tcx>,
|
2020-05-13 13:40:22 -07:00
|
|
|
cause: ObligationCause<'tcx>,
|
2019-12-22 17:42:04 -05:00
|
|
|
) -> bool {
|
2019-10-17 10:54:37 +02:00
|
|
|
let mut fulfillment_cx = traits::FulfillmentContext::new();
|
|
|
|
// require `#[derive(PartialEq)]`
|
2020-05-13 13:40:22 -07:00
|
|
|
let structural_peq_def_id =
|
2020-08-18 11:47:27 +01:00
|
|
|
infcx.tcx.require_lang_item(LangItem::StructuralPeq, Some(cause.span));
|
2019-10-17 10:54:37 +02:00
|
|
|
fulfillment_cx.register_bound(
|
2019-12-22 17:42:04 -05:00
|
|
|
infcx,
|
|
|
|
ty::ParamEnv::empty(),
|
|
|
|
adt_ty,
|
|
|
|
structural_peq_def_id,
|
2020-05-13 13:40:22 -07:00
|
|
|
cause.clone(),
|
2019-12-22 17:42:04 -05:00
|
|
|
);
|
2019-10-17 10:54:37 +02:00
|
|
|
// for now, require `#[derive(Eq)]`. (Doing so is a hack to work around
|
|
|
|
// the type `for<'a> fn(&'a ())` failing to implement `Eq` itself.)
|
2020-05-13 13:40:22 -07:00
|
|
|
let structural_teq_def_id =
|
2020-08-18 11:47:27 +01:00
|
|
|
infcx.tcx.require_lang_item(LangItem::StructuralTeq, Some(cause.span));
|
2019-10-17 10:54:37 +02:00
|
|
|
fulfillment_cx.register_bound(
|
2019-12-22 17:42:04 -05:00
|
|
|
infcx,
|
|
|
|
ty::ParamEnv::empty(),
|
|
|
|
adt_ty,
|
|
|
|
structural_teq_def_id,
|
|
|
|
cause,
|
|
|
|
);
|
2019-10-17 10:54:37 +02:00
|
|
|
|
|
|
|
// We deliberately skip *reporting* fulfillment errors (via
|
|
|
|
// `report_fulfillment_errors`), for two reasons:
|
|
|
|
//
|
|
|
|
// 1. The error messages would mention `std::marker::StructuralPartialEq`
|
|
|
|
// (a trait which is solely meant as an implementation detail
|
|
|
|
// for now), and
|
|
|
|
//
|
|
|
|
// 2. We are sometimes doing future-incompatibility lints for
|
|
|
|
// now, so we do not want unconditional errors here.
|
|
|
|
fulfillment_cx.select_all_or_error(infcx).is_ok()
|
|
|
|
}
|
|
|
|
|
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`).
|
2019-10-17 10:54:37 +02:00
|
|
|
struct Search<'a, 'tcx> {
|
|
|
|
span: Span,
|
2019-10-25 11:27:36 +02:00
|
|
|
|
2019-10-17 10:54:37 +02:00
|
|
|
infcx: InferCtxt<'a, 'tcx>,
|
2019-10-25 11:27:36 +02:00
|
|
|
|
2019-10-25 14:47:04 +02:00
|
|
|
/// Records first ADT that does not implement a structural-match trait.
|
2019-10-17 10:54:37 +02:00
|
|
|
found: Option<NonStructuralMatchTy<'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>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Search<'a, 'tcx> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.infcx.tcx
|
2019-10-25 11:27:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-17 10:54:37 +02:00
|
|
|
fn type_marked_structural(&self, adt_ty: Ty<'tcx>) -> bool {
|
2020-05-13 13:40:22 -07:00
|
|
|
adt_ty.is_structural_eq_shallow(self.tcx())
|
2019-10-17 10:54:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
|
2020-10-25 11:50:56 +01:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<()> {
|
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(_) => {
|
|
|
|
self.found = Some(NonStructuralMatchTy::Param);
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::BREAK;
|
2019-10-25 11:27:36 +02:00
|
|
|
}
|
2020-04-11 21:02:49 +02:00
|
|
|
ty::Dynamic(..) => {
|
|
|
|
self.found = Some(NonStructuralMatchTy::Dynamic);
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::BREAK;
|
2020-04-11 21:02:49 +02:00
|
|
|
}
|
2020-05-12 22:18:55 +02:00
|
|
|
ty::Foreign(_) => {
|
|
|
|
self.found = Some(NonStructuralMatchTy::Foreign);
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::BREAK;
|
2020-05-12 22:18:55 +02:00
|
|
|
}
|
2020-05-13 00:40:28 +02:00
|
|
|
ty::Opaque(..) => {
|
|
|
|
self.found = Some(NonStructuralMatchTy::Opaque);
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::BREAK;
|
2020-05-13 00:40:28 +02:00
|
|
|
}
|
|
|
|
ty::Projection(..) => {
|
|
|
|
self.found = Some(NonStructuralMatchTy::Projection);
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::BREAK;
|
2020-05-13 00:40:28 +02:00
|
|
|
}
|
|
|
|
ty::Generator(..) | ty::GeneratorWitness(..) => {
|
|
|
|
self.found = Some(NonStructuralMatchTy::Generator);
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::BREAK;
|
2020-05-13 00:40:28 +02:00
|
|
|
}
|
2020-06-14 19:16:35 +01:00
|
|
|
ty::Closure(..) => {
|
|
|
|
self.found = Some(NonStructuralMatchTy::Closure);
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::BREAK;
|
2020-06-14 19:16:35 +01:00
|
|
|
}
|
2019-10-17 10:54:37 +02:00
|
|
|
ty::RawPtr(..) => {
|
|
|
|
// structural-match ignores substructure of
|
2019-10-25 14:47:04 +02:00
|
|
|
// `*const _`/`*mut _`, so skip `super_visit_with`.
|
2019-10-17 10:54:37 +02:00
|
|
|
//
|
2019-10-25 14:47:04 +02:00
|
|
|
// 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.
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::CONTINUE;
|
2019-10-25 11:27:36 +02:00
|
|
|
}
|
2019-10-17 10:54:37 +02:00
|
|
|
ty::FnDef(..) | ty::FnPtr(..) => {
|
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`
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::CONTINUE;
|
2019-10-17 10:54:37 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
ty::Array(_, n)
|
|
|
|
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.
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::CONTINUE;
|
2019-10-25 11:27:36 +02:00
|
|
|
}
|
2020-05-13 00:40:28 +02:00
|
|
|
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | 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.
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::CONTINUE;
|
2020-05-12 22:18:55 +02: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.
|
2019-10-17 10:54:37 +02:00
|
|
|
ty.super_visit_with(self);
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::CONTINUE;
|
2019-10-17 10:54:37 +02:00
|
|
|
}
|
2020-06-14 19:16:35 +01: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(_) => {
|
2020-05-13 00:40:28 +02: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.
|
2020-10-21 14:24:35 +02: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
|
|
|
|
2019-10-17 10:54:37 +02:00
|
|
|
if !self.seen.insert(adt_def.did) {
|
|
|
|
debug!("Search already seen adt_def: {:?}", adt_def);
|
2020-10-21 14:24:35 +02: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);
|
|
|
|
self.found = Some(NonStructuralMatchTy::Adt(&adt_def));
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::BREAK;
|
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.
|
|
|
|
let tcx = self.tcx();
|
|
|
|
for field_ty in adt_def.all_fields().map(|field| field.ty(tcx, substs)) {
|
2020-06-02 09:31:10 +02: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-10-22 10:20:24 +02:00
|
|
|
if ty.visit_with(self).is_break() {
|
2019-10-17 10:54:37 +02:00
|
|
|
// found an ADT without structural-match; halt visiting!
|
|
|
|
assert!(self.found.is_some());
|
2020-10-21 14:24:35 +02:00
|
|
|
return ControlFlow::BREAK;
|
2019-10-17 10:54:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even though we do not want to recur on substs, we do
|
|
|
|
// want our caller to continue its own search.
|
2020-10-21 14:24:35 +02:00
|
|
|
ControlFlow::CONTINUE
|
2019-10-25 11:27:36 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-13 13:40:22 -07:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2020-05-13 13:40:22 -07:00
|
|
|
providers.has_structural_eq_impls = |tcx, ty| {
|
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
let cause = ObligationCause::dummy();
|
|
|
|
type_marked_structural(&infcx, ty, cause)
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|