Skip to content

Commit 5350edb

Browse files
committed
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.
1 parent fb4bca0 commit 5350edb

File tree

38 files changed

+113
-116
lines changed

38 files changed

+113
-116
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl Display for RegionName {
188188
}
189189

190190
impl rustc_errors::IntoDiagnosticArg for RegionName {
191-
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
191+
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue {
192192
self.to_string().into_diagnostic_arg()
193193
}
194194
}

compiler/rustc_codegen_gcc/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) enum PossibleFeature<'a> {
3535
struct ExitCode(Option<i32>);
3636

3737
impl IntoDiagnosticArg for ExitCode {
38-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
38+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
3939
let ExitCode(exit_code) = self;
4040
match exit_code {
4141
Some(t) => t.into_diagnostic_arg(),

compiler/rustc_codegen_ssa/src/assert_module_sources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl fmt::Display for CguReuse {
206206
}
207207

208208
impl IntoDiagnosticArg for CguReuse {
209-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
209+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
210210
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
211211
}
212212
}

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ type DiagnosticArgName<'source> = Cow<'source, str>;
999999

10001000
struct Diagnostic {
10011001
msgs: Vec<(DiagnosticMessage, Style)>,
1002-
args: FxHashMap<DiagnosticArgName<'static>, rustc_errors::DiagnosticArgValue<'static>>,
1002+
args: FxHashMap<DiagnosticArgName<'static>, rustc_errors::DiagnosticArgValue>,
10031003
code: Option<ErrCode>,
10041004
lvl: Level,
10051005
}
@@ -1811,7 +1811,7 @@ impl Translate for SharedEmitter {
18111811

18121812
impl Emitter for SharedEmitter {
18131813
fn emit_diagnostic(&mut self, diag: &rustc_errors::Diagnostic) {
1814-
let args: FxHashMap<Cow<'_, str>, rustc_errors::DiagnosticArgValue<'_>> =
1814+
let args: FxHashMap<Cow<'_, str>, rustc_errors::DiagnosticArgValue> =
18151815
diag.args().map(|(name, arg)| (name.clone(), arg.clone())).collect();
18161816
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
18171817
msgs: diag.messages.clone(),

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a> CopyPath<'a> {
147147
struct DebugArgPath<'a>(pub &'a Path);
148148

149149
impl IntoDiagnosticArg for DebugArgPath<'_> {
150-
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
150+
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue {
151151
DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.0)))
152152
}
153153
}
@@ -974,7 +974,7 @@ pub enum ExpectedPointerMutability {
974974
}
975975

