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