Skip to content

Commit c6aa16c

Browse files
committed
Use derivative for better derive bounds
1 parent c1fc1d1 commit c6aa16c

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4340,6 +4340,7 @@ dependencies = [
43404340
name = "rustc_pattern_analysis"
43414341
version = "0.0.0"
43424342
dependencies = [
4343+
"derivative",
43434344
"rustc_apfloat",
43444345
"rustc_arena",
43454346
"rustc_data_structures",

compiler/rustc_pattern_analysis/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
derivative = "2.2.0"
89
rustc_apfloat = "0.2.0"
910
rustc_arena = { path = "../rustc_arena", optional = true }
1011
rustc_data_structures = { path = "../rustc_data_structures", optional = true }

compiler/rustc_pattern_analysis/src/constructor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ impl OpaqueId {
642642
/// `specialize_constructor` returns the list of fields corresponding to a pattern, given a
643643
/// constructor. `Constructor::apply` reconstructs the pattern from a pair of `Constructor` and
644644
/// `Fields`.
645-
#[derive(Clone, Debug, PartialEq)]
645+
#[derive(derivative::Derivative)]
646+
#[derivative(Debug(bound = ""), Clone(bound = ""), PartialEq(bound = ""))]
646647
pub enum Constructor<Cx: TypeCx> {
647648
/// Tuples and structs.
648649
Struct,

compiler/rustc_pattern_analysis/src/lib.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'a, T: ?Sized> Captures<'a> for T {}
4949
/// Context that provides type information about constructors.
5050
///
5151
/// Most of the crate is parameterized on a type that implements this trait.
52-
pub trait TypeCx: Sized + Clone + fmt::Debug {
52+
pub trait TypeCx: Sized + fmt::Debug {
5353
/// The type of a pattern.
5454
type Ty: Copy + Clone + fmt::Debug; // FIXME: remove Copy
5555
/// The index of an enum variant.
@@ -86,26 +86,25 @@ pub trait TypeCx: Sized + Clone + fmt::Debug {
8686
}
8787

8888
/// Context that provides information global to a match.
89-
#[derive(Clone)]
89+
#[derive(derivative::Derivative)]
90+
#[derivative(Clone(bound = ""), Copy(bound = ""))]
9091
pub struct MatchCtxt<'a, 'p, Cx: TypeCx> {
9192
/// The context for type information.
9293
pub tycx: &'a Cx,
9394
/// An arena to store the wildcards we produce during analysis.
9495
pub wildcard_arena: &'a TypedArena<DeconstructedPat<'p, Cx>>,
9596
}
9697

97-
impl<'a, 'p, Cx: TypeCx> Copy for MatchCtxt<'a, 'p, Cx> {}
98-
9998
/// The arm of a match expression.
100-
#[derive(Clone, Debug)]
99+
#[derive(Debug)]
100+
#[derive(derivative::Derivative)]
101+
#[derivative(Clone(bound = ""), Copy(bound = ""))]
101102
pub struct MatchArm<'p, Cx: TypeCx> {
102103
pub pat: &'p DeconstructedPat<'p, Cx>,
103104
pub has_guard: bool,
104105
pub arm_data: Cx::ArmData,
105106
}
106107

107-
impl<'p, Cx: TypeCx> Copy for MatchArm<'p, Cx> {}
108-
109108
/// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are
110109
/// useful, and runs some lints.
111110
#[cfg(feature = "rustc")]

compiler/rustc_pattern_analysis/src/pat.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ impl<'p, Cx: TypeCx> fmt::Debug for DeconstructedPat<'p, Cx> {
160160

161161
/// Same idea as `DeconstructedPat`, except this is a fictitious pattern built up for diagnostics
162162
/// purposes. As such they don't use interning and can be cloned.
163-
#[derive(Debug, Clone)]
163+
#[derive(derivative::Derivative)]
164+
#[derivative(Debug(bound = ""), Clone(bound = ""))]
164165
pub struct WitnessPat<Cx: TypeCx> {
165166
ctor: Constructor<Cx>,
166167
pub(crate) fields: Vec<WitnessPat<Cx>>,

compiler/rustc_pattern_analysis/src/usefulness.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,10 @@ pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
569569
}
570570

571571
/// Context that provides information local to a place under investigation.
572-
#[derive(Clone)]
572+
#[derive(derivative::Derivative)]
573+
#[derivative(Debug(bound = ""), Clone(bound = ""), Copy(bound = ""))]
573574
pub(crate) struct PlaceCtxt<'a, 'p, Cx: TypeCx> {
575+
#[derivative(Debug = "ignore")]
574576
pub(crate) mcx: MatchCtxt<'a, 'p, Cx>,
575577
/// Type of the place under investigation.
576578
pub(crate) ty: Cx::Ty,
@@ -596,14 +598,6 @@ impl<'a, 'p, Cx: TypeCx> PlaceCtxt<'a, 'p, Cx> {
596598
}
597599
}
598600

599-
impl<'a, 'p, Cx: TypeCx> Copy for PlaceCtxt<'a, 'p, Cx> {}
600-
601-
impl<'a, 'p, Cx: TypeCx> fmt::Debug for PlaceCtxt<'a, 'p, Cx> {
602-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
603-
f.debug_struct("PlaceCtxt").field("ty", &self.ty).finish()
604-
}
605-
}
606-
607601
/// Serves two purposes:
608602
/// - in a wildcard, tracks whether the wildcard matches only valid values (i.e. is a binding `_a`)
609603
/// or also invalid values (i.e. is a true `_` pattern).
@@ -670,7 +664,8 @@ impl fmt::Display for ValidityConstraint {
670664
// - 'a allocated by us
671665
// - 'p coming from the input
672666
// - Cx global compilation context
673-
#[derive(Clone)]
667+
#[derive(derivative::Derivative)]
668+
#[derivative(Clone(bound = ""))]
674669
struct PatStack<'a, 'p, Cx: TypeCx> {
675670
// Rows of len 1 are very common, which is why `SmallVec[_; 2]` works well.
676671
pats: SmallVec<[&'a DeconstructedPat<'p, Cx>; 2]>,
@@ -1022,7 +1017,8 @@ impl<'a, 'p, Cx: TypeCx> fmt::Debug for Matrix<'a, 'p, Cx> {
10221017
/// The final `Pair(Some(_), true)` is then the resulting witness.
10231018
///
10241019
/// See the top of the file for more detailed explanations and examples.
1025-
#[derive(Debug, Clone)]
1020+
#[derive(derivative::Derivative)]
1021+
#[derivative(Debug(bound = ""), Clone(bound = ""))]
10261022
struct WitnessStack<Cx: TypeCx>(Vec<WitnessPat<Cx>>);
10271023

10281024
impl<Cx: TypeCx> WitnessStack<Cx> {
@@ -1069,7 +1065,8 @@ impl<Cx: TypeCx> WitnessStack<Cx> {
10691065
///
10701066
/// Just as the `Matrix` starts with a single column, by the end of the algorithm, this has a single
10711067
/// column, which contains the patterns that are missing for the match to be exhaustive.
1072-
#[derive(Debug, Clone)]
1068+
#[derive(derivative::Derivative)]
1069+
#[derivative(Debug(bound = ""), Clone(bound = ""))]
10731070
struct WitnessMatrix<Cx: TypeCx>(Vec<WitnessStack<Cx>>);
10741071

10751072
impl<Cx: TypeCx> WitnessMatrix<Cx> {

0 commit comments

Comments
 (0)