Skip to content

Commit f6919fa

Browse files
committed
Report associated types of private traits as being private themselves
1 parent d849942 commit f6919fa

File tree

5 files changed

+82
-48
lines changed

5 files changed

+82
-48
lines changed

compiler/rustc_privacy/src/lib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,20 +221,16 @@ where
221221
}
222222

223223
let kind = match kind {
224-
ty::Inherent => Some("associated type"),
225-
ty::Weak => Some("type alias"),
226-
// Trait associated types don't have visibility, they are
227-
// visible if their parent trait is.
228-
ty::Projection => None,
224+
ty::Inherent => "associated type",
225+
ty::Weak => "type alias",
226+
ty::Projection => "associated type",
229227
_ => unreachable!(),
230228
};
231-
if let Some(kind) = kind {
232-
self.def_id_visitor.visit_def_id(
233-
data.def_id,
234-
kind,
235-
&LazyDefPathStr { def_id: data.def_id, tcx },
236-
)?;
237-
}
229+
self.def_id_visitor.visit_def_id(
230+
data.def_id,
231+
kind,
232+
&LazyDefPathStr { def_id: data.def_id, tcx },
233+
)?;
238234

239235
// This will also visit args if necessary, so we don't need to recurse.
240236
return if V::SHALLOW {

tests/ui/privacy/associated-item-privacy-trait.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod priv_trait {
2323
let _: <Pub as PrivTr>::AssocTy;
2424
//~^ ERROR associated type `PrivTr::AssocTy` is private
2525
pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
26+
//~^ ERROR associated type `PrivTr::AssocTy` is private
2627
pub trait InSignatureTr: PrivTr {}
2728
//~^ ERROR trait `PrivTr` is private
2829
impl PrivTr for u8 {}

tests/ui/privacy/associated-item-privacy-trait.stderr

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,19 @@ LL | priv_trait::mac!();
5353
|
5454
= note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
5555

56+
error: associated type `PrivTr::AssocTy` is private
57+
--> $DIR/associated-item-privacy-trait.rs:25:34
58+
|
59+
LL | pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
60+
| ^^^^^^^^^^^^^^^^^^^^^^^^ private associated type
61+
...
62+
LL | priv_trait::mac!();
63+
| ------------------ in this macro invocation
64+
|
65+
= note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
66+
5667
error: trait `PrivTr` is private
57-
--> $DIR/associated-item-privacy-trait.rs:26:34
68+
--> $DIR/associated-item-privacy-trait.rs:27:34
5869
|
5970
LL | pub trait InSignatureTr: PrivTr {}
6071
| ^^^^^^ private trait
@@ -65,7 +76,7 @@ LL | priv_trait::mac!();
6576
= note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
6677

6778
error: trait `PrivTr` is private
68-
--> $DIR/associated-item-privacy-trait.rs:28:14
79+
--> $DIR/associated-item-privacy-trait.rs:29:14
6980
|
7081
LL | impl PrivTr for u8 {}
7182
| ^^^^^^ private trait
@@ -76,7 +87,7 @@ LL | priv_trait::mac!();
7687
= note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
7788

7889
error: type `priv_signature::Priv` is private
79-
--> $DIR/associated-item-privacy-trait.rs:45:21
90+
--> $DIR/associated-item-privacy-trait.rs:46:21
8091
|
8192
LL | let value = <Pub as PubTr>::method;
8293
| ^^^^^^^^^^^^^^^^^^^^^^ private type
@@ -87,7 +98,7 @@ LL | priv_signature::mac!();
8798
= note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
8899

89100
error: type `priv_signature::Priv` is private
90-
--> $DIR/associated-item-privacy-trait.rs:47:9
101+
--> $DIR/associated-item-privacy-trait.rs:48:9
91102
|
92103
LL | value;
93104
| ^^^^^ private type
@@ -98,7 +109,7 @@ LL | priv_signature::mac!();
98109
= note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
99110

100111
error: type `priv_signature::Priv` is private
101-
--> $DIR/associated-item-privacy-trait.rs:49:13
112+
--> $DIR/associated-item-privacy-trait.rs:50:13
102113
|
103114
LL | Pub.method(loop {});
104115
| ^^^^^^ private type
@@ -109,7 +120,7 @@ LL | priv_signature::mac!();
109120
= note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
110121

111122
error: type `priv_substs::Priv` is private
112-
--> $DIR/associated-item-privacy-trait.rs:66:21
123+
--> $DIR/associated-item-privacy-trait.rs:67:21
113124
|
114125
LL | let value = <Pub as PubTr>::method::<Priv>;
115126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type
@@ -120,7 +131,7 @@ LL | priv_substs::mac!();
120131
= note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
121132

122133
error: type `priv_substs::Priv` is private
123-
--> $DIR/associated-item-privacy-trait.rs:68:9
134+
--> $DIR/associated-item-privacy-trait.rs:69:9
124135
|
125136
LL | value;
126137
| ^^^^^ private type
@@ -131,7 +142,7 @@ LL | priv_substs::mac!();
131142
= note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
132143

133144
error: type `priv_substs::Priv` is private
134-
--> $DIR/associated-item-privacy-trait.rs:70:9
145+
--> $DIR/associated-item-privacy-trait.rs:71:9
135146
|
136147
LL | Pub.method::<Priv>();
137148
| ^^^^^^^^^^^^^^^^^^^^ private type
@@ -142,7 +153,7 @@ LL | priv_substs::mac!();
142153
= note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
143154

144155
error: type `priv_parent_substs::Priv` is private
145-
--> $DIR/associated-item-privacy-trait.rs:90:21
156+
--> $DIR/associated-item-privacy-trait.rs:91:21
146157
|
147158
LL | let value = <Pub as PubTr>::method;
148159
| ^^^^^^^^^^^^^^^^^^^^^^ private type
@@ -153,7 +164,7 @@ LL | priv_parent_substs::mac!();
153164
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
154165

155166
error: type `priv_parent_substs::Priv` is private
156-
--> $DIR/associated-item-privacy-trait.rs:92:9
167+
--> $DIR/associated-item-privacy-trait.rs:93:9
157168
|
158169
LL | value;
159170
| ^^^^^ private type
@@ -164,7 +175,7 @@ LL | priv_parent_substs::mac!();
164175
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
165176

166177
error: type `priv_parent_substs::Priv` is private
167-
--> $DIR/associated-item-privacy-trait.rs:94:21
178+
--> $DIR/associated-item-privacy-trait.rs:95:21
168179
|
169180
LL | let value = <Pub as PubTr<_>>::method;
170181
| ^^^^^^^^^^^^^^^^^^^^^^^^^ private type
@@ -175,7 +186,7 @@ LL | priv_parent_substs::mac!();
175186
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
176187

177188
error: type `priv_parent_substs::Priv` is private
178-
--> $DIR/associated-item-privacy-trait.rs:96:9
189+
--> $DIR/associated-item-privacy-trait.rs:97:9
179190
|
180191
LL | value;
181192
| ^^^^^ private type
@@ -186,7 +197,7 @@ LL | priv_parent_substs::mac!();
186197
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
187198

188199
error: type `priv_parent_substs::Priv` is private
189-
--> $DIR/associated-item-privacy-trait.rs:98:9
200+
--> $DIR/associated-item-privacy-trait.rs:99:9
190201
|
191202
LL | Pub.method();
192203
| ^^^^^^^^^^^^ private type
@@ -197,7 +208,7 @@ LL | priv_parent_substs::mac!();
197208
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
198209

199210
error: type `priv_parent_substs::Priv` is private
200-
--> $DIR/associated-item-privacy-trait.rs:101:21
211+
--> $DIR/associated-item-privacy-trait.rs:102:21
201212
|
202213
LL | let value = <Priv as PubTr<_>>::method;
203214
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ private type
@@ -208,7 +219,7 @@ LL | priv_parent_substs::mac!();
208219
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
209220

210221
error: type `priv_parent_substs::Priv` is private
211-
--> $DIR/associated-item-privacy-trait.rs:103:9
222+
--> $DIR/associated-item-privacy-trait.rs:104:9
212223
|
213224
LL | value;
214225
| ^^^^^ private type
@@ -219,7 +230,7 @@ LL | priv_parent_substs::mac!();
219230
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
220231

221232
error: type `priv_parent_substs::Priv` is private
222-
--> $DIR/associated-item-privacy-trait.rs:105:9
233+
--> $DIR/associated-item-privacy-trait.rs:106:9
223234
|
224235
LL | Priv.method();
225236
| ^^^^^^^^^^^^^ private type
@@ -230,7 +241,7 @@ LL | priv_parent_substs::mac!();
230241
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
231242

232243
error: type `priv_parent_substs::Priv` is private
233-
--> $DIR/associated-item-privacy-trait.rs:108:9
244+
--> $DIR/associated-item-privacy-trait.rs:109:9
234245
|
235246
LL | <Pub as PubTr>::CONST;
236247
| ^^^^^^^^^^^^^^^^^^^^^ private type
@@ -241,7 +252,7 @@ LL | priv_parent_substs::mac!();
241252
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
242253

243254
error: type `priv_parent_substs::Priv` is private
244-
--> $DIR/associated-item-privacy-trait.rs:110:9
255+
--> $DIR/associated-item-privacy-trait.rs:111:9
245256
|
246257
LL | <Pub as PubTr<_>>::CONST;
247258
| ^^^^^^^^^^^^^^^^^^^^^^^^ private type
@@ -252,7 +263,7 @@ LL | priv_parent_substs::mac!();
252263
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
253264

254265
error: type `priv_parent_substs::Priv` is private
255-
--> $DIR/associated-item-privacy-trait.rs:112:9
266+
--> $DIR/associated-item-privacy-trait.rs:113:9
256267
|
257268
LL | <Priv as PubTr<_>>::CONST;
258269
| ^^^^^^^^^^^^^^^^^^^^^^^^^ private type
@@ -263,7 +274,7 @@ LL | priv_parent_substs::mac!();
263274
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
264275

265276
error: type `priv_parent_substs::Priv` is private
266-
--> $DIR/associated-item-privacy-trait.rs:116:30
277+
--> $DIR/associated-item-privacy-trait.rs:117:30
267278
|
268279
LL | let _: <Pub as PubTr<_>>::AssocTy;
269280
| ^ private type
@@ -274,7 +285,7 @@ LL | priv_parent_substs::mac!();
274285
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
275286

276287
error: type `priv_parent_substs::Priv` is private
277-
--> $DIR/associated-item-privacy-trait.rs:118:17
288+
--> $DIR/associated-item-privacy-trait.rs:119:17
278289
|
279290
LL | let _: <Priv as PubTr<_>>::AssocTy;
280291
| ^^^^ private type
@@ -285,7 +296,7 @@ LL | priv_parent_substs::mac!();
285296
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
286297

287298
error: type `priv_parent_substs::Priv` is private
288-
--> $DIR/associated-item-privacy-trait.rs:121:35
299+
--> $DIR/associated-item-privacy-trait.rs:122:35
289300
|
290301
LL | pub type InSignatureTy1 = <Pub as PubTr>::AssocTy;
291302
| ^^^^^^^^^^^^^^^^^^^^^^^ private type
@@ -296,7 +307,7 @@ LL | priv_parent_substs::mac!();
296307
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
297308

298309
error: type `priv_parent_substs::Priv` is private
299-
--> $DIR/associated-item-privacy-trait.rs:123:35
310+
--> $DIR/associated-item-privacy-trait.rs:124:35
300311
|
301312
LL | pub type InSignatureTy2 = <Priv as PubTr<Pub>>::AssocTy;
302313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type
@@ -307,7 +318,7 @@ LL | priv_parent_substs::mac!();
307318
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
308319

309320
error: type `priv_parent_substs::Priv` is private
310-
--> $DIR/associated-item-privacy-trait.rs:125:14
321+
--> $DIR/associated-item-privacy-trait.rs:126:14
311322
|
312323
LL | impl PubTr for u8 {}
313324
| ^^^^^ private type
@@ -317,5 +328,5 @@ LL | priv_parent_substs::mac!();
317328
|
318329
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
319330

320-
error: aborting due to 29 previous errors
331+
error: aborting due to 30 previous errors
321332

tests/ui/privacy/private-in-public.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ mod aliases_pub {
105105
// This should be OK, but associated type aliases are not substituted yet
106106
pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
107107
//~^ WARNING type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
108+
//~| WARNING associated type `aliases_pub::PrivTr::Assoc` is more private than the item `aliases_pub::f3`
108109

109110
impl PrivUseAlias {
110111
pub fn f(arg: Priv) {}
@@ -133,6 +134,7 @@ mod aliases_priv {
133134
pub fn f2(arg: PrivAlias) {} //~ WARNING type `Priv2` is more private than the item `aliases_priv::f2`
134135
pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
135136
//~^ WARNING type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
137+
//~| WARNING associated type `aliases_priv::PrivTr::Assoc` is more private than the item `aliases_priv::f3`
136138
}
137139

138140
mod aliases_params {

tests/ui/privacy/private-in-public.stderr

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,18 @@ note: but type `impls::Priv` is only usable at visibility `pub(self)`
276276
LL | struct Priv;
277277
| ^^^^^^^^^^^
278278

279+
warning: associated type `aliases_pub::PrivTr::Assoc` is more private than the item `aliases_pub::f3`
280+
--> $DIR/private-in-public.rs:106:5
281+
|
282+
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
283+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_pub::f3` is reachable at visibility `pub(crate)`
284+
|
285+
note: but associated type `aliases_pub::PrivTr::Assoc` is only usable at visibility `pub(self)`
286+
--> $DIR/private-in-public.rs:101:9
287+
|
288+
LL | type Assoc = m::Pub3;
289+
| ^^^^^^^^^^
290+
279291
warning: type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
280292
--> $DIR/private-in-public.rs:106:5
281293
|
@@ -289,64 +301,76 @@ LL | struct Priv;
289301
| ^^^^^^^^^^^
290302

291303
warning: type `Priv1` is more private than the item `aliases_priv::f1`
292-
--> $DIR/private-in-public.rs:132:5
304+
--> $DIR/private-in-public.rs:133:5
293305
|
294306
LL | pub fn f1(arg: PrivUseAlias) {}
295307
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f1` is reachable at visibility `pub(crate)`
296308
|
297309
note: but type `Priv1` is only usable at visibility `pub(self)`
298-
--> $DIR/private-in-public.rs:117:5
310+
--> $DIR/private-in-public.rs:118:5
299311
|
300312
LL | struct Priv1;
301313
| ^^^^^^^^^^^^
302314

303315
warning: type `Priv2` is more private than the item `aliases_priv::f2`
304-
--> $DIR/private-in-public.rs:133:5
316+
--> $DIR/private-in-public.rs:134:5
305317
|
306318
LL | pub fn f2(arg: PrivAlias) {}
307319
| ^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f2` is reachable at visibility `pub(crate)`
308320
|
309321
note: but type `Priv2` is only usable at visibility `pub(self)`
310-
--> $DIR/private-in-public.rs:118:5
322+
--> $DIR/private-in-public.rs:119:5
311323
|
312324
LL | struct Priv2;
313325
| ^^^^^^^^^^^^
314326

327+
warning: associated type `aliases_priv::PrivTr::Assoc` is more private than the item `aliases_priv::f3`
328+
--> $DIR/private-in-public.rs:135:5
329+
|
330+
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
331+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f3` is reachable at visibility `pub(crate)`
332+
|
333+
note: but associated type `aliases_priv::PrivTr::Assoc` is only usable at visibility `pub(self)`
334+
--> $DIR/private-in-public.rs:129:9
335+
|
336+
LL | type Assoc = Priv3;
337+
| ^^^^^^^^^^
338+
315339
warning: type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
316-
--> $DIR/private-in-public.rs:134:5
340+
--> $DIR/private-in-public.rs:135:5
317341
|
318342
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
319343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f3` is reachable at visibility `pub(crate)`
320344
|
321345
note: but type `aliases_priv::Priv` is only usable at visibility `pub(self)`
322-
--> $DIR/private-in-public.rs:115:5
346+
--> $DIR/private-in-public.rs:116:5
323347
|
324348
LL | struct Priv;
325349
| ^^^^^^^^^^^
326350

327351
warning: type `aliases_params::Priv` is more private than the item `aliases_params::f2`
328-
--> $DIR/private-in-public.rs:143:5
352+
--> $DIR/private-in-public.rs:145:5
329353
|
330354
LL | pub fn f2(arg: PrivAliasGeneric) {}
331355
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_params::f2` is reachable at visibility `pub(crate)`
332356
|
333357
note: but type `aliases_params::Priv` is only usable at visibility `pub(self)`
334-
--> $DIR/private-in-public.rs:139:5
358+
--> $DIR/private-in-public.rs:141:5
335359
|
336360
LL | struct Priv;
337361
| ^^^^^^^^^^^
338362

339363
warning: type `aliases_params::Priv` is more private than the item `aliases_params::f3`
340-
--> $DIR/private-in-public.rs:145:5
364+
--> $DIR/private-in-public.rs:147:5
341365
|
342366
LL | pub fn f3(arg: Result<u8>) {}
343367
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_params::f3` is reachable at visibility `pub(crate)`
344368
|
345369
note: but type `aliases_params::Priv` is only usable at visibility `pub(self)`
346-
--> $DIR/private-in-public.rs:139:5
370+
--> $DIR/private-in-public.rs:141:5
347371
|
348372
LL | struct Priv;
349373
| ^^^^^^^^^^^
350374

351-
warning: 29 warnings emitted
375+
warning: 31 warnings emitted
352376

0 commit comments

Comments
 (0)