Skip to content

Commit be22157

Browse files
committed
UPDATE - Move IntoDiagnosticArg implementations to diagnostic_impls file
1 parent 5645cd5 commit be22157

File tree

3 files changed

+160
-152
lines changed

3 files changed

+160
-152
lines changed

Diff for: compiler/rustc_errors/src/diagnostic.rs

+2-149
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@ use crate::{
33
CodeSuggestion, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Level, MultiSpan,
44
SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle,
55
};
6-
use rustc_ast as ast;
7-
use rustc_ast_pretty::pprust;
86
use rustc_data_structures::fx::FxHashMap;
97
use rustc_error_messages::FluentValue;
10-
use rustc_hir as hir;
118
use rustc_lint_defs::{Applicability, LintExpectationId};
129
use rustc_span::edition::LATEST_STABLE_EDITION;
13-
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol};
14-
use rustc_span::{edition::Edition, Span, DUMMY_SP};
15-
use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple};
10+
use rustc_span::symbol::Symbol;
11+
use rustc_span::{Span, DUMMY_SP};
1612
use std::borrow::Cow;
1713
use std::fmt;
1814
use std::hash::{Hash, Hasher};
19-
use std::num::ParseIntError;
20-
use std::path::{Path, PathBuf};
2115

2216
/// Error type for `Diagnostic`'s `suggestions` field, indicating that
2317
/// `.disable_suggestions()` was called on the `Diagnostic`.
@@ -49,119 +43,6 @@ pub trait IntoDiagnosticArg {
4943
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
5044
}
5145

52-
pub struct DiagnosticArgFromDisplay<'a>(pub &'a dyn fmt::Display);
53-
54-
impl IntoDiagnosticArg for DiagnosticArgFromDisplay<'_> {
55-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
56-
self.0.to_string().into_diagnostic_arg()
57-
}
58-
}
59-
60-
impl<'a> From<&'a dyn fmt::Display> for DiagnosticArgFromDisplay<'a> {
61-
fn from(t: &'a dyn fmt::Display) -> Self {
62-
DiagnosticArgFromDisplay(t)
63-
}
64-
}
65-
66-
impl<'a, T: fmt::Display> From<&'a T> for DiagnosticArgFromDisplay<'a> {
67-
fn from(t: &'a T) -> Self {
68-
DiagnosticArgFromDisplay(t)
69-
}
70-
}
71-
72-
macro_rules! into_diagnostic_arg_using_display {
73-
($( $ty:ty ),+ $(,)?) => {
74-
$(
75-
impl IntoDiagnosticArg for $ty {
76-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
77-
self.to_string().into_diagnostic_arg()
78-
}
79-
}
80-
)+
81-
}
82-
}
83-
84-
into_diagnostic_arg_using_display!(
85-
i8,
86-
u8,
87-
i16,
88-
u16,
89-
i32,
90-
u32,
91-
i64,
92-
u64,
93-
i128,
94-
u128,
95-
std::io::Error,
96-
std::num::NonZeroU32,
97-
hir::Target,
98-
Edition,
99-
Ident,
100-
MacroRulesNormalizedIdent,
101-
ParseIntError,
102-
StackProtector,
103-
&TargetTriple,
104-
SplitDebuginfo
105-
);
106-
107-
impl IntoDiagnosticArg for bool {
108-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
109-
if self {
110-
DiagnosticArgValue::Str(Cow::Borrowed("true"))
111-
} else {
112-
DiagnosticArgValue::Str(Cow::Borrowed("false"))
113-
}
114-
}
115-
}
116-
117-
impl IntoDiagnosticArg for char {
118-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
119-
DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self)))
120-
}
121-
}
122-
123-
impl IntoDiagnosticArg for Symbol {
124-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
125-
self.to_ident_string().into_diagnostic_arg()
126-
}
127-
}
128-
129-
impl<'a> IntoDiagnosticArg for &'a str {
130-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
131-
self.to_string().into_diagnostic_arg()
132-
}
133-
}
134-
135-
impl IntoDiagnosticArg for String {
136-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
137-
DiagnosticArgValue::Str(Cow::Owned(self))
138-
}
139-
}
140-
141-
impl<'a> IntoDiagnosticArg for &'a Path {
142-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
143-
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
144-
}
145-
}
146-
147-
impl IntoDiagnosticArg for PathBuf {
148-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
149-
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
150-
}
151-
}
152-
153-
impl IntoDiagnosticArg for usize {
154-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
155-
DiagnosticArgValue::Number(self)
156-
}
157-
}
158-
159-
impl IntoDiagnosticArg for PanicStrategy {
160-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
161-
DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string()))
162-
}
163-
}
164-
16546
impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
16647
fn into(self) -> FluentValue<'source> {
16748
match self {
@@ -171,34 +52,6 @@ impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
17152
}
17253
}
17354

174-
impl IntoDiagnosticArg for hir::ConstContext {
175-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
176-
DiagnosticArgValue::Str(Cow::Borrowed(match self {
177-
hir::ConstContext::ConstFn => "constant function",
178-
hir::ConstContext::Static(_) => "static",
179-
hir::ConstContext::Const => "constant",
180-
}))
181-
}
182-
}
183-
184-
impl IntoDiagnosticArg for ast::Path {
185-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
186-
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
187-
}
188-
}
189-
190-
impl IntoDiagnosticArg for ast::token::Token {
191-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
192-
DiagnosticArgValue::Str(pprust::token_to_string(&self))
193-
}
194-
}
195-
196-
impl IntoDiagnosticArg for ast::token::TokenKind {
197-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
198-
DiagnosticArgValue::Str(pprust::token_kind_to_string(&self))
199-
}
200-
}
201-
20255
/// Trait implemented by error types. This should not be implemented manually. Instead, use
20356
/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
20457
#[cfg_attr(bootstrap, rustc_diagnostic_item = "AddSubdiagnostic")]

