]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir_build/src/errors.rs
New upstream version 1.69.0+dfsg1
[rustc.git] / compiler / rustc_mir_build / src / errors.rs
CommitLineData
9ffffee4
FG
1use crate::{
2 fluent_generated as fluent,
3 thir::pattern::{deconstruct_pat::DeconstructedPat, MatchCheckCtxt},
4};
9c376795
FG
5use rustc_errors::{
6 error_code, AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
9ffffee4 7 Handler, IntoDiagnostic, MultiSpan, SubdiagnosticMessage,
9c376795
FG
8};
9use rustc_hir::def::Res;
10use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
11use rustc_middle::thir::Pat;
12use rustc_middle::ty::{self, Ty};
13use rustc_span::{symbol::Ident, Span};
14
15#[derive(LintDiagnostic)]
16#[diag(mir_build_unconditional_recursion)]
17#[help]
18pub struct UnconditionalRecursion {
19 #[label]
20 pub span: Span,
21 #[label(mir_build_unconditional_recursion_call_site_label)]
22 pub call_sites: Vec<Span>,
23}
24
25#[derive(LintDiagnostic)]
26#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe)]
27#[note]
28pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> {
29 #[label]
30 pub span: Span,
31 pub function: &'a str,
32}
33
34#[derive(LintDiagnostic)]
35#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless)]
36#[note]
37pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
38 #[label]
39 pub span: Span,
40}
41
42#[derive(LintDiagnostic)]
43#[diag(mir_build_unsafe_op_in_unsafe_fn_inline_assembly_requires_unsafe)]
44#[note]
45pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe {
46 #[label]
47 pub span: Span,
48}
49
50#[derive(LintDiagnostic)]
51#[diag(mir_build_unsafe_op_in_unsafe_fn_initializing_type_with_requires_unsafe)]
52#[note]
53pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe {
54 #[label]
55 pub span: Span,
56}
57
58#[derive(LintDiagnostic)]
59#[diag(mir_build_unsafe_op_in_unsafe_fn_mutable_static_requires_unsafe)]
60#[note]
61pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe {
62 #[label]
63 pub span: Span,
64}
65
66#[derive(LintDiagnostic)]
67#[diag(mir_build_unsafe_op_in_unsafe_fn_extern_static_requires_unsafe)]
68#[note]
69pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe {
70 #[label]
71 pub span: Span,
72}
73
74#[derive(LintDiagnostic)]
75#[diag(mir_build_unsafe_op_in_unsafe_fn_deref_raw_pointer_requires_unsafe)]
76#[note]
77pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe {
78 #[label]
79 pub span: Span,
80}
81
82#[derive(LintDiagnostic)]
83#[diag(mir_build_unsafe_op_in_unsafe_fn_union_field_requires_unsafe)]
84#[note]
85pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe {
86 #[label]
87 pub span: Span,
88}
89
90#[derive(LintDiagnostic)]
91#[diag(mir_build_unsafe_op_in_unsafe_fn_mutation_of_layout_constrained_field_requires_unsafe)]
92#[note]
93pub struct UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe {
94 #[label]
95 pub span: Span,
96}
97
98#[derive(LintDiagnostic)]
99#[diag(mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe)]
100pub struct UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe {
101 #[label]
102 pub span: Span,
103}
104
105#[derive(LintDiagnostic)]
106#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe)]
107#[note]
108pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> {
109 #[label]
110 pub span: Span,
111 pub function: &'a str,
112}
113
114#[derive(Diagnostic)]
115#[diag(mir_build_call_to_unsafe_fn_requires_unsafe, code = "E0133")]
116#[note]
117pub struct CallToUnsafeFunctionRequiresUnsafe<'a> {
118 #[primary_span]
119 #[label]
120 pub span: Span,
121 pub function: &'a str,
122}
123
124#[derive(Diagnostic)]
125#[diag(mir_build_call_to_unsafe_fn_requires_unsafe_nameless, code = "E0133")]
126#[note]
127pub struct CallToUnsafeFunctionRequiresUnsafeNameless {
128 #[primary_span]
129 #[label]
130 pub span: Span,
131}
132
133#[derive(Diagnostic)]
134#[diag(mir_build_call_to_unsafe_fn_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
135#[note]
136pub struct CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> {
137 #[primary_span]
138 #[label]
139 pub span: Span,
140 pub function: &'a str,
141}
142
143#[derive(Diagnostic)]
144#[diag(
145 mir_build_call_to_unsafe_fn_requires_unsafe_nameless_unsafe_op_in_unsafe_fn_allowed,
146 code = "E0133"
147)]
148#[note]
149pub struct CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed {
150 #[primary_span]
151 #[label]
152 pub span: Span,
153}
154
155#[derive(Diagnostic)]
156#[diag(mir_build_inline_assembly_requires_unsafe, code = "E0133")]
157#[note]
158pub struct UseOfInlineAssemblyRequiresUnsafe {
159 #[primary_span]
160 #[label]
161 pub span: Span,
162}
163
164#[derive(Diagnostic)]
165#[diag(mir_build_inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
166#[note]
167pub struct UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
168 #[primary_span]
169 #[label]
170 pub span: Span,
171}
172
173#[derive(Diagnostic)]
174#[diag(mir_build_initializing_type_with_requires_unsafe, code = "E0133")]
175#[note]
176pub struct InitializingTypeWithRequiresUnsafe {
177 #[primary_span]
178 #[label]
179 pub span: Span,
180}
181
182#[derive(Diagnostic)]
183#[diag(
184 mir_build_initializing_type_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed,
185 code = "E0133"
186)]
187#[note]
188pub struct InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
189 #[primary_span]
190 #[label]
191 pub span: Span,
192}
193
194#[derive(Diagnostic)]
195#[diag(mir_build_mutable_static_requires_unsafe, code = "E0133")]
196#[note]
197pub struct UseOfMutableStaticRequiresUnsafe {
198 #[primary_span]
199 #[label]
200 pub span: Span,
201}
202
203#[derive(Diagnostic)]
204#[diag(mir_build_mutable_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
205#[note]
206pub struct UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
207 #[primary_span]
208 #[label]
209 pub span: Span,
210}
211
212#[derive(Diagnostic)]
213#[diag(mir_build_extern_static_requires_unsafe, code = "E0133")]
214#[note]
215pub struct UseOfExternStaticRequiresUnsafe {
216 #[primary_span]
217 #[label]
218 pub span: Span,
219}
220
221#[derive(Diagnostic)]
222#[diag(mir_build_extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
223#[note]
224pub struct UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
225 #[primary_span]
226 #[label]
227 pub span: Span,
228}
229
230#[derive(Diagnostic)]
231#[diag(mir_build_deref_raw_pointer_requires_unsafe, code = "E0133")]
232#[note]
233pub struct DerefOfRawPointerRequiresUnsafe {
234 #[primary_span]
235 #[label]
236 pub span: Span,
237}
238
239#[derive(Diagnostic)]
240#[diag(mir_build_deref_raw_pointer_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
241#[note]
242pub struct DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
243 #[primary_span]
244 #[label]
245 pub span: Span,
246}
247
248#[derive(Diagnostic)]
249#[diag(mir_build_union_field_requires_unsafe, code = "E0133")]
250#[note]
251pub struct AccessToUnionFieldRequiresUnsafe {
252 #[primary_span]
253 #[label]
254 pub span: Span,
255}
256
257#[derive(Diagnostic)]
258#[diag(mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
259#[note]
260pub struct AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
261 #[primary_span]
262 #[label]
263 pub span: Span,
264}
265
266#[derive(Diagnostic)]
267#[diag(mir_build_mutation_of_layout_constrained_field_requires_unsafe, code = "E0133")]
268#[note]
269pub struct MutationOfLayoutConstrainedFieldRequiresUnsafe {
270 #[primary_span]
271 #[label]
272 pub span: Span,
273}
274
275#[derive(Diagnostic)]
276#[diag(
277 mir_build_mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed,
278 code = "E0133"
279)]
280#[note]
281pub struct MutationOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
282 #[primary_span]
283 #[label]
284 pub span: Span,
285}
286
287#[derive(Diagnostic)]
288#[diag(mir_build_borrow_of_layout_constrained_field_requires_unsafe, code = "E0133")]
289#[note]
290pub struct BorrowOfLayoutConstrainedFieldRequiresUnsafe {
291 #[primary_span]
292 #[label]
293 pub span: Span,
294}
295
296#[derive(Diagnostic)]
297#[diag(
298 mir_build_borrow_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed,
299 code = "E0133"
300)]
301#[note]
302pub struct BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
303 #[primary_span]
304 #[label]
305 pub span: Span,
306}
307
308#[derive(Diagnostic)]
309#[diag(mir_build_call_to_fn_with_requires_unsafe, code = "E0133")]
310#[note]
311pub struct CallToFunctionWithRequiresUnsafe<'a> {
312 #[primary_span]
313 #[label]
314 pub span: Span,
315 pub function: &'a str,
316}
317
318#[derive(Diagnostic)]
319#[diag(mir_build_call_to_fn_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
320#[note]
321pub struct CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> {
322 #[primary_span]
323 #[label]
324 pub span: Span,
325 pub function: &'a str,
326}
327
328#[derive(LintDiagnostic)]
329#[diag(mir_build_unused_unsafe)]
330pub struct UnusedUnsafe {
331 #[label]
332 pub span: Span,
333 #[subdiagnostic]
334 pub enclosing: Option<UnusedUnsafeEnclosing>,
335}
336
337#[derive(Subdiagnostic)]
338pub enum UnusedUnsafeEnclosing {
339 #[label(mir_build_unused_unsafe_enclosing_block_label)]
340 Block {
341 #[primary_span]
342 span: Span,
343 },
344 #[label(mir_build_unused_unsafe_enclosing_fn_label)]
345 Function {
346 #[primary_span]
347 span: Span,
348 },
349}
350
351pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> {
352 pub cx: &'m MatchCheckCtxt<'p, 'tcx>,
353 pub expr_span: Span,
354 pub span: Span,
355 pub ty: Ty<'tcx>,
356}
357
358impl<'a> IntoDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> {
359 fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
360 let mut diag = handler.struct_span_err_with_code(
361 self.span,
9ffffee4 362 fluent::mir_build_non_exhaustive_patterns_type_not_empty,
9c376795
FG
363 error_code!(E0004),
364 );
365
366 let peeled_ty = self.ty.peel_refs();
367 diag.set_arg("ty", self.ty);
368 diag.set_arg("peeled_ty", peeled_ty);
369
370 if let ty::Adt(def, _) = peeled_ty.kind() {
371 let def_span = self
372 .cx
373 .tcx
374 .hir()
375 .get_if_local(def.did())
376 .and_then(|node| node.ident())
377 .map(|ident| ident.span)
378 .unwrap_or_else(|| self.cx.tcx.def_span(def.did()));
379
380 // workaround to make test pass
381 let mut span: MultiSpan = def_span.into();
382 span.push_span_label(def_span, "");
383
9ffffee4 384 diag.span_note(span, fluent::mir_build_def_note);
9c376795
FG
385 }
386
387 let is_variant_list_non_exhaustive = match self.ty.kind() {
388 ty::Adt(def, _) if def.is_variant_list_non_exhaustive() && !def.did().is_local() => {
389 true
390 }
391 _ => false,
392 };
393
394 if is_variant_list_non_exhaustive {
9ffffee4 395 diag.note(fluent::mir_build_non_exhaustive_type_note);
9c376795 396 } else {
9ffffee4 397 diag.note(fluent::mir_build_type_note);
9c376795
FG
398 }
399
400 if let ty::Ref(_, sub_ty, _) = self.ty.kind() {
401 if !sub_ty.is_inhabited_from(self.cx.tcx, self.cx.module, self.cx.param_env) {
9ffffee4 402 diag.note(fluent::mir_build_reference_note);
9c376795
FG
403 }
404 }
405
406 let mut suggestion = None;
407 let sm = self.cx.tcx.sess.source_map();
408 if self.span.eq_ctxt(self.expr_span) {
409 // Get the span for the empty match body `{}`.
410 let (indentation, more) = if let Some(snippet) = sm.indentation_before(self.span) {
411 (format!("\n{}", snippet), " ")
412 } else {
413 (" ".to_string(), "")
414 };
415 suggestion = Some((
416 self.span.shrink_to_hi().with_hi(self.expr_span.hi()),
417 format!(
418 " {{{indentation}{more}_ => todo!(),{indentation}}}",
419 indentation = indentation,
420 more = more,
421 ),
422 ));
423 }
424
425 if let Some((span, sugg)) = suggestion {
426 diag.span_suggestion_verbose(
427 span,
9ffffee4 428 fluent::mir_build_suggestion,
9c376795
FG
429 sugg,
430 Applicability::HasPlaceholders,
431 );
432 } else {
9ffffee4 433 diag.help(fluent::mir_build_help);
9c376795
FG
434 }
435
436 diag
437 }
438}
439
440#[derive(Diagnostic)]
441#[diag(mir_build_static_in_pattern, code = "E0158")]
442pub struct StaticInPattern {
443 #[primary_span]
444 pub span: Span,
445}
446
447#[derive(Diagnostic)]
448#[diag(mir_build_assoc_const_in_pattern, code = "E0158")]
449pub struct AssocConstInPattern {
450 #[primary_span]
451 pub span: Span,
452}
453
454#[derive(Diagnostic)]
455#[diag(mir_build_const_param_in_pattern, code = "E0158")]
456pub struct ConstParamInPattern {
457 #[primary_span]
458 pub span: Span,
459}
460
461#[derive(Diagnostic)]
462#[diag(mir_build_non_const_path, code = "E0080")]
463pub struct NonConstPath {
464 #[primary_span]
465 pub span: Span,
466}
467
468#[derive(LintDiagnostic)]
469#[diag(mir_build_unreachable_pattern)]
470pub struct UnreachablePattern {
471 #[label]
472 pub span: Option<Span>,
9ffffee4 473 #[label(mir_build_catchall_label)]
9c376795
FG
474 pub catchall: Option<Span>,
475}
476
477#[derive(Diagnostic)]
478#[diag(mir_build_const_pattern_depends_on_generic_parameter)]
479pub struct ConstPatternDependsOnGenericParameter {
480 #[primary_span]
481 pub span: Span,
482}
483
484#[derive(Diagnostic)]
485#[diag(mir_build_could_not_eval_const_pattern)]
486pub struct CouldNotEvalConstPattern {
487 #[primary_span]
488 pub span: Span,
489}
490
491#[derive(Diagnostic)]
492#[diag(mir_build_lower_range_bound_must_be_less_than_or_equal_to_upper, code = "E0030")]
493pub struct LowerRangeBoundMustBeLessThanOrEqualToUpper {
494 #[primary_span]
495 #[label]
496 pub span: Span,
9ffffee4 497 #[note(mir_build_teach_note)]
9c376795
FG
498 pub teach: Option<()>,
499}
500
501#[derive(Diagnostic)]
502#[diag(mir_build_literal_in_range_out_of_bounds)]
503pub struct LiteralOutOfRange<'tcx> {
504 #[primary_span]
505 #[label]
506 pub span: Span,
507 pub ty: Ty<'tcx>,
508 pub max: u128,
509}
510
511#[derive(Diagnostic)]
512#[diag(mir_build_lower_range_bound_must_be_less_than_upper, code = "E0579")]
513pub struct LowerRangeBoundMustBeLessThanUpper {
514 #[primary_span]
515 pub span: Span,
516}
517
518#[derive(LintDiagnostic)]
519#[diag(mir_build_leading_irrefutable_let_patterns)]
520#[note]
521#[help]
522pub struct LeadingIrrefutableLetPatterns {
523 pub count: usize,
524}
525
526#[derive(LintDiagnostic)]
527#[diag(mir_build_trailing_irrefutable_let_patterns)]
528#[note]
529#[help]
530pub struct TrailingIrrefutableLetPatterns {
531 pub count: usize,
532}
533
534#[derive(LintDiagnostic)]
535#[diag(mir_build_bindings_with_variant_name, code = "E0170")]
536pub struct BindingsWithVariantName {
537 #[suggestion(code = "{ty_path}::{ident}", applicability = "machine-applicable")]
538 pub suggestion: Option<Span>,
539 pub ty_path: String,
540 pub ident: Ident,
541}
542
543#[derive(LintDiagnostic)]
544#[diag(mir_build_irrefutable_let_patterns_generic_let)]
545#[note]
546#[help]
547pub struct IrrefutableLetPatternsGenericLet {
548 pub count: usize,
549}
550
551#[derive(LintDiagnostic)]
552#[diag(mir_build_irrefutable_let_patterns_if_let)]
553#[note]
554#[help]
555pub struct IrrefutableLetPatternsIfLet {
556 pub count: usize,
557}
558
559#[derive(LintDiagnostic)]
560#[diag(mir_build_irrefutable_let_patterns_if_let_guard)]
561#[note]
562#[help]
563pub struct IrrefutableLetPatternsIfLetGuard {
564 pub count: usize,
565}
566
567#[derive(LintDiagnostic)]
568#[diag(mir_build_irrefutable_let_patterns_let_else)]
569#[note]
570#[help]
571pub struct IrrefutableLetPatternsLetElse {
572 pub count: usize,
573}
574
575#[derive(LintDiagnostic)]
576#[diag(mir_build_irrefutable_let_patterns_while_let)]
577#[note]
578#[help]
579pub struct IrrefutableLetPatternsWhileLet {
580 pub count: usize,
581}
582
583#[derive(Diagnostic)]
584#[diag(mir_build_borrow_of_moved_value)]
585pub struct BorrowOfMovedValue<'tcx> {
586 #[primary_span]
587 pub span: Span,
588 #[label]
9ffffee4 589 #[label(mir_build_occurs_because_label)]
9c376795 590 pub binding_span: Span,
9ffffee4 591 #[label(mir_build_value_borrowed_label)]
9c376795
FG
592 pub conflicts_ref: Vec<Span>,
593 pub name: Ident,
594 pub ty: Ty<'tcx>,
595 #[suggestion(code = "ref ", applicability = "machine-applicable")]
596 pub suggest_borrowing: Option<Span>,
597}
598
599#[derive(Diagnostic)]
600#[diag(mir_build_multiple_mut_borrows)]
601pub struct MultipleMutBorrows {
602 #[primary_span]
603 pub span: Span,
9c376795 604 #[subdiagnostic]
9ffffee4
FG
605 pub occurences: Vec<Conflict>,
606}
607
608#[derive(Diagnostic)]
609#[diag(mir_build_already_borrowed)]
610pub struct AlreadyBorrowed {
611 #[primary_span]
612 pub span: Span,
613 #[subdiagnostic]
614 pub occurences: Vec<Conflict>,
615}
616
617#[derive(Diagnostic)]
618#[diag(mir_build_already_mut_borrowed)]
619pub struct AlreadyMutBorrowed {
620 #[primary_span]
621 pub span: Span,
622 #[subdiagnostic]
623 pub occurences: Vec<Conflict>,
624}
625
626#[derive(Diagnostic)]
627#[diag(mir_build_moved_while_borrowed)]
628pub struct MovedWhileBorrowed {
629 #[primary_span]
630 pub span: Span,
631 #[subdiagnostic]
632 pub occurences: Vec<Conflict>,
9c376795
FG
633}
634
635#[derive(Subdiagnostic)]
9ffffee4
FG
636pub enum Conflict {
637 #[label(mir_build_mutable_borrow)]
638 Mut {
9c376795
FG
639 #[primary_span]
640 span: Span,
9ffffee4 641 name: Ident,
9c376795 642 },
9ffffee4
FG
643 #[label(mir_build_borrow)]
644 Ref {
9c376795
FG
645 #[primary_span]
646 span: Span,
9ffffee4 647 name: Ident,
9c376795 648 },
9ffffee4 649 #[label(mir_build_moved)]
9c376795
FG
650 Moved {
651 #[primary_span]
652 span: Span,
9ffffee4 653 name: Ident,
9c376795
FG
654 },
655}
656
657#[derive(Diagnostic)]
658#[diag(mir_build_union_pattern)]
659pub struct UnionPattern {
660 #[primary_span]
661 pub span: Span,
662}
663
664#[derive(Diagnostic)]
665#[diag(mir_build_type_not_structural)]
666pub struct TypeNotStructural<'tcx> {
667 #[primary_span]
668 pub span: Span,
669 pub non_sm_ty: Ty<'tcx>,
670}
671
672#[derive(Diagnostic)]
673#[diag(mir_build_invalid_pattern)]
674pub struct InvalidPattern<'tcx> {
675 #[primary_span]
676 pub span: Span,
677 pub non_sm_ty: Ty<'tcx>,
678}
679
680#[derive(Diagnostic)]
681#[diag(mir_build_unsized_pattern)]
682pub struct UnsizedPattern<'tcx> {
683 #[primary_span]
684 pub span: Span,
685 pub non_sm_ty: Ty<'tcx>,
686}
687
688#[derive(LintDiagnostic)]
689#[diag(mir_build_float_pattern)]
690pub struct FloatPattern;
691
692#[derive(LintDiagnostic)]
693#[diag(mir_build_pointer_pattern)]
694pub struct PointerPattern;
695
696#[derive(LintDiagnostic)]
697#[diag(mir_build_indirect_structural_match)]
698pub struct IndirectStructuralMatch<'tcx> {
699 pub non_sm_ty: Ty<'tcx>,
700}
701
702#[derive(LintDiagnostic)]
703#[diag(mir_build_nontrivial_structural_match)]
704pub struct NontrivialStructuralMatch<'tcx> {
705 pub non_sm_ty: Ty<'tcx>,
706}
707
708#[derive(LintDiagnostic)]
709#[diag(mir_build_overlapping_range_endpoints)]
710#[note]
711pub struct OverlappingRangeEndpoints<'tcx> {
9ffffee4 712 #[label(mir_build_range)]
9c376795
FG
713 pub range: Span,
714 #[subdiagnostic]
715 pub overlap: Vec<Overlap<'tcx>>,
716}
717
718pub struct Overlap<'tcx> {
719 pub span: Span,
720 pub range: Pat<'tcx>,
721}
722
723impl<'tcx> AddToDiagnostic for Overlap<'tcx> {
724 fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
725 where
726 F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
727 {
728 let Overlap { span, range } = self;
729
730 // FIXME(mejrs) unfortunately `#[derive(LintDiagnostic)]`
731 // does not support `#[subdiagnostic(eager)]`...
732 let message = format!("this range overlaps on `{range}`...");
733 diag.span_label(span, message);
734 }
735}
736
737#[derive(LintDiagnostic)]
738#[diag(mir_build_non_exhaustive_omitted_pattern)]
739#[help]
740#[note]
741pub(crate) struct NonExhaustiveOmittedPattern<'tcx> {
742 pub scrut_ty: Ty<'tcx>,
743 #[subdiagnostic]
744 pub uncovered: Uncovered<'tcx>,
745}
746
747#[derive(Subdiagnostic)]
748#[label(mir_build_uncovered)]
749pub(crate) struct Uncovered<'tcx> {
750 #[primary_span]
751 span: Span,
752 count: usize,
753 witness_1: Pat<'tcx>,
754 witness_2: Pat<'tcx>,
755 witness_3: Pat<'tcx>,
756 remainder: usize,
757}
758
759impl<'tcx> Uncovered<'tcx> {
760 pub fn new<'p>(
761 span: Span,
762 cx: &MatchCheckCtxt<'p, 'tcx>,
763 witnesses: Vec<DeconstructedPat<'p, 'tcx>>,
764 ) -> Self {
765 let witness_1 = witnesses.get(0).unwrap().to_pat(cx);
766 Self {
767 span,
768 count: witnesses.len(),
769 // Substitute dummy values if witnesses is smaller than 3. These will never be read.
770 witness_2: witnesses.get(1).map(|w| w.to_pat(cx)).unwrap_or_else(|| witness_1.clone()),
771 witness_3: witnesses.get(2).map(|w| w.to_pat(cx)).unwrap_or_else(|| witness_1.clone()),
772 witness_1,
773 remainder: witnesses.len().saturating_sub(3),
774 }
775 }
776}
777
778#[derive(Diagnostic)]
779#[diag(mir_build_pattern_not_covered, code = "E0005")]
780pub(crate) struct PatternNotCovered<'s, 'tcx> {
781 #[primary_span]
782 pub span: Span,
783 pub origin: &'s str,
784 #[subdiagnostic]
785 pub uncovered: Uncovered<'tcx>,
786 #[subdiagnostic]
787 pub inform: Option<Inform>,
788 #[subdiagnostic]
789 pub interpreted_as_const: Option<InterpretedAsConst>,
790 #[subdiagnostic]
791 pub adt_defined_here: Option<AdtDefinedHere<'tcx>>,
9ffffee4 792 #[note(mir_build_pattern_ty)]
9c376795
FG
793 pub _p: (),
794 pub pattern_ty: Ty<'tcx>,
795 #[subdiagnostic]
796 pub let_suggestion: Option<SuggestLet>,
797 #[subdiagnostic]
798 pub misc_suggestion: Option<MiscPatternSuggestion>,
799 #[subdiagnostic]
800 pub res_defined_here: Option<ResDefinedHere>,
801}
802
803#[derive(Subdiagnostic)]
804#[note(mir_build_inform_irrefutable)]
805#[note(mir_build_more_information)]
806pub struct Inform;
807
808pub struct AdtDefinedHere<'tcx> {
809 pub adt_def_span: Span,
810 pub ty: Ty<'tcx>,
811 pub variants: Vec<Variant>,
812}
813
814pub struct Variant {
815 pub span: Span,
816}
817
818impl<'tcx> AddToDiagnostic for AdtDefinedHere<'tcx> {
819 fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
820 where
821 F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
822 {
823 diag.set_arg("ty", self.ty);
824 let mut spans = MultiSpan::from(self.adt_def_span);
825
826 for Variant { span } in self.variants {
9ffffee4 827 spans.push_span_label(span, fluent::mir_build_variant_defined_here);
9c376795
FG
828 }
829
9ffffee4 830 diag.span_note(spans, fluent::mir_build_adt_defined_here);
9c376795
FG
831 }
832}
833
834#[derive(Subdiagnostic)]
835#[label(mir_build_res_defined_here)]
836pub struct ResDefinedHere {
837 #[primary_span]
838 pub def_span: Span,
839 pub res: Res,
840}
841
842#[derive(Subdiagnostic)]
843#[suggestion(
844 mir_build_interpreted_as_const,
845 code = "{variable}_var",
846 applicability = "maybe-incorrect"
847)]
848#[label(mir_build_confused)]
849pub struct InterpretedAsConst {
850 #[primary_span]
851 pub span: Span,
852 pub article: &'static str,
853 pub variable: String,
854 pub res: Res,
855}
856
857#[derive(Subdiagnostic)]
858pub enum SuggestLet {
859 #[multipart_suggestion(mir_build_suggest_if_let, applicability = "has-placeholders")]
860 If {
861 #[suggestion_part(code = "if ")]
862 start_span: Span,
863 #[suggestion_part(code = " {{ todo!() }}")]
864 semi_span: Span,
865 count: usize,
866 },
867 #[suggestion(
868 mir_build_suggest_let_else,
869 code = " else {{ todo!() }}",
870 applicability = "has-placeholders"
871 )]
872 Else {
873 #[primary_span]
874 end_span: Span,
875 count: usize,
876 },
877}
878
879#[derive(Subdiagnostic)]
880pub enum MiscPatternSuggestion {
881 #[suggestion(
882 mir_build_suggest_attempted_int_lit,
883 code = "_",
884 applicability = "maybe-incorrect"
885 )]
886 AttemptedIntegerLiteral {
887 #[primary_span]
888 start_span: Span,
889 },
890}
9ffffee4
FG
891
892#[derive(Diagnostic)]
893#[diag(mir_build_rustc_box_attribute_error)]
894pub struct RustcBoxAttributeError {
895 #[primary_span]
896 pub span: Span,
897 #[subdiagnostic]
898 pub reason: RustcBoxAttrReason,
899}
900
901#[derive(Subdiagnostic)]
902pub enum RustcBoxAttrReason {
903 #[note(mir_build_attributes)]
904 Attributes,
905 #[note(mir_build_not_box)]
906 NotBoxNew,
907 #[note(mir_build_missing_box)]
908 MissingBox,
909}