Skip to content

Commit f7edbcb

Browse files
committed
Tweak output of some const pattern errors
- Add primary span labels. - Point at const generic parameter used as pattern. - Point at statics used as pattern. - Point at let bindings used in const pattern.
1 parent 913558f commit f7edbcb

File tree

12 files changed

+57
-24
lines changed

12 files changed

+57
-24
lines changed

compiler/rustc_mir_build/messages.ftl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ mir_build_confused = missing patterns are not covered because `{$variable}` is i
8686
8787
mir_build_const_defined_here = constant defined here
8888
89-
mir_build_const_param_in_pattern = const parameters cannot be referenced in patterns
89+
mir_build_const_param_in_pattern = constant parameters cannot be referenced in patterns
90+
.label = can't be used in patterns
91+
mir_build_const_param_in_pattern_def = constant defined here
9092
9193
mir_build_const_pattern_depends_on_generic_parameter = constant pattern depends on a generic parameter, which is not allowed
9294
.label = `const` depends on a generic parameter
@@ -237,10 +239,12 @@ mir_build_mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsa
237239
.label = mutation of layout constrained field
238240
239241
mir_build_nan_pattern = cannot use NaN in patterns
242+
.label = evaluates to `NaN`, which is not allowed in patterns
240243
.note = NaNs compare inequal to everything, even themselves, so this pattern would never match
241244
.help = try using the `is_nan` method instead
242245
243246
mir_build_non_const_path = runtime values cannot be referenced in patterns
247+
.label = references a runtime value
244248
245249
mir_build_non_empty_never_pattern =
246250
mismatched types
@@ -260,6 +264,7 @@ mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type
260264
261265
mir_build_non_partial_eq_match =
262266
to use a constant of type `{$non_peq_ty}` in a pattern, the type must implement `PartialEq`
267+
.label = constant of non-structural type
263268
264269
mir_build_pattern_not_covered = refutable pattern in {$origin}
265270
.pattern_ty = the matched value is of type `{$pattern_ty}`
@@ -278,6 +283,8 @@ mir_build_rustc_box_attribute_error = `#[rustc_box]` attribute used incorrectly
278283
.missing_box = `#[rustc_box]` requires the `owned_box` lang item
279284
280285
mir_build_static_in_pattern = statics cannot be referenced in patterns
286+
.label = can't be used in patterns
287+
mir_build_static_in_pattern_def = `static` defined here
281288
282289
mir_build_suggest_attempted_int_lit = alternatively, you could prepend the pattern with an underscore to define a new named variable; identifiers cannot begin with digits
283290
@@ -329,6 +336,7 @@ mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
329336
.label = access to union field
330337
331338
mir_build_union_pattern = cannot use unions in constant patterns
339+
.label = can't use a `union` here
332340
333341
mir_build_unreachable_making_this_unreachable = collectively making this unreachable
334342

compiler/rustc_mir_build/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,20 +564,27 @@ pub(crate) struct NonExhaustiveMatchAllArmsGuarded;
564564
#[diag(mir_build_static_in_pattern, code = E0158)]
565565
pub(crate) struct StaticInPattern {
566566
#[primary_span]
567+
#[label]
567568
pub(crate) span: Span,
569+
#[label(mir_build_static_in_pattern_def)]
570+
pub(crate) static_span: Span,
568571
}
569572

570573
#[derive(Diagnostic)]
571574
#[diag(mir_build_const_param_in_pattern, code = E0158)]
572575
pub(crate) struct ConstParamInPattern {
573576
#[primary_span]
577+
#[label]
574578
pub(crate) span: Span,
579+
#[label(mir_build_const_param_in_pattern_def)]
580+
pub(crate) const_span: Span,
575581
}
576582

577583
#[derive(Diagnostic)]
578584
#[diag(mir_build_non_const_path, code = E0080)]
579585
pub(crate) struct NonConstPath {
580586
#[primary_span]
587+
#[label]
581588
pub(crate) span: Span,
582589
}
583590

