Skip to content

Commit 4e6a401

Browse files
committed
review comments: reword messages and simplify logic
1 parent da58406 commit 4e6a401

10 files changed

+48
-68
lines changed

compiler/rustc_mir_build/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ mir_build_const_param_in_pattern = constant parameters cannot be referenced in p
9090
.label = can't be used in patterns
9191
mir_build_const_param_in_pattern_def = constant defined here
9292
93-
mir_build_const_pattern_depends_on_generic_parameter = constant pattern depends on a generic parameter, which is not allowed
93+
mir_build_const_pattern_depends_on_generic_parameter = constant pattern cannot depend on generic parameters
9494
.label = `const` depends on a generic parameter
9595
9696
mir_build_could_not_eval_const_pattern = could not evaluate constant pattern

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

+22-42
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_abi::{FieldIdx, VariantIdx};
22
use rustc_apfloat::Float;
33
use rustc_data_structures::fx::FxHashSet;
4-
use rustc_errors::{Diag, PResult};
4+
use rustc_errors::Diag;
55
use rustc_hir as hir;
66
use rustc_index::Idx;
77
use rustc_infer::infer::TyCtxtInferExt;
@@ -42,10 +42,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
4242

4343
match c.kind() {
4444
ty::ConstKind::Unevaluated(uv) => convert.unevaluated_to_pat(uv, ty),
45-
ty::ConstKind::Value(_, val) => match convert.valtree_to_pat(val, ty) {
46-
Ok(pat) => pat,
47-
Err(err) => convert.mk_err(err, ty),
48-
},
45+
ty::ConstKind::Value(_, val) => convert.valtree_to_pat(val, ty),
4946
_ => span_bug!(span, "Invalid `ConstKind` for `const_to_pat`: {:?}", c),
5047
}
5148
}
@@ -151,7 +148,7 @@ impl<'tcx> ConstToPat<'tcx> {
151148
let generics = self.tcx.generics_of(def_id);
152149
let param = generics.type_param(*param_ty, self.tcx);
153150
let span = self.tcx.def_span(param.def_id);
154-
e.span_label(span, "constant depends on this generic param");
151+
e.span_label(span, "constant depends on this generic parameter");
155152
if let Some(ident) = self.tcx.def_ident_span(def_id)
156153
&& self.tcx.sess.source_map().is_multiline(ident.between(span))
157154
{
@@ -184,10 +181,7 @@ impl<'tcx> ConstToPat<'tcx> {
184181
};
185182

186183
// Convert the valtree to a const.
187-
let inlined_const_as_pat = match self.valtree_to_pat(valtree, ty) {
188-
Ok(pat) => pat,
189-
Err(err) => self.mk_err(err, ty),
190-
};
184+
let inlined_const_as_pat = self.valtree_to_pat(valtree, ty);
191185

192186
if !inlined_const_as_pat.references_error() {
193187
// Always check for `PartialEq` if we had no other errors yet.
@@ -210,33 +204,27 @@ impl<'tcx> ConstToPat<'tcx> {
210204
let field = FieldIdx::new(idx);
211205
// Patterns can only use monomorphic types.
212206
let ty = self.tcx.normalize_erasing_regions(self.typing_env, ty);
213-
FieldPat {
214-
field,
215-
pattern: match self.valtree_to_pat(val, ty) {
216-
Ok(pat) => pat,
217-
Err(err) => self.mk_err(err, ty),
218-
},
219-
}
207+
FieldPat { field, pattern: self.valtree_to_pat(val, ty) }
220208
})
221209
.collect()
222210
}
223211

224212
// Recursive helper for `to_pat`; invoke that (instead of calling this directly).
225213
#[instrument(skip(self), level = "debug")]
226-
fn valtree_to_pat(&self, cv: ValTree<'tcx>, ty: Ty<'tcx>) -> PResult<'_, Box<Pat<'tcx>>> {
214+
fn valtree_to_pat(&self, cv: ValTree<'tcx>, ty: Ty<'tcx>) -> Box<Pat<'tcx>> {
227215
let span = self.span;
228216
let tcx = self.tcx;
229217
let kind = match ty.kind() {
230218
ty::Adt(adt_def, _) if !self.type_marked_structural(ty) => {
231219
// Extremely important check for all ADTs! Make sure they opted-in to be used in
232220
// patterns.
233221
debug!("adt_def {:?} has !type_marked_structural for cv.ty: {:?}", adt_def, ty);
234-
let (impls_partial_eq, derived, structural, impl_def_id) =
222+
let (_impls_partial_eq, derived, structural, impl_def_id) =
235223
type_has_partial_eq_impl(self.tcx, self.typing_env, ty);
236224
let (manual_partialeq_impl_span, manual_partialeq_impl_note) =
237-
match (impls_partial_eq, derived, structural, impl_def_id) {
238-
(_, _, true, _) => (None, false),
239-
(_, false, _, Some(def_id)) if def_id.is_local() => {
225+
match (structural, impl_def_id) {
226+
(true, _) => (None, false),
227+
(_, Some(def_id)) if def_id.is_local() && !derived => {
240228
(Some(tcx.def_span(def_id)), false)
241229
}
242230
_ => (None, true),
@@ -249,7 +237,7 @@ impl<'tcx> ConstToPat<'tcx> {
249237
manual_partialeq_impl_span,
250238
manual_partialeq_impl_note,
251239
};
252-
return Err(tcx.dcx().create_err(err));
240+
return self.mk_err(tcx.dcx().create_err(err), ty);
253241
}
254242
ty::Adt(adt_def, args) if adt_def.is_enum() => {
255243
let (&variant_index, fields) = cv.unwrap_branch().split_first().unwrap();
@@ -283,10 +271,7 @@ impl<'tcx> ConstToPat<'tcx> {
283271
prefix: cv
284272
.unwrap_branch()
285273
.iter()
286-
.map(|val| match self.valtree_to_pat(*val, *elem_ty) {
287-
Ok(pat) => pat,
288-
Err(err) => self.mk_err(err, ty),
289-
})
274+
.map(|val| self.valtree_to_pat(*val, *elem_ty))
290275
.collect(),
291276
slice: None,
292277
suffix: Box::new([]),
@@ -295,10 +280,7 @@ impl<'tcx> ConstToPat<'tcx> {
295280
prefix: cv
296281
.unwrap_branch()
297282
.iter()
298-
.map(|val| match self.valtree_to_pat(*val, *elem_ty) {
299-
Ok(pat) => pat,
300-
Err(err) => self.mk_err(err, ty),
301-
})
283+
.map(|val| self.valtree_to_pat(*val, *elem_ty))
302284
.collect(),
303285
slice: None,
304286
suffix: Box::new([]),
@@ -314,9 +296,10 @@ impl<'tcx> ConstToPat<'tcx> {
314296
// deref pattern.
315297
_ => {
316298
if !pointee_ty.is_sized(tcx, self.typing_env) && !pointee_ty.is_slice() {
317-
return Err(tcx
318-
.dcx()
319-
.create_err(UnsizedPattern { span, non_sm_ty: *pointee_ty }));
299+
return self.mk_err(
300+
tcx.dcx().create_err(UnsizedPattern { span, non_sm_ty: *pointee_ty }),
301+
ty,
302+
);
320303
} else {
321304
// `b"foo"` produces a `&[u8; 3]`, but you can't use constants of array type when
322305
// matching against references, you can only use byte string literals.
@@ -331,10 +314,7 @@ impl<'tcx> ConstToPat<'tcx> {
331314
_ => *pointee_ty,
332315
};
333316
// References have the same valtree representation as their pointee.
334-
let subpattern = match self.valtree_to_pat(cv, pointee_ty) {
335-
Ok(pat) => pat,
336-
Err(err) => self.mk_err(err, ty),
337-
};
317+
let subpattern = self.valtree_to_pat(cv, pointee_ty);
338318
PatKind::Deref { subpattern }
339319
}
340320
}
@@ -350,7 +330,7 @@ impl<'tcx> ConstToPat<'tcx> {
350330
if is_nan {
351331
// NaNs are not ever equal to anything so they make no sense as patterns.
352332
// Also see <https://github.com/rust-lang/rfcs/pull/3535>.
353-
return Err(tcx.dcx().create_err(NaNPattern { span }));
333+
return self.mk_err(tcx.dcx().create_err(NaNPattern { span }), ty);
354334
} else {
355335
PatKind::Constant {
356336
value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)),
@@ -373,16 +353,16 @@ impl<'tcx> ConstToPat<'tcx> {
373353
non_sm_ty: ty,
374354
prefix: ty.prefix_string(tcx).to_string(),
375355
};
376-
return Err(tcx.dcx().create_err(err));
356+
return self.mk_err(tcx.dcx().create_err(err), ty);
377357
}
378358
};
379359

380-
Ok(Box::new(Pat { span, ty, kind }))
360+
Box::new(Pat { span, ty, kind })
381361
}
382362
}
383363

384364
/// Given a type with type parameters, visit every ADT looking for types that need to
385-
/// `#[derive(PartialEq)]` to be a structural type.
365+
/// `#[derive(PartialEq)]` for it to be a structural type.
386366
fn extend_type_not_partial_eq<'tcx>(
387367
tcx: TyCtxt<'tcx>,
388368
typing_env: ty::TypingEnv<'tcx>,

tests/ui/associated-consts/associated-const-type-parameter-pattern.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ impl Foo for Def {
1818
pub fn test<A: Foo, B: Foo>(arg: EFoo) {
1919
match arg {
2020
A::X => println!("A::X"),
21-
//~^ error: constant pattern depends on a generic parameter
21+
//~^ ERROR constant pattern cannot depend on generic parameters
2222
B::X => println!("B::X"),
23-
//~^ error: constant pattern depends on a generic parameter
23+
//~^ ERROR constant pattern cannot depend on generic parameters
2424
_ => (),
2525
}
2626
}
2727

2828
pub fn test_let_pat<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
29-
//~^ ERROR constant pattern depends on a generic parameter
29+
//~^ ERROR constant pattern cannot depend on generic parameters
3030
let A::X = arg;
31-
//~^ ERROR constant pattern depends on a generic parameter
31+
//~^ ERROR constant pattern cannot depend on generic parameters
3232
}
3333

3434
fn main() {

tests/ui/associated-consts/associated-const-type-parameter-pattern.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0158]: constant pattern depends on a generic parameter, which is not allowed
1+
error[E0158]: constant pattern cannot depend on generic parameters
22
--> $DIR/associated-const-type-parameter-pattern.rs:20:9
33
|
44
LL | pub trait Foo {
@@ -7,12 +7,12 @@ LL | const X: EFoo;
77
| ------------- constant defined here
88
...
99
LL | pub fn test<A: Foo, B: Foo>(arg: EFoo) {
10-
| - constant depends on this generic param
10+
| - constant depends on this generic parameter
1111
LL | match arg {
1212
LL | A::X => println!("A::X"),
1313
| ^^^^ `const` depends on a generic parameter
1414

15-
error[E0158]: constant pattern depends on a generic parameter, which is not allowed
15+
error[E0158]: constant pattern cannot depend on generic parameters
1616
--> $DIR/associated-const-type-parameter-pattern.rs:22:9
1717
|
1818
LL | pub trait Foo {
@@ -21,12 +21,12 @@ LL | const X: EFoo;
2121
| ------------- constant defined here
2222
...
2323
LL | pub fn test<A: Foo, B: Foo>(arg: EFoo) {
24-
| - constant depends on this generic param
24+
| - constant depends on this generic parameter
2525
...
2626
LL | B::X => println!("B::X"),
2727
| ^^^^ `const` depends on a generic parameter
2828

29-
error[E0158]: constant pattern depends on a generic parameter, which is not allowed
29+
error[E0158]: constant pattern cannot depend on generic parameters
3030
--> $DIR/associated-const-type-parameter-pattern.rs:30:9
3131
|
3232
LL | pub trait Foo {
@@ -35,12 +35,12 @@ LL | const X: EFoo;
3535
| ------------- constant defined here
3636
...
3737
LL | pub fn test_let_pat<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
38-
| - constant depends on this generic param
38+
| - constant depends on this generic parameter
3939
LL |
4040
LL | let A::X = arg;
4141
| ^^^^ `const` depends on a generic parameter
4242

43-
error[E0158]: constant pattern depends on a generic parameter, which is not allowed
43+
error[E0158]: constant pattern cannot depend on generic parameters
4444
--> $DIR/associated-const-type-parameter-pattern.rs:28:48
4545
|
4646
LL | pub trait Foo {
@@ -51,7 +51,7 @@ LL | const X: EFoo;
5151
LL | pub fn test_let_pat<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
5252
| - ^^^^ `const` depends on a generic parameter
5353
| |
54-
| constant depends on this generic param
54+
| constant depends on this generic parameter
5555

5656
error: aborting due to 4 previous errors
5757

tests/ui/consts/issue-73976-polymorphic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<T: 'static> GetTypeId<T> {
1818

1919
const fn check_type_id<T: 'static>() -> bool {
2020
matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
21-
//~^ ERROR constant pattern depends on a generic parameter
21+
//~^ ERROR constant pattern cannot depend on generic parameters
2222
}
2323

2424
pub struct GetTypeNameLen<T>(T);
@@ -29,7 +29,7 @@ impl<T: 'static> GetTypeNameLen<T> {
2929

3030
const fn check_type_name_len<T: 'static>() -> bool {
3131
matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
32-
//~^ ERROR constant pattern depends on a generic parameter
32+
//~^ ERROR constant pattern cannot depend on generic parameters
3333
}
3434

3535
fn main() {

tests/ui/consts/issue-73976-polymorphic.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0158]: constant pattern depends on a generic parameter, which is not allowed
1+
error[E0158]: constant pattern cannot depend on generic parameters
22
--> $DIR/issue-73976-polymorphic.rs:20:37
33
|
44
LL | impl<T: 'static> GetTypeId<T> {
@@ -7,11 +7,11 @@ LL | pub const VALUE: TypeId = TypeId::of::<T>();
77
| ----------------------- constant defined here
88
...
99
LL | const fn check_type_id<T: 'static>() -> bool {
10-
| - constant depends on this generic param
10+
| - constant depends on this generic parameter
1111
LL | matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
1212
| ^^^^^^^^^^^^^^^^^^^^^ `const` depends on a generic parameter
1313

14-
error[E0158]: constant pattern depends on a generic parameter, which is not allowed
14+
error[E0158]: constant pattern cannot depend on generic parameters
1515
--> $DIR/issue-73976-polymorphic.rs:31:42
1616
|
1717
LL | impl<T: 'static> GetTypeNameLen<T> {
@@ -20,7 +20,7 @@ LL | pub const VALUE: usize = any::type_name::<T>().len();
2020
| ---------------------- constant defined here
2121
...
2222
LL | const fn check_type_name_len<T: 'static>() -> bool {
23-
| - constant depends on this generic param
23+
| - constant depends on this generic parameter
2424
LL | matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `const` depends on a generic parameter
2626

tests/ui/consts/issue-79137-toogeneric.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl<T> GetVariantCount<T> {
1010

1111
const fn check_variant_count<T>() -> bool {
1212
matches!(GetVariantCount::<T>::VALUE, GetVariantCount::<T>::VALUE)
13-
//~^ ERROR constant pattern depends on a generic parameter
13+
//~^ ERROR constant pattern cannot depend on generic parameters
1414
}
1515

1616
fn main() {

tests/ui/consts/issue-79137-toogeneric.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0158]: constant pattern depends on a generic parameter, which is not allowed
1+
error[E0158]: constant pattern cannot depend on generic parameters
22
--> $DIR/issue-79137-toogeneric.rs:12:43
33
|
44
LL | impl<T> GetVariantCount<T> {
@@ -7,7 +7,7 @@ LL | pub const VALUE: usize = std::mem::variant_count::<T>();
77
| ---------------------- constant defined here
88
...
99
LL | const fn check_variant_count<T>() -> bool {
10-
| - constant depends on this generic param
10+
| - constant depends on this generic parameter
1111
LL | matches!(GetVariantCount::<T>::VALUE, GetVariantCount::<T>::VALUE)
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `const` depends on a generic parameter
1313

tests/ui/inline-const/const-match-pat-generic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
fn foo<const V: usize>() {
66
match 0 {
77
const { V } => {},
8-
//~^ ERROR constant pattern depends on a generic parameter
8+
//~^ ERROR constant pattern cannot depend on generic parameters
99
_ => {},
1010
}
1111
}
@@ -17,7 +17,7 @@ const fn f(x: usize) -> usize {
1717
fn bar<const V: usize>() {
1818
match 0 {
1919
const { f(V) } => {},
20-
//~^ ERROR constant pattern depends on a generic parameter
20+
//~^ ERROR constant pattern cannot depend on generic parameters
2121
_ => {},
2222
}
2323
}

tests/ui/inline-const/const-match-pat-generic.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0158]: constant pattern depends on a generic parameter, which is not allowed
1+
error[E0158]: constant pattern cannot depend on generic parameters
22
--> $DIR/const-match-pat-generic.rs:7:9
33
|
44
LL | const { V } => {},
55
| ^^^^^^^^^^^ `const` depends on a generic parameter
66

7-
error[E0158]: constant pattern depends on a generic parameter, which is not allowed
7+
error[E0158]: constant pattern cannot depend on generic parameters
88
--> $DIR/const-match-pat-generic.rs:19:9
99
|
1010
LL | const { f(V) } => {},

0 commit comments

Comments
 (0)