Diff for: compiler/rustc_errors/src/diagnostic_impls.rs

+155-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,159 @@
1-
use crate::{fluent, DiagnosticBuilder, Handler, IntoDiagnostic};
1+
use crate::{
2+
fluent, DiagnosticArgValue, DiagnosticBuilder, Handler, IntoDiagnostic, IntoDiagnosticArg,
3+
};
24
use rustc_target::abi::TargetDataLayoutErrors;
5+
use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple};
6+
7+
use rustc_ast as ast;
8+
use rustc_ast_pretty::pprust;
9+
use rustc_hir as hir;
10+
use rustc_span::edition::Edition;
11+
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol};
12+
use std::borrow::Cow;
13+
use std::fmt;
14+
use std::num::ParseIntError;
15+
use std::path::{Path, PathBuf};
16+
17+
pub struct DiagnosticArgFromDisplay<'a>(pub &'a dyn fmt::Display);
18+
19+
impl IntoDiagnosticArg for DiagnosticArgFromDisplay<'_> {
20+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
21+
self.0.to_string().into_diagnostic_arg()
22+
}
23+
}
24+
25+
impl<'a> From<&'a dyn fmt::Display> for DiagnosticArgFromDisplay<'a> {
26+
fn from(t: &'a dyn fmt::Display) -> Self {
27+
DiagnosticArgFromDisplay(t)
28+
}
29+
}
30+
31+
impl<'a, T: fmt::Display> From<&'a T> for DiagnosticArgFromDisplay<'a> {
32+
fn from(t: &'a T) -> Self {
33+
DiagnosticArgFromDisplay(t)
34+
}
35+
}
36+
37+
macro_rules! into_diagnostic_arg_using_display {
38+
($( $ty:ty ),+ $(,)?) => {
39+
$(
40+
impl IntoDiagnosticArg for $ty {
41+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
42+
self.to_string().into_diagnostic_arg()
43+
}
44+
}
45+
)+
46+
}
47+
}
48+
49+
into_diagnostic_arg_using_display!(
50+
i8,
51+
u8,
52+
i16,
53+
u16,
54+
i32,
55+
u32,
56+
i64,
57+
u64,
58+
i128,
59+
u128,
60+
std::io::Error,
61+
std::num::NonZeroU32,
62+
hir::Target,
63+
Edition,
64+
Ident,
65+
MacroRulesNormalizedIdent,
66+
ParseIntError,
67+
StackProtector,
68+
&TargetTriple,
69+
SplitDebuginfo
70+
);
71+
72+
impl IntoDiagnosticArg for bool {
73+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
74+
if self {
75+
DiagnosticArgValue::Str(Cow::Borrowed("true"))
76+
} else {
77+
DiagnosticArgValue::Str(Cow::Borrowed("false"))
78+
}
79+
}
80+
}
81+
82+
impl IntoDiagnosticArg for char {
83+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
84+
DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self)))
85+
}
86+
}
87+
88+
impl IntoDiagnosticArg for Symbol {
89+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
90+
self.to_ident_string().into_diagnostic_arg()
91+
}
92+
}
93+
94+
impl<'a> IntoDiagnosticArg for &'a str {
95+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
96+
self.to_string().into_diagnostic_arg()
97+
}
98+
}
99+
100+
impl IntoDiagnosticArg for String {
101+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
102+
DiagnosticArgValue::Str(Cow::Owned(self))
103+
}
104+
}
105+
106+
impl<'a> IntoDiagnosticArg for &'a Path {
107+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
108+
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
109+
}
110+
}
111+
112+
impl IntoDiagnosticArg for PathBuf {
113+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
114+
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
115+
}
116+
}
117+
118+
impl IntoDiagnosticArg for usize {
119+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
120+
DiagnosticArgValue::Number(self)
121+
}
122+
}
123+
124+
impl IntoDiagnosticArg for PanicStrategy {
125+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
126+
DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string()))
127+
}
128+
}
129+
130+
impl IntoDiagnosticArg for hir::ConstContext {
131+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
132+
DiagnosticArgValue::Str(Cow::Borrowed(match self {
133+
hir::ConstContext::ConstFn => "constant function",
134+
hir::ConstContext::Static(_) => "static",
135+
hir::ConstContext::Const => "constant",
136+
}))
137+
}
138+
}
139+
140+
impl IntoDiagnosticArg for ast::Path {
141+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
142+
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
143+
}
144+
}
145+
146+
impl IntoDiagnosticArg for ast::token::Token {
147+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
148+
DiagnosticArgValue::Str(pprust::token_to_string(&self))
149+
}
150+
}
151+
152+
impl IntoDiagnosticArg for ast::token::TokenKind {
153+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
154+
DiagnosticArgValue::Str(pprust::token_kind_to_string(&self))
155+
}
156+
}
3157

4158
impl IntoDiagnostic<'_, !> for TargetDataLayoutErrors<'_> {
5159
fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> {

Diff for: compiler/rustc_errors/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,11 @@ impl fmt::Display for ExplicitBug {
372372
impl error::Error for ExplicitBug {}
373373

374374
pub use diagnostic::{
375-
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgFromDisplay,
376-
DiagnosticArgValue, DiagnosticId, DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
375+
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
376+
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
377377
};
378378
pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee, Noted};
379+
pub use diagnostic_impls::DiagnosticArgFromDisplay;
379380
use std::backtrace::Backtrace;
380381

381382
/// A handler deals with errors and other compiler output.

0 commit comments

Comments
 (0)