Skip to content

Commit 76b0484

Browse files
committed
Auto merge of #99893 - compiler-errors:issue-99387, r=davidtwco
Delay formatting trimmed path until lint/error is emitted Fixes #99387 r? `@davidtwco`
2 parents bb71929 + 694a010 commit 76b0484

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
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-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_errors::DiagnosticArgFromDisplay;
12
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
23
use rustc_span::{Span, Symbol};
34

@@ -35,7 +36,7 @@ pub struct ItemIsPrivate<'a> {
3536
#[label]
3637
pub span: Span,
3738
pub kind: &'a str,
38-
pub descr: String,
39+
pub descr: DiagnosticArgFromDisplay<'a>,
3940
}
4041

4142
#[derive(SessionDiagnostic)]
@@ -55,7 +56,7 @@ pub struct InPublicInterfaceTraits<'a> {
5556
pub span: Span,
5657
pub vis_descr: &'static str,
5758
pub kind: &'a str,
58-
pub descr: String,
59+
pub descr: DiagnosticArgFromDisplay<'a>,
5960
#[label(privacy::visibility_label)]
6061
pub vis_span: Span,
6162
}
@@ -69,7 +70,7 @@ pub struct InPublicInterface<'a> {
6970
pub span: Span,
7071
pub vis_descr: &'static str,
7172
pub kind: &'a str,
72-
pub descr: String,
73+
pub descr: DiagnosticArgFromDisplay<'a>,
7374
#[label(privacy::visibility_label)]
7475
pub vis_span: Span,
7576
}
@@ -78,7 +79,7 @@ pub struct InPublicInterface<'a> {
7879
#[lint(privacy::from_private_dep_in_public_interface)]
7980
pub struct FromPrivateDependencyInPublicInterface<'a> {
8081
pub kind: &'a str,
81-
pub descr: String,
82+
pub descr: DiagnosticArgFromDisplay<'a>,
8283
pub krate: Symbol,
8384
}
8485

@@ -87,5 +88,5 @@ pub struct FromPrivateDependencyInPublicInterface<'a> {
8788
pub struct PrivateInPublicLint<'a> {
8889
pub vis_descr: &'static str,
8990
pub kind: &'a str,
90-
pub descr: String,
91+
pub descr: DiagnosticArgFromDisplay<'a>,
9192
}

compiler/rustc_privacy/src/lib.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1079,11 +1079,7 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
10791079
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
10801080
let is_error = !self.item_is_accessible(def_id);
10811081
if is_error {
1082-
self.tcx.sess.emit_err(ItemIsPrivate {
1083-
span: self.span,
1084-
kind,
1085-
descr: descr.to_string(),
1086-
});
1082+
self.tcx.sess.emit_err(ItemIsPrivate { span: self.span, kind, descr: descr.into() });
10871083
}
10881084
is_error
10891085
}
@@ -1255,7 +1251,9 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
12551251
};
12561252
let kind = kind.descr(def_id);
12571253
let _ = match name {
1258-
Some(name) => sess.emit_err(ItemIsPrivate { span, kind, descr: name }),
1254+
Some(name) => {
1255+
sess.emit_err(ItemIsPrivate { span, kind, descr: (&name).into() })
1256+
}
12591257
None => sess.emit_err(UnnamedItemIsPrivate { span, kind }),
12601258
};
12611259
return;
@@ -1723,7 +1721,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17231721
self.tcx.def_span(self.item_def_id.to_def_id()),
17241722
FromPrivateDependencyInPublicInterface {
17251723
kind,
1726-
descr: descr.to_string(),
1724+
descr: descr.into(),
17271725
krate: self.tcx.crate_name(def_id.krate),
17281726
},
17291727
);
@@ -1750,7 +1748,6 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17501748
}
17511749
};
17521750
let span = self.tcx.def_span(self.item_def_id.to_def_id());
1753-
let descr = descr.to_string();
17541751
if self.has_old_errors
17551752
|| self.in_assoc_ty
17561753
|| self.tcx.resolutions(()).has_pub_restricted
@@ -1761,15 +1758,15 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17611758
span,
17621759
vis_descr,
17631760
kind,
1764-
descr,
1761+
descr: descr.into(),
17651762
vis_span,
17661763
});
17671764
} else {
17681765
self.tcx.sess.emit_err(InPublicInterface {
17691766
span,
17701767
vis_descr,
17711768
kind,
1772-
descr,
1769+
descr: descr.into(),
17731770
vis_span,
17741771
});
17751772
}
@@ -1778,7 +1775,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17781775
lint::builtin::PRIVATE_IN_PUBLIC,
17791776
hir_id,
17801777
span,
1781-
PrivateInPublicLint { vis_descr, kind, descr },
1778+
PrivateInPublicLint { vis_descr, kind, descr: descr.into() },
17821779
);
17831780
}
17841781
}

src/test/ui/lint/issue-99387.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// check-pass
2+
3+
#![feature(type_alias_impl_trait)]
4+
#![allow(private_in_public)]
5+
6+
pub type Successors<'a> = impl Iterator<Item = &'a ()>;
7+
8+
pub fn f<'a>() -> Successors<'a> {
9+
None.into_iter()
10+
}
11+
12+
trait Tr {
13+
type Item;
14+
}
15+
16+
impl<'a> Tr for &'a () {
17+
type Item = Successors<'a>;
18+
}
19+
20+
pub fn ohno<'a>() -> <&'a () as Tr>::Item {
21+
None.into_iter()
22+
}
23+
24+
fn main() {}

0 commit comments

Comments
 (0)