macros: add args for non-subdiagnostic fields
Non-subdiagnostic fields (i.e. those that don't have `#[label]` attributes or similar and are just additional context) have to be added as arguments for Fluent messages to refer them. This commit extends the `SessionDiagnostic` derive to do this for all fields that do not have attributes and introduces an `IntoDiagnosticArg` trait that is implemented on all types that can be converted to a argument for Fluent. Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
parent
8677fef192
commit
9956d4f99d
6 changed files with 122 additions and 30 deletions
|
@ -8,6 +8,7 @@ use rustc_error_messages::FluentValue;
|
|||
use rustc_lint_defs::{Applicability, LintExpectationId};
|
||||
use rustc_serialize::json::Json;
|
||||
use rustc_span::edition::LATEST_STABLE_EDITION;
|
||||
use rustc_span::symbol::{Ident, Symbol};
|
||||
use rustc_span::{Span, DUMMY_SP};
|
||||
use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
|
@ -31,6 +32,44 @@ pub enum DiagnosticArgValue<'source> {
|
|||
Number(usize),
|
||||
}
|
||||
|
||||
/// Converts a value of a type into a `DiagnosticArg` (typically a field of a `SessionDiagnostic`
|
||||
/// 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.
|
||||
pub trait IntoDiagnosticArg {
|
||||
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
|
||||
}
|
||||
|
||||
impl IntoDiagnosticArg for String {
|
||||
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
||||
DiagnosticArgValue::Str(Cow::Owned(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoDiagnosticArg for Symbol {
|
||||
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
||||
self.to_ident_string().into_diagnostic_arg()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoDiagnosticArg for Ident {
|
||||
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
||||
self.to_string().into_diagnostic_arg()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoDiagnosticArg for &'a str {
|
||||
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
||||
self.to_string().into_diagnostic_arg()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoDiagnosticArg for usize {
|
||||
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
||||
DiagnosticArgValue::Number(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
|
||||
fn into(self) -> FluentValue<'source> {
|
||||
match self {
|
||||
|
@ -788,6 +827,15 @@ impl Diagnostic {
|
|||
&self.args
|
||||
}
|
||||
|
||||
pub fn set_arg(
|
||||
&mut self,
|
||||
name: impl Into<Cow<'static, str>>,
|
||||
arg: DiagnosticArgValue<'static>,
|
||||
) -> &mut Self {
|
||||
self.args.push((name.into(), arg.into()));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn styled_message(&self) -> &Vec<(DiagnosticMessage, Style)> {
|
||||
&self.message
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue