Rename DiagnosticArg{,Map,Name,Value} as DiagArg{,Map,Name,Value}.

This commit is contained in:
Nicholas Nethercote 2024-02-23 14:37:48 +11:00
parent 899cb40809
commit 8199632aa8
40 changed files with 196 additions and 198 deletions

View file

@ -24,17 +24,17 @@ use std::thread::panicking;
pub struct SuggestionsDisabled;
/// Simplified version of `FluentArg` that can implement `Encodable` and `Decodable`. Collection of
/// `DiagnosticArg` are converted to `FluentArgs` (consuming the collection) at the start of
/// diagnostic emission.
pub type DiagnosticArg<'iter> = (&'iter DiagnosticArgName, &'iter DiagnosticArgValue);
/// `DiagArg` are converted to `FluentArgs` (consuming the collection) at the start of diagnostic
/// emission.
pub type DiagArg<'iter> = (&'iter DiagArgName, &'iter DiagArgValue);
/// Name of a diagnostic argument.
pub type DiagnosticArgName = Cow<'static, str>;
pub type DiagArgName = Cow<'static, 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 {
pub enum DiagArgValue {
Str(Cow<'static, str>),
// This gets converted to a `FluentNumber`, which is an `f64`. An `i32`
// safely fits in an `f64`. Any integers bigger than that will be converted
@ -43,7 +43,7 @@ pub enum DiagnosticArgValue {
StrListSepByAnd(Vec<Cow<'static, str>>),
}
pub type DiagnosticArgMap = FxIndexMap<DiagnosticArgName, DiagnosticArgValue>;
pub type DiagArgMap = FxIndexMap<DiagArgName, DiagArgValue>;
/// Trait for types that `Diag::emit` can return as a "guarantee" (or "proof")
/// token that the emission happened.
@ -125,26 +125,26 @@ where
}
}
/// Converts a value of a type into a `DiagnosticArg` (typically a field of an `IntoDiagnostic`
/// struct). Implemented as a custom trait rather than `From` so that it is implemented on the type
/// being converted rather than on `DiagnosticArgValue`, which enables types from other `rustc_*`
/// crates to implement this.
/// Converts a value of a type into a `DiagArg` (typically a field of an `IntoDiagnostic` struct).
/// Implemented as a custom trait rather than `From` so that it is implemented on the type being
/// converted rather than on `DiagArgValue`, which enables types from other `rustc_*` crates to
/// implement this.
pub trait IntoDiagnosticArg {
fn into_diagnostic_arg(self) -> DiagnosticArgValue;
fn into_diagnostic_arg(self) -> DiagArgValue;
}
impl IntoDiagnosticArg for DiagnosticArgValue {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
impl IntoDiagnosticArg for DiagArgValue {
fn into_diagnostic_arg(self) -> DiagArgValue {
self
}
}
impl Into<FluentValue<'static>> for DiagnosticArgValue {
impl Into<FluentValue<'static>> for DiagArgValue {
fn into(self) -> FluentValue<'static> {
match self {
DiagnosticArgValue::Str(s) => From::from(s),
DiagnosticArgValue::Number(n) => From::from(n),
DiagnosticArgValue::StrListSepByAnd(l) => fluent_value_from_str_list_sep_by_and(l),
DiagArgValue::Str(s) => From::from(s),
DiagArgValue::Number(n) => From::from(n),
DiagArgValue::StrListSepByAnd(l) => fluent_value_from_str_list_sep_by_and(l),
}
}
}
@ -277,7 +277,7 @@ pub struct DiagInner {
pub span: MultiSpan,
pub children: Vec<Subdiag>,
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
pub args: DiagnosticArgMap,
pub args: DiagArgMap,
/// 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
@ -401,7 +401,7 @@ impl DiagInner {
self.children.push(sub);
}
pub(crate) fn arg(&mut self, name: impl Into<DiagnosticArgName>, arg: impl IntoDiagnosticArg) {
pub(crate) fn arg(&mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagnosticArg) {
self.args.insert(name.into(), arg.into_diagnostic_arg());
}
@ -415,7 +415,7 @@ impl DiagInner {
&MultiSpan,
&[Subdiag],
&Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
Vec<(&DiagnosticArgName, &DiagnosticArgValue)>,
Vec<(&DiagArgName, &DiagArgValue)>,
&Option<IsLint>,
) {
(
@ -1193,7 +1193,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
/// Add an argument.
pub fn arg(
&mut self,
name: impl Into<DiagnosticArgName>,
name: impl Into<DiagArgName>,
arg: impl IntoDiagnosticArg,
) -> &mut Self {
self.deref_mut().arg(name, arg);

View file

@ -1,8 +1,8 @@
use crate::diagnostic::DiagnosticLocation;
use crate::{fluent_generated as fluent, AddToDiagnostic};
use crate::{
Diag, DiagCtxt, DiagnosticArgValue, EmissionGuarantee, ErrCode, IntoDiagnostic,
IntoDiagnosticArg, Level, SubdiagnosticMessageOp,
Diag, DiagArgValue, DiagCtxt, EmissionGuarantee, ErrCode, IntoDiagnostic, IntoDiagnosticArg,
Level, SubdiagnosticMessageOp,
};
use rustc_ast as ast;
use rustc_ast_pretty::pprust;
@ -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 {
fn into_diagnostic_arg(self) -> DiagArgValue {
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 {
fn into_diagnostic_arg(self) -> DiagArgValue {
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 {
fn into_diagnostic_arg(self) -> DiagArgValue {
self.to_string().into_diagnostic_arg()
}
}
@ -62,10 +62,10 @@ macro_rules! into_diagnostic_arg_for_number {
($( $ty:ty ),+ $(,)?) => {
$(
impl IntoDiagnosticArg for $ty {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
fn into_diagnostic_arg(self) -> DiagArgValue {
// Convert to a string if it won't fit into `Number`.
if let Ok(n) = TryInto::<i32>::try_into(self) {
DiagnosticArgValue::Number(n)
DiagArgValue::Number(n)
} else {
self.to_string().into_diagnostic_arg()
}
@ -95,74 +95,74 @@ 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 {
fn into_diagnostic_arg(self) -> DiagArgValue {
if self {
DiagnosticArgValue::Str(Cow::Borrowed("true"))
DiagArgValue::Str(Cow::Borrowed("true"))
} else {
DiagnosticArgValue::Str(Cow::Borrowed("false"))
DiagArgValue::Str(Cow::Borrowed("false"))
}
}
}
impl IntoDiagnosticArg for char {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(format!("{self:?}")))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(format!("{self:?}")))
}
}
impl IntoDiagnosticArg for Vec<char> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::StrListSepByAnd(
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::StrListSepByAnd(
self.into_iter().map(|c| Cow::Owned(format!("{c:?}"))).collect(),
)
}
}
impl IntoDiagnosticArg for Symbol {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
fn into_diagnostic_arg(self) -> DiagArgValue {
self.to_ident_string().into_diagnostic_arg()
}
}
impl<'a> IntoDiagnosticArg for &'a str {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
fn into_diagnostic_arg(self) -> DiagArgValue {
self.to_string().into_diagnostic_arg()
}
}
impl IntoDiagnosticArg for String {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(self))
}
}
impl<'a> IntoDiagnosticArg for Cow<'a, str> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.into_owned()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(self.into_owned()))
}
}
impl<'a> IntoDiagnosticArg for &'a Path {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(self.display().to_string()))
}
}
impl IntoDiagnosticArg for PathBuf {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(self.display().to_string()))
}
}
impl IntoDiagnosticArg for PanicStrategy {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(self.desc().to_string()))
}
}
impl IntoDiagnosticArg for hir::ConstContext {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Borrowed(match self {
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Borrowed(match self {
hir::ConstContext::ConstFn => "const_fn",
hir::ConstContext::Static(_) => "static",
hir::ConstContext::Const { .. } => "const",
@ -171,58 +171,58 @@ impl IntoDiagnosticArg for hir::ConstContext {
}
impl IntoDiagnosticArg for ast::Expr {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(pprust::expr_to_string(&self)))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(pprust::expr_to_string(&self)))
}
}
impl IntoDiagnosticArg for ast::Path {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
}
}
impl IntoDiagnosticArg for ast::token::Token {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(pprust::token_to_string(&self))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(pprust::token_to_string(&self))
}
}
impl IntoDiagnosticArg for ast::token::TokenKind {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(pprust::token_kind_to_string(&self))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(pprust::token_kind_to_string(&self))
}
}
impl IntoDiagnosticArg for type_ir::FloatTy {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Borrowed(self.name_str()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Borrowed(self.name_str()))
}
}
impl IntoDiagnosticArg for std::ffi::CString {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::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 {
DiagnosticArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
}
}
impl IntoDiagnosticArg for ast::Visibility {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
fn into_diagnostic_arg(self) -> DiagArgValue {
let s = pprust::vis_to_string(&self);
let s = s.trim_end().to_string();
DiagnosticArgValue::Str(Cow::Owned(s))
DiagArgValue::Str(Cow::Owned(s))
}
}
impl IntoDiagnosticArg for rustc_lint_defs::Level {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Borrowed(self.to_cmd_flag()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Borrowed(self.to_cmd_flag()))
}
}
@ -236,16 +236,16 @@ impl From<Vec<Symbol>> for DiagnosticSymbolList {
}
impl IntoDiagnosticArg for DiagnosticSymbolList {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::StrListSepByAnd(
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::StrListSepByAnd(
self.0.into_iter().map(|sym| Cow::Owned(format!("`{sym}`"))).collect(),
)
}
}
impl<Id> IntoDiagnosticArg for hir::def::Res<Id> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::Borrowed(self.descr()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Borrowed(self.descr()))
}
}
@ -316,20 +316,20 @@ pub struct ExpectedLifetimeParameter {
}
impl IntoDiagnosticArg for DiagnosticLocation {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::from(self.to_string()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::from(self.to_string()))
}
}
impl IntoDiagnosticArg for Backtrace {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::from(self.to_string()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::from(self.to_string()))
}
}
impl IntoDiagnosticArg for Level {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(Cow::from(self.to_string()))
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::from(self.to_string()))
}
}
@ -343,7 +343,7 @@ pub struct IndicateAnonymousLifetime {
}
impl IntoDiagnosticArg for type_ir::ClosureKind {
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
DiagnosticArgValue::Str(self.as_str().into())
fn into_diagnostic_arg(self) -> DiagArgValue {
DiagArgValue::Str(self.as_str().into())
}
}

