rustc_mir_transform/
errors.rs

1use rustc_errors::codes::*;
2use rustc_errors::{Applicability, Diag, EmissionGuarantee, LintDiagnostic, Subdiagnostic};
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    pub ty_descr: &'static str,
103    pub align: u64,
104}
105
106#[derive(Diagnostic)]
107#[diag(mir_transform_unknown_pass_name)]
108pub(crate) struct UnknownPassName<'a> {
109    pub(crate) name: &'a str,
110}
111
112pub(crate) struct AssertLint<P> {
113    pub span: Span,
114    pub assert_kind: AssertKind<P>,
115    pub lint_kind: AssertLintKind,
116}
117
118pub(crate) enum AssertLintKind {
119    ArithmeticOverflow,
120    UnconditionalPanic,
121}
122
123impl<'a, P: std::fmt::Debug> LintDiagnostic<'a, ()> for AssertLint<P> {
124    fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
125        diag.primary_message(match self.lint_kind {
126            AssertLintKind::ArithmeticOverflow => fluent::mir_transform_arithmetic_overflow,
127            AssertLintKind::UnconditionalPanic => fluent::mir_transform_operation_will_panic,
128        });
129        let label = self.assert_kind.diagnostic_message();
130        self.assert_kind.add_args(&mut |name, value| {
131            diag.arg(name, value);
132        });
133        diag.span_label(self.span, label);
134    }
135}
136
137impl AssertLintKind {
138    pub(crate) fn lint(&self) -> &'static Lint {
139        match self {
140            AssertLintKind::ArithmeticOverflow => lint::builtin::ARITHMETIC_OVERFLOW,
141            AssertLintKind::UnconditionalPanic => lint::builtin::UNCONDITIONAL_PANIC,
142        }
143    }
144}
145
146#[derive(LintDiagnostic)]
147#[diag(mir_transform_asm_unwind_call)]
148pub(crate) struct AsmUnwindCall {
149    #[label(mir_transform_asm_unwind_call)]
150    pub span: Span,
151}
152
153#[derive(LintDiagnostic)]
154#[diag(mir_transform_ffi_unwind_call)]
155pub(crate) struct FfiUnwindCall {
156    #[label(mir_transform_ffi_unwind_call)]
157    pub span: Span,
158    pub foreign: bool,
159}
160
161#[derive(LintDiagnostic)]
162#[diag(mir_transform_fn_item_ref)]
163pub(crate) struct FnItemRef {
164    #[suggestion(code = "{sugg}", applicability = "unspecified")]
165    pub span: Span,
166    pub sugg: String,
167    pub ident: Ident,
168}
169
170#[derive(LintDiagnostic)]
171#[diag(mir_transform_unused_capture_maybe_capture_ref)]
172#[help]
173pub(crate) struct UnusedCaptureMaybeCaptureRef {
174    pub name: Symbol,
175}
176
177#[derive(LintDiagnostic)]
178#[diag(mir_transform_unused_var_assigned_only)]
179#[note]
180pub(crate) struct UnusedVarAssignedOnly {
181    pub name: Symbol,
182    #[subdiagnostic]
183    pub typo: Option<PatternTypo>,
184}
185
186#[derive(LintDiagnostic)]
187#[diag(mir_transform_unused_assign)]
188pub(crate) struct UnusedAssign {
189    pub name: Symbol,
190    #[subdiagnostic]
191    pub suggestion: Option<UnusedAssignSuggestion>,
192    #[help]
193    pub help: bool,
194}
195
196#[derive(Subdiagnostic)]
197#[multipart_suggestion(mir_transform_unused_assign_suggestion, applicability = "maybe-incorrect")]
198pub(crate) struct UnusedAssignSuggestion {
199    pub pre: &'static str,
200    #[suggestion_part(code = "{pre}mut ")]
201    pub ty_span: Option<Span>,
202    #[suggestion_part(code = "")]
203    pub ty_ref_span: Span,
204    #[suggestion_part(code = "*")]
205    pub pre_lhs_span: Span,
206    #[suggestion_part(code = "")]
207    pub rhs_borrow_span: Span,
208}
209
210#[derive(LintDiagnostic)]
211#[diag(mir_transform_unused_assign_passed)]
212#[help]
213pub(crate) struct UnusedAssignPassed {
214    pub name: Symbol,
215}
216
217#[derive(LintDiagnostic)]
218#[diag(mir_transform_unused_variable)]
219pub(crate) struct UnusedVariable {
220    pub name: Symbol,
221    #[subdiagnostic]
222    pub string_interp: Vec<UnusedVariableStringInterp>,
223    #[subdiagnostic]
224    pub sugg: UnusedVariableSugg,
225}
226
227#[derive(Subdiagnostic)]
228pub(crate) enum UnusedVariableSugg {
229    #[multipart_suggestion(
230        mir_transform_unused_variable_try_ignore,
231        applicability = "machine-applicable"
232    )]
233    TryIgnore {
234        #[suggestion_part(code = "{name}: _")]
235        shorthands: Vec<Span>,
236        #[suggestion_part(code = "_")]
237        non_shorthands: Vec<Span>,
238        name: Symbol,
239    },
240
241    #[multipart_suggestion(
242        mir_transform_unused_var_underscore,
243        applicability = "machine-applicable"
244    )]
245    TryPrefix {
246        #[suggestion_part(code = "_{name}")]
247        spans: Vec<Span>,
248        name: Symbol,
249        #[subdiagnostic]
250        typo: Option<PatternTypo>,
251    },
252
253    #[help(mir_transform_unused_variable_args_in_macro)]
254    NoSugg {
255        #[primary_span]
256        span: Span,
257        name: Symbol,
258    },
259}
260
261pub(crate) struct UnusedVariableStringInterp {
262    pub lit: Span,
263}
264
265impl Subdiagnostic for UnusedVariableStringInterp {
266    fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
267        diag.span_label(
268            self.lit,
269            crate::fluent_generated::mir_transform_maybe_string_interpolation,
270        );
271        diag.multipart_suggestion(
272            crate::fluent_generated::mir_transform_string_interpolation_only_works,
273            vec![
274                (self.lit.shrink_to_lo(), String::from("format!(")),
275                (self.lit.shrink_to_hi(), String::from(")")),
276            ],
277            Applicability::MachineApplicable,
278        );
279    }
280}
281
282#[derive(Subdiagnostic)]
283#[multipart_suggestion(
284    mir_transform_unused_variable_typo,
285    style = "verbose",
286    applicability = "maybe-incorrect"
287)]
288pub(crate) struct PatternTypo {
289    #[suggestion_part(code = "{code}")]
290    pub span: Span,
291    pub code: String,
292    pub item_name: Symbol,
293    pub kind: &'static str,
294}
295
296pub(crate) struct MustNotSupend<'a, 'tcx> {
297    pub tcx: TyCtxt<'tcx>,
298    pub yield_sp: Span,
299    pub reason: Option<MustNotSuspendReason>,
300    pub src_sp: Span,
301    pub pre: &'a str,
302    pub def_id: DefId,
303    pub post: &'a str,
304}
305
306// Needed for def_path_str
307impl<'a> LintDiagnostic<'a, ()> for MustNotSupend<'_, '_> {
308    fn decorate_lint<'b>(self, diag: &'b mut rustc_errors::Diag<'a, ()>) {
309        diag.primary_message(fluent::mir_transform_must_not_suspend);
310        diag.span_label(self.yield_sp, fluent::_subdiag::label);
311        if let Some(reason) = self.reason {
312            diag.subdiagnostic(reason);
313        }
314        diag.span_help(self.src_sp, fluent::_subdiag::help);
315        diag.arg("pre", self.pre);
316        diag.arg("def_path", self.tcx.def_path_str(self.def_id));
317        diag.arg("post", self.post);
318    }
319}
320
321#[derive(Subdiagnostic)]
322#[note(mir_transform_note)]
323pub(crate) struct MustNotSuspendReason {
324    #[primary_span]
325    pub span: Span,
326    pub reason: String,
327}
328
329#[derive(Diagnostic)]
330#[diag(mir_transform_force_inline)]
331#[note]
332pub(crate) struct ForceInlineFailure {
333    #[label(mir_transform_caller)]
334    pub caller_span: Span,
335    #[label(mir_transform_callee)]
336    pub callee_span: Span,
337    #[label(mir_transform_attr)]
338    pub attr_span: Span,
339    #[primary_span]
340    #[label(mir_transform_call)]
341    pub call_span: Span,
342    pub callee: String,
343    pub caller: String,
344    pub reason: &'static str,
345    #[subdiagnostic]
346    pub justification: Option<ForceInlineJustification>,
347}
348
349#[derive(Subdiagnostic)]
350#[note(mir_transform_force_inline_justification)]
351pub(crate) struct ForceInlineJustification {
352    pub sym: Symbol,
353}