Skip to content

Commit 06f4806

Browse files
committed
errors: impl IntoDiagnosticArg for char
Implements `IntoDiagnosticArg` for `char` using its `Debug` implementation and introduces a macro for those types which just delegate the implementation to `ToString`. Signed-off-by: David Wood <david.wood@huawei.com>
1 parent 116819f commit 06f4806

File tree

1 file changed

+33
-77
lines changed

1 file changed

+33
-77
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 33 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ pub trait IntoDiagnosticArg {
4040
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
4141
}
4242

43+
macro_rules! into_diagnostic_arg_using_display {
44+
($( $ty:ty ),+ $(,)?) => {
45+
$(
46+
impl IntoDiagnosticArg for $ty {
47+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
48+
self.to_string().into_diagnostic_arg()
49+
}
50+
}
51+
)+
52+
}
53+
}
54+
55+
into_diagnostic_arg_using_display!(
56+
i8,
57+
u8,
58+
i16,
59+
u16,
60+
i32,
61+
u32,
62+
i64,
63+
u64,
64+
i128,
65+
u128,
66+
std::num::NonZeroU32,
67+
Edition,
68+
Ident,
69+
);
70+
4371
impl IntoDiagnosticArg for bool {
4472
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
4573
if self {
@@ -50,81 +78,9 @@ impl IntoDiagnosticArg for bool {
5078
}
5179
}
5280

53-
impl IntoDiagnosticArg for i8 {
54-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
55-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
56-
}
57-
}
58-
59-
impl IntoDiagnosticArg for u8 {
81+
impl IntoDiagnosticArg for char {
6082
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
61-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
62-
}
63-
}
64-
65-
impl IntoDiagnosticArg for i16 {
66-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
67-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
68-
}
69-
}
70-
71-
impl IntoDiagnosticArg for u16 {
72-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
73-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
74-
}
75-
}
76-
77-
impl IntoDiagnosticArg for i32 {
78-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
79-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
80-
}
81-
}
82-
83-
impl IntoDiagnosticArg for u32 {
84-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
85-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
86-
}
87-
}
88-
89-
impl IntoDiagnosticArg for i64 {
90-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
91-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
92-
}
93-
}
94-
95-
impl IntoDiagnosticArg for u64 {
96-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
97-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
98-
}
99-
}
100-
101-
impl IntoDiagnosticArg for i128 {
102-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
103-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
104-
}
105-
}
106-
107-
impl IntoDiagnosticArg for u128 {
108-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
109-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
110-
}
111-
}
112-
113-
impl IntoDiagnosticArg for String {
114-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
115-
DiagnosticArgValue::Str(Cow::Owned(self))
116-
}
117-
}
118-
119-
impl IntoDiagnosticArg for std::num::NonZeroU32 {
120-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
121-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
122-
}
123-
}
124-
125-
impl IntoDiagnosticArg for Edition {
126-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
127-
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
83+
DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self)))
12884
}
12985
}
13086

@@ -134,15 +90,15 @@ impl IntoDiagnosticArg for Symbol {
13490
}
13591
}
13692

137-
impl IntoDiagnosticArg for Ident {
93+
impl<'a> IntoDiagnosticArg for &'a str {
13894
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
13995
self.to_string().into_diagnostic_arg()
14096
}
14197
}
14298

143-
impl<'a> IntoDiagnosticArg for &'a str {
99+
impl IntoDiagnosticArg for String {
144100
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
145-
self.to_string().into_diagnostic_arg()
101+
DiagnosticArgValue::Str(Cow::Owned(self))
146102
}
147103
}
148104

0 commit comments

Comments
 (0)