Skip to content

Commit f7b4210

Browse files
committed
errors: implement IntoDiagnosticArg for &T
Implement `IntoDiagnosticArg` for `&'a T` when `T` implements `IntoDiagnosticArg` and `Clone`. Makes it easier to write diagnostic structs that borrow something which implements `IntoDiagnosticArg`. Signed-off-by: David Wood <[email protected]>
1 parent 59cc5e5 commit f7b4210

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+6
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ impl Display for RegionName {
187187
}
188188
}
189189

190+
impl rustc_errors::IntoDiagnosticArg for RegionName {
191+
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
192+
self.to_string().into_diagnostic_arg()
193+
}
194+
}
195+
190196
impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
191197
pub(crate) fn mir_def_id(&self) -> hir::def_id::LocalDefId {
192198
self.body.source.def_id().expect_local()

compiler/rustc_borrowck/src/session_diagnostics.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_errors::{IntoDiagnosticArg, MultiSpan};
1+
use rustc_errors::MultiSpan;
22
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
33
use rustc_middle::ty::{GenericArg, Ty};
44
use rustc_span::Span;
@@ -128,18 +128,6 @@ pub(crate) enum LifetimeReturnCategoryErr<'a> {
128128
},
129129
}
130130

131-
impl IntoDiagnosticArg for &RegionName {
132-
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
133-
format!("{}", self).into_diagnostic_arg()
134-
}
135-
}
136-
137-
impl IntoDiagnosticArg for RegionName {
138-
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
139-
format!("{}", self).into_diagnostic_arg()
140-
}
141-
}
142-
143131
#[derive(Subdiagnostic)]
144132
pub(crate) enum RequireStaticErr {
145133
#[note(borrowck_used_impl_require_static)]

compiler/rustc_errors/src/diagnostic_impls.rs

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ impl<'a, T: fmt::Display> From<&'a T> for DiagnosticArgFromDisplay<'a> {
3636
}
3737
}
3838

39+
impl<'a, T: Clone + IntoDiagnosticArg> IntoDiagnosticArg for &'a T {
40+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
41+
self.clone().into_diagnostic_arg()
42+
}
43+
}
44+
3945
macro_rules! into_diagnostic_arg_using_display {
4046
($( $ty:ty ),+ $(,)?) => {
4147
$(

0 commit comments

Comments
 (0)