about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-11-20 03:57:30 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-12-04 20:29:36 +0000
commit253eb95d4582e1abffc4a74dd8eaccd5b94b5d6f (patch)
tree4d01f8ccc47d042f2f9ec4a240989a574bdf02e6
parent87ddc1ea33edbe1be119d73e57c2783940e797fe (diff)
downloadrust-253eb95d4582e1abffc4a74dd8eaccd5b94b5d6f.tar.gz
rust-253eb95d4582e1abffc4a74dd8eaccd5b94b5d6f.zip
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.
-rw-r--r--compiler/rustc_mir_build/messages.ftl10
-rw-r--r--compiler/rustc_mir_build/src/errors.rs10
-rw-r--r--compiler/rustc_mir_build/src/thir/pattern/mod.rs14
-rw-r--r--tests/ui/binding/const-param.rs2
-rw-r--r--tests/ui/binding/const-param.stderr7
-rw-r--r--tests/ui/consts/const_in_pattern/issue-65466.stderr2
-rw-r--r--tests/ui/consts/const_in_pattern/reject_non_partial_eq.stderr2
-rw-r--r--tests/ui/match/issue-72896-non-partial-eq-const.stderr2
-rw-r--r--tests/ui/pattern/non-constant-in-const-path.stderr14
-rw-r--r--tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-61188-match-slice-forbidden-without-eq.stderr2
-rw-r--r--tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr14
-rw-r--r--tests/ui/union/union-const-pat.stderr2
12 files changed, 57 insertions, 24 deletions
diff --git a/compiler/rustc_mir_build/messages.ftl b/compiler/rustc_mir_build/messages.ftl
index 4bc7dcf9564..28011833e58 100644
--- a/compiler/rustc_mir_build/messages.ftl
+++ b/compiler/rustc_mir_build/messages.ftl
@@ -86,7 +86,9 @@ mir_build_confused = missing patterns are not covered because `{$variable}` is i
 
 mir_build_const_defined_here = constant defined here
 
-mir_build_const_param_in_pattern = const parameters cannot be referenced in patterns
+mir_build_const_param_in_pattern = constant parameters cannot be referenced in patterns
+    .label = can't be used in patterns
+mir_build_const_param_in_pattern_def = constant defined here
 
 mir_build_const_pattern_depends_on_generic_parameter = constant pattern depends on a generic parameter, which is not allowed
     .label = `const` depends on a generic parameter
@@ -247,10 +249,12 @@ mir_build_mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsa
     .label = mutation of layout constrained field
 
 mir_build_nan_pattern = cannot use NaN in patterns
+    .label = evaluates to `NaN`, which is not allowed in patterns
     .note = NaNs compare inequal to everything, even themselves, so this pattern would never match
     .help = try using the `is_nan` method instead
 
 mir_build_non_const_path = runtime values cannot be referenced in patterns
+    .label = references a runtime value
 
 mir_build_non_empty_never_pattern =
     mismatched types
@@ -270,6 +274,7 @@ mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type
 
 mir_build_non_partial_eq_match =
     to use a constant of type `{$non_peq_ty}` in a pattern, the type must implement `PartialEq`
+    .label = constant of non-structural type
 
 mir_build_pattern_not_covered = refutable pattern in {$origin}
     .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
     .missing_box = `#[rustc_box]` requires the `owned_box` lang item
 
 mir_build_static_in_pattern = statics cannot be referenced in patterns
+    .label = can't be used in patterns
+mir_build_static_in_pattern_def = `static` defined here
 
 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
 
@@ -339,6 +346,7 @@ mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
     .label = access to union field
 
 mir_build_union_pattern = cannot use unions in constant patterns
+    .label = can't use a `union` here
 
 mir_build_unreachable_making_this_unreachable = collectively making this unreachable
 
diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs
index 1068c56eeca..1c4de5c07a1 100644
--- a/compiler/rustc_mir_build/src/errors.rs
+++ b/compiler/rustc_mir_build/src/errors.rs
@@ -631,20 +631,27 @@ pub(crate) struct NonExhaustiveMatchAllArmsGuarded;
 #[diag(mir_build_static_in_pattern, code = E0158)]
 pub(crate) struct StaticInPattern {
     #[primary_span]
+    #[label]
     pub(crate) span: Span,
+    #[label(mir_build_static_in_pattern_def)]
+    pub(crate) static_span: Span,
 }
 
 #[derive(Diagnostic)]
 #[diag(mir_build_const_param_in_pattern, code = E0158)]
 pub(crate) struct ConstParamInPattern {
     #[primary_span]
+    #[label]
     pub(crate) span: Span,
+    #[label(mir_build_const_param_in_pattern_def)]
+    pub(crate) const_span: Span,
 }
 
 #[derive(Diagnostic)]
 #[diag(mir_build_non_const_path, code = E0080)]
 pub(crate) struct NonConstPath {
     #[primary_span]
+    #[label]
     pub(crate) span: Span,
 }
 
@@ -869,6 +876,7 @@ pub(crate) enum Conflict {
 #[diag(mir_build_union_pattern)]
 pub(crate) struct UnionPattern {
     #[primary_span]
+    #[label]
     pub(crate) span: Span,
 }
 
@@ -886,6 +894,7 @@ pub(crate) struct TypeNotStructural<'tcx> {
 #[diag(mir_build_non_partial_eq_match)]
 pub(crate) struct TypeNotPartialEq<'tcx> {
     #[primary_span]
+    #[label]
     pub(crate) span: Span,
     pub(crate) non_peq_ty: Ty<'tcx>,
 }
@@ -912,6 +921,7 @@ pub(crate) struct UnsizedPattern<'tcx> {
 #[help]
 pub(crate) struct NaNPattern {
     #[primary_span]
+    #[label]
     pub(crate) span: Span,
 }
 
diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
index 08c6b4abd3b..3ac53fa6272 100644
--- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs
+++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
@@ -528,11 +528,17 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
             | Res::SelfCtor(..) => PatKind::Leaf { subpatterns },
             _ => {
                 let e = match res {
-                    Res::Def(DefKind::ConstParam, _) => {
-                        self.tcx.dcx().emit_err(ConstParamInPattern { span })
+                    Res::Def(DefKind::ConstParam, def_id) => {
+                        self.tcx.dcx().emit_err(ConstParamInPattern {
+                            span,
+                            const_span: self.tcx().def_span(def_id),
+                        })
                     }
-                    Res::Def(DefKind::Static { .. }, _) => {
-                        self.tcx.dcx().emit_err(StaticInPattern { span })
+                    Res::Def(DefKind::Static { .. }, def_id) => {
+                        self.tcx.dcx().emit_err(StaticInPattern {
+                            span,
+                            static_span: self.tcx().def_span(def_id),
+                        })
                     }
                     _ => self.tcx.dcx().emit_err(NonConstPath { span }),
                 };
diff --git a/tests/ui/binding/const-param.rs b/tests/ui/binding/const-param.rs
index 2d051808fe0..98bc90f508a 100644
--- a/tests/ui/binding/const-param.rs
+++ b/tests/ui/binding/const-param.rs
@@ -2,7 +2,7 @@
 
 fn check<const N: usize>() {
     match 1 {
-        N => {} //~ ERROR const parameters cannot be referenced in patterns
+        N => {} //~ ERROR constant parameters cannot be referenced in patterns
         _ => {}
     }
 }
diff --git a/tests/ui/binding/const-param.stderr b/tests/ui/binding/const-param.stderr
index e68893a59e4..b0b8108945c 100644
--- a/tests/ui/binding/const-param.stderr
+++ b/tests/ui/binding/const-param.stderr
@@ -1,8 +1,11 @@
-error[E0158]: const parameters cannot be referenced in patterns
+error[E0158]: constant parameters cannot be referenced in patterns
   --> $DIR/const-param.rs:5:9
    |
+LL | fn check<const N: usize>() {
+   |          -------------- constant defined here
+LL |     match 1 {
 LL |         N => {}
-   |         ^
+   |         ^ can't be used in patterns
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/consts/const_in_pattern/issue-65466.stderr b/tests/ui/consts/const_in_pattern/issue-65466.stderr
index 070ce384f6a..90bac6262cc 100644
--- a/tests/ui/consts/const_in_pattern/issue-65466.stderr
+++ b/tests/ui/consts/const_in_pattern/issue-65466.stderr
@@ -5,7 +5,7 @@ LL | const C: &[O<B>] = &[O::None];
    | ---------------- constant defined here
 ...
 LL |         C => (),
-   |         ^
+   |         ^ constant of non-structural type
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/consts/const_in_pattern/reject_non_partial_eq.stderr b/tests/ui/consts/const_in_pattern/reject_non_partial_eq.stderr
index 463b37e7caa..d4952a8bfb8 100644
--- a/tests/ui/consts/const_in_pattern/reject_non_partial_eq.stderr
+++ b/tests/ui/consts/const_in_pattern/reject_non_partial_eq.stderr
@@ -5,7 +5,7 @@ LL | const NO_PARTIAL_EQ_NONE: Option<NoPartialEq> = None;
    | --------------------------------------------- constant defined here
 ...
 LL |         NO_PARTIAL_EQ_NONE => println!("NO_PARTIAL_EQ_NONE"),
-   |         ^^^^^^^^^^^^^^^^^^
+   |         ^^^^^^^^^^^^^^^^^^ constant of non-structural type
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/match/issue-72896-non-partial-eq-const.stderr b/tests/ui/match/issue-72896-non-partial-eq-const.stderr
index eeca541fc3c..d6ae9e56377 100644
--- a/tests/ui/match/issue-72896-non-partial-eq-const.stderr
+++ b/tests/ui/match/issue-72896-non-partial-eq-const.stderr
@@ -5,7 +5,7 @@ LL | const CONST_SET: EnumSet<Enum8> = EnumSet { __enumset_underlying: 3 };
    | ------------------------------- constant defined here
 ...
 LL |         CONST_SET => { /* ok */ }
-   |         ^^^^^^^^^
+   |         ^^^^^^^^^ constant of non-structural type
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/pattern/non-constant-in-const-path.stderr b/tests/ui/pattern/non-constant-in-const-path.stderr
index 53c3974f780..2ba4963e6dc 100644
--- a/tests/ui/pattern/non-constant-in-const-path.stderr
+++ b/tests/ui/pattern/non-constant-in-const-path.stderr
@@ -2,25 +2,31 @@ error[E0080]: runtime values cannot be referenced in patterns
   --> $DIR/non-constant-in-const-path.rs:8:15
    |
 LL |     let 0u8..=x = 0;
-   |               ^
+   |               ^ references a runtime value
 
 error[E0158]: statics cannot be referenced in patterns
   --> $DIR/non-constant-in-const-path.rs:10:15
    |
+LL | static FOO: u8 = 10;
+   | -------------- `static` defined here
+...
 LL |     let 0u8..=FOO = 0;
-   |               ^^^
+   |               ^^^ can't be used in patterns
 
 error[E0080]: runtime values cannot be referenced in patterns
   --> $DIR/non-constant-in-const-path.rs:13:15
    |
 LL |         0 ..= x => {}
-   |               ^
+   |               ^ references a runtime value
 
 error[E0158]: statics cannot be referenced in patterns
   --> $DIR/non-constant-in-const-path.rs:15:15
    |
+LL | static FOO: u8 = 10;
+   | -------------- `static` defined here
+...
 LL |         0 ..= FOO => {}
-   |               ^^^
+   |               ^^^ can't be used in patterns
 
 error: aborting due to 4 previous errors
 
diff --git a/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-61188-match-slice-forbidden-without-eq.stderr b/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-61188-match-slice-forbidden-without-eq.stderr
index e105c6a1f98..51bcfc07c9e 100644
--- a/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-61188-match-slice-forbidden-without-eq.stderr
+++ b/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-61188-match-slice-forbidden-without-eq.stderr
@@ -5,7 +5,7 @@ LL | const A: &[B] = &[];
    | ------------- constant defined here
 ...
 LL |         A => (),
-   |         ^
+   |         ^ constant of non-structural type
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr b/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr
index 4e2dcb52da8..7c49870e5d0 100644
--- a/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr
+++ b/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr
@@ -5,7 +5,7 @@ LL | const NAN: f64 = f64::NAN;
    | -------------- constant defined here
 ...
 LL |         NAN => {},
-   |         ^^^
+   |         ^^^ evaluates to `NaN`, which is not allowed in patterns
    |
    = note: NaNs compare inequal to everything, even themselves, so this pattern would never match
    = help: try using the `is_nan` method instead
@@ -17,7 +17,7 @@ LL | const NAN: f64 = f64::NAN;
    | -------------- constant defined here
 ...
 LL |         [NAN, _] => {},
-   |          ^^^
+   |          ^^^ evaluates to `NaN`, which is not allowed in patterns
    |
    = note: NaNs compare inequal to everything, even themselves, so this pattern would never match
    = help: try using the `is_nan` method instead
@@ -29,7 +29,7 @@ LL | const C: MyType<f32> = MyType(f32::NAN);
    | -------------------- constant defined here
 ...
 LL |         C => {},
-   |         ^
+   |         ^ evaluates to `NaN`, which is not allowed in patterns
    |
    = note: NaNs compare inequal to everything, even themselves, so this pattern would never match
    = help: try using the `is_nan` method instead
@@ -41,7 +41,7 @@ LL | const NAN: f64 = f64::NAN;
    | -------------- constant defined here
 ...
 LL |         NAN..=1.0 => {},
-   |         ^^^
+   |         ^^^ evaluates to `NaN`, which is not allowed in patterns
    |
    = note: NaNs compare inequal to everything, even themselves, so this pattern would never match
    = help: try using the `is_nan` method instead
@@ -53,7 +53,7 @@ LL | const NAN: f64 = f64::NAN;
    | -------------- constant defined here
 ...
 LL |         -1.0..=NAN => {},
-   |                ^^^
+   |                ^^^ evaluates to `NaN`, which is not allowed in patterns
    |
    = note: NaNs compare inequal to everything, even themselves, so this pattern would never match
    = help: try using the `is_nan` method instead
@@ -65,7 +65,7 @@ LL | const NAN: f64 = f64::NAN;
    | -------------- constant defined here
 ...
 LL |         NAN.. => {},
-   |         ^^^
+   |         ^^^ evaluates to `NaN`, which is not allowed in patterns
    |
    = note: NaNs compare inequal to everything, even themselves, so this pattern would never match
    = help: try using the `is_nan` method instead
@@ -77,7 +77,7 @@ LL | const NAN: f64 = f64::NAN;
    | -------------- constant defined here
 ...
 LL |         ..NAN => {},
-   |           ^^^
+   |           ^^^ evaluates to `NaN`, which is not allowed in patterns
    |
    = note: NaNs compare inequal to everything, even themselves, so this pattern would never match
    = help: try using the `is_nan` method instead
diff --git a/tests/ui/union/union-const-pat.stderr b/tests/ui/union/union-const-pat.stderr
index baf32bf4849..59dc89c77a4 100644
--- a/tests/ui/union/union-const-pat.stderr
+++ b/tests/ui/union/union-const-pat.stderr
@@ -5,7 +5,7 @@ LL | const C: U = U { a: 10 };
    | ---------- constant defined here
 ...
 LL |         C => {}
-   |         ^
+   |         ^ can't use a `union` here
 
 error: aborting due to 1 previous error