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}
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
161#[derive(LintDiagnostic)]
162#[diag(mir_transform_unused_capture_maybe_capture_ref)]
163#[help]
164pub(crate) struct UnusedCaptureMaybeCaptureRef {
165    pub name: Symbol,
166}
167
168#[derive(LintDiagnostic)]
169#[diag(mir_transform_unused_var_assigned_only)]
170#[note]
171pub(crate) struct UnusedVarAssignedOnly {
172    pub name: Symbol,
173    #[subdiagnostic]
174    pub typo: Option<PatternTypo>,
175}
176
177#[derive(LintDiagnostic)]
178#[diag(mir_transform_unused_assign)]
179pub(crate) struct UnusedAssign {
180    pub name: Symbol,
181    #[subdiagnostic]
182    pub suggestion: Option<UnusedAssignSuggestion>,
183    #[help]
184    pub help: bool,
185}
186
187#[derive(Subdiagnostic)]
188#[multipart_suggestion(mir_transform_unused_assign_suggestion, applicability = "maybe-incorrect")]
189pub(crate) struct UnusedAssignSuggestion {
190    pub pre: &'static str,
191    #[suggestion_part(code = "{pre}mut ")]
192    pub ty_span: Option<Span>,
193    #[suggestion_part(code = "")]
194    pub ty_ref_span: Span,
195    #[suggestion_part(code = "*")]
196    pub pre_lhs_span: Span,
197    #[suggestion_part(code = "")]
198    pub rhs_borrow_span: Span,
199}
200
201#[derive(LintDiagnostic)]
202#[diag(mir_transform_unused_assign_passed)]
203#[help]
204pub(crate) struct UnusedAssignPassed {
205    pub name: Symbol,
206}
207
208#[derive(LintDiagnostic)]
209#[diag(mir_transform_unused_variable)]
210pub(crate) struct UnusedVariable {
211    pub name: Symbol,
212    #[subdiagnostic]
213    pub string_interp: Vec<UnusedVariableStringInterp>,
214    #[subdiagnostic]
215    pub sugg: UnusedVariableSugg,
216}
217
218#[derive(Subdiagnostic)]
219pub(crate) enum UnusedVariableSugg {
220    #[multipart_suggestion(
221        mir_transform_unused_variable_try_ignore,
222        applicability = "machine-applicable"
223    )]
224    TryIgnore {
225        #[suggestion_part(code = "{name}: _")]
226        shorthands: Vec<Span>,
227        #[suggestion_part(code = "_")]
228        non_shorthands: Vec<Span>,
229        name: Symbol,
230    },
231
232    #[multipart_suggestion(
233        mir_transform_unused_var_underscore,
234        applicability = "machine-applicable"
235    )]
236    TryPrefix {
237        #[suggestion_part(code = "_{name}")]
238        spans: Vec<Span>,
239        name: Symbol,
240        #[subdiagnostic]
241        typo: Option<PatternTypo>,
242    },
243
244    #[help(mir_transform_unused_variable_args_in_macro)]
245    NoSugg {
246        #[primary_span]
247        span: Span,
248        name: Symbol,
249    },
250}
251
252pub(crate) struct UnusedVariableStringInterp {
253    pub lit: Span,
254}
255
256impl Subdiagnostic for UnusedVariableStringInterp {
257    fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
258        diag.span_label(
259            self.lit,
260            crate::fluent_generated::mir_transform_maybe_string_interpolation,
261        );
262        diag.multipart_suggestion(
263            crate::fluent_generated::mir_transform_string_interpolation_only_works,
264            vec![
265                (self.lit.shrink_to_lo(), String::from("format!(")),
266                (self.lit.shrink_to_hi(), String::from(")")),
267            ],
268            Applicability::MachineApplicable,
269        );
270    }
271}
272
273#[derive(Subdiagnostic)]
274#[multipart_suggestion(
275    mir_transform_unused_variable_typo,
276    style = "verbose",
277    applicability = "maybe-incorrect"
278)]
279pub(crate) struct PatternTypo {
280    #[suggestion_part(code = "{code}")]
281    pub span: Span,
282    pub code: String,
283    pub item_name: Symbol,
284    pub kind: &'static str,
285}
286
287pub(crate) struct MustNotSupend<'a, 'tcx> {
288    pub tcx: TyCtxt<'tcx>,
289    pub yield_sp: Span,
290    pub reason: Option<MustNotSuspendReason>,
291    pub src_sp: Span,
292    pub pre: &'a str,
293    pub def_id: DefId,
294    pub post: &'a str,
295}
296
297// Needed for def_path_str
298impl<'a> LintDiagnostic<'a, ()> for MustNotSupend<'_, '_> {
299    fn decorate_lint<'b>(self, diag: &'b mut rustc_errors::Diag<'a, ()>) {
300        diag.primary_message(fluent::mir_transform_must_not_suspend);
301        diag.span_label(self.yield_sp, fluent::_subdiag::label);
302        if let Some(reason) = self.reason {
303            diag.subdiagnostic(reason);
304        }
305        diag.span_help(self.src_sp, fluent::_subdiag::help);
306        diag.arg("pre", self.pre);
307        diag.arg("def_path", self.tcx.def_path_str(self.def_id));
308        diag.arg("post", self.post);
309    }
310}
311
312#[derive(Subdiagnostic)]
313#[note(mir_transform_note)]
314pub(crate) struct MustNotSuspendReason {
315    #[primary_span]
316    pub span: Span,
317    pub reason: String,
318}
319
320#[derive(Diagnostic)]
321#[diag(mir_transform_force_inline)]
322#[note]
323pub(crate) struct ForceInlineFailure {
324    #[label(mir_transform_caller)]
325    pub caller_span: Span,
326    #[label(mir_transform_callee)]
327    pub callee_span: Span,
328    #[label(mir_transform_attr)]
329    pub attr_span: Span,
330    #[primary_span]
331    #[label(mir_transform_call)]
332    pub call_span: Span,
333    pub callee: String,
334    pub caller: String,
335    pub reason: &'static str,
336    #[subdiagnostic]
337    pub justification: Option<ForceInlineJustification>,
338}
339
340#[derive(Subdiagnostic)]
341#[note(mir_transform_force_inline_justification)]
342pub(crate) struct ForceInlineJustification {
343    pub sym: Symbol,
344}