View file

@ -37,9 +37,9 @@ extern crate self as rustc_errors;
pub use codes::*;
pub use diagnostic::{
AddToDiagnostic, BugAbort, DecorateLint, Diag, DiagInner, DiagnosticArg, DiagnosticArgMap,
DiagnosticArgName, DiagnosticArgValue, DiagnosticStyledString, EmissionGuarantee, FatalAbort,
IntoDiagnostic, IntoDiagnosticArg, StringPart, Subdiag, SubdiagnosticMessageOp,
AddToDiagnostic, BugAbort, DecorateLint, Diag, DiagArg, DiagArgMap, DiagArgName, DiagArgValue,
DiagInner, DiagnosticStyledString, EmissionGuarantee, FatalAbort, IntoDiagnostic,
IntoDiagnosticArg, StringPart, Subdiag, SubdiagnosticMessageOp,
};
pub use diagnostic_impls::{
DiagnosticArgFromDisplay, DiagnosticSymbolList, ExpectedLifetimeParameter,
@ -639,7 +639,7 @@ impl DiagCtxt {
pub fn eagerly_translate<'a>(
&self,
message: DiagnosticMessage,
args: impl Iterator<Item = DiagnosticArg<'a>>,
args: impl Iterator<Item = DiagArg<'a>>,
) -> SubdiagnosticMessage {
let inner = self.inner.borrow();
inner.eagerly_translate(message, args)
@ -649,7 +649,7 @@ impl DiagCtxt {
pub fn eagerly_translate_to_string<'a>(
&self,
message: DiagnosticMessage,
args: impl Iterator<Item = DiagnosticArg<'a>>,
args: impl Iterator<Item = DiagArg<'a>>,
) -> String {
let inner = self.inner.borrow();
inner.eagerly_translate_to_string(message, args)
@ -1461,7 +1461,7 @@ impl DiagCtxtInner {
pub fn eagerly_translate<'a>(
&self,
message: DiagnosticMessage,
args: impl Iterator<Item = DiagnosticArg<'a>>,
args: impl Iterator<Item = DiagArg<'a>>,
) -> SubdiagnosticMessage {
SubdiagnosticMessage::Translated(Cow::from(self.eagerly_translate_to_string(message, args)))
}
@ -1470,7 +1470,7 @@ impl DiagCtxtInner {
pub fn eagerly_translate_to_string<'a>(
&self,
message: DiagnosticMessage,
args: impl Iterator<Item = DiagnosticArg<'a>>,
args: impl Iterator<Item = DiagArg<'a>>,
) -> String {
let args = crate::translation::to_fluent_args(args);
self.emitter.translate_message(&message, &args).map_err(Report::new).unwrap().to_string()

View file

@ -1,6 +1,6 @@
use crate::error::{TranslateError, TranslateErrorKind};
use crate::snippet::Style;
use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle};
use crate::{DiagArg, DiagnosticMessage, FluentBundle};
use rustc_data_structures::sync::Lrc;
pub use rustc_error_messages::FluentArgs;
use std::borrow::Cow;
@ -12,9 +12,7 @@ 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>(
iter: impl Iterator<Item = DiagnosticArg<'iter>>,
) -> FluentArgs<'static> {
pub fn to_fluent_args<'iter>(iter: impl Iterator<Item = DiagArg<'iter>>) -> FluentArgs<'static> {
let mut args = if let Some(size) = iter.size_hint().1 {
FluentArgs::with_capacity(size)
} else {