Skip to content

Commit fe6a54d

Browse files
committed
Auto merge of #56878 - petrochenkov:privdyn, r=arielb1
privacy: Use common `DefId` visiting infrastructure for all privacy visitors One repeating pattern in privacy checking is going through a type, visiting all `DefId`s inside it and doing something with them. This is the case because visibilities and reachabilities are attached to `DefId`s. Previously various privacy visitors visited types slightly differently using their own methods, with most recently written `TypePrivacyVisitor` being the "gold standard". This mostly worked okay, but differences could manifest in overly conservative reachability analysis, some errors being reported twice, some private-in-public lints (not errors) being wrongly reported or not reported. This PR does something that I wanted to do since #32674 (comment) - factoring out the common visiting logic! Now all the common logic is contained in `struct DefIdVisitorSkeleton`, with specific privacy visitors deciding only what to do with visited `DefId`s (via `trait DefIdVisitor`). A bunch of cleanups is also applied in the process. This area is somewhat tricky due to lots of easily miss-able details, but thankfully it's was well covered by tests in #46083 and previous PRs, so I'm relatively sure in the refactoring correctness. Fixes #56837 (comment) in particular. Also this will help with implementing #48054.
2 parents 9eac386 + 60d1fa7 commit fe6a54d

22 files changed

+621
-559
lines changed

src/librustc/hir/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,15 @@ impl VisibilityKind {
20902090
VisibilityKind::Restricted { .. } => true,
20912091
}
20922092
}
2093+
2094+
pub fn descr(&self) -> &'static str {
2095+
match *self {
2096+
VisibilityKind::Public => "public",
2097+
VisibilityKind::Inherited => "private",
2098+
VisibilityKind::Crate(..) => "crate-visible",
2099+
VisibilityKind::Restricted { .. } => "restricted",
2100+
}
2101+
}
20932102
}
20942103

20952104
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]

src/librustc/util/ppaux.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,9 @@ define_print! {
709709

710710
define_print! {
711711
('tcx) ty::ExistentialTraitRef<'tcx>, (self, f, cx) {
712+
display {
713+
cx.parameterized(f, self.substs, self.def_id, &[])
714+
}
712715
debug {
713716
ty::tls::with(|tcx| {
714717
let dummy_self = tcx.mk_infer(ty::FreshTy(0));

src/librustc_privacy/lib.rs

Lines changed: 446 additions & 514 deletions
Large diffs are not rendered by default.

src/libstd/sys/windows/handle.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,6 @@ impl RawHandle {
160160
}
161161
}
162162

163-
pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
164-
let mut me = self;
165-
(&mut me).read_to_end(buf)
166-
}
167-
168163
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
169164
let mut amt = 0;
170165
let len = cmp::min(buf.len(), <c::DWORD>::max_value() as usize) as c::DWORD;

src/libstd/sys/windows/stdio.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ impl Stdin {
128128
// MemReader shouldn't error here since we just filled it
129129
utf8.read(buf)
130130
}
131-
132-
pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
133-
let mut me = self;
134-
(&mut me).read_to_end(buf)
135-
}
136131
}
137132

138133
#[unstable(reason = "not public", issue = "0", feature = "fd_read")]

src/test/ui/error-codes/E0445.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ trait Foo {
44

55
pub trait Bar : Foo {}
66
//~^ ERROR private trait `Foo` in public interface [E0445]
7-
//~| NOTE can't leak private trait
87
pub struct Bar2<T: Foo>(pub T);
98
//~^ ERROR private trait `Foo` in public interface [E0445]
10-
//~| NOTE can't leak private trait
119
pub fn foo<T: Foo> (t: T) {}
1210
//~^ ERROR private trait `Foo` in public interface [E0445]
13-
//~| NOTE can't leak private trait
1411

1512
fn main() {}

src/test/ui/error-codes/E0445.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ LL | pub trait Bar : Foo {}
55
| ^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
66

77
error[E0445]: private trait `Foo` in public interface
8-
--> $DIR/E0445.rs:8:1
8+
--> $DIR/E0445.rs:7:1
99
|
1010
LL | pub struct Bar2<T: Foo>(pub T);
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
1212

1313
error[E0445]: private trait `Foo` in public interface
14-
--> $DIR/E0445.rs:11:1
14+
--> $DIR/E0445.rs:9:1
1515
|
1616
LL | pub fn foo<T: Foo> (t: T) {}
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait

src/test/ui/impl-trait/issue-49376.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ fn gen() -> impl PartialOrd + PartialEq + Debug { }
99

1010
struct Bar {}
1111
trait Foo<T = Self> {}
12+
trait FooNested<T = Option<Self>> {}
1213
impl Foo for Bar {}
14+
impl FooNested for Bar {}
1315

14-
fn foo() -> impl Foo {
16+
fn foo() -> impl Foo + FooNested {
1517
Bar {}
1618
}
1719

src/test/ui/issues/issue-18389.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0445]: private trait `Private<<Self as Public>::P, <Self as Public>::R>` in public interface
22
--> $DIR/issue-18389.rs:7:1
33
|
4+
LL | trait Private<P, R> {
5+
| - `Private<<Self as Public>::P, <Self as Public>::R>` declared as private
6+
...
47
LL | / pub trait Public: Private<
58
LL | | //~^ ERROR private trait `Private<<Self as Public>::P, <Self as Public>::R>` in public interface
69
LL | | <Self as Public>::P,

src/test/ui/privacy/associated-item-privacy-type-binding.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ mod priv_trait {
99

1010
pub macro mac1() {
1111
let _: Box<PubTr<AssocTy = u8>>;
12-
//~^ ERROR type `(dyn priv_trait::PubTr<AssocTy=u8> + '<empty>)` is private
13-
//~| ERROR type `(dyn priv_trait::PubTr<AssocTy=u8> + '<empty>)` is private
12+
//~^ ERROR trait `priv_trait::PrivTr` is private
13+
//~| ERROR trait `priv_trait::PrivTr` is private
1414
type InSignatureTy2 = Box<PubTr<AssocTy = u8>>;
15-
//~^ ERROR type `(dyn priv_trait::PubTr<AssocTy=u8> + 'static)` is private
15+
//~^ ERROR trait `priv_trait::PrivTr` is private
1616
trait InSignatureTr2: PubTr<AssocTy = u8> {}
1717
//~^ ERROR trait `priv_trait::PrivTr` is private
1818
}
1919
pub macro mac2() {
2020
let _: Box<PrivTr<AssocTy = u8>>;
21-
//~^ ERROR type `(dyn priv_trait::PrivTr<AssocTy=u8> + '<empty>)` is private
22-
//~| ERROR type `(dyn priv_trait::PrivTr<AssocTy=u8> + '<empty>)` is private
21+
//~^ ERROR trait `priv_trait::PrivTr` is private
22+
//~| ERROR trait `priv_trait::PrivTr` is private
2323
type InSignatureTy1 = Box<PrivTr<AssocTy = u8>>;
24-
//~^ ERROR type `(dyn priv_trait::PrivTr<AssocTy=u8> + 'static)` is private
24+
//~^ ERROR trait `priv_trait::PrivTr` is private
2525
trait InSignatureTr1: PrivTr<AssocTy = u8> {}
2626
//~^ ERROR trait `priv_trait::PrivTr` is private
2727
}

0 commit comments

Comments
 (0)