Skip to content

Commit 0ad57d8

Browse files
Delay formatting trimmed path until lint/error is emitted
1 parent d77da9d commit 0ad57d8

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

Diff for: compiler/rustc_privacy/src/errors.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::fmt::Display;
2+
3+
use rustc_errors::IntoDiagnosticArg;
14
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
25
use rustc_span::{Span, Symbol};
36

@@ -35,7 +38,7 @@ pub struct ItemIsPrivate<'a> {
3538
#[label]
3639
pub span: Span,
3740
pub kind: &'a str,
38-
pub descr: String,
41+
pub descr: FromDisplay<'a>,
3942
}
4043

4144
#[derive(SessionDiagnostic)]
@@ -55,7 +58,7 @@ pub struct InPublicInterfaceTraits<'a> {
5558
pub span: Span,
5659
pub vis_descr: &'static str,
5760
pub kind: &'a str,
58-
pub descr: String,
61+
pub descr: FromDisplay<'a>,
5962
#[label(privacy::visibility_label)]
6063
pub vis_span: Span,
6164
}
@@ -69,7 +72,7 @@ pub struct InPublicInterface<'a> {
6972
pub span: Span,
7073
pub vis_descr: &'static str,
7174
pub kind: &'a str,
72-
pub descr: String,
75+
pub descr: FromDisplay<'a>,
7376
#[label(privacy::visibility_label)]
7477
pub vis_span: Span,
7578
}
@@ -78,7 +81,7 @@ pub struct InPublicInterface<'a> {
7881
#[lint(privacy::from_private_dep_in_public_interface)]
7982
pub struct FromPrivateDependencyInPublicInterface<'a> {
8083
pub kind: &'a str,
81-
pub descr: String,
84+
pub descr: FromDisplay<'a>,
8285
pub krate: Symbol,
8386
}
8487

@@ -87,5 +90,13 @@ pub struct FromPrivateDependencyInPublicInterface<'a> {
8790
pub struct PrivateInPublicLint<'a> {
8891
pub vis_descr: &'static str,
8992
pub kind: &'a str,
90-
pub descr: String,
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+
}
91102
}

Diff for: compiler/rustc_privacy/src/lib.rs

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

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

4546
////////////////////////////////////////////////////////////////////////////////
@@ -1082,7 +1083,7 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
10821083
self.tcx.sess.emit_err(ItemIsPrivate {
10831084
span: self.span,
10841085
kind,
1085-
descr: descr.to_string(),
1086+
descr: FromDisplay(descr),
10861087
});
10871088
}
10881089
is_error
@@ -1255,7 +1256,9 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
12551256
};
12561257
let kind = kind.descr(def_id);
12571258
let _ = match name {
1258-
Some(name) => sess.emit_err(ItemIsPrivate { span, kind, descr: name }),
1259+
Some(name) => {
1260+
sess.emit_err(ItemIsPrivate { span, kind, descr: FromDisplay(&name) })
1261+
}
12591262
None => sess.emit_err(UnnamedItemIsPrivate { span, kind }),
12601263
};
12611264
return;
@@ -1723,7 +1726,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17231726
self.tcx.def_span(self.item_def_id.to_def_id()),
17241727
FromPrivateDependencyInPublicInterface {
17251728
kind,
1726-
descr: descr.to_string(),
1729+
descr: FromDisplay(descr),
17271730
krate: self.tcx.crate_name(def_id.krate),
17281731
},
17291732
);
@@ -1750,7 +1753,6 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17501753
}
17511754
};
17521755
let span = self.tcx.def_span(self.item_def_id.to_def_id());
1753-
let descr = descr.to_string();
17541756
if self.has_old_errors
17551757
|| self.in_assoc_ty
17561758
|| self.tcx.resolutions(()).has_pub_restricted
@@ -1761,15 +1763,15 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17611763
span,
17621764
vis_descr,
17631765
kind,
1764-
descr,
1766+
descr: FromDisplay(descr),
17651767
vis_span,
17661768
});
17671769
} else {
17681770
self.tcx.sess.emit_err(InPublicInterface {
17691771
span,
17701772
vis_descr,
17711773
kind,
1772-
descr,
1774+
descr: FromDisplay(descr),
17731775
vis_span,
17741776
});
17751777
}
@@ -1778,7 +1780,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17781780
lint::builtin::PRIVATE_IN_PUBLIC,
17791781
hir_id,
17801782
span,
1781-
PrivateInPublicLint { vis_descr, kind, descr },
1783+
PrivateInPublicLint { vis_descr, kind, descr: FromDisplay(descr) },
17821784
);
17831785
}
17841786
}

Diff for: 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)