rustc_mir_transform/
errors.rs
1use rustc_errors::codes::*;
2use rustc_errors::{Diag, LintDiagnostic};
3use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
4use rustc_middle::mir::AssertKind;
5use rustc_middle::ty::TyCtxt;
6use rustc_session::lint::{self, Lint};
7use rustc_span::def_id::DefId;
8use rustc_span::{Ident, Span, Symbol};
9
10use crate::fluent_generated as fluent;
11
12#[derive(LintDiagnostic)]
13#[diag(mir_transform_unconditional_recursion)]
14#[help]
15pub(crate) struct UnconditionalRecursion {
16 #[label]
17 pub(crate) span: Span,
18 #[label(mir_transform_unconditional_recursion_call_site_label)]
19 pub(crate) call_sites: Vec<Span>,
20}
21
22#[derive(Diagnostic)]
23#[diag(mir_transform_force_inline_attr)]
24#[note]
25pub(crate) struct InvalidForceInline {
26 #[primary_span]
27 pub attr_span: Span,
28 #[label(mir_transform_callee)]
29 pub callee_span: Span,
30 pub callee: String,
31 pub reason: &'static str,
32}
33
34#[derive(LintDiagnostic)]
35pub(crate) enum ConstMutate {
36 #[diag(mir_transform_const_modify)]
37 #[note]
38 Modify {
39 #[note(mir_transform_const_defined_here)]
40 konst: Span,
41 },
42 #[diag(mir_transform_const_mut_borrow)]
43 #[note]
44 #[note(mir_transform_note2)]
45 MutBorrow {
46 #[note(mir_transform_note3)]
47 method_call: Option<Span>,
48 #[note(mir_transform_const_defined_here)]
49 konst: Span,
50 },
51}
52
53#[derive(Diagnostic)]
54#[diag(mir_transform_unaligned_packed_ref, code = E0793)]
55#[note]
56#[note(mir_transform_note_ub)]
57#[help]
58pub(crate) struct UnalignedPackedRef {
59 #[primary_span]
60 pub span: Span,
61}
62
63#[derive(Diagnostic)]
64#[diag(mir_transform_unknown_pass_name)]
65pub(crate) struct UnknownPassName<'a> {
66 pub(crate) name: &'a str,
67}
68
69pub(crate) struct AssertLint<P> {
70 pub span: Span,
71 pub assert_kind: AssertKind<P>,
72 pub lint_kind: AssertLintKind,
73}
74
75pub(crate) enum AssertLintKind {
76 ArithmeticOverflow,
77 UnconditionalPanic,
78}
79
80impl<'a, P: std::fmt::Debug> LintDiagnostic<'a, ()> for AssertLint<P> {
81 fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
82 diag.primary_message(match self.lint_kind {
83 AssertLintKind::ArithmeticOverflow => fluent::mir_transform_arithmetic_overflow,
84 AssertLintKind::UnconditionalPanic => fluent::mir_transform_operation_will_panic,
85 });
86 let label = self.assert_kind.diagnostic_message();
87 self.assert_kind.add_args(&mut |name, value| {
88 diag.arg(name, value);
89 });
90 diag.span_label(self.span, label);
91 }
92}
93
94impl AssertLintKind {
95 pub(crate) fn lint(&self) -> &'static Lint {
96 match self {
97 AssertLintKind::ArithmeticOverflow => lint::builtin::ARITHMETIC_OVERFLOW,
98 AssertLintKind::UnconditionalPanic => lint::builtin::UNCONDITIONAL_PANIC,
99 }
100 }
101}
102
103#[derive(LintDiagnostic)]
104#[diag(mir_transform_ffi_unwind_call)]
105pub(crate) struct FfiUnwindCall {
106 #[label(mir_transform_ffi_unwind_call)]
107 pub span: Span,
108 pub foreign: bool,
109}
110
111#[derive(LintDiagnostic)]
112#[diag(mir_transform_fn_item_ref)]
113pub(crate) struct FnItemRef {
114 #[suggestion(code = "{sugg}", applicability = "unspecified")]
115 pub span: Span,
116 pub sugg: String,
117 pub ident: Ident,
118}
119
120#[derive(Diagnostic)]
121#[diag(mir_transform_exceeds_mcdc_test_vector_limit)]
122pub(crate) struct MCDCExceedsTestVectorLimit {
123 #[primary_span]
124 pub(crate) span: Span,
125 pub(crate) max_num_test_vectors: usize,
126}
127
128pub(crate) struct MustNotSupend<'a, 'tcx> {
129 pub tcx: TyCtxt<'tcx>,
130 pub yield_sp: Span,
131 pub reason: Option<MustNotSuspendReason>,
132 pub src_sp: Span,
133 pub pre: &'a str,
134 pub def_id: DefId,
135 pub post: &'a str,
136}
137
138impl<'a> LintDiagnostic<'a, ()> for MustNotSupend<'_, '_> {
140 fn decorate_lint<'b>(self, diag: &'b mut rustc_errors::Diag<'a, ()>) {
141 diag.primary_message(fluent::mir_transform_must_not_suspend);
142 diag.span_label(self.yield_sp, fluent::_subdiag::label);
143 if let Some(reason) = self.reason {
144 diag.subdiagnostic(reason);
145 }
146 diag.span_help(self.src_sp, fluent::_subdiag::help);
147 diag.arg("pre", self.pre);
148 diag.arg("def_path", self.tcx.def_path_str(self.def_id));
149 diag.arg("post", self.post);
150 }
151}
152
153#[derive(Subdiagnostic)]
154#[note(mir_transform_note)]
155pub(crate) struct MustNotSuspendReason {
156 #[primary_span]
157 pub span: Span,
158 pub reason: String,
159}
160
161#[derive(LintDiagnostic)]
162#[diag(mir_transform_undefined_transmute)]
163#[note]
164#[note(mir_transform_note2)]
165#[help]
166pub(crate) struct UndefinedTransmute;
167
168#[derive(Diagnostic)]
169#[diag(mir_transform_force_inline)]
170#[note]
171pub(crate) struct ForceInlineFailure {
172 #[label(mir_transform_caller)]
173 pub caller_span: Span,
174 #[label(mir_transform_callee)]
175 pub callee_span: Span,
176 #[label(mir_transform_attr)]
177 pub attr_span: Span,
178 #[primary_span]
179 #[label(mir_transform_call)]
180 pub call_span: Span,
181 pub callee: String,
182 pub caller: String,
183 pub reason: &'static str,
184 #[subdiagnostic]
185 pub justification: Option<ForceInlineJustification>,
186}
187
188#[derive(Subdiagnostic)]
189#[note(mir_transform_force_inline_justification)]
190pub(crate) struct ForceInlineJustification {
191 pub sym: Symbol,
192}