@@ -780,6 +787,7 @@ pub(crate) enum Conflict {
780787
#[diag(mir_build_union_pattern)]
781788
pub(crate) struct UnionPattern {
782789
#[primary_span]
790+
#[label]
783791
pub(crate) span: Span,
784792
}
785793

@@ -797,6 +805,7 @@ pub(crate) struct TypeNotStructural<'tcx> {
797805
#[diag(mir_build_non_partial_eq_match)]
798806
pub(crate) struct TypeNotPartialEq<'tcx> {
799807
#[primary_span]
808+
#[label]
800809
pub(crate) span: Span,
801810
pub(crate) non_peq_ty: Ty<'tcx>,
802811
}
@@ -823,6 +832,7 @@ pub(crate) struct UnsizedPattern<'tcx> {
823832
#[help]
824833
pub(crate) struct NaNPattern {
825834
#[primary_span]
835+
#[label]
826836
pub(crate) span: Span,
827837
}
828838

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,17 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
515515
| Res::SelfCtor(..) => PatKind::Leaf { subpatterns },
516516
_ => {
517517
let e = match res {
518-
Res::Def(DefKind::ConstParam, _) => {
519-
self.tcx.dcx().emit_err(ConstParamInPattern { span })
518+
Res::Def(DefKind::ConstParam, def_id) => {
519+
self.tcx.dcx().emit_err(ConstParamInPattern {
520+
span,
521+
const_span: self.tcx().def_span(def_id),
522+
})
520523
}
521-
Res::Def(DefKind::Static { .. }, _) => {
522-
self.tcx.dcx().emit_err(StaticInPattern { span })
524+
Res::Def(DefKind::Static { .. }, def_id) => {
525+
self.tcx.dcx().emit_err(StaticInPattern {
526+
span,
527+
static_span: self.tcx().def_span(def_id),
528+
})
523529
}
524530
_ => self.tcx.dcx().emit_err(NonConstPath { span }),
525531
};

tests/ui/binding/const-param.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
fn check<const N: usize>() {
44
match 1 {
5-
N => {} //~ ERROR const parameters cannot be referenced in patterns
5+
N => {} //~ ERROR constant parameters cannot be referenced in patterns
66
_ => {}
77
}
88
}

tests/ui/binding/const-param.stderr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error[E0158]: const parameters cannot be referenced in patterns
1+
error[E0158]: constant parameters cannot be referenced in patterns
22
--> $DIR/const-param.rs:5:9
33
|
4+
LL | fn check<const N: usize>() {
5+
| -------------- constant defined here
6+
LL | match 1 {
47
LL | N => {}
5-
| ^
8+
| ^ can't be used in patterns
69

710
error: aborting due to 1 previous error
811

tests/ui/consts/const_in_pattern/issue-65466.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const C: &[O<B>] = &[O::None];
55
| ---------------- constant defined here
66
...
77
LL | C => (),
8-
| ^
8+
| ^ constant of non-structural type
99

1010
error: aborting due to 1 previous error
1111

tests/ui/consts/const_in_pattern/reject_non_partial_eq.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const NO_PARTIAL_EQ_NONE: Option<NoPartialEq> = None;
55
| --------------------------------------------- constant defined here
66
...
77
LL | NO_PARTIAL_EQ_NONE => println!("NO_PARTIAL_EQ_NONE"),
8-
| ^^^^^^^^^^^^^^^^^^
8+
| ^^^^^^^^^^^^^^^^^^ constant of non-structural type
99

1010
error: aborting due to 1 previous error
1111

tests/ui/match/issue-72896-non-partial-eq-const.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const CONST_SET: EnumSet<Enum8> = EnumSet { __enumset_underlying: 3 };
55
| ------------------------------- constant defined here
66
...
77
LL | CONST_SET => { /* ok */ }
8-
| ^^^^^^^^^
8+
| ^^^^^^^^^ constant of non-structural type
99

1010
error: aborting due to 1 previous error
1111

tests/ui/pattern/non-constant-in-const-path.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@ error[E0080]: runtime values cannot be referenced in patterns
22
--> $DIR/non-constant-in-const-path.rs:8:15
33
|
44
LL | let 0u8..=x = 0;
5-
| ^
5+
| ^ references a runtime value
66

77
error[E0158]: statics cannot be referenced in patterns
88
--> $DIR/non-constant-in-const-path.rs:10:15
99
|
10+
LL | static FOO: u8 = 10;
11+
| -------------- `static` defined here
12+
...
1013
LL | let 0u8..=FOO = 0;
11-
| ^^^
14+
| ^^^ can't be used in patterns
1215

1316
error[E0080]: runtime values cannot be referenced in patterns
1417
--> $DIR/non-constant-in-const-path.rs:13:15
1518
|
1619
LL | 0 ..= x => {}
17-
| ^
20+
| ^ references a runtime value
1821

1922
error[E0158]: statics cannot be referenced in patterns
2023
--> $DIR/non-constant-in-const-path.rs:15:15
2124
|
25+
LL | static FOO: u8 = 10;
26+
| -------------- `static` defined here
27+
...
2228
LL | 0 ..= FOO => {}
23-
| ^^^
29+
| ^^^ can't be used in patterns
2430

2531
error: aborting due to 4 previous errors
2632

tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-61188-match-slice-forbidden-without-eq.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const A: &[B] = &[];
55
| ------------- constant defined here
66
...
77
LL | A => (),
8-
| ^
8+
| ^ constant of non-structural type
99

1010
error: aborting due to 1 previous error
1111

tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const NAN: f64 = f64::NAN;
55
| -------------- constant defined here
66
...
77
LL | NAN => {},
8-
| ^^^
8+
| ^^^ evaluates to `NaN`, which is not allowed in patterns
99
|
1010
= note: NaNs compare inequal to everything, even themselves, so this pattern would never match
1111
= help: try using the `is_nan` method instead
@@ -17,7 +17,7 @@ LL | const NAN: f64 = f64::NAN;
1717
| -------------- constant defined here
1818
...
1919
LL | [NAN, _] => {},
20-
| ^^^
20+
| ^^^ evaluates to `NaN`, which is not allowed in patterns
2121
|
2222
= note: NaNs compare inequal to everything, even themselves, so this pattern would never match
2323
= help: try using the `is_nan` method instead
@@ -29,7 +29,7 @@ LL | const C: MyType<f32> = MyType(f32::NAN);
2929
| -------------------- constant defined here
3030
...
3131
LL | C => {},
32-
| ^
32+
| ^ evaluates to `NaN`, which is not allowed in patterns
3333
|
3434
= note: NaNs compare inequal to everything, even themselves, so this pattern would never match
3535
= help: try using the `is_nan` method instead
@@ -41,7 +41,7 @@ LL | const NAN: f64 = f64::NAN;
4141
| -------------- constant defined here
4242
...
4343
LL | NAN..=1.0 => {},
44-
| ^^^
44+
| ^^^ evaluates to `NaN`, which is not allowed in patterns
4545
|
4646
= note: NaNs compare inequal to everything, even themselves, so this pattern would never match
4747
= help: try using the `is_nan` method instead
@@ -53,7 +53,7 @@ LL | const NAN: f64 = f64::NAN;
5353
| -------------- constant defined here
5454
...
5555
LL | -1.0..=NAN => {},
56-
| ^^^
56+
| ^^^ evaluates to `NaN`, which is not allowed in patterns
5757
|
5858
= note: NaNs compare inequal to everything, even themselves, so this pattern would never match
5959
= help: try using the `is_nan` method instead
@@ -65,7 +65,7 @@ LL | const NAN: f64 = f64::NAN;
6565
| -------------- constant defined here
6666
...
6767
LL | NAN.. => {},
68-
| ^^^
68+
| ^^^ evaluates to `NaN`, which is not allowed in patterns
6969
|
7070
= note: NaNs compare inequal to everything, even themselves, so this pattern would never match
7171
= help: try using the `is_nan` method instead
@@ -77,7 +77,7 @@ LL | const NAN: f64 = f64::NAN;
7777
| -------------- constant defined here
7878
...
7979
LL | ..NAN => {},
80-
| ^^^
80+
| ^^^ evaluates to `NaN`, which is not allowed in patterns
8181
|
8282
= note: NaNs compare inequal to everything, even themselves, so this pattern would never match
8383
= help: try using the `is_nan` method instead

tests/ui/union/union-const-pat.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const C: U = U { a: 10 };
55
| ---------- constant defined here
66
...
77
LL | C => {}
8-
| ^
8+
| ^ can't use a `union` here
99

1010
error: aborting due to 1 previous error
1111

0 commit comments

Comments
 (0)