2024-05-22 14:46:05 +00:00
|
|
|
use rustc_errors::{codes::*, Diag, LintDiagnostic};
|
2023-04-30 01:20:53 +00:00
|
|
|
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
|
2024-02-27 11:57:52 +00:00
|
|
|
use rustc_middle::mir::AssertKind;
|
2023-05-16 10:47:50 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2023-04-30 01:20:53 +00:00
|
|
|
use rustc_session::lint::{self, Lint};
|
2023-05-16 10:47:50 +00:00
|
|
|
use rustc_span::def_id::DefId;
|
2023-04-30 01:20:53 +00:00
|
|
|
use rustc_span::Span;
|
|
|
|
|
2023-11-26 18:55:01 +00:00
|
|
|
use crate::fluent_generated as fluent;
|
|
|
|
|
2023-04-30 01:20:53 +00:00
|
|
|
#[derive(LintDiagnostic)]
|
|
|
|
pub(crate) enum ConstMutate {
|
|
|
|
#[diag(mir_transform_const_modify)]
|
|
|
|
#[note]
|
|
|
|
Modify {
|
|
|
|
#[note(mir_transform_const_defined_here)]
|
|
|
|
konst: Span,
|
|
|
|
},
|
|
|
|
#[diag(mir_transform_const_mut_borrow)]
|
|
|
|
#[note]
|
|
|
|
#[note(mir_transform_note2)]
|
|
|
|
MutBorrow {
|
|
|
|
#[note(mir_transform_note3)]
|
|
|
|
method_call: Option<Span>,
|
|
|
|
#[note(mir_transform_const_defined_here)]
|
|
|
|
konst: Span,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Diagnostic)]
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-13 23:57:07 +00:00
|
|
|
#[diag(mir_transform_unaligned_packed_ref, code = E0793)]
|
2023-04-30 01:20:53 +00:00
|
|
|
#[note]
|
|
|
|
#[note(mir_transform_note_ub)]
|
|
|
|
#[help]
|
|
|
|
pub(crate) struct UnalignedPackedRef {
|
|
|
|
#[primary_span]
|
|
|
|
pub span: Span,
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:18:50 +00:00
|
|
|
pub(crate) struct AssertLint<P> {
|
|
|
|
pub span: Span,
|
|
|
|
pub assert_kind: AssertKind<P>,
|
|
|
|
pub lint_kind: AssertLintKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) enum AssertLintKind {
|
|
|
|
ArithmeticOverflow,
|
|
|
|
UnconditionalPanic,
|
2023-04-30 01:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 01:03:51 +00:00
|
|
|
impl<'a, P: std::fmt::Debug> LintDiagnostic<'a, ()> for AssertLint<P> {
|
2024-02-22 23:20:45 +00:00
|
|
|
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
|
2024-05-22 14:46:05 +00:00
|
|
|
diag.primary_message(match self.lint_kind {
|
|
|
|
AssertLintKind::ArithmeticOverflow => fluent::mir_transform_arithmetic_overflow,
|
|
|
|
AssertLintKind::UnconditionalPanic => fluent::mir_transform_operation_will_panic,
|
|
|
|
});
|
|
|
|
let label = self.assert_kind.diagnostic_message();
|
2024-01-15 13:18:50 +00:00
|
|
|
self.assert_kind.add_args(&mut |name, value| {
|
2023-12-23 22:08:41 +00:00
|
|
|
diag.arg(name, value);
|
2023-05-17 10:30:14 +00:00
|
|
|
});
|
2024-05-22 14:46:05 +00:00
|
|
|
diag.span_label(self.span, label);
|
2023-04-30 01:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:18:50 +00:00
|
|
|
impl AssertLintKind {
|
2023-04-30 01:20:53 +00:00
|
|
|
pub fn lint(&self) -> &'static Lint {
|
|
|
|
match self {
|
2024-01-15 13:18:50 +00:00
|
|
|
AssertLintKind::ArithmeticOverflow => lint::builtin::ARITHMETIC_OVERFLOW,
|
|
|
|
AssertLintKind::UnconditionalPanic => lint::builtin::UNCONDITIONAL_PANIC,
|
2023-04-30 01:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(LintDiagnostic)]
|
|
|
|
#[diag(mir_transform_ffi_unwind_call)]
|
|
|
|
pub(crate) struct FfiUnwindCall {
|
|
|
|
#[label(mir_transform_ffi_unwind_call)]
|
|
|
|
pub span: Span,
|
|
|
|
pub foreign: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(LintDiagnostic)]
|
|
|
|
#[diag(mir_transform_fn_item_ref)]
|
|
|
|
pub(crate) struct FnItemRef {
|
|
|
|
#[suggestion(code = "{sugg}", applicability = "unspecified")]
|
|
|
|
pub span: Span,
|
|
|
|
pub sugg: String,
|
|
|
|
pub ident: String,
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:47:50 +00:00
|
|
|
pub(crate) struct MustNotSupend<'tcx, 'a> {
|
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2023-04-30 01:20:53 +00:00
|
|
|
pub yield_sp: Span,
|
|
|
|
pub reason: Option<MustNotSuspendReason>,
|
|
|
|
pub src_sp: Span,
|
|
|
|
pub pre: &'a str,
|
2023-05-16 10:47:50 +00:00
|
|
|
pub def_id: DefId,
|
2023-04-30 01:20:53 +00:00
|
|
|
pub post: &'a str,
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:47:50 +00:00
|
|
|
// Needed for def_path_str
|
2024-03-08 01:03:51 +00:00
|
|
|
impl<'a> LintDiagnostic<'a, ()> for MustNotSupend<'_, '_> {
|
2024-02-22 23:20:45 +00:00
|
|
|
fn decorate_lint<'b>(self, diag: &'b mut rustc_errors::Diag<'a, ()>) {
|
2024-05-22 14:46:05 +00:00
|
|
|
diag.primary_message(fluent::mir_transform_must_not_suspend);
|
2023-11-26 18:55:01 +00:00
|
|
|
diag.span_label(self.yield_sp, fluent::_subdiag::label);
|
2023-05-16 10:47:50 +00:00
|
|
|
if let Some(reason) = self.reason {
|
2024-02-14 14:17:27 +00:00
|
|
|
diag.subdiagnostic(diag.dcx, reason);
|
2023-05-16 10:47:50 +00:00
|
|
|
}
|
2023-11-26 18:55:01 +00:00
|
|
|
diag.span_help(self.src_sp, fluent::_subdiag::help);
|
2023-12-23 22:08:41 +00:00
|
|
|
diag.arg("pre", self.pre);
|
|
|
|
diag.arg("def_path", self.tcx.def_path_str(self.def_id));
|
|
|
|
diag.arg("post", self.post);
|
2023-05-16 10:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 01:20:53 +00:00
|
|
|
#[derive(Subdiagnostic)]
|
|
|
|
#[note(mir_transform_note)]
|
|
|
|
pub(crate) struct MustNotSuspendReason {
|
|
|
|
#[primary_span]
|
|
|
|
pub span: Span,
|
|
|
|
pub reason: String,
|
|
|
|
}
|