Skip to content

Commit 694a010

Browse files
move DiagnosticArgFromDisplay into rustc_errors
1 parent 0ad57d8 commit 694a010

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

compiler/rustc_errors/src/diagnostic.rs

+20
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ pub trait IntoDiagnosticArg {
4040
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
4141
}
4242

43+
pub struct DiagnosticArgFromDisplay<'a>(pub &'a dyn fmt::Display);
44+
45+
impl IntoDiagnosticArg for DiagnosticArgFromDisplay<'_> {
46+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
47+
self.0.to_string().into_diagnostic_arg()
48+
}
49+
}
50+
51+
impl<'a> From<&'a dyn fmt::Display> for DiagnosticArgFromDisplay<'a> {
52+
fn from(t: &'a dyn fmt::Display) -> Self {
53+
DiagnosticArgFromDisplay(t)
54+
}
55+
}
56+
57+
impl<'a, T: fmt::Display> From<&'a T> for DiagnosticArgFromDisplay<'a> {
58+
fn from(t: &'a T) -> Self {
59+
DiagnosticArgFromDisplay(t)
60+
}
61+
}
62+
4363
macro_rules! into_diagnostic_arg_using_display {
4464
($( $ty:ty ),+ $(,)?) => {
4565
$(

compiler/rustc_errors/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ impl fmt::Display for ExplicitBug {
371371
impl error::Error for ExplicitBug {}
372372

373373
pub use diagnostic::{
374-
AddSubdiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
375-
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
374+
AddSubdiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgFromDisplay,
375+
DiagnosticArgValue, DiagnosticId, DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
376376
};
377377
pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee, LintDiagnosticBuilder};
378378
use std::backtrace::Backtrace;

compiler/rustc_privacy/src/errors.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::fmt::Display;
2-
3-
use rustc_errors::IntoDiagnosticArg;
1+
use rustc_errors::DiagnosticArgFromDisplay;
42
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
53
use rustc_span::{Span, Symbol};
64

@@ -38,7 +36,7 @@ pub struct ItemIsPrivate<'a> {
3836
#[label]
3937
pub span: Span,
4038
pub kind: &'a str,
41-
pub descr: FromDisplay<'a>,
39+
pub descr: DiagnosticArgFromDisplay<'a>,
4240
}
4341

4442
#[derive(SessionDiagnostic)]
@@ -58,7 +56,7 @@ pub struct InPublicInterfaceTraits<'a> {
5856
pub span: Span,
5957
pub vis_descr: &'static str,
6058
pub kind: &'a str,
61-
pub descr: FromDisplay<'a>,
59+
pub descr: DiagnosticArgFromDisplay<'a>,
6260
#[label(privacy::visibility_label)]
6361
pub vis_span: Span,
6462
}
@@ -72,7 +70,7 @@ pub struct InPublicInterface<'a> {
7270
pub span: Span,
7371
pub vis_descr: &'static str,
7472
pub kind: &'a str,
75-
pub descr: FromDisplay<'a>,
73+
pub descr: DiagnosticArgFromDisplay<'a>,
7674
#[label(privacy::visibility_label)]
7775
pub vis_span: Span,
7876
}
@@ -81,7 +79,7 @@ pub struct InPublicInterface<'a> {
8179
#[lint(privacy::from_private_dep_in_public_interface)]
8280
pub struct FromPrivateDependencyInPublicInterface<'a> {
8381
pub kind: &'a str,
84-
pub descr: FromDisplay<'a>,
82+
pub descr: DiagnosticArgFromDisplay<'a>,
8583
pub krate: Symbol,
8684
}
8785

@@ -90,13 +88,5 @@ pub struct FromPrivateDependencyInPublicInterface<'a> {
9088
pub struct PrivateInPublicLint<'a> {
9189
pub vis_descr: &'static str,
9290
pub kind: &'a str,
93-
pub descr: FromDisplay<'a>,
94-
}
95-
96-
pub struct FromDisplay<'a>(pub &'a dyn Display);
97-
98-
impl IntoDiagnosticArg for FromDisplay<'_> {
99-
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
100-
self.0.to_string().into_diagnostic_arg()
101-
}
91+
pub descr: DiagnosticArgFromDisplay<'a>,
10292
}

compiler/rustc_privacy/src/lib.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ use std::ops::ControlFlow;
3838
use std::{cmp, fmt, mem};
3939

4040
use errors::{
41-
FieldIsPrivate, FieldIsPrivateLabel, FromDisplay, FromPrivateDependencyInPublicInterface,
42-
InPublicInterface, InPublicInterfaceTraits, ItemIsPrivate, PrivateInPublicLint,
43-
UnnamedItemIsPrivate,
41+
FieldIsPrivate, FieldIsPrivateLabel, FromPrivateDependencyInPublicInterface, InPublicInterface,
42+
InPublicInterfaceTraits, ItemIsPrivate, PrivateInPublicLint, UnnamedItemIsPrivate,
4443
};
4544

4645
////////////////////////////////////////////////////////////////////////////////
@@ -1080,11 +1079,7 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
10801079
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
10811080
let is_error = !self.item_is_accessible(def_id);
10821081
if is_error {
1083-
self.tcx.sess.emit_err(ItemIsPrivate {
1084-
span: self.span,
1085-
kind,
1086-
descr: FromDisplay(descr),
1087-
});
1082+
self.tcx.sess.emit_err(ItemIsPrivate { span: self.span, kind, descr: descr.into() });
10881083
}
10891084
is_error
10901085
}
@@ -1257,7 +1252,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
12571252
let kind = kind.descr(def_id);
12581253
let _ = match name {
12591254
Some(name) => {
1260-
sess.emit_err(ItemIsPrivate { span, kind, descr: FromDisplay(&name) })
1255+
sess.emit_err(ItemIsPrivate { span, kind, descr: (&name).into() })
12611256
}
12621257
None => sess.emit_err(UnnamedItemIsPrivate { span, kind }),
12631258
};
@@ -1726,7 +1721,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17261721
self.tcx.def_span(self.item_def_id.to_def_id()),
17271722
FromPrivateDependencyInPublicInterface {
17281723
kind,
1729-
descr: FromDisplay(descr),
1724+
descr: descr.into(),
17301725
krate: self.tcx.crate_name(def_id.krate),
17311726
},
17321727
);
@@ -1763,15 +1758,15 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17631758
span,
17641759
vis_descr,
17651760
kind,
1766-
descr: FromDisplay(descr),
1761+
descr: descr.into(),
17671762
vis_span,
17681763
});
17691764
} else {
17701765
self.tcx.sess.emit_err(InPublicInterface {
17711766
span,
17721767
vis_descr,
17731768
kind,
1774-
descr: FromDisplay(descr),
1769+
descr: descr.into(),
17751770
vis_span,
17761771
});
17771772
}
@@ -1780,7 +1775,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17801775
lint::builtin::PRIVATE_IN_PUBLIC,
17811776
hir_id,
17821777
span,
1783-
PrivateInPublicLint { vis_descr, kind, descr: FromDisplay(descr) },
1778+
PrivateInPublicLint { vis_descr, kind, descr: descr.into() },
17841779
);
17851780
}
17861781
}

0 commit comments

Comments
 (0)