Skip to content

Commit c1f54c3

Browse files
Get rid of redundant NonStructuralMatchTyKind
1 parent 1152e70 commit c1f54c3

File tree

4 files changed

+28
-73
lines changed

4 files changed

+28
-73
lines changed

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

+11-17
Original file line numberDiff line numberDiff line change
@@ -122,37 +122,31 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
122122
fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
123123
traits::search_for_structural_match_violation(self.span, self.tcx(), ty, false).map(
124124
|non_sm_ty| {
125-
with_no_trimmed_paths!(match non_sm_ty.kind {
126-
traits::NonStructuralMatchTyKind::Adt(adt) => self.adt_derive_msg(adt),
127-
traits::NonStructuralMatchTyKind::Dynamic => {
125+
with_no_trimmed_paths!(match non_sm_ty.kind() {
126+
ty::Adt(adt, _) => self.adt_derive_msg(*adt),
127+
ty::Dynamic(..) => {
128128
"trait objects cannot be used in patterns".to_string()
129129
}
130-
traits::NonStructuralMatchTyKind::Opaque => {
130+
ty::Opaque(..) => {
131131
"opaque types cannot be used in patterns".to_string()
132132
}
133-
traits::NonStructuralMatchTyKind::Closure => {
133+
ty::Closure(..) => {
134134
"closures cannot be used in patterns".to_string()
135135
}
136-
traits::NonStructuralMatchTyKind::Generator => {
136+
ty::Generator(..) | ty::GeneratorWitness(..) => {
137137
"generators cannot be used in patterns".to_string()
138138
}
139-
traits::NonStructuralMatchTyKind::Float => {
139+
ty::Float(..) => {
140140
"floating-point numbers cannot be used in patterns".to_string()
141141
}
142-
traits::NonStructuralMatchTyKind::FnPtr => {
142+
ty::FnPtr(..) => {
143143
"function pointers cannot be used in patterns".to_string()
144144
}
145-
traits::NonStructuralMatchTyKind::RawPtr => {
145+
ty::RawPtr(..) => {
146146
"raw pointers cannot be used in patterns".to_string()
147147
}
148-
traits::NonStructuralMatchTyKind::Param => {
149-
bug!("use of a constant whose type is a parameter inside a pattern")
150-
}
151-
traits::NonStructuralMatchTyKind::Projection => {
152-
bug!("use of a constant whose type is a projection inside a pattern")
153-
}
154-
traits::NonStructuralMatchTyKind::Foreign => {
155-
bug!("use of a value of a foreign type inside a pattern")
148+
_ => {
149+
bug!("use of a value of `{non_sm_ty}` inside a pattern")
156150
}
157151
})
158152
},

compiler/rustc_trait_selection/src/traits/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub use self::specialize::specialization_graph::FutureCompatOverlapError;
6161
pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind;
6262
pub use self::specialize::{specialization_graph, translate_substs, OverlapError};
6363
pub use self::structural_match::search_for_structural_match_violation;
64-
pub use self::structural_match::{NonStructuralMatchTy, NonStructuralMatchTyKind};
6564
pub use self::util::{
6665
elaborate_obligations, elaborate_predicates, elaborate_predicates_with_span,
6766
elaborate_trait_ref, elaborate_trait_refs,

compiler/rustc_trait_selection/src/traits/structural_match.rs

+14-52
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,10 @@ use rustc_data_structures::fx::FxHashSet;
66
use rustc_hir as hir;
77
use rustc_hir::lang_items::LangItem;
88
use rustc_middle::ty::query::Providers;
9-
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
9+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
1010
use rustc_span::Span;
1111
use std::ops::ControlFlow;
1212

13-
#[derive(Debug)]
14-
pub struct NonStructuralMatchTy<'tcx> {
15-
pub ty: Ty<'tcx>,
16-
pub kind: NonStructuralMatchTyKind<'tcx>,
17-
}
18-
19-
#[derive(Debug)]
20-
pub enum NonStructuralMatchTyKind<'tcx> {
21-
Adt(AdtDef<'tcx>),
22-
Param,
23-
Dynamic,
24-
Foreign,
25-
Opaque,
26-
Closure,
27-
Generator,
28-
Projection,
29-
Float,
30-
FnPtr,
31-
RawPtr,
32-
}
33-
3413
/// This method traverses the structure of `ty`, trying to find an
3514
/// instance of an ADT (i.e. struct or enum) that doesn't implement
3615
/// the structural-match traits, or a generic type parameter
@@ -64,7 +43,7 @@ pub fn search_for_structural_match_violation<'tcx>(
6443
tcx: TyCtxt<'tcx>,
6544
ty: Ty<'tcx>,
6645
valtree_semantics: bool,
67-
) -> Option<NonStructuralMatchTy<'tcx>> {
46+
) -> Option<Ty<'tcx>> {
6847
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), valtree_semantics })
6948
.break_value()
7049
}
@@ -140,40 +119,33 @@ impl<'tcx> Search<'tcx> {
140119
}
141120

142121
impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
143-
type BreakTy = NonStructuralMatchTy<'tcx>;
122+
type BreakTy = Ty<'tcx>;
144123

145124
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
146125
debug!("Search visiting ty: {:?}", ty);
147126

148127
let (adt_def, substs) = match *ty.kind() {
149128
ty::Adt(adt_def, substs) => (adt_def, substs),
150129
ty::Param(_) => {
151-
let kind = NonStructuralMatchTyKind::Param;
152-
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
130+
return ControlFlow::Break(ty);
153131
}
154132
ty::Dynamic(..) => {
155-
let kind = NonStructuralMatchTyKind::Dynamic;
156-
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
133+
return ControlFlow::Break(ty);
157134
}
158135
ty::Foreign(_) => {
159-
let kind = NonStructuralMatchTyKind::Foreign;
160-
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
136+
return ControlFlow::Break(ty);
161137
}
162138
ty::Opaque(..) => {
163-
let kind = NonStructuralMatchTyKind::Opaque;
164-
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
139+
return ControlFlow::Break(ty);
165140
}
166141
ty::Projection(..) => {
167-
let kind = NonStructuralMatchTyKind::Projection;
168-
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
142+
return ControlFlow::Break(ty);
169143
}
170144
ty::Closure(..) => {
171-
let kind = NonStructuralMatchTyKind::Closure;
172-
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
145+
return ControlFlow::Break(ty);
173146
}
174147
ty::Generator(..) | ty::GeneratorWitness(..) => {
175-
let kind = NonStructuralMatchTyKind::Generator;
176-
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
148+
return ControlFlow::Break(ty);
177149
}
178150
ty::FnDef(..) => {
179151
// Types of formals and return in `fn(_) -> _` are also irrelevant;
@@ -198,10 +170,7 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
198170
if !self.valtree_semantics {
199171
return ControlFlow::CONTINUE;
200172
} else {
201-
return ControlFlow::Break(NonStructuralMatchTy {
202-
ty,
203-
kind: NonStructuralMatchTyKind::FnPtr,
204-
});
173+
return ControlFlow::Break(ty);
205174
}
206175
}
207176

@@ -223,21 +192,15 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
223192
// pointer. Therefore, one can still use `C` in a pattern.
224193
return ControlFlow::CONTINUE;
225194
} else {
226-
return ControlFlow::Break(NonStructuralMatchTy {
227-
ty,
228-
kind: NonStructuralMatchTyKind::FnPtr,
229-
});
195+
return ControlFlow::Break(ty);
230196
}
231197
}
232198

233199
ty::Float(_) => {
234200
if !self.valtree_semantics {
235201
return ControlFlow::CONTINUE;
236202
} else {
237-
return ControlFlow::Break(NonStructuralMatchTy {
238-
ty,
239-
kind: NonStructuralMatchTyKind::Float,
240-
});
203+
return ControlFlow::Break(ty);
241204
}
242205
}
243206

@@ -263,8 +226,7 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
263226

264227
if !self.type_marked_structural(ty) {
265228
debug!("Search found ty: {:?}", ty);
266-
let kind = NonStructuralMatchTyKind::Adt(adt_def);
267-
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
229+
return ControlFlow::Break(ty);
268230
}
269231

270232
// structural-match does not care about the

compiler/rustc_typeck/src/check/wfcheck.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
854854
// We use the same error code in both branches, because this is really the same
855855
// issue: we just special-case the message for type parameters to make it
856856
// clearer.
857-
match non_structural_match_ty.ty.kind() {
857+
match non_structural_match_ty.kind() {
858858
ty::Param(_) => {
859859
// Const parameters may not have type parameters as their types,
860860
// because we cannot be sure that the type parameter derives `PartialEq`
@@ -911,10 +911,10 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
911911
E0741,
912912
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
913913
the type of a const parameter",
914-
non_structural_match_ty.ty,
914+
non_structural_match_ty,
915915
);
916916

917-
if ty == non_structural_match_ty.ty {
917+
if ty == non_structural_match_ty {
918918
diag.span_label(
919919
hir_ty.span,
920920
format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"),

0 commit comments

Comments
 (0)