Skip to content

Commit 8a2f9c3

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 c5b9cb4 commit 8a2f9c3

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 13 deletions
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::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
33
use rustc_middle::ty::Ty;
44
use rustc_span::Span;
@@ -137,18 +137,6 @@ pub(crate) enum LifetimeReturnCategoryErr<'a> {
137137
},
138138
}
139139

140-
impl IntoDiagnosticArg for &RegionName {
141-
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
142-
format!("{}", self).into_diagnostic_arg()
143-
}
144-
}
145-
146-
impl IntoDiagnosticArg for RegionName {
147-
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
148-
format!("{}", self).into_diagnostic_arg()
149-
}
150-
}
151-
152140
#[derive(SessionSubdiagnostic)]
153141
pub(crate) enum RequireStaticErr {
154142
#[note(borrowck::used_impl_require_static)]

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ pub trait IntoDiagnosticArg {
4343
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
4444
}
4545

46+
impl<'a, T: Clone + IntoDiagnosticArg> IntoDiagnosticArg for &'a T {
47+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
48+
self.clone().into_diagnostic_arg()
49+
}
50+
}
51+
4652
pub struct DiagnosticArgFromDisplay<'a>(pub &'a dyn fmt::Display);
4753

4854
impl IntoDiagnosticArg for DiagnosticArgFromDisplay<'_> {

0 commit comments

Comments
 (0)