mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 09:13:07 +03:00
b711f5689f
Currently, `rustc_errors` depends on `rustc_abi`, which depends on `rustc_error_messages`. This is a bit odd. `rustc_errors` depends on `rustc_abi` for a single reason: `rustc_abi` defines a type `TargetDataLayoutErrors` and `rustc_errors` impls `Diagnostic` for that type. We can get a more natural relationship by inverting the dependency, moving the `Diagnostic` trait upstream. Then `rustc_abi` defines `TargetDataLayoutErrors` and also impls `Diagnostic` for it. `rustc_errors` is already pretty far upstream in the crate graph, it doesn't hurt to push it a little further because errors are a very low-level concept.
86 lines
2.1 KiB
Rust
86 lines
2.1 KiB
Rust
use std::borrow::Cow;
|
|
|
|
use rustc_error_messages::{DiagArgValue, IntoDiagArg};
|
|
use rustc_macros::Subdiagnostic;
|
|
use rustc_span::{Span, Symbol};
|
|
|
|
use crate::diagnostic::DiagLocation;
|
|
use crate::{Diag, EmissionGuarantee, Subdiagnostic};
|
|
|
|
impl IntoDiagArg for DiagLocation {
|
|
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
|
|
DiagArgValue::Str(Cow::from(self.to_string()))
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct DiagSymbolList<S = Symbol>(Vec<S>);
|
|
|
|
impl<S> From<Vec<S>> for DiagSymbolList<S> {
|
|
fn from(v: Vec<S>) -> Self {
|
|
DiagSymbolList(v)
|
|
}
|
|
}
|
|
|
|
impl<S> FromIterator<S> for DiagSymbolList<S> {
|
|
fn from_iter<T: IntoIterator<Item = S>>(iter: T) -> Self {
|
|
iter.into_iter().collect::<Vec<_>>().into()
|
|
}
|
|
}
|
|
|
|
impl<S: std::fmt::Display> IntoDiagArg for DiagSymbolList<S> {
|
|
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
|
|
DiagArgValue::StrListSepByAnd(
|
|
self.0.into_iter().map(|sym| Cow::Owned(format!("`{sym}`"))).collect(),
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Utility struct used to apply a single label while highlighting multiple spans
|
|
pub struct SingleLabelManySpans {
|
|
pub spans: Vec<Span>,
|
|
pub label: &'static str,
|
|
}
|
|
impl Subdiagnostic for SingleLabelManySpans {
|
|
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
|
|
diag.span_labels(self.spans, self.label);
|
|
}
|
|
}
|
|
|
|
#[derive(Subdiagnostic)]
|
|
#[label(
|
|
"expected lifetime {$count ->
|
|
[1] parameter
|
|
*[other] parameters
|
|
}"
|
|
)]
|
|
pub struct ExpectedLifetimeParameter {
|
|
#[primary_span]
|
|
pub span: Span,
|
|
pub count: usize,
|
|
}
|
|
|
|
#[derive(Subdiagnostic)]
|
|
#[suggestion(
|
|
"indicate the anonymous {$count ->
|
|
[1] lifetime
|
|
*[other] lifetimes
|
|
}",
|
|
code = "{suggestion}",
|
|
style = "verbose"
|
|
)]
|
|
pub struct IndicateAnonymousLifetime {
|
|
#[primary_span]
|
|
pub span: Span,
|
|
pub count: usize,
|
|
pub suggestion: String,
|
|
}
|
|
|
|
#[derive(Subdiagnostic)]
|
|
pub struct ElidedLifetimeInPathSubdiag {
|
|
#[subdiagnostic]
|
|
pub expected: ExpectedLifetimeParameter,
|
|
#[subdiagnostic]
|
|
pub indicate: Option<IndicateAnonymousLifetime>,
|
|
}
|