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