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
120pub(crate) struct MustNotSupend<'a, 'tcx> {
121    pub tcx: TyCtxt<'tcx>,
122    pub yield_sp: Span,
123    pub reason: Option<MustNotSuspendReason>,
124    pub src_sp: Span,
125    pub pre: &'a str,
126    pub def_id: DefId,
127    pub post: &'a str,
128}
129
130// Needed for def_path_str
131impl<'a> LintDiagnostic<'a, ()> for MustNotSupend<'_, '_> {
132    fn decorate_lint<'b>(self, diag: &'b mut rustc_errors::Diag<'a, ()>) {
133        diag.primary_message(fluent::mir_transform_must_not_suspend);
134        diag.span_label(self.yield_sp, fluent::_subdiag::label);
135        if let Some(reason) = self.reason {
136            diag.subdiagnostic(reason);
137        }
138        diag.span_help(self.src_sp, fluent::_subdiag::help);
139        diag.arg("pre", self.pre);
140        diag.arg("def_path", self.tcx.def_path_str(self.def_id));
141        diag.arg("post", self.post);
142    }
143}
144
145#[derive(Subdiagnostic)]
146#[note(mir_transform_note)]
147pub(crate) struct MustNotSuspendReason {
148    #[primary_span]
149    pub span: Span,
150    pub reason: String,
151}
152
153#[derive(Diagnostic)]
154#[diag(mir_transform_force_inline)]
155#[note]
156pub(crate) struct ForceInlineFailure {
157    #[label(mir_transform_caller)]
158    pub caller_span: Span,
159    #[label(mir_transform_callee)]
160    pub callee_span: Span,
161    #[label(mir_transform_attr)]
162    pub attr_span: Span,
163    #[primary_span]
164    #[label(mir_transform_call)]
165    pub call_span: Span,
166    pub callee: String,
167    pub caller: String,
168    pub reason: &'static str,
169    #[subdiagnostic]
170    pub justification: Option<ForceInlineJustification>,
171}
172
173#[derive(Subdiagnostic)]
174#[note(mir_transform_force_inline_justification)]
175pub(crate) struct ForceInlineJustification {
176    pub sym: Symbol,
177}