Skip to content

Commit 253eb95

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 87ddc1e commit 253eb95

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
@@ -247,10 +249,12 @@ mir_build_mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsa
247249
.label = mutation of layout constrained field
248250
249251
mir_build_nan_pattern = cannot use NaN in patterns
252+
.label = evaluates to `NaN`, which is not allowed in patterns
250253
.note = NaNs compare inequal to everything, even themselves, so this pattern would never match
251254
.help = try using the `is_nan` method instead
252255
253256
mir_build_non_const_path = runtime values cannot be referenced in patterns
257+
.label = references a runtime value
254258
255259
mir_build_non_empty_never_pattern =
256260
mismatched types
@@ -270,6 +274,7 @@ mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type
270274
271275
mir_build_non_partial_eq_match =
272276
to use a constant of type `{$non_peq_ty}` in a pattern, the type must implement `PartialEq`
277+
.label = constant of non-structural type
273278
274279
mir_build_pattern_not_covered = refutable pattern in {$origin}
275280
.pattern_ty = the matched value is of type `{$pattern_ty}`
@@ -288,6 +293,8 @@ mir_build_rustc_box_attribute_error = `#[rustc_box]` attribute used incorrectly
288293
.missing_box = `#[rustc_box]` requires the `owned_box` lang item
289294
290295
mir_build_static_in_pattern = statics cannot be referenced in patterns
296+
.label = can't be used in patterns
297+
mir_build_static_in_pattern_def = `static` defined here
291298
292299
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
293300
@@ -339,6 +346,7 @@ mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
339346
.label = access to union field
340347
341348
mir_build_union_pattern = cannot use unions in constant patterns
349+
.label = can't use a `union` here
342350
343351
mir_build_unreachable_making_this_unreachable = collectively making this unreachable
344352

compiler/rustc_mir_build/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,20 +631,27 @@ pub(crate) struct NonExhaustiveMatchAllArmsGuarded;
631631
#[diag(mir_build_static_in_pattern, code = E0158)]
632632
pub(crate) struct StaticInPattern {
633633
#[primary_span]
634+
#[label]
634635
pub(crate) span: Span,
636+
#[label(mir_build_static_in_pattern_def)]
637+
pub(crate) static_span: Span,
635638
}
636639

637640
#[derive(Diagnostic)]
638641
#[diag(mir_build_const_param_in_pattern, code = E0158)]
639642
pub(crate) struct ConstParamInPattern {
640643
#[primary_span]
644+
#[label]
641645
pub(crate) span: Span,
646+
#[label(mir_build_const_param_in_pattern_def)]
647+
pub(crate) const_span: Span,
642648
}
643649

644650
#[derive(Diagnostic)]
645651
#[diag(mir_build_non_const_path, code = E0080)]
646652
pub(crate) struct NonConstPath {
647653
#[primary_span]
654+
#[label]
648655
pub(crate) span: Span,
649656
}
650657

@@ -869,6 +876,7 @@ pub(crate) enum Conflict {
869876
#[diag(mir_build_union_pattern)]
870877
pub(crate) struct UnionPattern {
871878
#[primary_span]
879+
#[label]
872880
pub(crate) span: Span,
873881
}
874882

@@ -886,6 +894,7 @@ pub(crate) struct TypeNotStructural<'tcx> {
886894
#[diag(mir_build_non_partial_eq_match)]
887895
pub(crate) struct TypeNotPartialEq<'tcx> {
888896
#[primary_span]
897+
#[label]
889898
pub(crate) span: Span,
890899
pub(crate) non_peq_ty: Ty<'tcx>,
891900
}
@@ -912,6 +921,7 @@ pub(crate) struct UnsizedPattern<'tcx> {
912921
#[help]
913922
pub(crate) struct NaNPattern {
914923
#[primary_span]
924+
#[label]
915925
pub(crate) span: Span,
916926
}
917927

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,17 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
528528
| Res::SelfCtor(..) => PatKind::Leaf { subpatterns },
529529
_ => {
530530
let e = match res {
531-
Res::Def(DefKind::ConstParam, _) => {
532-
self.tcx.dcx().emit_err(ConstParamInPattern { span })
531+
Res::Def(DefKind::ConstParam, def_id) => {
532+
self.tcx.dcx().emit_err(ConstParamInPattern {
533+
span,
534+
const_span: self.tcx().def_span(def_id),
535+
})
533536
}
534-
Res::Def(DefKind::Static { .. }, _) => {
535-
self.tcx.dcx().emit_err(StaticInPattern { span })
537+
Res::Def(DefKind::Static { .. }, def_id) => {
538+
self.tcx.dcx().emit_err(StaticInPattern {
539+
span,
540+
static_span: self.tcx().def_span(def_id),
541+
})
536542
}
537543
_ => self.tcx.dcx().emit_err(NonConstPath { span }),
538544
};

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)