Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra debug assertions for equality for Adt/Variant/FieldDef #111775

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions compiler/rustc_middle/src/ty/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,30 @@ impl Ord for AdtDefData {
}
}

/// There should be only one AdtDef for each `did`, therefore
/// it is fine to implement `PartialEq` only based on `did`.
impl PartialEq for AdtDefData {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.did == other.did
// There should be only one `AdtDefData` for each `def_id`, therefore
// it is fine to implement `PartialEq` only based on `def_id`.
//
// Below, we exhaustively destructure `self` and `other` so that if the
// definition of `AdtDefData` changes, a compile-error will be produced,
// reminding us to revisit this assumption.

let Self { did: self_def_id, variants: _, flags: _, repr: _ } = self;
let Self { did: other_def_id, variants: _, flags: _, repr: _ } = other;

let res = self_def_id == other_def_id;

// Double check that implicit assumption detailed above.
if cfg!(debug_assertions) && res {
let deep = self.flags == other.flags
&& self.repr == other.repr
&& self.variants == other.variants;
assert!(deep, "AdtDefData for the same def-id has differing data");
}

res
}
}

Expand Down
25 changes: 23 additions & 2 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,20 @@ impl PartialEq for VariantDef {

let Self { def_id: lhs_def_id, ctor: _, name: _, discr: _, fields: _, flags: _ } = &self;
let Self { def_id: rhs_def_id, ctor: _, name: _, discr: _, fields: _, flags: _ } = other;
lhs_def_id == rhs_def_id

let res = lhs_def_id == rhs_def_id;

// Double check that implicit assumption detailed above.
if cfg!(debug_assertions) && res {
let deep = self.ctor == other.ctor
&& self.name == other.name
&& self.discr == other.discr
&& self.fields == other.fields
&& self.flags == other.flags;
assert!(deep, "VariantDef for the same def-id has differing data");
}

res
}
}

Expand Down Expand Up @@ -1937,7 +1950,15 @@ impl PartialEq for FieldDef {

let Self { did: rhs_did, name: _, vis: _ } = other;

lhs_did == rhs_did
let res = lhs_did == rhs_did;

// Double check that implicit assumption detailed above.
if cfg!(debug_assertions) && res {
let deep = self.name == other.name && self.vis == other.vis;
assert!(deep, "FieldDef for the same def-id has differing data");
}

res
}
}

Expand Down