Skip to content

Commit

Permalink
Auto merge of #117407 - compiler-errors:derive-clone, r=oli-obk
Browse files Browse the repository at this point in the history
Use derivative for `Clone`/`PartialOrd`/`Ord`/`Hash` in `rustc_type_ir`

This uses `derivative` to derive `Clone`/`PartialOrd`/`Ord`/`Hash` for types in `rustc_type_ir`. This doesn't derive `PartialEq`/`Eq` yet, because I have no idea why those are generating slower implementations from derivative.
  • Loading branch information
bors committed Oct 31, 2023
2 parents 045f158 + 8b4fa0f commit d7d9f15
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 391 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,17 @@ dependencies = [
"syn 2.0.29",
]

[[package]]
name = "derivative"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]

[[package]]
name = "derive_builder"
version = "0.12.0"
Expand Down Expand Up @@ -4667,6 +4678,7 @@ name = "rustc_type_ir"
version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"derivative",
"rustc_data_structures",
"rustc_index",
"rustc_macros",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_type_ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
# tidy-alphabetical-start
bitflags = "1.2.1"
derivative = "2.2.0"
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_index = { path = "../rustc_index" }
rustc_macros = { path = "../rustc_macros" }
Expand Down
22 changes: 3 additions & 19 deletions compiler/rustc_type_ir/src/canonical.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;
use std::hash;
use std::hash::Hash;
use std::ops::ControlFlow;

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
Expand All @@ -13,6 +13,8 @@ use crate::{HashStableContext, Interner, TyEncoder, UniverseIndex};
/// A "canonicalized" type `V` is one where all free inference
/// variables have been rewritten to "canonical vars". These are
/// numbered starting from 0 in order of first appearance.
#[derive(derivative::Derivative)]
#[derivative(Clone(bound = "V: Clone"), Hash(bound = "V: Hash"))]
pub struct Canonical<I: Interner, V> {
pub value: V,
pub max_universe: UniverseIndex,
Expand Down Expand Up @@ -59,14 +61,6 @@ impl<I: Interner, V> Canonical<I, V> {
}
}

impl<I: Interner, V: hash::Hash> hash::Hash for Canonical<I, V> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.value.hash(state);
self.max_universe.hash(state);
self.variables.hash(state);
}
}

impl<CTX: HashStableContext, I: Interner, V: HashStable<CTX>> HashStable<CTX> for Canonical<I, V>
where
I::CanonicalVars: HashStable<CTX>,
Expand Down Expand Up @@ -108,16 +102,6 @@ impl<I: Interner, V: fmt::Debug> fmt::Debug for Canonical<I, V> {
}
}

impl<I: Interner, V: Clone> Clone for Canonical<I, V> {
fn clone(&self) -> Self {
Canonical {
value: self.value.clone(),
max_universe: self.max_universe.clone(),
variables: self.variables.clone(),
}
}
}

impl<I: Interner, V: Copy> Copy for Canonical<I, V> where I::CanonicalVars: Copy {}

impl<I: Interner, V: TypeFoldable<I>> TypeFoldable<I> for Canonical<I, V>
Expand Down
73 changes: 9 additions & 64 deletions compiler/rustc_type_ir/src/const_kind.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use rustc_data_structures::stable_hasher::HashStable;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_serialize::{Decodable, Decoder, Encodable};
use std::cmp::Ordering;
use std::fmt;
use std::hash;

use crate::{
DebruijnIndex, DebugWithInfcx, HashStableContext, InferCtxtLike, Interner, TyDecoder,
Expand All @@ -13,7 +11,15 @@ use crate::{
use self::ConstKind::*;

/// Represents a constant in Rust.
// #[derive(derive_more::From)]
#[derive(derivative::Derivative)]
#[derivative(
Clone(bound = ""),
PartialOrd(bound = ""),
PartialOrd = "feature_allow_slow_enum",
Ord(bound = ""),
Ord = "feature_allow_slow_enum",
Hash(bound = "")
)]
pub enum ConstKind<I: Interner> {
/// A const generic parameter.
Param(I::ParamConst),
Expand Down Expand Up @@ -57,25 +63,6 @@ const fn const_kind_discriminant<I: Interner>(value: &ConstKind<I>) -> usize {
}
}

