Skip to content

some derive_more refactors #145145

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

Merged
merged 1 commit into from
Aug 10, 2025
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
16 changes: 12 additions & 4 deletions compiler/rustc_next_trait_solver/src/solve/inspect/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<I: Interner> From<WipCanonicalGoalEvaluationStep<I>> for DebugSolver<I> {
}
}

#[derive_where(PartialEq, Eq, Debug; I: Interner)]
#[derive_where(PartialEq, Debug; I: Interner)]
struct WipGoalEvaluation<I: Interner> {
pub uncanonicalized_goal: Goal<I, I::Predicate>,
pub orig_values: Vec<I::GenericArg>,
Expand All @@ -78,6 +78,8 @@ struct WipGoalEvaluation<I: Interner> {
pub result: Option<QueryResult<I>>,
}

impl<I: Interner> Eq for WipGoalEvaluation<I> {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussion: should we leave a comment about why the manual Eq derives somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it will be worth it to sprinkle a comment everywhere. People attempting to go back to derives might wonder why there were manual impls so they'd look at the blame.. Or the reviewer could look at the blame.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope they do look at the blame... But yeah, good point about sprinkling them around. What I had in mind was having a single comment somewhere, but then it probably will just be never read again. So this is fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we going to return to using the derives instead of the manual impls once ModProg/derive-where#128 lands?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be better once my PR lands, yeah, so we could return to it, but unlike the built-in Eq derive which uses a single type AssertParamIsEq across all derives, the derive-where impl creates a new struct every time you derive Eq, so I am still not completely happy about it


impl<I: Interner> WipGoalEvaluation<I> {
fn finalize(self) -> inspect::GoalEvaluation<I> {
inspect::GoalEvaluation {
Expand All @@ -98,7 +100,7 @@ impl<I: Interner> WipGoalEvaluation<I> {
/// This only exists during proof tree building and does not have
/// a corresponding struct in `inspect`. We need this to track a
/// bunch of metadata about the current evaluation.
#[derive_where(PartialEq, Eq, Debug; I: Interner)]
#[derive_where(PartialEq, Debug; I: Interner)]
struct WipCanonicalGoalEvaluationStep<I: Interner> {
/// Unlike `EvalCtxt::var_values`, we append a new
/// generic arg here whenever we create a new inference
Expand All @@ -111,6 +113,8 @@ struct WipCanonicalGoalEvaluationStep<I: Interner> {
evaluation: WipProbe<I>,
}

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

impl<I: Interner> WipCanonicalGoalEvaluationStep<I> {
fn current_evaluation_scope(&mut self) -> &mut WipProbe<I> {
let mut current = &mut self.evaluation;
Expand All @@ -132,14 +136,16 @@ impl<I: Interner> WipCanonicalGoalEvaluationStep<I> {
}
}

#[derive_where(PartialEq, Eq, Debug; I: Interner)]
#[derive_where(PartialEq, Debug; I: Interner)]
struct WipProbe<I: Interner> {
initial_num_var_values: usize,
steps: Vec<WipProbeStep<I>>,
kind: Option<inspect::ProbeKind<I>>,
final_state: Option<inspect::CanonicalState<I, ()>>,
}

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

impl<I: Interner> WipProbe<I> {
fn finalize(self) -> inspect::Probe<I> {
inspect::Probe {
Expand All @@ -150,14 +156,16 @@ impl<I: Interner> WipProbe<I> {
}
}

#[derive_where(PartialEq, Eq, Debug; I: Interner)]
#[derive_where(PartialEq, Debug; I: Interner)]
enum WipProbeStep<I: Interner> {
AddGoal(GoalSource, inspect::CanonicalState<I, Goal<I, I::Predicate>>),
NestedProbe(WipProbe<I>),
MakeCanonicalResponse { shallow_certainty: Certainty },
RecordImplArgs { impl_args: inspect::CanonicalState<I, I::GenericArgs> },
}

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

impl<I: Interner> WipProbeStep<I> {
fn finalize(self) -> inspect::ProbeStep<I> {
match self {
Expand Down
21 changes: 7 additions & 14 deletions compiler/rustc_type_ir/src/binder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::Debug;
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::{ControlFlow, Deref};

Expand All @@ -23,18 +21,16 @@ use crate::{self as ty, Interner};
/// for more details.
///
/// `Decodable` and `Encodable` are implemented for `Binder<T>` using the `impl_binder_encode_decode!` macro.
#[derive_where(Clone; I: Interner, T: Clone)]
#[derive_where(Clone, Hash, PartialEq, Debug; I: Interner, T)]
#[derive_where(Copy; I: Interner, T: Copy)]
#[derive_where(Hash; I: Interner, T: Hash)]
#[derive_where(PartialEq; I: Interner, T: PartialEq)]
#[derive_where(Eq; I: Interner, T: Eq)]
#[derive_where(Debug; I: Interner, T: Debug)]
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
pub struct Binder<I: Interner, T> {
value: T,
bound_vars: I::BoundVarKinds,
}

impl<I: Interner, T: Eq> Eq for Binder<I, T> {}

// FIXME: We manually derive `Lift` because the `derive(Lift_Generic)` doesn't
// understand how to turn `T` to `T::Lifted` in the output `type Lifted`.
impl<I: Interner, U: Interner, T> Lift<U> for Binder<I, T>
Expand Down Expand Up @@ -356,14 +352,9 @@ impl<I: Interner> TypeVisitor<I> for ValidateBoundVars<I> {
/// `instantiate`.
///
/// See <https://rustc-dev-guide.rust-lang.org/ty_module/early_binder.html> for more details.
#[derive_where(Clone; I: Interner, T: Clone)]
#[derive_where(Copy; I: Interner, T: Copy)]
#[derive_where(PartialEq; I: Interner, T: PartialEq)]
#[derive_where(Eq; I: Interner, T: Eq)]
#[derive_where(Ord; I: Interner, T: Ord)]
#[derive_where(Clone, PartialEq, Ord, Hash, Debug; I: Interner, T)]
#[derive_where(PartialOrd; I: Interner, T: Ord)]
#[derive_where(Hash; I: Interner, T: Hash)]
#[derive_where(Debug; I: Interner, T: Debug)]
#[derive_where(Copy; I: Interner, T: Copy)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
Expand All @@ -374,6 +365,8 @@ pub struct EarlyBinder<I: Interner, T> {
_tcx: PhantomData<fn() -> I>,
}

impl<I: Interner, T: Eq> Eq for EarlyBinder<I, T> {}

/// For early binders, you should first call `instantiate` before using any visitors.
#[cfg(feature = "nightly")]
impl<I: Interner, T> !TypeFoldable<I> for ty::EarlyBinder<I, T> {}
Expand Down
24 changes: 12 additions & 12 deletions compiler/rustc_type_ir/src/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ use crate::data_structures::HashMap;
use crate::inherent::*;
use crate::{self as ty, Interner, TypingMode, UniverseIndex};

#[derive_where(Clone; I: Interner, V: Clone)]
#[derive_where(Hash; I: Interner, V: Hash)]
#[derive_where(PartialEq; I: Interner, V: PartialEq)]
#[derive_where(Eq; I: Interner, V: Eq)]
#[derive_where(Debug; I: Interner, V: fmt::Debug)]
#[derive_where(Clone, Hash, PartialEq, Debug; I: Interner, V)]
#[derive_where(Copy; I: Interner, V: Copy)]
#[cfg_attr(
feature = "nightly",
Expand All @@ -26,14 +22,12 @@ pub struct CanonicalQueryInput<I: Interner, V> {
pub typing_mode: TypingMode<I>,
}

impl<I: Interner, V: Eq> Eq for CanonicalQueryInput<I, V> {}

/// 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_where(Clone; I: Interner, V: Clone)]
#[derive_where(Hash; I: Interner, V: Hash)]
#[derive_where(PartialEq; I: Interner, V: PartialEq)]
#[derive_where(Eq; I: Interner, V: Eq)]
#[derive_where(Debug; I: Interner, V: fmt::Debug)]
#[derive_where(Clone, Hash, PartialEq, Debug; I: Interner, V)]
#[derive_where(Copy; I: Interner, V: Copy)]
#[cfg_attr(
feature = "nightly",
Expand All @@ -45,6 +39,8 @@ pub struct Canonical<I: Interner, V> {
pub variables: I::CanonicalVarKinds,
}

impl<I: Interner, V: Eq> Eq for Canonical<I, V> {}

impl<I: Interner, V> Canonical<I, V> {
/// Allows you to map the `value` of a canonical while keeping the
/// same set of bound variables.
Expand Down Expand Up @@ -89,7 +85,7 @@ impl<I: Interner, V: fmt::Display> fmt::Display for Canonical<I, V> {
/// canonical value. This is sufficient information for code to create
/// a copy of the canonical value in some other inference context,
/// with fresh inference variables replacing the canonical values.
#[derive_where(Clone, Copy, Hash, PartialEq, Eq, Debug; I: Interner)]
#[derive_where(Clone, Copy, Hash, PartialEq, Debug; I: Interner)]
#[cfg_attr(
feature = "nightly",
derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext)
Expand All @@ -116,6 +112,8 @@ pub enum CanonicalVarKind<I: Interner> {
PlaceholderConst(I::PlaceholderConst),
}

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

impl<I: Interner> CanonicalVarKind<I> {
pub fn universe(self) -> UniverseIndex {
match self {
Expand Down Expand Up @@ -223,7 +221,7 @@ pub enum CanonicalTyVarKind {
/// vectors with the original values that were replaced by canonical
/// variables. You will need to supply it later to instantiate the
/// canonicalized query response.
#[derive_where(Clone, Copy, Hash, PartialEq, Eq, Debug; I: Interner)]
#[derive_where(Clone, Copy, Hash, PartialEq, Debug; I: Interner)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
Expand All @@ -233,6 +231,8 @@ pub struct CanonicalVarValues<I: Interner> {
pub var_values: I::GenericArgs,
}

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

impl<I: Interner> CanonicalVarValues<I> {
pub fn is_identity(&self) -> bool {
self.var_values.iter().enumerate().all(|(bv, arg)| match arg.kind() {
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_type_ir/src/const_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Gen
use crate::{self as ty, DebruijnIndex, Interner};

/// Represents a constant in Rust.
#[derive_where(Clone, Copy, Hash, PartialEq, Eq; I: Interner)]
#[derive_where(Clone, Copy, Hash, PartialEq; I: Interner)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
Expand Down Expand Up @@ -45,6 +45,8 @@ pub enum ConstKind<I: Interner> {
Expr(I::ExprConst),
}

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

impl<I: Interner> fmt::Debug for ConstKind<I> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use ConstKind::*;
Expand All @@ -63,7 +65,7 @@ impl<I: Interner> fmt::Debug for ConstKind<I> {
}

/// An unevaluated (potentially generic) constant used in the type-system.
#[derive_where(Clone, Copy, Debug, Hash, PartialEq, Eq; I: Interner)]
#[derive_where(Clone, Copy, Debug, Hash, PartialEq; I: Interner)]
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
#[cfg_attr(
feature = "nightly",
Expand All @@ -74,6 +76,8 @@ pub struct UnevaluatedConst<I: Interner> {
pub args: I::GenericArgs,
}

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

impl<I: Interner> UnevaluatedConst<I> {
#[inline]
pub fn new(def: I::DefId, args: I::GenericArgs) -> UnevaluatedConst<I> {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_type_ir/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<T> ExpectedFound<T> {
}

// Data structures used in type unification
#[derive_where(Clone, Copy, PartialEq, Eq, Debug; I: Interner)]
#[derive_where(Clone, Copy, PartialEq, Debug; I: Interner)]
#[derive(TypeVisitable_Generic)]
#[cfg_attr(feature = "nightly", rustc_pass_by_value)]
pub enum TypeError<I: Interner> {
Expand Down Expand Up @@ -58,6 +58,8 @@ pub enum TypeError<I: Interner> {
TargetFeatureCast(I::DefId),
}

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

impl<I: Interner> TypeError<I> {
pub fn involves_regions(self) -> bool {
match self {
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_type_ir/src/generic_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContex

use crate::Interner;

#[derive_where(Clone, Copy, PartialEq, Eq, Debug; I: Interner)]
#[derive_where(Clone, Copy, PartialEq, Debug; I: Interner)]
#[cfg_attr(
feature = "nightly",
derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext)
Expand All @@ -15,7 +15,9 @@ pub enum GenericArgKind<I: Interner> {
Const(I::Const),
}

#[derive_where(Clone, Copy, PartialEq, Eq, Debug; I: Interner)]
impl<I: Interner> Eq for GenericArgKind<I> {}

#[derive_where(Clone, Copy, PartialEq, Debug; I: Interner)]
#[cfg_attr(
feature = "nightly",
derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext)
Expand All @@ -24,3 +26,5 @@ pub enum TermKind<I: Interner> {
Ty(I::Ty),
Const(I::Const),
}

impl<I: Interner> Eq for TermKind<I> {}
4 changes: 3 additions & 1 deletion compiler/rustc_type_ir/src/infer_ctxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{self as ty, Interner};
///
/// If neither of these functions are available, feel free to reach out to
/// t-types for help.
#[derive_where(Clone, Copy, Hash, PartialEq, Eq, Debug; I: Interner)]
#[derive_where(Clone, Copy, Hash, PartialEq, Debug; I: Interner)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
Expand Down Expand Up @@ -90,6 +90,8 @@ pub enum TypingMode<I: Interner> {
PostAnalysis,
}

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

impl<I: Interner> TypingMode<I> {
/// Analysis outside of a body does not define any opaque types.
pub fn non_body_analysis() -> TypingMode<I> {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_type_ir/src/opaque_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
use crate::inherent::*;
use crate::{self as ty, Interner};

#[derive_where(Clone, Copy, Hash, PartialEq, Eq, Debug; I: Interner)]
#[derive_where(Clone, Copy, Hash, PartialEq, Debug; I: Interner)]
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
#[cfg_attr(
feature = "nightly",
Expand All @@ -17,6 +17,8 @@ pub struct OpaqueTypeKey<I: Interner> {
pub args: I::GenericArgs,
}

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

impl<I: Interner> OpaqueTypeKey<I> {
pub fn iter_captured_args(self, cx: I) -> impl Iterator<Item = (usize, I::GenericArg)> {
let variances = cx.variances_of(self.def_id.into());
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_type_ir/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Gen

use crate::Interner;

#[derive_where(Clone, Copy, Hash, PartialEq, Eq; I: Interner)]
#[derive_where(Clone, Copy, Hash, PartialEq; I: Interner)]
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
#[cfg_attr(
feature = "nightly",
Expand All @@ -15,3 +15,5 @@ pub enum PatternKind<I: Interner> {
Range { start: I::Const, end: I::Const },
Or(I::PatList),
}

impl<I: Interner> Eq for PatternKind<I> {}
Loading
Loading