]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/errors.rs
68179001b916dbc3ac5128f91d7ed1cdcd7fe6e6
[rust.git] / compiler / rustc_mir_build / src / errors.rs
1 use crate::thir::pattern::MatchCheckCtxt;
2 use rustc_errors::Handler;
3 use rustc_errors::{
4     error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic, MultiSpan,
5 };
6 use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
7 use rustc_middle::ty::{self, Ty};
8 use rustc_span::{symbol::Ident, Span};
9
10 #[derive(LintDiagnostic)]
11 #[diag(mir_build_unconditional_recursion)]
12 #[help]
13 pub struct UnconditionalRecursion {
14     #[label]
15     pub span: Span,
16     #[label(mir_build_unconditional_recursion_call_site_label)]
17     pub call_sites: Vec<Span>,
18 }
19
20 #[derive(LintDiagnostic)]
21 #[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe)]
22 #[note]
23 pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> {
24     #[label]
25     pub span: Span,
26     pub function: &'a str,
27 }
28
29 #[derive(LintDiagnostic)]
30 #[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless)]
31 #[note]
32 pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
33     #[label]
34     pub span: Span,
35 }
36
37 #[derive(LintDiagnostic)]
38 #[diag(mir_build_unsafe_op_in_unsafe_fn_inline_assembly_requires_unsafe)]
39 #[note]
40 pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe {
41     #[label]
42     pub span: Span,
43 }
44
45 #[derive(LintDiagnostic)]
46 #[diag(mir_build_unsafe_op_in_unsafe_fn_initializing_type_with_requires_unsafe)]
47 #[note]
48 pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe {
49     #[label]
50     pub span: Span,
51 }
52
53 #[derive(LintDiagnostic)]
54 #[diag(mir_build_unsafe_op_in_unsafe_fn_mutable_static_requires_unsafe)]
55 #[note]
56 pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe {
57     #[label]
58     pub span: Span,
59 }
60
61 #[derive(LintDiagnostic)]
62 #[diag(mir_build_unsafe_op_in_unsafe_fn_extern_static_requires_unsafe)]
63 #[note]
64 pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe {
65     #[label]
66     pub span: Span,
67 }
68
69 #[derive(LintDiagnostic)]
70 #[diag(mir_build_unsafe_op_in_unsafe_fn_deref_raw_pointer_requires_unsafe)]
71 #[note]
72 pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe {
73     #[label]
74     pub span: Span,
75 }
76
77 #[derive(LintDiagnostic)]
78 #[diag(mir_build_unsafe_op_in_unsafe_fn_union_field_requires_unsafe)]
79 #[note]
80 pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe {
81     #[label]
82     pub span: Span,
83 }
84
85 #[derive(LintDiagnostic)]
86 #[diag(mir_build_unsafe_op_in_unsafe_fn_mutation_of_layout_constrained_field_requires_unsafe)]
87 #[note]
88 pub struct UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe {
89     #[label]
90     pub span: Span,
91 }
92
93 #[derive(LintDiagnostic)]
94 #[diag(mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe)]
95 pub struct UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe {
96     #[label]
97     pub span: Span,
98 }
99
100 #[derive(LintDiagnostic)]
101 #[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe)]
102 #[note]
103 pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> {
104     #[label]
105     pub span: Span,
106     pub function: &'a str,
107 }
108
109 #[derive(Diagnostic)]
110 #[diag(mir_build_call_to_unsafe_fn_requires_unsafe, code = "E0133")]
111 #[note]
112 pub struct CallToUnsafeFunctionRequiresUnsafe<'a> {
113     #[primary_span]
114     #[label]
115     pub span: Span,
116     pub function: &'a str,
117 }
118
119 #[derive(Diagnostic)]
120 #[diag(mir_build_call_to_unsafe_fn_requires_unsafe_nameless, code = "E0133")]
121 #[note]
122 pub struct CallToUnsafeFunctionRequiresUnsafeNameless {
123     #[primary_span]
124     #[label]
125     pub span: Span,
126 }
127
128 #[derive(Diagnostic)]
129 #[diag(mir_build_call_to_unsafe_fn_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
130 #[note]
131 pub struct CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> {
132     #[primary_span]
133     #[label]
134     pub span: Span,
135     pub function: &'a str,
136 }
137
138 #[derive(Diagnostic)]
139 #[diag(
140     mir_build_call_to_unsafe_fn_requires_unsafe_nameless_unsafe_op_in_unsafe_fn_allowed,
141     code = "E0133"
142 )]
143 #[note]
144 pub struct CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed {
145     #[primary_span]
146     #[label]
147     pub span: Span,
148 }
149
150 #[derive(Diagnostic)]
151 #[diag(mir_build_inline_assembly_requires_unsafe, code = "E0133")]
152 #[note]
153 pub struct UseOfInlineAssemblyRequiresUnsafe {
154     #[primary_span]
155     #[label]
156     pub span: Span,
157 }
158
159 #[derive(Diagnostic)]
160 #[diag(mir_build_inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
161 #[note]
162 pub struct UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
163     #[primary_span]
164     #[label]
165     pub span: Span,
166 }
167
168 #[derive(Diagnostic)]
169 #[diag(mir_build_initializing_type_with_requires_unsafe, code = "E0133")]
170 #[note]
171 pub struct InitializingTypeWithRequiresUnsafe {
172     #[primary_span]
173     #[label]
174     pub span: Span,
175 }
176
177 #[derive(Diagnostic)]
178 #[diag(
179     mir_build_initializing_type_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed,
180     code = "E0133"
181 )]
182 #[note]
183 pub struct InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
184     #[primary_span]
185     #[label]
186     pub span: Span,
187 }
188
189 #[derive(Diagnostic)]
190 #[diag(mir_build_mutable_static_requires_unsafe, code = "E0133")]
191 #[note]
192 pub struct UseOfMutableStaticRequiresUnsafe {
193     #[primary_span]
194     #[label]
195     pub span: Span,
196 }
197
198 #[derive(Diagnostic)]
199 #[diag(mir_build_mutable_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
200 #[note]
201 pub struct UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
202     #[primary_span]
203     #[label]
204     pub span: Span,
205 }
206
207 #[derive(Diagnostic)]
208 #[diag(mir_build_extern_static_requires_unsafe, code = "E0133")]
209 #[note]
210 pub struct UseOfExternStaticRequiresUnsafe {
211     #[primary_span]
212     #[label]
213     pub span: Span,
214 }
215
216 #[derive(Diagnostic)]
217 #[diag(mir_build_extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
218 #[note]
219 pub struct UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
220     #[primary_span]
221     #[label]
222     pub span: Span,
223 }
224
225 #[derive(Diagnostic)]
226 #[diag(mir_build_deref_raw_pointer_requires_unsafe, code = "E0133")]
227 #[note]
228 pub struct DerefOfRawPointerRequiresUnsafe {
229     #[primary_span]
230     #[label]
231     pub span: Span,
232 }
233
234 #[derive(Diagnostic)]
235 #[diag(mir_build_deref_raw_pointer_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
236 #[note]
237 pub struct DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
238     #[primary_span]
239     #[label]
240     pub span: Span,
241 }
242
243 #[derive(Diagnostic)]
244 #[diag(mir_build_union_field_requires_unsafe, code = "E0133")]
245 #[note]
246 pub struct AccessToUnionFieldRequiresUnsafe {
247     #[primary_span]
248     #[label]
249     pub span: Span,
250 }
251
252 #[derive(Diagnostic)]
253 #[diag(mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
254 #[note]
255 pub struct AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
256     #[primary_span]
257     #[label]
258     pub span: Span,
259 }
260
261 #[derive(Diagnostic)]
262 #[diag(mir_build_mutation_of_layout_constrained_field_requires_unsafe, code = "E0133")]
263 #[note]
264 pub struct MutationOfLayoutConstrainedFieldRequiresUnsafe {
265     #[primary_span]
266     #[label]
267     pub span: Span,
268 }
269
270 #[derive(Diagnostic)]
271 #[diag(
272     mir_build_mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed,
273     code = "E0133"
274 )]
275 #[note]
276 pub struct MutationOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
277     #[primary_span]
278     #[label]
279     pub span: Span,
280 }
281
282 #[derive(Diagnostic)]
283 #[diag(mir_build_borrow_of_layout_constrained_field_requires_unsafe, code = "E0133")]
284 #[note]
285 pub struct BorrowOfLayoutConstrainedFieldRequiresUnsafe {
286     #[primary_span]
287     #[label]
288     pub span: Span,
289 }
290
291 #[derive(Diagnostic)]
292 #[diag(
293     mir_build_borrow_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed,
294     code = "E0133"
295 )]
296 #[note]
297 pub struct BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
298     #[primary_span]
299     #[label]
300     pub span: Span,
301 }
302
303 #[derive(Diagnostic)]
304 #[diag(mir_build_call_to_fn_with_requires_unsafe, code = "E0133")]
305 #[note]
306 pub struct CallToFunctionWithRequiresUnsafe<'a> {
307     #[primary_span]
308     #[label]
309     pub span: Span,
310     pub function: &'a str,
311 }
312
313 #[derive(Diagnostic)]
314 #[diag(mir_build_call_to_fn_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")]
315 #[note]
316 pub struct CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> {
317     #[primary_span]
318     #[label]
319     pub span: Span,
320     pub function: &'a str,
321 }
322
323 #[derive(LintDiagnostic)]
324 #[diag(mir_build_unused_unsafe)]
325 pub struct UnusedUnsafe {
326     #[label]
327     pub span: Span,
328     #[subdiagnostic]
329     pub enclosing: Option<UnusedUnsafeEnclosing>,
330 }
331
332 #[derive(Subdiagnostic)]
333 pub enum UnusedUnsafeEnclosing {
334     #[label(mir_build_unused_unsafe_enclosing_block_label)]
335     Block {
336         #[primary_span]
337         span: Span,
338     },
339     #[label(mir_build_unused_unsafe_enclosing_fn_label)]
340     Function {
341         #[primary_span]
342         span: Span,
343     },
344 }
345
346 pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> {
347     pub cx: &'m MatchCheckCtxt<'p, 'tcx>,
348     pub expr_span: Span,
349     pub span: Span,
350     pub ty: Ty<'tcx>,
351 }
352
353 impl<'a> IntoDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> {
354     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
355         let mut diag = handler.struct_span_err_with_code(
356             self.span,
357             rustc_errors::fluent::mir_build_non_exhaustive_patterns_type_not_empty,
358             error_code!(E0004),
359         );
360
361         let peeled_ty = self.ty.peel_refs();
362         diag.set_arg("ty", self.ty);
363         diag.set_arg("peeled_ty", peeled_ty);
364
365         if let ty::Adt(def, _) = peeled_ty.kind() {
366             let def_span = self
367                 .cx
368                 .tcx
369                 .hir()
370                 .get_if_local(def.did())
371                 .and_then(|node| node.ident())
372                 .map(|ident| ident.span)
373                 .unwrap_or_else(|| self.cx.tcx.def_span(def.did()));
374
375             // workaround to make test pass
376             let mut span: MultiSpan = def_span.into();
377             span.push_span_label(def_span, "");
378
379             diag.span_note(span, rustc_errors::fluent::def_note);
380         }
381
382         let is_variant_list_non_exhaustive = match self.ty.kind() {
383             ty::Adt(def, _) if def.is_variant_list_non_exhaustive() && !def.did().is_local() => {
384                 true
385             }
386             _ => false,
387         };
388
389         if is_variant_list_non_exhaustive {
390             diag.note(rustc_errors::fluent::non_exhaustive_type_note);
391         } else {
392             diag.note(rustc_errors::fluent::type_note);
393         }
394
395         if let ty::Ref(_, sub_ty, _) = self.ty.kind() {
396             if !sub_ty.is_inhabited_from(self.cx.tcx, self.cx.module, self.cx.param_env) {
397                 diag.note(rustc_errors::fluent::reference_note);
398             }
399         }
400
401         let mut suggestion = None;
402         let sm = self.cx.tcx.sess.source_map();
403         if self.span.eq_ctxt(self.expr_span) {
404             // Get the span for the empty match body `{}`.
405             let (indentation, more) = if let Some(snippet) = sm.indentation_before(self.span) {
406                 (format!("\n{}", snippet), "    ")
407             } else {
408                 (" ".to_string(), "")
409             };
410             suggestion = Some((
411                 self.span.shrink_to_hi().with_hi(self.expr_span.hi()),
412                 format!(
413                     " {{{indentation}{more}_ => todo!(),{indentation}}}",
414                     indentation = indentation,
415                     more = more,
416                 ),
417             ));
418         }
419
420         if let Some((span, sugg)) = suggestion {
421             diag.span_suggestion_verbose(
422                 span,
423                 rustc_errors::fluent::suggestion,
424                 sugg,
425                 Applicability::HasPlaceholders,
426             );
427         } else {
428             diag.help(rustc_errors::fluent::help);
429         }
430
431         diag
432     }
433 }
434
435 #[derive(Diagnostic)]
436 #[diag(mir_build_static_in_pattern, code = "E0158")]
437 pub struct StaticInPattern {
438     #[primary_span]
439     pub span: Span,
440 }
441
442 #[derive(Diagnostic)]
443 #[diag(mir_build_assoc_const_in_pattern, code = "E0158")]
444 pub struct AssocConstInPattern {
445     #[primary_span]
446     pub span: Span,
447 }
448
449 #[derive(Diagnostic)]
450 #[diag(mir_build_const_param_in_pattern, code = "E0158")]
451 pub struct ConstParamInPattern {
452     #[primary_span]
453     pub span: Span,
454 }
455
456 #[derive(Diagnostic)]
457 #[diag(mir_build_non_const_path, code = "E0080")]
458 pub struct NonConstPath {
459     #[primary_span]
460     pub span: Span,
461 }
462
463 #[derive(LintDiagnostic)]
464 #[diag(mir_build_unreachable_pattern)]
465 pub struct UnreachablePattern {
466     #[label]
467     pub span: Option<Span>,
468     #[label(catchall_label)]
469     pub catchall: Option<Span>,
470 }
471
472 #[derive(Diagnostic)]
473 #[diag(mir_build_const_pattern_depends_on_generic_parameter)]
474 pub struct ConstPatternDependsOnGenericParameter {
475     #[primary_span]
476     pub span: Span,
477 }
478
479 #[derive(Diagnostic)]
480 #[diag(mir_build_could_not_eval_const_pattern)]
481 pub struct CouldNotEvalConstPattern {
482     #[primary_span]
483     pub span: Span,
484 }
485
486 #[derive(Diagnostic)]
487 #[diag(mir_build_lower_range_bound_must_be_less_than_or_equal_to_upper, code = "E0030")]
488 pub struct LowerRangeBoundMustBeLessThanOrEqualToUpper {
489     #[primary_span]
490     #[label]
491     pub span: Span,
492     #[note(teach_note)]
493     pub teach: Option<()>,
494 }
495
496 #[derive(Diagnostic)]
497 #[diag(mir_build_lower_range_bound_must_be_less_than_upper, code = "E0579")]
498 pub struct LowerRangeBoundMustBeLessThanUpper {
499     #[primary_span]
500     pub span: Span,
501 }
502
503 #[derive(LintDiagnostic)]
504 #[diag(mir_build_leading_irrefutable_let_patterns)]
505 #[note]
506 #[help]
507 pub struct LeadingIrrefutableLetPatterns {
508     pub count: usize,
509 }
510
511 #[derive(LintDiagnostic)]
512 #[diag(mir_build_trailing_irrefutable_let_patterns)]
513 #[note]
514 #[help]
515 pub struct TrailingIrrefutableLetPatterns {
516     pub count: usize,
517 }
518
519 #[derive(LintDiagnostic)]
520 #[diag(mir_build_bindings_with_variant_name, code = "E0170")]
521 pub struct BindingsWithVariantName {
522     #[suggestion(code = "{ty_path}::{ident}", applicability = "machine-applicable")]
523     pub suggestion: Option<Span>,
524     pub ty_path: String,
525     pub ident: Ident,
526 }
527
528 #[derive(LintDiagnostic)]
529 #[diag(mir_build_irrefutable_let_patterns_generic_let)]
530 #[note]
531 #[help]
532 pub struct IrrefutableLetPatternsGenericLet {
533     pub count: usize,
534 }
535
536 #[derive(LintDiagnostic)]
537 #[diag(mir_build_irrefutable_let_patterns_if_let)]
538 #[note]
539 #[help]
540 pub struct IrrefutableLetPatternsIfLet {
541     pub count: usize,
542 }
543
544 #[derive(LintDiagnostic)]
545 #[diag(mir_build_irrefutable_let_patterns_if_let_guard)]
546 #[note]
547 #[help]
548 pub struct IrrefutableLetPatternsIfLetGuard {
549     pub count: usize,
550 }
551
552 #[derive(LintDiagnostic)]
553 #[diag(mir_build_irrefutable_let_patterns_let_else)]
554 #[note]
555 #[help]
556 pub struct IrrefutableLetPatternsLetElse {
557     pub count: usize,
558 }
559
560 #[derive(LintDiagnostic)]
561 #[diag(mir_build_irrefutable_let_patterns_while_let)]
562 #[note]
563 #[help]
564 pub struct IrrefutableLetPatternsWhileLet {
565     pub count: usize,
566 }
567
568 #[derive(Diagnostic)]
569 #[diag(mir_build_borrow_of_moved_value)]
570 pub struct BorrowOfMovedValue<'tcx> {
571     #[primary_span]
572     pub span: Span,
573     #[label]
574     #[label(occurs_because_label)]
575     pub binding_span: Span,
576     #[label(value_borrowed_label)]
577     pub conflicts_ref: Vec<Span>,
578     pub name: Ident,
579     pub ty: Ty<'tcx>,
580     #[suggestion(code = "ref ", applicability = "machine-applicable")]
581     pub suggest_borrowing: Option<Span>,
582 }
583
584 #[derive(Diagnostic)]
585 #[diag(mir_build_multiple_mut_borrows)]
586 pub struct MultipleMutBorrows {
587     #[primary_span]
588     pub span: Span,
589     #[label]
590     pub binding_span: Span,
591     #[subdiagnostic]
592     pub occurences: Vec<MultipleMutBorrowOccurence>,
593     pub name: Ident,
594 }
595
596 #[derive(Subdiagnostic)]
597 pub enum MultipleMutBorrowOccurence {
598     #[label(mutable_borrow)]
599     Mutable {
600         #[primary_span]
601         span: Span,
602         name_mut: Ident,
603     },
604     #[label(immutable_borrow)]
605     Immutable {
606         #[primary_span]
607         span: Span,
608         name_immut: Ident,
609     },
610     #[label(moved)]
611     Moved {
612         #[primary_span]
613         span: Span,
614         name_moved: Ident,
615     },
616 }