Remove the lifetime from DiagnosticArgValue.

Because it's almost always static.

This makes `impl IntoDiagnosticArg for DiagnosticArgValue` trivial,
which is nice.

There are a few diagnostics constructed in
`compiler/rustc_mir_build/src/check_unsafety.rs` and
`compiler/rustc_mir_transform/src/errors.rs` that now need symbols
converted to `String` with `to_string` instead of `&str` with `as_str`,
but that' no big deal, and worth it for the simplifications elsewhere.
This commit is contained in:
Nicholas Nethercote 2024-01-30 15:27:16 +11:00
parent fb4bca04fa
commit 5350edb9e8
38 changed files with 113 additions and 116 deletions

View file

@ -24,7 +24,7 @@ pub struct SuggestionsDisabled;
/// `DiagnosticArg` are converted to `FluentArgs` (consuming the collection) at the start of
/// diagnostic emission.
pub type DiagnosticArg<'iter, 'source> =
(&'iter DiagnosticArgName<'source>, &'iter DiagnosticArgValue<'source>);
(&'iter DiagnosticArgName<'source>, &'iter DiagnosticArgValue);
/// Name of a diagnostic argument.
pub type DiagnosticArgName<'source> = Cow<'source, str>;
@ -32,10 +32,10 @@ pub type DiagnosticArgName<'source> = Cow<'source, str>;
/// Simplified version of `FluentValue` that can implement `Encodable` and `Decodable`. Converted
/// to a `FluentValue` by the emitter to be used in diagnostic translation.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
pub enum DiagnosticArgValue<'source> {
Str(Cow<'source, str>),
pub enum DiagnosticArgValue {
Str(Cow<'static, str>),
Number(i128),
StrListSepByAnd(Vec<Cow<'source, str>>),
StrListSepByAnd(Vec<Cow<'static, str>>),
}
/// Converts a value of a type into a `DiagnosticArg` (typically a field of an `IntoDiagnostic`
@ -43,23 +43,17 @@ pub enum DiagnosticArgValue<'source> {
/// being converted rather than on `DiagnosticArgValue`, which enables types from other `rustc_*`
/// crates to implement this.
pub trait IntoDiagnosticArg {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
fn into_diagnostic_arg(self) -> DiagnosticArgValue;
}
impl<'source> IntoDiagnosticArg for DiagnosticArgValue<'source> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
match self {
DiagnosticArgValue::Str(s) => DiagnosticArgValue::Str(Cow::Owned(s.into_owned())),
DiagnosticArgValue::Number(n) => DiagnosticArgValue::Number(n),
DiagnosticArgValue::StrListSepByAnd(l) => DiagnosticArgValue::StrListSepByAnd(
l.into_iter().map(|s| Cow::Owned(s.into_owned())).collect(),
),
}
impl IntoDiagnosticArg for DiagnosticArgValue {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
self
}
}
impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
fn into(self) -> FluentValue<'source> {
impl Into<FluentValue<'static>> for DiagnosticArgValue {
fn into(self) -> FluentValue<'static> {
match self {
DiagnosticArgValue::Str(s) => From::from(s),
DiagnosticArgValue::Number(n) => From::from(n),
@ -109,7 +103,7 @@ pub struct Diagnostic {
pub span: MultiSpan,
pub children: Vec<SubDiagnostic>,
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
args: FxHashMap<DiagnosticArgName<'static>, DiagnosticArgValue<'static>>,
args: FxHashMap<DiagnosticArgName<'static>, DiagnosticArgValue>,
/// This is not used for highlighting or rendering any error message. Rather, it can be used
/// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
@ -931,7 +925,7 @@ impl Diagnostic {
pub fn replace_args(
&mut self,
args: FxHashMap<DiagnosticArgName<'static>, DiagnosticArgValue<'static>>,
args: FxHashMap<DiagnosticArgName<'static>, DiagnosticArgValue>,
) {
self.args = args;
}
@ -990,7 +984,7 @@ impl Diagnostic {
) -> (
&Level,
&[(DiagnosticMessage, Style)],
Vec<(&Cow<'static, str>, &DiagnosticArgValue<'static>)>,
Vec<(&Cow<'static, str>, &DiagnosticArgValue)>,
&Option<ErrCode>,
&Option<IsLint>,
&MultiSpan,

View file

@ -23,7 +23,7 @@ use std::process::ExitStatus;
pub struct DiagnosticArgFromDisplay<'a>(pub &'a dyn fmt::Display);
impl IntoDiagnosticArg for DiagnosticArgFromDisplay<'_> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
self.0.to_string().into_diagnostic_arg()
}
}
@ -41,7 +41,7 @@ impl<'a, T: fmt::Display> From<&'a T> for DiagnosticArgFromDisplay<'a> {
}
impl<'a, T: Clone + IntoDiagnosticArg> IntoDiagnosticArg for &'a T {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
self.clone().into_diagnostic_arg()
}
}
@ -50,7 +50,7 @@ macro_rules! into_diagnostic_arg_using_display {
($( $ty:ty ),+ $(,)?) => {
$(
impl IntoDiagnosticArg for $ty {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
self.to_string().into_diagnostic_arg()
}
}
@ -62,7 +62,7 @@ macro_rules! into_diagnostic_arg_for_number {
($( $ty:ty ),+ $(,)?) => {
$(
impl IntoDiagnosticArg for $ty {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
// HACK: `FluentNumber` the underline backing struct represent
// numbers using a f64 which can't represent all the i128 numbers
// So in order to be able to use fluent selectors and still
@ -99,7 +99,7 @@ into_diagnostic_arg_using_display!(
into_diagnostic_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
impl IntoDiagnosticArg for bool {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
if self {
DiagnosticArgValue::Str(Cow::Borrowed("true"))
} else {
@ -109,13 +109,13 @@ impl IntoDiagnosticArg for bool {
}
impl IntoDiagnosticArg for char {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(format!("{self:?}")))
}
}
impl IntoDiagnosticArg for Vec<char> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::StrListSepByAnd(
self.into_iter().map(|c| Cow::Owned(format!("{c:?}"))).collect(),
)
@ -123,49 +123,49 @@ impl IntoDiagnosticArg for Vec<char> {
}
impl IntoDiagnosticArg for Symbol {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
self.to_ident_string().into_diagnostic_arg()
}
}
impl<'a> IntoDiagnosticArg for &'a str {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
self.to_string().into_diagnostic_arg()
}
}
impl IntoDiagnosticArg for String {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self))
}
}
impl<'a> IntoDiagnosticArg for Cow<'a, str> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.into_owned()))
}
}
impl<'a> IntoDiagnosticArg for &'a Path {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
}
}
impl IntoDiagnosticArg for PathBuf {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
}
}
impl IntoDiagnosticArg for PanicStrategy {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string()))
}
}
impl IntoDiagnosticArg for hir::ConstContext {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Borrowed(match self {
hir::ConstContext::ConstFn => "const_fn",
hir::ConstContext::Static(_) => "static",
@ -175,49 +175,49 @@ impl IntoDiagnosticArg for hir::ConstContext {
}
impl IntoDiagnosticArg for ast::Expr {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(pprust::expr_to_string(&self)))
}
}
impl IntoDiagnosticArg for ast::Path {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
}
}
impl IntoDiagnosticArg for ast::token::Token {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(pprust::token_to_string(&self))
}
}
impl IntoDiagnosticArg for ast::token::TokenKind {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(pprust::token_kind_to_string(&self))
}
}
impl IntoDiagnosticArg for type_ir::FloatTy {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Borrowed(self.name_str()))
}
}
impl IntoDiagnosticArg for std::ffi::CString {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
}
}
impl IntoDiagnosticArg for rustc_data_structures::small_c_str::SmallCStr {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
}
}
impl IntoDiagnosticArg for ast::Visibility {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
let s = pprust::vis_to_string(&self);
let s = s.trim_end().to_string();
DiagnosticArgValue::Str(Cow::Owned(s))
@ -225,7 +225,7 @@ impl IntoDiagnosticArg for ast::Visibility {
}
impl IntoDiagnosticArg for rustc_lint_defs::Level {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Borrowed(self.to_cmd_flag()))
}
}
@ -240,7 +240,7 @@ impl From<Vec<Symbol>> for DiagnosticSymbolList {
}
impl IntoDiagnosticArg for DiagnosticSymbolList {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::StrListSepByAnd(
self.0.into_iter().map(|sym| Cow::Owned(format!("`{sym}`"))).collect(),
)
@ -248,7 +248,7 @@ impl IntoDiagnosticArg for DiagnosticSymbolList {
}
impl<Id> IntoDiagnosticArg for hir::def::Res<Id> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Borrowed(self.descr()))
}
}
@ -334,13 +334,13 @@ pub struct DelayedAtWithoutNewline {
}
impl IntoDiagnosticArg for DiagnosticLocation {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::from(self.to_string()))
}
}
impl IntoDiagnosticArg for Backtrace {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::from(self.to_string()))
}
}
@ -353,7 +353,7 @@ pub struct InvalidFlushedDelayedDiagnosticLevel {
pub level: Level,
}
impl IntoDiagnosticArg for Level {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::from(self.to_string()))
}
}
@ -368,7 +368,7 @@ pub struct IndicateAnonymousLifetime {
}
impl IntoDiagnosticArg for type_ir::ClosureKind {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(self.as_str().into())
}
}

View file

@ -12,9 +12,9 @@ use std::error::Report;
///
/// Typically performed once for each diagnostic at the start of `emit_diagnostic` and then
/// passed around as a reference thereafter.
pub fn to_fluent_args<'iter, 'arg: 'iter>(
iter: impl Iterator<Item = DiagnosticArg<'iter, 'arg>>,
) -> FluentArgs<'arg> {
pub fn to_fluent_args<'iter>(
iter: impl Iterator<Item = DiagnosticArg<'iter, 'static>>,
) -> FluentArgs<'static> {
let mut args = if let Some(size) = iter.size_hint().1 {
FluentArgs::with_capacity(size)
} else {