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