impl<I: Interner> hash::Hash for ConstKind<I> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
const_kind_discriminant(self).hash(state);
match self {
Param(p) => p.hash(state),
Infer(i) => i.hash(state),
Bound(d, b) => {
d.hash(state);
b.hash(state);
}
Placeholder(p) => p.hash(state),
Unevaluated(u) => u.hash(state),
Value(v) => v.hash(state),
Error(e) => e.hash(state),
Expr(e) => e.hash(state),
}
}
}

impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ConstKind<I>
where
I::ParamConst: HashStable<CTX>,
Expand Down Expand Up @@ -166,33 +153,6 @@ where
}
}

impl<I: Interner> PartialOrd for ConstKind<I> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl<I: Interner> Ord for ConstKind<I> {
fn cmp(&self, other: &Self) -> Ordering {
const_kind_discriminant(self)
.cmp(&const_kind_discriminant(other))
.then_with(|| match (self, other) {
(Param(p1), Param(p2)) => p1.cmp(p2),
(Infer(i1), Infer(i2)) => i1.cmp(i2),
(Bound(d1, b1), Bound(d2, b2)) => d1.cmp(d2).then_with(|| b1.cmp(b2)),
(Placeholder(p1), Placeholder(p2)) => p1.cmp(p2),
(Unevaluated(u1), Unevaluated(u2)) => u1.cmp(u2),
(Value(v1), Value(v2)) => v1.cmp(v2),
(Error(e1), Error(e2)) => e1.cmp(e2),
(Expr(e1), Expr(e2)) => e1.cmp(e2),
_ => {
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}");
Ordering::Equal
}
})
}
}

impl<I: Interner> PartialEq for ConstKind<I> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Expand All @@ -211,21 +171,6 @@ impl<I: Interner> PartialEq for ConstKind<I> {

impl<I: Interner> Eq for ConstKind<I> {}

impl<I: Interner> Clone for ConstKind<I> {
fn clone(&self) -> Self {
match self {
Param(arg0) => Param(arg0.clone()),
Infer(arg0) => Infer(arg0.clone()),
Bound(arg0, arg1) => Bound(arg0.clone(), arg1.clone()),
Placeholder(arg0) => Placeholder(arg0.clone()),
Unevaluated(arg0) => Unevaluated(arg0.clone()),
Value(arg0) => Value(arg0.clone()),
Error(arg0) => Error(arg0.clone()),
Expr(arg0) => Expr(arg0.clone()),
}
}
}

impl<I: Interner> fmt::Debug for ConstKind<I> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
WithInfcx::with_no_infcx(self).fmt(f)
Expand Down
83 changes: 4 additions & 79 deletions compiler/rustc_type_ir/src/predicate_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_serialize::Decoder;
use rustc_serialize::{Decodable, Encodable};
use std::fmt;
use std::hash;
use std::ops::ControlFlow;

use crate::fold::{FallibleTypeFolder, TypeFoldable};
Expand All @@ -12,6 +11,8 @@ use crate::{TyDecoder, TyEncoder};

/// A clause is something that can appear in where bounds or be inferred
/// by implied bounds.
#[derive(derivative::Derivative)]
#[derivative(Clone(bound = ""), Hash(bound = ""))]
pub enum ClauseKind<I: Interner> {
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
/// the `Self` type of the trait reference and `A`, `B`, and `C`
Expand Down Expand Up @@ -39,20 +40,6 @@ pub enum ClauseKind<I: Interner> {
ConstEvaluatable(I::Const),
}

impl<I: Interner> Clone for ClauseKind<I> {
fn clone(&self) -> Self {
match self {
Self::Trait(arg0) => Self::Trait(arg0.clone()),
Self::RegionOutlives(arg0) => Self::RegionOutlives(arg0.clone()),
Self::TypeOutlives(arg0) => Self::TypeOutlives(arg0.clone()),
Self::Projection(arg0) => Self::Projection(arg0.clone()),
Self::ConstArgHasType(arg0, arg1) => Self::ConstArgHasType(arg0.clone(), arg1.clone()),
Self::WellFormed(arg0) => Self::WellFormed(arg0.clone()),
Self::ConstEvaluatable(arg0) => Self::ConstEvaluatable(arg0.clone()),
}
}
}

impl<I: Interner> Copy for ClauseKind<I>
where
I::Ty: Copy,
Expand Down Expand Up @@ -94,24 +81,6 @@ fn clause_kind_discriminant<I: Interner>(value: &ClauseKind<I>) -> usize {
}
}

impl<I: Interner> hash::Hash for ClauseKind<I> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
clause_kind_discriminant(self).hash(state);
match self {
ClauseKind::Trait(p) => p.hash(state),
ClauseKind::RegionOutlives(p) => p.hash(state),
ClauseKind::TypeOutlives(p) => p.hash(state),
ClauseKind::Projection(p) => p.hash(state),
ClauseKind::ConstArgHasType(c, t) => {
c.hash(state);
t.hash(state);
}
ClauseKind::WellFormed(t) => t.hash(state),
ClauseKind::ConstEvaluatable(c) => c.hash(state),
}
}
}

impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ClauseKind<I>
where
I::Ty: HashStable<CTX>,
Expand Down Expand Up @@ -249,6 +218,8 @@ where
}
}

#[derive(derivative::Derivative)]
#[derivative(Clone(bound = ""), Hash(bound = ""))]
pub enum PredicateKind<I: Interner> {
/// Prove a clause
Clause(ClauseKind<I>),
Expand Down Expand Up @@ -305,25 +276,6 @@ where
{
}

impl<I: Interner> Clone for PredicateKind<I> {
fn clone(&self) -> Self {
match self {
Self::Clause(arg0) => Self::Clause(arg0.clone()),
Self::ObjectSafe(arg0) => Self::ObjectSafe(arg0.clone()),
Self::ClosureKind(arg0, arg1, arg2) => {
Self::ClosureKind(arg0.clone(), arg1.clone(), arg2.clone())
}
Self::Subtype(arg0) => Self::Subtype(arg0.clone()),
Self::Coerce(arg0) => Self::Coerce(arg0.clone()),
Self::ConstEquate(arg0, arg1) => Self::ConstEquate(arg0.clone(), arg1.clone()),
Self::Ambiguous => Self::Ambiguous,
Self::AliasRelate(arg0, arg1, arg2) => {
Self::AliasRelate(arg0.clone(), arg1.clone(), arg2.clone())
}
}
}
}

impl<I: Interner> PartialEq for PredicateKind<I> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Expand Down Expand Up @@ -358,33 +310,6 @@ fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
}
}

impl<I: Interner> hash::Hash for PredicateKind<I> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
predicate_kind_discriminant(self).hash(state);
match self {
PredicateKind::Clause(p) => p.hash(state),
PredicateKind::ObjectSafe(d) => d.hash(state),
PredicateKind::ClosureKind(d, g, k) => {
d.hash(state);
g.hash(state);
k.hash(state);
}
PredicateKind::Subtype(p) => p.hash(state),
PredicateKind::Coerce(p) => p.hash(state),
PredicateKind::ConstEquate(c1, c2) => {
c1.hash(state);
c2.hash(state);
}
PredicateKind::Ambiguous => {}
PredicateKind::AliasRelate(t1, t2, r) => {
t1.hash(state);
t2.hash(state);
r.hash(state);
}
}
}
}

impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for PredicateKind<I>
where
I::DefId: HashStable<CTX>,
Expand Down
Loading

0 comments on commit d7d9f15

Please sign in to comment.