Skip to content

Commit 8ce14f0

Browse files
committed
Demonstrate why we need to handle the trait of a projection
1 parent c2bcced commit 8ce14f0

7 files changed

+51
-113
lines changed

compiler/rustc_privacy/src/lib.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,21 @@ where
221221
return ControlFlow::Continue(());
222222
}
223223

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

234240
// This will also visit args if necessary, so we don't need to recurse.
235241
return if V::SHALLOW {
@@ -1364,7 +1370,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
13641370
match item.kind {
13651371
hir::ItemKind::Macro(_, _) => intravisit::walk_item(self, item),
13661372
hir::ItemKind::Static(_, _, body_id)
1367-
| hir::ItemKind::Const(_, body_id)
1373+
| hir::ItemKind::Const(_, _, body_id)
13681374
| hir::ItemKind::Fn(_, _, body_id) => self.visit_nested_body(body_id),
13691375
hir::ItemKind::Impl(hir::Impl { of_trait: Some(tr), .. }) => {
13701376
self.span = tr.path.span;

tests/ui/impl-trait/fresh-lifetime-from-bare-trait-obj-114664.rs

-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
fn ice() -> impl AsRef<Fn(&())> {
66
//~^ WARN trait objects without an explicit `dyn` are deprecated
7-
//~| WARN trait objects without an explicit `dyn` are deprecated
8-
//~| WARN trait objects without an explicit `dyn` are deprecated
9-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
10-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
117
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
128
Foo
139
}

tests/ui/impl-trait/fresh-lifetime-from-bare-trait-obj-114664.stderr

+1-27
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,5 @@ help: use `dyn`
1212
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
1313
| +++
1414

15-
warning: trait objects without an explicit `dyn` are deprecated
16-
--> $DIR/fresh-lifetime-from-bare-trait-obj-114664.rs:5:24
17-
|
18-
LL | fn ice() -> impl AsRef<Fn(&())> {
19-
| ^^^^^^^
20-
|
21-
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
22-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
23-
help: use `dyn`
24-
|
25-
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
26-
| +++
27-
28-
warning: trait objects without an explicit `dyn` are deprecated
29-
--> $DIR/fresh-lifetime-from-bare-trait-obj-114664.rs:5:24
30-
|
31-
LL | fn ice() -> impl AsRef<Fn(&())> {
32-
| ^^^^^^^
33-
|
34-
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
35-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
36-
help: use `dyn`
37-
|
38-
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
39-
| +++
40-
41-
warning: 3 warnings emitted
15+
warning: 1 warning emitted
4216

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

-1
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

+25-36
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

+2-4
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 {

0 commit comments

Comments
 (0)