Skip to content

Commit 402e50a

Browse files
committed
Remove Ty: Copy bound
1 parent 714b29a commit 402e50a

File tree

6 files changed

+32
-31
lines changed

6 files changed

+32
-31
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ fn collect_non_exhaustive_tys<'tcx>(
11131113
non_exhaustive_tys.insert(pat.ty().inner());
11141114
}
11151115
if let Constructor::IntRange(range) = pat.ctor() {
1116-
if cx.is_range_beyond_boundaries(range, pat.ty()) {
1116+
if cx.is_range_beyond_boundaries(range, *pat.ty()) {
11171117
// The range denotes the values before `isize::MIN` or the values after `usize::MAX`/`isize::MAX`.
11181118
non_exhaustive_tys.insert(pat.ty().inner());
11191119
}

compiler/rustc_pattern_analysis/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a, T: ?Sized> Captures<'a> for T {}
4646
/// Most of the crate is parameterized on a type that implements this trait.
4747
pub trait TypeCx: Sized + fmt::Debug {
4848
/// The type of a pattern.
49-
type Ty: Copy + Clone + fmt::Debug; // FIXME: remove Copy
49+
type Ty: Clone + fmt::Debug;
5050
/// Errors that can abort analysis.
5151
type Error: fmt::Debug;
5252
/// The index of an enum variant.
@@ -61,16 +61,16 @@ pub trait TypeCx: Sized + fmt::Debug {
6161
fn is_exhaustive_patterns_feature_on(&self) -> bool;
6262

6363
/// The number of fields for this constructor.
64-
fn ctor_arity(&self, ctor: &Constructor<Self>, ty: Self::Ty) -> usize;
64+
fn ctor_arity(&self, ctor: &Constructor<Self>, ty: &Self::Ty) -> usize;
6565

6666
/// The types of the fields for this constructor. The result must have a length of
6767
/// `ctor_arity()`.
68-
fn ctor_sub_tys(&self, ctor: &Constructor<Self>, ty: Self::Ty) -> &[Self::Ty];
68+
fn ctor_sub_tys(&self, ctor: &Constructor<Self>, ty: &Self::Ty) -> &[Self::Ty];
6969

7070
/// The set of all the constructors for `ty`.
7171
///
7272
/// This must follow the invariants of `ConstructorSet`
73-
fn ctors_for_ty(&self, ty: Self::Ty) -> Result<ConstructorSet<Self>, Self::Error>;
73+
fn ctors_for_ty(&self, ty: &Self::Ty) -> Result<ConstructorSet<Self>, Self::Error>;
7474

7575
/// Best-effort `Debug` implementation.
7676
fn debug_pat(f: &mut fmt::Formatter<'_>, pat: &DeconstructedPat<'_, Self>) -> fmt::Result;

compiler/rustc_pattern_analysis/src/lints.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
4949
}
5050

5151
fn head_ty(&self) -> Option<RevealedTy<'tcx>> {
52-
self.patterns.first().map(|pat| pat.ty())
52+
self.patterns.first().map(|pat| *pat.ty())
5353
}
5454

5555
/// Do constructor splitting on the constructors of the column.
@@ -104,7 +104,7 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
104104
let Some(ty) = column.head_ty() else {
105105
return Ok(Vec::new());
106106
};
107-
let pcx = &PlaceCtxt::new_dummy(cx, ty);
107+
let pcx = &PlaceCtxt::new_dummy(cx, &ty);
108108

109109
let set = column.analyze_ctors(pcx)?;
110110
if set.present.is_empty() {
@@ -203,7 +203,7 @@ pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>(
203203
) {
204204
let rcx = cx.tycx;
205205
for overlap in overlapping_range_endpoints {
206-
let overlap_as_pat = rcx.hoist_pat_range(&overlap.overlaps_on, overlap.pat.ty());
206+
let overlap_as_pat = rcx.hoist_pat_range(&overlap.overlaps_on, *overlap.pat.ty());
207207
let overlaps: Vec<_> = overlap
208208
.overlaps_with
209209
.iter()

compiler/rustc_pattern_analysis/src/pat.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
5454
pub fn ctor(&self) -> &Constructor<Cx> {
5555
&self.ctor
5656
}
57-
pub fn ty(&self) -> Cx::Ty {
58-
self.ty
57+
pub fn ty(&self) -> &Cx::Ty {
58+
&self.ty
5959
}
6060
/// Returns the extra data stored in a pattern. Returns `None` if the pattern is a wildcard that
6161
/// does not correspond to a user-supplied pattern.
@@ -242,15 +242,15 @@ impl<Cx: TypeCx> WitnessPat<Cx> {
242242
/// `Some(_)`.
243243
pub(crate) fn wild_from_ctor(pcx: &PlaceCtxt<'_, Cx>, ctor: Constructor<Cx>) -> Self {
244244
let field_tys = pcx.ctor_sub_tys(&ctor);
245-
let fields = field_tys.iter().map(|ty| Self::wildcard(*ty)).collect();
246-
Self::new(ctor, fields, pcx.ty)
245+
let fields = field_tys.iter().cloned().map(|ty| Self::wildcard(ty)).collect();
246+
Self::new(ctor, fields, pcx.ty.clone())
247247
}
248248

249249
pub fn ctor(&self) -> &Constructor<Cx> {
250250
&self.ctor
251251
}
252-
pub fn ty(&self) -> Cx::Ty {
253-
self.ty
252+
pub fn ty(&self) -> &Cx::Ty {
253+
&self.ty
254254
}
255255

256256
pub fn iter_fields(&self) -> impl Iterator<Item = &WitnessPat<Cx>> {

compiler/rustc_pattern_analysis/src/rustc.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
770770
let mut subpatterns = pat.iter_fields().map(|p| Box::new(cx.hoist_witness_pat(p)));
771771
let kind = match pat.ctor() {
772772
Bool(b) => PatKind::Constant { value: mir::Const::from_bool(cx.tcx, *b) },
773-
IntRange(range) => return self.hoist_pat_range(range, pat.ty()),
773+
IntRange(range) => return self.hoist_pat_range(range, *pat.ty()),
774774
Struct | Variant(_) | UnionField => match pat.ty().kind() {
775775
ty::Tuple(..) => PatKind::Leaf {
776776
subpatterns: subpatterns
@@ -789,7 +789,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
789789
RustcMatchCheckCtxt::variant_index_for_adt(&pat.ctor(), *adt_def);
790790
let variant = &adt_def.variant(variant_index);
791791
let subpatterns = cx
792-
.list_variant_nonhidden_fields(pat.ty(), variant)
792+
.list_variant_nonhidden_fields(*pat.ty(), variant)
793793
.zip(subpatterns)
794794
.map(|((field, _ty), pattern)| FieldPat { field, pattern })
795795
.collect();
@@ -800,7 +800,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
800800
PatKind::Leaf { subpatterns }
801801
}
802802
}
803-
_ => bug!("unexpected ctor for type {:?} {:?}", pat.ctor(), pat.ty()),
803+
_ => bug!("unexpected ctor for type {:?} {:?}", pat.ctor(), *pat.ty()),
804804
},
805805
// Note: given the expansion of `&str` patterns done in `expand_pattern`, we should
806806
// be careful to reconstruct the correct constant pattern here. However a string
@@ -965,21 +965,21 @@ impl<'p, 'tcx> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
965965
self.tcx.features().exhaustive_patterns
966966
}
967967

968-
fn ctor_arity(&self, ctor: &crate::constructor::Constructor<Self>, ty: Self::Ty) -> usize {
969-
self.ctor_arity(ctor, ty)
968+
fn ctor_arity(&self, ctor: &crate::constructor::Constructor<Self>, ty: &Self::Ty) -> usize {
969+
self.ctor_arity(ctor, *ty)
970970
}
971971
fn ctor_sub_tys(
972972
&self,
973973
ctor: &crate::constructor::Constructor<Self>,
974-
ty: Self::Ty,
974+
ty: &Self::Ty,
975975
) -> &[Self::Ty] {
976-
self.ctor_sub_tys(ctor, ty)
976+
self.ctor_sub_tys(ctor, *ty)
977977
}
978978
fn ctors_for_ty(
979979
&self,
980-
ty: Self::Ty,
980+
ty: &Self::Ty,
981981
) -> Result<crate::constructor::ConstructorSet<Self>, Self::Error> {
982-
self.ctors_for_ty(ty)
982+
self.ctors_for_ty(*ty)
983983
}
984984

985985
fn debug_pat(

compiler/rustc_pattern_analysis/src/usefulness.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -736,15 +736,16 @@ pub(crate) struct PlaceCtxt<'a, Cx: TypeCx> {
736736
#[derivative(Debug = "ignore")]
737737
pub(crate) mcx: MatchCtxt<'a, Cx>,
738738
/// Type of the place under investigation.
739-
pub(crate) ty: Cx::Ty,
739+
#[derivative(Clone(clone_with = "Clone::clone"))] // See rust-derivative#90
740+
pub(crate) ty: &'a Cx::Ty,
740741
/// Whether the place is the original scrutinee place, as opposed to a subplace of it.
741742
pub(crate) is_scrutinee: bool,
742743
}
743744

744745
impl<'a, Cx: TypeCx> PlaceCtxt<'a, Cx> {
745746
/// A `PlaceCtxt` when code other than `is_useful` needs one.
746747
#[cfg_attr(not(feature = "rustc"), allow(dead_code))]
747-
pub(crate) fn new_dummy(mcx: MatchCtxt<'a, Cx>, ty: Cx::Ty) -> Self {
748+
pub(crate) fn new_dummy(mcx: MatchCtxt<'a, Cx>, ty: &'a Cx::Ty) -> Self {
748749
PlaceCtxt { mcx, ty, is_scrutinee: false }
749750
}
750751

@@ -1039,8 +1040,8 @@ impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
10391040
matrix
10401041
}
10411042

1042-
fn head_ty(&self) -> Option<Cx::Ty> {
1043-
self.place_ty.first().copied()
1043+
fn head_ty(&self) -> Option<&Cx::Ty> {
1044+
self.place_ty.first()
10441045
}
10451046
fn column_count(&self) -> usize {
10461047
self.place_ty.len()
@@ -1074,7 +1075,7 @@ impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
10741075
let ctor_sub_tys = pcx.ctor_sub_tys(ctor);
10751076
let arity = ctor_sub_tys.len();
10761077
let specialized_place_ty =
1077-
ctor_sub_tys.iter().chain(self.place_ty[1..].iter()).copied().collect();
1078+
ctor_sub_tys.iter().chain(self.place_ty[1..].iter()).cloned().collect();
10781079
let ctor_sub_validity = self.place_validity[0].specialize(ctor);
10791080
let specialized_place_validity = std::iter::repeat(ctor_sub_validity)
10801081
.take(arity)
@@ -1230,7 +1231,7 @@ impl<Cx: TypeCx> WitnessStack<Cx> {
12301231
let len = self.0.len();
12311232
let arity = ctor.arity(pcx);
12321233
let fields = self.0.drain((len - arity)..).rev().collect();
1233-
let pat = WitnessPat::new(ctor.clone(), fields, pcx.ty);
1234+
let pat = WitnessPat::new(ctor.clone(), fields, pcx.ty.clone());
12341235
self.0.push(pat);
12351236
}
12361237
}
@@ -1435,7 +1436,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
14351436
return Ok(WitnessMatrix::empty());
14361437
}
14371438

1438-
let Some(ty) = matrix.head_ty() else {
1439+
let Some(ty) = matrix.head_ty().cloned() else {
14391440
// The base case: there are no columns in the matrix. We are morally pattern-matching on ().
14401441
// A row is useful iff it has no (unguarded) rows above it.
14411442
let mut useful = true; // Whether the next row is useful.
@@ -1456,7 +1457,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
14561457
};
14571458

14581459
debug!("ty: {ty:?}");
1459-
let pcx = &PlaceCtxt { mcx, ty, is_scrutinee: is_top_level };
1460+
let pcx = &PlaceCtxt { mcx, ty: &ty, is_scrutinee: is_top_level };
14601461

14611462
// Whether the place/column we are inspecting is known to contain valid data.
14621463
let place_validity = matrix.place_validity[0];

0 commit comments

Comments
 (0)