Skip to content

Commit

Permalink
use a must_hold variant for checking PartialEq
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Sep 24, 2023
1 parent c3ed0c4 commit 2bbd5c3
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<'tcx> ConstToPat<'tcx> {
}

if let Some(non_sm_ty) = structural {
if !self.type_may_have_partial_eq_impl(cv.ty()) {
if !self.type_has_partial_eq_impl(cv.ty()) {
if let ty::Adt(def, ..) = non_sm_ty.kind() {
if def.is_union() {
let err = UnionPattern { span: self.span };
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<'tcx> ConstToPat<'tcx> {

// Always check for `PartialEq`, even if we emitted other lints. (But not if there were
// any errors.) This ensures it shows up in cargo's future-compat reports as well.
if !self.type_may_have_partial_eq_impl(cv.ty()) {
if !self.type_has_partial_eq_impl(cv.ty()) {
self.tcx().emit_spanned_lint(
lint::builtin::MATCH_WITHOUT_PARTIAL_EQ,
self.id,
Expand All @@ -258,7 +258,7 @@ impl<'tcx> ConstToPat<'tcx> {
}

#[instrument(level = "trace", skip(self), ret)]
fn type_may_have_partial_eq_impl(&self, ty: Ty<'tcx>) -> bool {
fn type_has_partial_eq_impl(&self, ty: Ty<'tcx>) -> bool {
// double-check there even *is* a semantic `PartialEq` to dispatch to.
//
// (If there isn't, then we can safely issue a hard
Expand All @@ -273,8 +273,13 @@ impl<'tcx> ConstToPat<'tcx> {
ty::TraitRef::new(self.tcx(), partial_eq_trait_id, [ty, ty]),
);

// FIXME: should this call a `predicate_must_hold` variant instead?
self.infcx.predicate_may_hold(&partial_eq_obligation)
// This *could* conservatively fail when outlives obligations are required.
// However, we anyway require all `PartialEq` to be derived, so that shouldn't be possible.
// If this ever becomes a problem, we need to somehow leave a note in the MIR body
// asking borrowck to precisely check that the relevant types actually *do*
// implement `PartialEq`, even if we don't end up calling `PartialEq::eq`
// in the generated code.
self.infcx.predicate_must_hold_considering_regions(&partial_eq_obligation)
}

fn field_pats(
Expand Down

0 comments on commit 2bbd5c3

Please sign in to comment.