Skip to content

Commit d849942

Browse files
committed
Demonstrate why we need to handle the trait of a projection
1 parent 05b0ab9 commit d849942

File tree

5 files changed

+49
-81
lines changed

5 files changed

+49
-81
lines changed

compiler/rustc_privacy/src/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,21 @@ where
220220
return ControlFlow::Continue(());
221221
}
222222

223-
let (def_id, kind) = match kind {
224-
ty::Inherent => (data.def_id, "associated type"),
225-
ty::Weak => (data.def_id, "type alias"),
223+
let kind = match kind {
224+
ty::Inherent => Some("associated type"),
225+
ty::Weak => Some("type alias"),
226226
// Trait associated types don't have visibility, they are
227227
// visible if their parent trait is.
228-
ty::Projection => (tcx.parent(data.def_id), "trait"),
228+
ty::Projection => None,
229229
_ => unreachable!(),
230230
};
231-
self.def_id_visitor.visit_def_id(def_id, kind, &LazyDefPathStr { def_id, tcx })?;
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+
}
232238

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ 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 trait `PrivTr` is private
2726
pub trait InSignatureTr: PrivTr {}
2827
//~^ ERROR trait `PrivTr` is private
2928
impl PrivTr for u8 {}

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

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

5656
error: trait `PrivTr` is private
57-
--> $DIR/associated-item-privacy-trait.rs:25:34
58-
|
59-
LL | pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
60-
| ^^^^^^^^^^^^^^^^^^^^^^^^ private trait
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-
67-
error: trait `PrivTr` is private
68-
--> $DIR/associated-item-privacy-trait.rs:27:34
57+
--> $DIR/associated-item-privacy-trait.rs:26:34
6958
|
7059
LL | pub trait InSignatureTr: PrivTr {}
7160
| ^^^^^^ private trait
@@ -76,7 +65,7 @@ LL | priv_trait::mac!();
7665
= note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
7766

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ mod aliases_pub {
104104

105105
// This should be OK, but associated type aliases are not substituted yet
106106
pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
107-
//~^ WARNING trait `aliases_pub::PrivTr` is more private than the item `aliases_pub::f3`
108-
//~| WARNING type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
107+
//~^ WARNING type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
109108

110109
impl PrivUseAlias {
111110
pub fn f(arg: Priv) {}
@@ -133,8 +132,7 @@ mod aliases_priv {
133132
pub fn f1(arg: PrivUseAlias) {} //~ WARNING type `Priv1` is more private than the item `aliases_priv::f1`
134133
pub fn f2(arg: PrivAlias) {} //~ WARNING type `Priv2` is more private than the item `aliases_priv::f2`
135134
pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
136-
//~^ WARNING trait `aliases_priv::PrivTr` is more private than the item `aliases_priv::f3`
137-
//~| WARNING type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
135+
//~^ WARNING type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
138136
}
139137

140138
mod aliases_params {

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

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

279-
warning: trait `aliases_pub::PrivTr` 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 trait `aliases_pub::PrivTr` is only usable at visibility `pub(self)`
286-
--> $DIR/private-in-public.rs:100:5
287-
|
288-
LL | trait PrivTr {
289-
| ^^^^^^^^^^^^
290-
291279
warning: type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
292280
--> $DIR/private-in-public.rs:106:5
293281
|
@@ -301,76 +289,64 @@ LL | struct Priv;
301289
| ^^^^^^^^^^^
302290

303291
warning: type `Priv1` is more private than the item `aliases_priv::f1`
304-
--> $DIR/private-in-public.rs:133:5
292+
--> $DIR/private-in-public.rs:132:5
305293
|
306294
LL | pub fn f1(arg: PrivUseAlias) {}
307295
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f1` is reachable at visibility `pub(crate)`
308296
|
309297
note: but type `Priv1` is only usable at visibility `pub(self)`
310-
--> $DIR/private-in-public.rs:118:5
298+
--> $DIR/private-in-public.rs:117:5
311299
|
312300
LL | struct Priv1;
313301
| ^^^^^^^^^^^^
314302

315303
warning: type `Priv2` is more private than the item `aliases_priv::f2`
316-
--> $DIR/private-in-public.rs:134:5
304+
--> $DIR/private-in-public.rs:133:5
317305
|
318306
LL | pub fn f2(arg: PrivAlias) {}
319307
| ^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f2` is reachable at visibility `pub(crate)`
320308
|
321309
note: but type `Priv2` is only usable at visibility `pub(self)`
322-
--> $DIR/private-in-public.rs:119:5
310+
--> $DIR/private-in-public.rs:118:5
323311
|
324312
LL | struct Priv2;
325313
| ^^^^^^^^^^^^
326314

327-
warning: trait `aliases_priv::PrivTr` 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 trait `aliases_priv::PrivTr` is only usable at visibility `pub(self)`
334-
--> $DIR/private-in-public.rs:128:5
335-
|
336-
LL | trait PrivTr {
337-
| ^^^^^^^^^^^^
338-
339315
warning: type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
340-
--> $DIR/private-in-public.rs:135:5
316+
--> $DIR/private-in-public.rs:134:5
341317
|
342318
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
343319
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f3` is reachable at visibility `pub(crate)`
344320
|
345321
note: but type `aliases_priv::Priv` is only usable at visibility `pub(self)`
346-
--> $DIR/private-in-public.rs:116:5
322+
--> $DIR/private-in-public.rs:115:5
347323
|
348324
LL | struct Priv;
349325
| ^^^^^^^^^^^
350326

351327
warning: type `aliases_params::Priv` is more private than the item `aliases_params::f2`
352-
--> $DIR/private-in-public.rs:145:5
328+
--> $DIR/private-in-public.rs:143:5
353329
|
354330
LL | pub fn f2(arg: PrivAliasGeneric) {}
355331
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_params::f2` is reachable at visibility `pub(crate)`
356332
|
357333
note: but type `aliases_params::Priv` is only usable at visibility `pub(self)`
358-
--> $DIR/private-in-public.rs:141:5
334+
--> $DIR/private-in-public.rs:139:5
359335
|
360336
LL | struct Priv;
361337
| ^^^^^^^^^^^
362338

363339
warning: type `aliases_params::Priv` is more private than the item `aliases_params::f3`
364-
--> $DIR/private-in-public.rs:147:5
340+
--> $DIR/private-in-public.rs:145:5
365341
|
366342
LL | pub fn f3(arg: Result<u8>) {}
367343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_params::f3` is reachable at visibility `pub(crate)`
368344
|
369345
note: but type `aliases_params::Priv` is only usable at visibility `pub(self)`
370-
--> $DIR/private-in-public.rs:141:5
346+
--> $DIR/private-in-public.rs:139:5
371347
|
372348
LL | struct Priv;
373349
| ^^^^^^^^^^^
374350

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

0 commit comments

Comments
 (0)