1
Fork 0

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.
This commit is contained in:
Esteban Küber 2024-11-20 03:57:30 +00:00
parent 87ddc1ea33
commit 253eb95d45
12 changed files with 57 additions and 24 deletions

View file

@ -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

View file

@ -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,
}

View file

@ -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 }),
};

View file

@ -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
_ => {}
}
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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