Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context to "const in pattern" errors #133233

Merged
merged 12 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,17 @@ mir_build_call_to_unsafe_fn_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =

mir_build_confused = missing patterns are not covered because `{$variable}` is interpreted as a constant pattern, not a new variable

mir_build_const_param_in_pattern = const parameters cannot be referenced in patterns
mir_build_const_defined_here = constant defined here

mir_build_const_pattern_depends_on_generic_parameter =
constant pattern depends on a generic parameter
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 cannot depend on generic parameters
.label = `const` depends on a generic parameter

mir_build_could_not_eval_const_pattern = could not evaluate constant pattern
.label = could not evaluate constant

mir_build_deref_raw_pointer_requires_unsafe =
dereference of raw pointer is unsafe and requires unsafe block
Expand Down Expand Up @@ -147,7 +152,8 @@ mir_build_inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =

mir_build_interpreted_as_const = introduce a variable instead

mir_build_invalid_pattern = `{$non_sm_ty}` cannot be used in patterns
mir_build_invalid_pattern = {$prefix} `{$non_sm_ty}` cannot be used in patterns
.label = {$prefix} can't be used in patterns

mir_build_irrefutable_let_patterns_if_let = irrefutable `if let` {$count ->
[one] pattern
Expand Down Expand Up @@ -244,10 +250,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
Expand All @@ -265,13 +273,15 @@ mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type
.suggestion = ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
.help = ensure that all possible cases are being handled by adding a match arm with a wildcard pattern

mir_build_non_partial_eq_match =
to use a constant of type `{$non_peq_ty}` in a pattern, the type must implement `PartialEq`
mir_build_non_partial_eq_match = constant of non-structural type `{$ty}` in a pattern
.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}`

mir_build_pointer_pattern = function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
mir_build_pointer_pattern = function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon
.label = can't be used in patterns
.note = see https://github.com/rust-lang/rust/issues/70861 for details

mir_build_privately_uninhabited = pattern `{$witness_1}` is currently uninhabited, but this variant contains private fields which may become inhabited in the future

Expand All @@ -283,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

Expand Down Expand Up @@ -310,12 +322,12 @@ mir_build_trailing_irrefutable_let_patterns = trailing irrefutable {$count ->
*[other] them
} into the body

mir_build_type_not_structural =
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq)]`

mir_build_type_not_structural = constant of non-structural type `{$ty}` in a pattern
.label = constant of non-structural type
mir_build_type_not_structural_def = `{$ty}` must be annotated with `#[derive(PartialEq)]` to be usable in patterns
mir_build_type_not_structural_more_info = see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details

mir_build_type_not_structural_tip = the traits must be derived, manual `impl`s are not sufficient
mir_build_type_not_structural_tip =
the `PartialEq` trait must be derived, manual `impl`s are not sufficient; see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details

mir_build_unconditional_recursion = function cannot return without recursing
.label = cannot return without recursing
Expand All @@ -334,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

Expand Down
30 changes: 26 additions & 4 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -695,13 +702,15 @@ pub(crate) struct WantedConstant {
#[diag(mir_build_const_pattern_depends_on_generic_parameter, code = E0158)]
pub(crate) struct ConstPatternDependsOnGenericParameter {
#[primary_span]
#[label]
pub(crate) span: Span,
}

#[derive(Diagnostic)]
#[diag(mir_build_could_not_eval_const_pattern)]
pub(crate) struct CouldNotEvalConstPattern {
#[primary_span]
#[label]
pub(crate) span: Span,
}

Expand Down Expand Up @@ -867,33 +876,43 @@ pub(crate) enum Conflict {
#[diag(mir_build_union_pattern)]
pub(crate) struct UnionPattern {
#[primary_span]
#[label]
pub(crate) span: Span,
}

#[derive(Diagnostic)]
#[diag(mir_build_type_not_structural)]
#[note(mir_build_type_not_structural_tip)]
#[note(mir_build_type_not_structural_more_info)]
pub(crate) struct TypeNotStructural<'tcx> {
#[primary_span]
#[label]
pub(crate) span: Span,
pub(crate) non_sm_ty: Ty<'tcx>,
#[label(mir_build_type_not_structural_def)]
pub(crate) ty_def_span: Span,
pub(crate) ty: Ty<'tcx>,
#[note(mir_build_type_not_structural_tip)]
pub(crate) manual_partialeq_impl_span: Option<Span>,
#[note(mir_build_type_not_structural_more_info)]
pub(crate) manual_partialeq_impl_note: bool,
}

#[derive(Diagnostic)]
#[diag(mir_build_non_partial_eq_match)]
#[note(mir_build_type_not_structural_more_info)]
pub(crate) struct TypeNotPartialEq<'tcx> {
#[primary_span]
#[label]
pub(crate) span: Span,
pub(crate) non_peq_ty: Ty<'tcx>,
pub(crate) ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag(mir_build_invalid_pattern)]
pub(crate) struct InvalidPattern<'tcx> {
#[primary_span]
#[label]
pub(crate) span: Span,
pub(crate) non_sm_ty: Ty<'tcx>,
pub(crate) prefix: String,
}

#[derive(Diagnostic)]
Expand All @@ -910,13 +929,16 @@ pub(crate) struct UnsizedPattern<'tcx> {
#[help]
pub(crate) struct NaNPattern {
#[primary_span]
#[label]
pub(crate) span: Span,
}

#[derive(Diagnostic)]
#[diag(mir_build_pointer_pattern)]
#[note]
pub(crate) struct PointerPattern {
#[primary_span]
#[label]
pub(crate) span: Span,
}

Expand Down
Loading
Loading