976976
impl IntoDiagnosticArg for ExpectedPointerMutability {
977-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
977+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
978978
match self {
979979
ExpectedPointerMutability::Mut => DiagnosticArgValue::Str(Cow::Borrowed("*mut")),
980980
ExpectedPointerMutability::Not => DiagnosticArgValue::Str(Cow::Borrowed("*_")),

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl MachineStopType for ConstEvalErrKind {
3434
}
3535
fn add_args(
3636
self: Box<Self>,
37-
adder: &mut dyn FnMut(std::borrow::Cow<'static, str>, DiagnosticArgValue<'static>),
37+
adder: &mut dyn FnMut(std::borrow::Cow<'static, str>, DiagnosticArgValue),
3838
) {
3939
use ConstEvalErrKind::*;
4040
match *self {

compiler/rustc_const_eval/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ impl ReportErrorExt for ResourceExhaustionInfo {
906906
}
907907

908908
impl rustc_errors::IntoDiagnosticArg for InternKind {
909-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
909+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
910910
DiagnosticArgValue::Str(Cow::Borrowed(match self {
911911
InternKind::Static(Mutability::Not) => "static",
912912
InternKind::Static(Mutability::Mut) => "static_mut",

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,36 @@ pub struct SuggestionsDisabled;
2424
/// `DiagnosticArg` are converted to `FluentArgs` (consuming the collection) at the start of
2525
/// diagnostic emission.
2626
pub type DiagnosticArg<'iter, 'source> =
27-
(&'iter DiagnosticArgName<'source>, &'iter DiagnosticArgValue<'source>);
27+
(&'iter DiagnosticArgName<'source>, &'iter DiagnosticArgValue);
2828

2929
/// Name of a diagnostic argument.
3030
pub type DiagnosticArgName<'source> = Cow<'source, str>;
3131

3232
/// Simplified version of `FluentValue` that can implement `Encodable` and `Decodable`. Converted
3333
/// to a `FluentValue` by the emitter to be used in diagnostic translation.
3434
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
35-
pub enum DiagnosticArgValue<'source> {
36-
Str(Cow<'source, str>),
35+
pub enum DiagnosticArgValue {
36+
Str(Cow<'static, str>),
3737
Number(i128),
38-
StrListSepByAnd(Vec<Cow<'source, str>>),
38+
StrListSepByAnd(Vec<Cow<'static, str>>),
3939
}
4040

4141
/// Converts a value of a type into a `DiagnosticArg` (typically a field of an `IntoDiagnostic`
4242
/// struct). Implemented as a custom trait rather than `From` so that it is implemented on the type
4343
/// being converted rather than on `DiagnosticArgValue`, which enables types from other `rustc_*`
4444
/// crates to implement this.
4545
pub trait IntoDiagnosticArg {
46-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
46+
fn into_diagnostic_arg(self) -> DiagnosticArgValue;
4747
}
4848

49-
impl<'source> IntoDiagnosticArg for DiagnosticArgValue<'source> {
50-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
51-
match self {
52-
DiagnosticArgValue::Str(s) => DiagnosticArgValue::Str(Cow::Owned(s.into_owned())),
53-
DiagnosticArgValue::Number(n) => DiagnosticArgValue::Number(n),
54-
DiagnosticArgValue::StrListSepByAnd(l) => DiagnosticArgValue::StrListSepByAnd(
55-
l.into_iter().map(|s| Cow::Owned(s.into_owned())).collect(),
56-
),
57-
}
49+
impl IntoDiagnosticArg for DiagnosticArgValue {
50+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
51+
self
5852
}
5953
}
6054

61-
impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
62-
fn into(self) -> FluentValue<'source> {
55+
impl Into<FluentValue<'static>> for DiagnosticArgValue {
56+
fn into(self) -> FluentValue<'static> {
6357
match self {
6458
DiagnosticArgValue::Str(s) => From::from(s),
6559
DiagnosticArgValue::Number(n) => From::from(n),
@@ -109,7 +103,7 @@ pub struct Diagnostic {
109103
pub span: MultiSpan,
110104
pub children: Vec<SubDiagnostic>,
111105
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
112-
args: FxHashMap<DiagnosticArgName<'static>, DiagnosticArgValue<'static>>,
106+
args: FxHashMap<DiagnosticArgName<'static>, DiagnosticArgValue>,
113107

114108
/// This is not used for highlighting or rendering any error message. Rather, it can be used
115109
/// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
@@ -931,7 +925,7 @@ impl Diagnostic {
931925

932926
pub fn replace_args(
933927
&mut self,
934-
args: FxHashMap<DiagnosticArgName<'static>, DiagnosticArgValue<'static>>,
928+
args: FxHashMap<DiagnosticArgName<'static>, DiagnosticArgValue>,
935929
) {
936930
self.args = args;
937931
}
@@ -990,7 +984,7 @@ impl Diagnostic {
990984
) -> (
991985
&Level,
992986
&[(DiagnosticMessage, Style)],
993-
Vec<(&Cow<'static, str>, &DiagnosticArgValue<'static>)>,
987+
Vec<(&Cow<'static, str>, &DiagnosticArgValue)>,
994988
&Option<ErrCode>,
995989
&Option<IsLint>,
996990
&MultiSpan,

compiler/rustc_errors/src/diagnostic_impls.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::process::ExitStatus;
2323
pub struct DiagnosticArgFromDisplay<'a>(pub &'a dyn fmt::Display);
2424

2525
impl IntoDiagnosticArg for DiagnosticArgFromDisplay<'_> {
26-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
26+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
2727
self.0.to_string().into_diagnostic_arg()
2828
}
2929
}
@@ -41,7 +41,7 @@ impl<'a, T: fmt::Display> From<&'a T> for DiagnosticArgFromDisplay<'a> {
4141
}
4242

4343
impl<'a, T: Clone + IntoDiagnosticArg> IntoDiagnosticArg for &'a T {
44-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
44+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
4545
self.clone().into_diagnostic_arg()
4646
}
4747
}
@@ -50,7 +50,7 @@ macro_rules! into_diagnostic_arg_using_display {
5050
($( $ty:ty ),+ $(,)?) => {
5151
$(
5252
impl IntoDiagnosticArg for $ty {
53-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
53+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
5454
self.to_string().into_diagnostic_arg()
5555
}
5656
}
@@ -62,7 +62,7 @@ macro_rules! into_diagnostic_arg_for_number {
6262
($( $ty:ty ),+ $(,)?) => {
6363
$(
6464
impl IntoDiagnosticArg for $ty {
65-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
65+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
6666
// HACK: `FluentNumber` the underline backing struct represent
6767
// numbers using a f64 which can't represent all the i128 numbers
6868
// So in order to be able to use fluent selectors and still
@@ -99,7 +99,7 @@ into_diagnostic_arg_using_display!(
9999
into_diagnostic_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
100100

101101
impl IntoDiagnosticArg for bool {
102-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
102+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
103103
if self {
104104
DiagnosticArgValue::Str(Cow::Borrowed("true"))
105105
} else {
@@ -109,63 +109,63 @@ impl IntoDiagnosticArg for bool {
109109
}
110110

111111
impl IntoDiagnosticArg for char {
112-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
112+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
113113
DiagnosticArgValue::Str(Cow::Owned(format!("{self:?}")))
114114
}
115115
}
116116

117117
impl IntoDiagnosticArg for Vec<char> {
118-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
118+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
119119
DiagnosticArgValue::StrListSepByAnd(
120120
self.into_iter().map(|c| Cow::Owned(format!("{c:?}"))).collect(),
121121
)
122122
}
123123
}
124124

125125
impl IntoDiagnosticArg for Symbol {
126-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
126+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
127127
self.to_ident_string().into_diagnostic_arg()
128128
}
129129
}
130130

131131
impl<'a> IntoDiagnosticArg for &'a str {
132-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
132+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
133133
self.to_string().into_diagnostic_arg()
134134
}
135135
}
136136

137137
impl IntoDiagnosticArg for String {
138-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
138+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
139139
DiagnosticArgValue::Str(Cow::Owned(self))
140140
}
141141
}
142142

143143
impl<'a> IntoDiagnosticArg for Cow<'a, str> {
144-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
144+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
145145
DiagnosticArgValue::Str(Cow::Owned(self.into_owned()))
146146
}
147147
}
148148

149149
impl<'a> IntoDiagnosticArg for &'a Path {
150-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
150+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
151151
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
152152
}
153153
}
154154

155155
impl IntoDiagnosticArg for PathBuf {
156-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
156+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
157157
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
158158
}
159159
}
160160

161161
impl IntoDiagnosticArg for PanicStrategy {
162-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
162+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
163163
DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string()))
164164
}
165165
}
166166

167167
impl IntoDiagnosticArg for hir::ConstContext {
168-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
168+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
169169
DiagnosticArgValue::Str(Cow::Borrowed(match self {
170170
hir::ConstContext::ConstFn => "const_fn",
171171
hir::ConstContext::Static(_) => "static",
@@ -175,57 +175,57 @@ impl IntoDiagnosticArg for hir::ConstContext {
175175
}
176176

177177
impl IntoDiagnosticArg for ast::Expr {
178-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
178+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
179179
DiagnosticArgValue::Str(Cow::Owned(pprust::expr_to_string(&self)))
180180
}
181181
}
182182

183183
impl IntoDiagnosticArg for ast::Path {
184-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
184+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
185185
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
186186
}
187187
}
188188

189189
impl IntoDiagnosticArg for ast::token::Token {
190-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
190+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
191191
DiagnosticArgValue::Str(pprust::token_to_string(&self))
192192
}
193193
}
194194

195195
impl IntoDiagnosticArg for ast::token::TokenKind {
196-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
196+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
197197
DiagnosticArgValue::Str(pprust::token_kind_to_string(&self))
198198
}
199199
}
200200

201201
impl IntoDiagnosticArg for type_ir::FloatTy {
202-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
202+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
203203
DiagnosticArgValue::Str(Cow::Borrowed(self.name_str()))
204204
}
205205
}
206206

207207
impl IntoDiagnosticArg for std::ffi::CString {
208-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
208+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
209209
DiagnosticArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
210210
}
211211
}
212212

213213
impl IntoDiagnosticArg for rustc_data_structures::small_c_str::SmallCStr {
214-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
214+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
215215
DiagnosticArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
216216
}
217217
}
218218

219219
impl IntoDiagnosticArg for ast::Visibility {
220-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
220+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
221221
let s = pprust::vis_to_string(&self);
222222
let s = s.trim_end().to_string();
223223
DiagnosticArgValue::Str(Cow::Owned(s))
224224
}
225225
}
226226

227227
impl IntoDiagnosticArg for rustc_lint_defs::Level {
228-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
228+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
229229
DiagnosticArgValue::Str(Cow::Borrowed(self.to_cmd_flag()))
230230
}
231231
}
@@ -240,15 +240,15 @@ impl From<Vec<Symbol>> for DiagnosticSymbolList {
240240
}
241241

242242
impl IntoDiagnosticArg for DiagnosticSymbolList {
243-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
243+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
244244
DiagnosticArgValue::StrListSepByAnd(
245245
self.0.into_iter().map(|sym| Cow::Owned(format!("`{sym}`"))).collect(),
246246
)
247247
}
248248
}
249249

250250
impl<Id> IntoDiagnosticArg for hir::def::Res<Id> {
251-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
251+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
252252
DiagnosticArgValue::Str(Cow::Borrowed(self.descr()))
253253
}
254254
}
@@ -334,13 +334,13 @@ pub struct DelayedAtWithoutNewline {
334334
}
335335

336336
impl IntoDiagnosticArg for DiagnosticLocation {
337-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
337+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
338338
DiagnosticArgValue::Str(Cow::from(self.to_string()))
339339
}
340340
}
341341

342342
impl IntoDiagnosticArg for Backtrace {
343-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
343+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
344344
DiagnosticArgValue::Str(Cow::from(self.to_string()))
345345
}
346346
}
@@ -353,7 +353,7 @@ pub struct InvalidFlushedDelayedDiagnosticLevel {
353353
pub level: Level,
354354
}
355355
impl IntoDiagnosticArg for Level {
356-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
356+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
357357
DiagnosticArgValue::Str(Cow::from(self.to_string()))
358358
}
359359
}
@@ -368,7 +368,7 @@ pub struct IndicateAnonymousLifetime {
368368
}
369369

370370
impl IntoDiagnosticArg for type_ir::ClosureKind {
371-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
371+
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
372372
DiagnosticArgValue::Str(self.as_str().into())
373373
}
374374
}

0 commit comments

Comments
 (0)