rust/compiler/rustc_trait_selection/src/errors.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

173 lines
5.1 KiB
Rust
Raw Normal View History

use crate::fluent_generated as fluent;
2023-06-28 18:08:21 +00:00
use rustc_errors::{
codes::*, AddToDiagnostic, Applicability, Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic,
Level, SubdiagnosticMessageOp,
2023-06-28 18:08:21 +00:00
};
use rustc_macros::Diagnostic;
use rustc_middle::ty::{self, ClosureKind, PolyTraitRef, Ty};
use rustc_span::{Span, Symbol};
#[derive(Diagnostic)]
2022-10-22 09:07:54 +00:00
#[diag(trait_selection_dump_vtable_entries)]
pub struct DumpVTableEntries<'a> {
#[primary_span]
pub span: Span,
pub trait_ref: PolyTraitRef<'a>,
pub entries: String,
}
#[derive(Diagnostic)]
2022-10-22 09:07:54 +00:00
#[diag(trait_selection_unable_to_construct_constant_value)]
pub struct UnableToConstructConstantValue<'a> {
#[primary_span]
pub span: Span,
2022-09-22 10:34:23 +00:00
pub unevaluated: ty::UnevaluatedConst<'a>,
}
#[derive(Diagnostic)]
#[diag(trait_selection_empty_on_clause_in_rustc_on_unimplemented, code = E0232)]
pub struct EmptyOnClauseInOnUnimplemented {
#[primary_span]
#[label]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(trait_selection_invalid_on_clause_in_rustc_on_unimplemented, code = E0232)]
pub struct InvalidOnClauseInOnUnimplemented {
#[primary_span]
#[label]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(trait_selection_no_value_in_rustc_on_unimplemented, code = E0232)]
#[note]
pub struct NoValueInOnUnimplemented {
#[primary_span]
#[label]
pub span: Span,
}
pub struct NegativePositiveConflict<'tcx> {
pub impl_span: Span,
pub trait_desc: ty::TraitRef<'tcx>,
pub self_ty: Option<Ty<'tcx>>,
pub negative_impl_span: Result<Span, Symbol>,
pub positive_impl_span: Result<Span, Symbol>,
}
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for NegativePositiveConflict<'_> {
2022-10-31 15:14:29 +00:00
#[track_caller]
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::trait_selection_negative_positive_conflict);
diag.arg("trait_desc", self.trait_desc.print_only_trait_path().to_string());
diag.arg("self_desc", self.self_ty.map_or_else(|| "none".to_string(), |ty| ty.to_string()));
diag.span(self.impl_span);
diag.code(E0751);
match self.negative_impl_span {
Ok(span) => {
diag.span_label(span, fluent::trait_selection_negative_implementation_here);
}
Err(cname) => {
diag.note(fluent::trait_selection_negative_implementation_in_crate);
diag.arg("negative_impl_cname", cname.to_string());
}
}
match self.positive_impl_span {
Ok(span) => {
diag.span_label(span, fluent::trait_selection_positive_implementation_here);
}
Err(cname) => {
diag.note(fluent::trait_selection_positive_implementation_in_crate);
diag.arg("positive_impl_cname", cname.to_string());
}
}
diag
}
}
2023-03-21 00:46:52 +00:00
#[derive(Diagnostic)]
#[diag(trait_selection_inherent_projection_normalization_overflow)]
pub struct InherentProjectionNormalizationOverflow {
#[primary_span]
pub span: Span,
pub ty: String,
}
2023-06-28 18:08:21 +00:00
pub enum AdjustSignatureBorrow {
Borrow { to_borrow: Vec<(Span, String)> },
RemoveBorrow { remove_borrow: Vec<(Span, String)> },
}
impl AddToDiagnostic for AdjustSignatureBorrow {
Reduce capabilities of `Diagnostic`. Currently many diagnostic modifier methods are available on both `Diagnostic` and `DiagnosticBuilder`. This commit removes most of them from `Diagnostic`. To minimize the diff size, it keeps them within `diagnostic.rs` but changes the surrounding `impl Diagnostic` block to `impl DiagnosticBuilder`. (I intend to move things around later, to give a more sensible code layout.) `Diagnostic` keeps a few methods that it still needs, like `sub`, `arg`, and `replace_args`. The `forward!` macro, which defined two additional methods per call (e.g. `note` and `with_note`), is replaced by the `with_fn!` macro, which defines one additional method per call (e.g. `with_note`). It's now also only used when necessary -- not all modifier methods currently need a `with_*` form. (New ones can be easily added as necessary.) All this also requires changing `trait AddToDiagnostic` so its methods take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`. There are three subdiagnostics -- `DelayedAtWithoutNewline`, `DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` -- that are created within the diagnostics machinery and appended to external diagnostics. These are handled at the `Diagnostic` level, which means it's now hard to construct them via `derive(Diagnostic)`, so instead we construct them by hand. This has no effect on what they look like when printed. There are lots of new `allow` markers for `untranslatable_diagnostics` and `diagnostics_outside_of_impl`. This is because `#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic` modifier methods, but missing from the `DiagnosticBuilder` modifier methods. They're now present.
2024-02-06 05:44:30 +00:00
fn add_to_diagnostic_with<G: EmissionGuarantee, F: SubdiagnosticMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
Reduce capabilities of `Diagnostic`. Currently many diagnostic modifier methods are available on both `Diagnostic` and `DiagnosticBuilder`. This commit removes most of them from `Diagnostic`. To minimize the diff size, it keeps them within `diagnostic.rs` but changes the surrounding `impl Diagnostic` block to `impl DiagnosticBuilder`. (I intend to move things around later, to give a more sensible code layout.) `Diagnostic` keeps a few methods that it still needs, like `sub`, `arg`, and `replace_args`. The `forward!` macro, which defined two additional methods per call (e.g. `note` and `with_note`), is replaced by the `with_fn!` macro, which defines one additional method per call (e.g. `with_note`). It's now also only used when necessary -- not all modifier methods currently need a `with_*` form. (New ones can be easily added as necessary.) All this also requires changing `trait AddToDiagnostic` so its methods take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`. There are three subdiagnostics -- `DelayedAtWithoutNewline`, `DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` -- that are created within the diagnostics machinery and appended to external diagnostics. These are handled at the `Diagnostic` level, which means it's now hard to construct them via `derive(Diagnostic)`, so instead we construct them by hand. This has no effect on what they look like when printed. There are lots of new `allow` markers for `untranslatable_diagnostics` and `diagnostics_outside_of_impl`. This is because `#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic` modifier methods, but missing from the `DiagnosticBuilder` modifier methods. They're now present.
2024-02-06 05:44:30 +00:00
_f: F,
) {
2023-06-28 18:08:21 +00:00
match self {
AdjustSignatureBorrow::Borrow { to_borrow } => {
diag.arg("len", to_borrow.len());
2023-06-28 18:08:21 +00:00
diag.multipart_suggestion_verbose(
fluent::trait_selection_adjust_signature_borrow,
to_borrow,
Applicability::MaybeIncorrect,
);
}
AdjustSignatureBorrow::RemoveBorrow { remove_borrow } => {
diag.arg("len", remove_borrow.len());
2023-06-28 18:08:21 +00:00
diag.multipart_suggestion_verbose(
fluent::trait_selection_adjust_signature_remove_borrow,
remove_borrow,
Applicability::MaybeIncorrect,
);
}
}
}
}
#[derive(Diagnostic)]
#[diag(trait_selection_closure_kind_mismatch, code = E0525)]
pub struct ClosureKindMismatch {
#[primary_span]
#[label]
pub closure_span: Span,
pub expected: ClosureKind,
pub found: ClosureKind,
#[label(trait_selection_closure_kind_requirement)]
pub cause_span: Span,
2024-02-14 23:52:57 +00:00
pub trait_prefix: &'static str,
#[subdiagnostic]
pub fn_once_label: Option<ClosureFnOnceLabel>,
#[subdiagnostic]
pub fn_mut_label: Option<ClosureFnMutLabel>,
}
#[derive(Subdiagnostic)]
#[label(trait_selection_closure_fn_once_label)]
pub struct ClosureFnOnceLabel {
#[primary_span]
pub span: Span,
pub place: String,
}
#[derive(Subdiagnostic)]
#[label(trait_selection_closure_fn_mut_label)]
pub struct ClosureFnMutLabel {
#[primary_span]
pub span: Span,
pub place: String,
}
2024-02-14 23:52:57 +00:00
#[derive(Diagnostic)]
#[diag(trait_selection_async_closure_not_fn)]
pub(crate) struct AsyncClosureNotFn {
#[primary_span]
pub span: Span,
pub kind: &'static str,
}