Skip to content

Commit 626db06

Browse files
authored
Rollup merge of rust-lang#133233 - estebank:const-errors, r=Nadrieril
Add context to "const in pattern" errors *Each commit addresses specific diagnostics.* - Add primary span labels - Point at `const` item, and `const` generic param definition - Reword messages and notes - Point at generic param through which an associated `const` is being referenced - Silence const in pattern with evaluation errors when they come from `const` items that already emit a diagnostic - On non-structural type in const used as pattern, point at the type that should derive `PartialEq`
2 parents bc13c82 + 4e6a401 commit 626db06

File tree

88 files changed

+1117
-511
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1117
-511
lines changed

compiler/rustc_mir_build/messages.ftl

+25-12
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,17 @@ mir_build_call_to_unsafe_fn_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
8484
8585
mir_build_confused = missing patterns are not covered because `{$variable}` is interpreted as a constant pattern, not a new variable
8686
87-
mir_build_const_param_in_pattern = const parameters cannot be referenced in patterns
87+
mir_build_const_defined_here = constant defined here
8888
89-
mir_build_const_pattern_depends_on_generic_parameter =
90-
constant pattern depends on a generic parameter
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
92+
93+
mir_build_const_pattern_depends_on_generic_parameter = constant pattern cannot depend on generic parameters
94+
.label = `const` depends on a generic parameter
9195
9296
mir_build_could_not_eval_const_pattern = could not evaluate constant pattern
97+
.label = could not evaluate constant
9398
9499
mir_build_deref_raw_pointer_requires_unsafe =
95100
dereference of raw pointer is unsafe and requires unsafe block
@@ -147,7 +152,8 @@ mir_build_inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
147152
148153
mir_build_interpreted_as_const = introduce a variable instead
149154
150-
mir_build_invalid_pattern = `{$non_sm_ty}` cannot be used in patterns
155+
mir_build_invalid_pattern = {$prefix} `{$non_sm_ty}` cannot be used in patterns
156+
.label = {$prefix} can't be used in patterns
151157
152158
mir_build_irrefutable_let_patterns_if_let = irrefutable `if let` {$count ->
153159
[one] pattern
@@ -244,10 +250,12 @@ mir_build_mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsa
244250
.label = mutation of layout constrained field
245251
246252
mir_build_nan_pattern = cannot use NaN in patterns
253+
.label = evaluates to `NaN`, which is not allowed in patterns
247254
.note = NaNs compare inequal to everything, even themselves, so this pattern would never match
248255
.help = try using the `is_nan` method instead
249256
250257
mir_build_non_const_path = runtime values cannot be referenced in patterns
258+
.label = references a runtime value
251259
252260
mir_build_non_empty_never_pattern =
253261
mismatched types
@@ -265,13 +273,15 @@ mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type
265273
.suggestion = ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
266274
.help = ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
267275
268-
mir_build_non_partial_eq_match =
269-
to use a constant of type `{$non_peq_ty}` in a pattern, the type must implement `PartialEq`
276+
mir_build_non_partial_eq_match = constant of non-structural type `{$ty}` in a pattern
277+
.label = constant of non-structural type
270278
271279
mir_build_pattern_not_covered = refutable pattern in {$origin}
272280
.pattern_ty = the matched value is of type `{$pattern_ty}`
273281
274-
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.
282+
mir_build_pointer_pattern = function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon
283+
.label = can't be used in patterns
284+
.note = see https://github.com/rust-lang/rust/issues/70861 for details
275285
276286
mir_build_privately_uninhabited = pattern `{$witness_1}` is currently uninhabited, but this variant contains private fields which may become inhabited in the future
277287
@@ -283,6 +293,8 @@ mir_build_rustc_box_attribute_error = `#[rustc_box]` attribute used incorrectly
283293
.missing_box = `#[rustc_box]` requires the `owned_box` lang item
284294
285295
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
286298
287299
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
288300
@@ -310,12 +322,12 @@ mir_build_trailing_irrefutable_let_patterns = trailing irrefutable {$count ->
310322
*[other] them
311323
} into the body
312324
313-
mir_build_type_not_structural =
314-
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq)]`
315-
325+
mir_build_type_not_structural = constant of non-structural type `{$ty}` in a pattern
326+
.label = constant of non-structural type
327+
mir_build_type_not_structural_def = `{$ty}` must be annotated with `#[derive(PartialEq)]` to be usable in patterns
316328
mir_build_type_not_structural_more_info = see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
317-
318-
mir_build_type_not_structural_tip = the traits must be derived, manual `impl`s are not sufficient
329+
mir_build_type_not_structural_tip =
330+
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
319331
320332
mir_build_unconditional_recursion = function cannot return without recursing
321333
.label = cannot return without recursing
@@ -334,6 +346,7 @@ mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
334346
.label = access to union field
335347
336348
mir_build_union_pattern = cannot use unions in constant patterns
349+
.label = can't use a `union` here
337350
338351
mir_build_unreachable_making_this_unreachable = collectively making this unreachable
339352

