Skip to content

Commit 10b69ab

Browse files
Remove non-descriptive boolean from search_for_structural_match_violation
1 parent c1f54c3 commit 10b69ab

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl Qualif for CustomEq {
226226
// because that component may be part of an enum variant (e.g.,
227227
// `Option::<NonStructuralMatchTy>::Some`), in which case some values of this type may be
228228
// structural-match (`Option::None`).
229-
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty, false).is_some()
229+
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty).is_some()
230230
}
231231

232232
fn in_adt_inherently<'tcx>(

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

+29-31
Original file line numberDiff line numberDiff line change
@@ -120,37 +120,35 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
120120
}
121121

122122
fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
123-
traits::search_for_structural_match_violation(self.span, self.tcx(), ty, false).map(
124-
|non_sm_ty| {
125-
with_no_trimmed_paths!(match non_sm_ty.kind() {
126-
ty::Adt(adt, _) => self.adt_derive_msg(*adt),
127-
ty::Dynamic(..) => {
128-
"trait objects cannot be used in patterns".to_string()
129-
}
130-
ty::Opaque(..) => {
131-
"opaque types cannot be used in patterns".to_string()
132-
}
133-
ty::Closure(..) => {
134-
"closures cannot be used in patterns".to_string()
135-
}
136-
ty::Generator(..) | ty::GeneratorWitness(..) => {
137-
"generators cannot be used in patterns".to_string()
138-
}
139-
ty::Float(..) => {
140-
"floating-point numbers cannot be used in patterns".to_string()
141-
}
142-
ty::FnPtr(..) => {
143-
"function pointers cannot be used in patterns".to_string()
144-
}
145-
ty::RawPtr(..) => {
146-
"raw pointers cannot be used in patterns".to_string()
147-
}
148-
_ => {
149-
bug!("use of a value of `{non_sm_ty}` inside a pattern")
150-
}
151-
})
152-
},
153-
)
123+
traits::search_for_structural_match_violation(self.span, self.tcx(), ty).map(|non_sm_ty| {
124+
with_no_trimmed_paths!(match non_sm_ty.kind() {
125+
ty::Adt(adt, _) => self.adt_derive_msg(*adt),
126+
ty::Dynamic(..) => {
127+
"trait objects cannot be used in patterns".to_string()
128+
}
129+
ty::Opaque(..) => {
130+
"opaque types cannot be used in patterns".to_string()
131+
}
132+
ty::Closure(..) => {
133+
"closures cannot be used in patterns".to_string()
134+
}
135+
ty::Generator(..) | ty::GeneratorWitness(..) => {
136+
"generators cannot be used in patterns".to_string()
137+
}
138+
ty::Float(..) => {
139+
"floating-point numbers cannot be used in patterns".to_string()
140+
}
141+
ty::FnPtr(..) => {
142+
"function pointers cannot be used in patterns".to_string()
143+
}
144+
ty::RawPtr(..) => {
145+
"raw pointers cannot be used in patterns".to_string()
146+
}
147+
_ => {
148+
bug!("use of a value of `{non_sm_ty}` inside a pattern")
149+
}
150+
})
151+
})
154152
}
155153

156154
fn type_marked_structural(&self, ty: Ty<'tcx>) -> bool {

compiler/rustc_trait_selection/src/traits/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError
6060
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};
63-
pub use self::structural_match::search_for_structural_match_violation;
63+
pub use self::structural_match::{
64+
search_for_adt_const_param_violation, search_for_structural_match_violation,
65+
};
6466
pub use self::util::{
6567
elaborate_obligations, elaborate_predicates, elaborate_predicates_with_span,
6668
elaborate_trait_ref, elaborate_trait_refs,

compiler/rustc_trait_selection/src/traits/structural_match.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,28 @@ use std::ops::ControlFlow;
3535
/// For more background on why Rust has this requirement, and issues
3636
/// that arose when the requirement was not enforced completely, see
3737
/// Rust RFC 1445, rust-lang/rust#61188, and rust-lang/rust#62307.
38-
///
39-
/// When the `valtree_semantics` flag is set, then we also deny additional
40-
/// types that are not evaluatable to valtrees, such as floats and fn ptrs.
4138
pub fn search_for_structural_match_violation<'tcx>(
4239
span: Span,
4340
tcx: TyCtxt<'tcx>,
4441
ty: Ty<'tcx>,
45-
valtree_semantics: bool,
4642
) -> Option<Ty<'tcx>> {
47-
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), valtree_semantics })
43+
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), adt_const_param: false })
44+
.break_value()
45+
}
46+
47+
/// This method traverses the structure of `ty`, trying to find any
48+
/// types that are not allowed to be used in a const generic.
49+
///
50+
/// This is either because the type does not implement `StructuralEq`
51+
/// and `StructuralPartialEq`, or because the type is intentionally
52+
/// not supported in const generics (such as floats and raw pointers,
53+
/// which are allowed in match blocks).
54+
pub fn search_for_adt_const_param_violation<'tcx>(
55+
span: Span,
56+
tcx: TyCtxt<'tcx>,
57+
ty: Ty<'tcx>,
58+
) -> Option<Ty<'tcx>> {
59+
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), adt_const_param: true })
4860
.break_value()
4961
}
5062

@@ -108,8 +120,9 @@ struct Search<'tcx> {
108120
seen: FxHashSet<hir::def_id::DefId>,
109121

110122
// Additionally deny things that have been allowed in patterns,
111-
// but are not evaluatable to a valtree, such as floats and fn ptrs.
112-
valtree_semantics: bool,
123+
// but are not allowed in adt const params, such as floats and
124+
// fn ptrs.
125+
adt_const_param: bool,
113126
}
114127

115128
impl<'tcx> Search<'tcx> {
@@ -167,15 +180,15 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
167180
}
168181

169182
ty::FnPtr(..) => {
170-
if !self.valtree_semantics {
183+
if !self.adt_const_param {
171184
return ControlFlow::CONTINUE;
172185
} else {
173186
return ControlFlow::Break(ty);
174187
}
175188
}
176189

177190
ty::RawPtr(..) => {
178-
if !self.valtree_semantics {
191+
if !self.adt_const_param {
179192
// structural-match ignores substructure of
180193
// `*const _`/`*mut _`, so skip `super_visit_with`.
181194
//
@@ -197,7 +210,7 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
197210
}
198211

199212
ty::Float(_) => {
200-
if !self.valtree_semantics {
213+
if !self.adt_const_param {
201214
return ControlFlow::CONTINUE;
202215
} else {
203216
return ControlFlow::Break(ty);

compiler/rustc_typeck/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
849849

850850
if tcx.features().adt_const_params {
851851
if let Some(non_structural_match_ty) =
852-
traits::search_for_structural_match_violation(param.span, tcx, ty, true)
852+
traits::search_for_adt_const_param_violation(param.span, tcx, ty)
853853
{
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

0 commit comments

Comments
 (0)