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::query::Key;
6use rustc_middle::ty::TyCtxt;
7use rustc_session::lint::{self, Lint};
8use rustc_span::def_id::DefId;
9use rustc_span::{Ident, Span, Symbol};
10
11use crate::fluent_generated as fluent;
12
13/// Emit diagnostic for calls to `#[inline(always)]`-annotated functions with a
14/// `#[target_feature]` attribute where the caller enables a different set of target features.
15pub(crate) fn emit_inline_always_target_feature_diagnostic<'a, 'tcx>(
16    tcx: TyCtxt<'tcx>,
17    call_span: Span,
18    callee_def_id: DefId,
19    caller_def_id: DefId,
20    callee_only: &[&'a str],
21) {
22    let callee = tcx.def_path_str(callee_def_id);
23    let caller = tcx.def_path_str(caller_def_id);
24
25    tcx.node_span_lint(
26        lint::builtin::INLINE_ALWAYS_MISMATCHING_TARGET_FEATURES,
27        tcx.local_def_id_to_hir_id(caller_def_id.as_local().unwrap()),
28        call_span,
29        |lint| {
30            lint.primary_message(format!(
31                "call to `#[inline(always)]`-annotated `{callee}` \
32                requires the same target features to be inlined"
33            ));
34            lint.note("function will not be inlined");
35
36            lint.note(format!(
37                "the following target features are on `{callee}` but missing from `{caller}`: {}",
38                callee_only.join(", ")
39            ));
40            lint.span_note(callee_def_id.default_span(tcx), format!("`{callee}` is defined here"));
41
42            let feats = callee_only.join(",");
43            lint.span_suggestion(
44                tcx.def_span(caller_def_id).shrink_to_lo(),
45                format!("add `#[target_feature]` attribute to `{caller}`"),
46                format!("#[target_feature(enable = \"{feats}\")]\n"),
47                lint::Applicability::MaybeIncorrect,
48            );
49        },
50    );
51}
52
53#[derive(LintDiagnostic)]
54#[diag(mir_transform_unconditional_recursion)]
55#[help]
56pub(crate) struct UnconditionalRecursion {
57    #[label]
58    pub(crate) span: Span,
59    #[label(mir_transform_unconditional_recursion_call_site_label)]
60    pub(crate) call_sites: Vec<Span>,
61}
62
63#[derive(Diagnostic)]
64#[diag(mir_transform_force_inline_attr)]
65#[note]
66pub(crate) struct InvalidForceInline {
67    #[primary_span]
68    pub attr_span: Span,
69    #[label(mir_transform_callee)]
70    pub callee_span: Span,
71    pub callee: String,
72    pub reason: &'static str,
73}
74
75#[derive(LintDiagnostic)]
76pub(crate) enum ConstMutate {
77    #[diag(mir_transform_const_modify)]
78    #[note]
79    Modify {
80        #[note(mir_transform_const_defined_here)]
81        konst: Span,
82    },
83    #[diag(mir_transform_const_mut_borrow)]
84    #[note]
85    #[note(mir_transform_note2)]
86    MutBorrow {
87        #[note(mir_transform_note3)]
88        method_call: Option<Span>,
89        #[note(mir_transform_const_defined_here)]
90        konst: Span,
91    },
92}
93
94#[derive(Diagnostic)]
95#[diag(mir_transform_unaligned_packed_ref, code = E0793)]
96#[note]
97#[note(mir_transform_note_ub)]
98#[help]
99pub(crate) struct UnalignedPackedRef {
100    #[primary_span]
101    pub span: Span,
102}
103
104#[derive(Diagnostic)]
105#[diag(mir_transform_unknown_pass_name)]
106pub(crate) struct UnknownPassName<'a> {
107    pub(crate) name: &'a str,
108}
109
110pub(crate) struct AssertLint<P> {
111    pub span: Span,
112    pub assert_kind: AssertKind<P>,
113    pub lint_kind: AssertLintKind,
114}
115
116pub(crate) enum AssertLintKind {
117    ArithmeticOverflow,
118    UnconditionalPanic,
119}
120
121impl<'a, P: std::fmt::Debug> LintDiagnostic<'a, ()> for AssertLint<P> {
122    fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
123        diag.primary_message(match self.lint_kind {
124            AssertLintKind::ArithmeticOverflow => fluent::mir_transform_arithmetic_overflow,
125            AssertLintKind::UnconditionalPanic => fluent::mir_transform_operation_will_panic,
126        });
127        let label = self.assert_kind.diagnostic_message();
128        self.assert_kind.add_args(&mut |name, value| {
129            diag.arg(name, value);
130        });
131        diag.span_label(self.span, label);
132    }
133}
134
135impl AssertLintKind {
136    pub(crate) fn lint(&self) -> &'static Lint {
137        match self {
138            AssertLintKind::ArithmeticOverflow => lint::builtin::ARITHMETIC_OVERFLOW,
139            AssertLintKind::UnconditionalPanic => lint::builtin::UNCONDITIONAL_PANIC,
140        }
141    }
142}
143
144#[derive(LintDiagnostic)]
145#[diag(mir_transform_ffi_unwind_call)]
146pub(crate) struct FfiUnwindCall {
147    #[label(mir_transform_ffi_unwind_call)]
148    pub span: Span,
149    pub foreign: bool,
150}
151
152#[derive(LintDiagnostic)]
153#[diag(mir_transform_fn_item_ref)]
154pub(crate) struct FnItemRef {
155    #[suggestion(code = "{sugg}", applicability = "unspecified")]
156    pub span: Span,
157    pub sugg: String,
158    pub ident: Ident,
159}
160
161pub(crate) struct MustNotSupend<'a, 'tcx> {
162    pub tcx: TyCtxt<'tcx>,
163    pub yield_sp: Span,
164    pub reason: Option<MustNotSuspendReason>,
165    pub src_sp: Span,
166    pub pre: &'a str,
167    pub def_id: DefId,
168    pub post: &'a str,
169}
170
171// Needed for def_path_str
172impl<'a> LintDiagnostic<'a, ()> for MustNotSupend<'_, '_> {
173    fn decorate_lint<'b>(self, diag: &'b mut rustc_errors::Diag<'a, ()>) {
174        diag.primary_message(fluent::mir_transform_must_not_suspend);
175        diag.span_label(self.yield_sp, fluent::_subdiag::label);
176        if let Some(reason) = self.reason {
177            diag.subdiagnostic(reason);
178        }
179        diag.span_help(self.src_sp, fluent::_subdiag::help);
180        diag.arg("pre", self.pre);
181        diag.arg("def_path", self.tcx.def_path_str(self.def_id));
182        diag.arg("post", self.post);
183    }
184}
185
186#[derive(Subdiagnostic)]
187#[note(mir_transform_note)]
188pub(crate) struct MustNotSuspendReason {
189    #[primary_span]
190    pub span: Span,
191    pub reason: String,
192}
193
194#[derive(Diagnostic)]
195#[diag(mir_transform_force_inline)]
196#[note]
197pub(crate) struct ForceInlineFailure {
198    #[label(mir_transform_caller)]
199    pub caller_span: Span,
200    #[label(mir_transform_callee)]
201    pub callee_span: Span,
202    #[label(mir_transform_attr)]
203    pub attr_span: Span,
204    #[primary_span]
205    #[label(mir_transform_call)]
206    pub call_span: Span,
207    pub callee: String,
208    pub caller: String,
209    pub reason: &'static str,
210    #[subdiagnostic]
211    pub justification: Option<ForceInlineJustification>,
212}
213
214#[derive(Subdiagnostic)]
215#[note(mir_transform_force_inline_justification)]
216pub(crate) struct ForceInlineJustification {
217    pub sym: Symbol,
218}