compiler/rustc_mir_build/src/errors.rs

+26-4
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

@@ -695,13 +702,15 @@ pub(crate) struct WantedConstant {
695702
#[diag(mir_build_const_pattern_depends_on_generic_parameter, code = E0158)]
696703
pub(crate) struct ConstPatternDependsOnGenericParameter {
697704
#[primary_span]
705+
#[label]
698706
pub(crate) span: Span,
699707
}
700708

701709
#[derive(Diagnostic)]
702710
#[diag(mir_build_could_not_eval_const_pattern)]
703711
pub(crate) struct CouldNotEvalConstPattern {
704712
#[primary_span]
713+
#[label]
705714
pub(crate) span: Span,
706715
}
707716

@@ -867,33 +876,43 @@ pub(crate) enum Conflict {
867876
#[diag(mir_build_union_pattern)]
868877
pub(crate) struct UnionPattern {
869878
#[primary_span]
879+
#[label]
870880
pub(crate) span: Span,
871881
}
872882

873883
#[derive(Diagnostic)]
874884
#[diag(mir_build_type_not_structural)]
875-
#[note(mir_build_type_not_structural_tip)]
876-
#[note(mir_build_type_not_structural_more_info)]
877885
pub(crate) struct TypeNotStructural<'tcx> {
878886
#[primary_span]
887+
#[label]
879888
pub(crate) span: Span,
880-
pub(crate) non_sm_ty: Ty<'tcx>,
889+
#[label(mir_build_type_not_structural_def)]
890+
pub(crate) ty_def_span: Span,
891+
pub(crate) ty: Ty<'tcx>,
892+
#[note(mir_build_type_not_structural_tip)]
893+
pub(crate) manual_partialeq_impl_span: Option<Span>,
894+
#[note(mir_build_type_not_structural_more_info)]
895+
pub(crate) manual_partialeq_impl_note: bool,
881896
}
882897

883898
#[derive(Diagnostic)]
884899
#[diag(mir_build_non_partial_eq_match)]
900+
#[note(mir_build_type_not_structural_more_info)]
885901
pub(crate) struct TypeNotPartialEq<'tcx> {
886902
#[primary_span]
903+
#[label]
887904
pub(crate) span: Span,
888-
pub(crate) non_peq_ty: Ty<'tcx>,
905+
pub(crate) ty: Ty<'tcx>,
889906
}
890907

891908
#[derive(Diagnostic)]
892909
#[diag(mir_build_invalid_pattern)]
893910
pub(crate) struct InvalidPattern<'tcx> {
894911
#[primary_span]
912+
#[label]
895913
pub(crate) span: Span,
896914
pub(crate) non_sm_ty: Ty<'tcx>,
915+
pub(crate) prefix: String,
897916
}
898917

899918
#[derive(Diagnostic)]
@@ -910,13 +929,16 @@ pub(crate) struct UnsizedPattern<'tcx> {
910929
#[help]
911930
pub(crate) struct NaNPattern {
912931
#[primary_span]
932+
#[label]
913933
pub(crate) span: Span,
914934
}
915935

916936
#[derive(Diagnostic)]
917937
#[diag(mir_build_pointer_pattern)]
938+
#[note]
918939
pub(crate) struct PointerPattern {
919940
#[primary_span]
941+
#[label]
920942
pub(crate) span: Span,
921943
}
922944

0 commit comments

Comments
 (0)