]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ty_utils/src/structural_match.rs
Rollup merge of #105375 - WaffleLapkin:docfix, r=cjgillot
[rust.git] / compiler / rustc_ty_utils / src / structural_match.rs
1 use rustc_hir::lang_items::LangItem;
2 use rustc_middle::ty::query::Providers;
3 use rustc_middle::ty::{self, Ty, TyCtxt};
4
5 use rustc_infer::infer::TyCtxtInferExt;
6 use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
7
8 /// This method returns true if and only if `adt_ty` itself has been marked as
9 /// eligible for structural-match: namely, if it implements both
10 /// `StructuralPartialEq` and `StructuralEq` (which are respectively injected by
11 /// `#[derive(PartialEq)]` and `#[derive(Eq)]`).
12 ///
13 /// Note that this does *not* recursively check if the substructure of `adt_ty`
14 /// implements the traits.
15 fn has_structural_eq_impls<'tcx>(tcx: TyCtxt<'tcx>, adt_ty: Ty<'tcx>) -> bool {
16     let ref infcx = tcx.infer_ctxt().build();
17     let cause = ObligationCause::dummy();
18
19     let ocx = ObligationCtxt::new(infcx);
20     // require `#[derive(PartialEq)]`
21     let structural_peq_def_id =
22         infcx.tcx.require_lang_item(LangItem::StructuralPeq, Some(cause.span));
23     ocx.register_bound(cause.clone(), ty::ParamEnv::empty(), adt_ty, structural_peq_def_id);
24     // for now, require `#[derive(Eq)]`. (Doing so is a hack to work around
25     // the type `for<'a> fn(&'a ())` failing to implement `Eq` itself.)
26     let structural_teq_def_id =
27         infcx.tcx.require_lang_item(LangItem::StructuralTeq, Some(cause.span));
28     ocx.register_bound(cause, ty::ParamEnv::empty(), adt_ty, structural_teq_def_id);
29
30     // We deliberately skip *reporting* fulfillment errors (via
31     // `report_fulfillment_errors`), for two reasons:
32     //
33     // 1. The error messages would mention `std::marker::StructuralPartialEq`
34     //    (a trait which is solely meant as an implementation detail
35     //    for now), and
36     //
37     // 2. We are sometimes doing future-incompatibility lints for
38     //    now, so we do not want unconditional errors here.
39     ocx.select_all_or_error().is_empty()
40 }
41
42 pub fn provide(providers: &mut Providers) {
43     providers.has_structural_eq_impls = has_structural_eq_impls;
44 }