Skip to content

Commit d2de292

Browse files
authored
Rollup merge of #118715 - davidtwco:issue-117997-privacy-visit-trait-ref-and-args, r=TaKO8Ki
privacy: visit trait def id of projections Fixes #117997. A refactoring in #117076 changed the `DefIdVisitorSkeleton` to avoid calling `visit_projection_ty` for `ty::Projection` aliases, and instead just iterate over the args - this makes sense, as `visit_projection_ty` will indirectly visit all of the same args, but in doing so, will also create a `TraitRef` containing the trait's `DefId`, which also gets visited. The trait's `DefId` isn't visited when we only visit the arguments without separating them into `TraitRef` and own args first. Eventually this influences the reachability set and whether a function is encoded into the metadata.
2 parents 1889e5a + 5d97724 commit d2de292

File tree

5 files changed

+89
-19
lines changed

5 files changed

+89
-19
lines changed

compiler/rustc_privacy/src/lib.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,21 @@ where
218218
return ControlFlow::Continue(());
219219
}
220220

221-
let kind = match kind {
222-
ty::Inherent | ty::Projection => "associated type",
223-
ty::Weak => "type alias",
224-
ty::Opaque => unreachable!(),
225-
};
226221
self.def_id_visitor.visit_def_id(
227222
data.def_id,
228-
kind,
223+
match kind {
224+
ty::Inherent | ty::Projection => "associated type",
225+
ty::Weak => "type alias",
226+
ty::Opaque => unreachable!(),
227+
},
229228
&LazyDefPathStr { def_id: data.def_id, tcx },
230229
)?;
231230

232231
// This will also visit args if necessary, so we don't need to recurse.
233232
return if V::SHALLOW {
234233
ControlFlow::Continue(())
234+
} else if kind == ty::Projection {
235+
self.visit_projection_ty(data)
235236
} else {
236237
data.args.iter().try_for_each(|subst| subst.visit_with(self))
237238
};
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// no-prefer-dynamic
2+
// compile-flags: --crate-type=rlib
3+
4+
pub use impl_mod::TraitImplementer as Implementer;
5+
6+
pub use trait_mod::get_assoc;
7+
8+
mod impl_mod {
9+
use crate::trait_mod::TraitWithAssocType;
10+
11+
pub struct TraitImplementer {}
12+
pub struct AssociatedType {}
13+
14+
impl AssociatedType {
15+
pub fn method_on_assoc(&self) -> i32 {
16+
todo!()
17+
}
18+
}
19+
20+
impl TraitWithAssocType for TraitImplementer {
21+
type AssocType = AssociatedType;
22+
}
23+
}
24+
25+
mod trait_mod {
26+
use crate::Implementer;
27+
28+
pub fn get_assoc() -> <Implementer as TraitWithAssocType>::AssocType {
29+
todo!()
30+
}
31+
32+
pub trait TraitWithAssocType {
33+
type AssocType;
34+
}
35+
}

tests/ui/privacy/issue-117997.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// aux-build:issue-117997.rs
2+
// build-pass
3+
4+
extern crate issue_117997;
5+
6+
pub fn main() {
7+
issue_117997::get_assoc().method_on_assoc();
8+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ mod aliases_pub {
106106
pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
107107
//~^ WARNING type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
108108
//~| WARNING associated type `aliases_pub::PrivTr::Assoc` is more private than the item `aliases_pub::f3`
109+
//~^^^ WARNING trait `aliases_pub::PrivTr` is more private than the item `aliases_pub::f3`
109110

110111
impl PrivUseAlias {
111112
pub fn f(arg: Priv) {}
@@ -135,6 +136,7 @@ mod aliases_priv {
135136
pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
136137
//~^ WARNING type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
137138
//~| WARNING associated type `aliases_priv::PrivTr::Assoc` is more private than the item `aliases_priv::f3`
139+
//~^^^ WARNING trait `aliases_priv::PrivTr` is more private than the item `aliases_priv::f3`
138140
}
139141

140142
mod aliases_params {

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

+37-13
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,18 @@ note: but associated type `aliases_pub::PrivTr::Assoc` is only usable at visibil
288288
LL | type Assoc = m::Pub3;
289289
| ^^^^^^^^^^
290290

291+
warning: trait `aliases_pub::PrivTr` is more private than the item `aliases_pub::f3`
292+
--> $DIR/private-in-public.rs:106:5
293+
|
294+
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
295+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_pub::f3` is reachable at visibility `pub(crate)`
296+
|
297+
note: but trait `aliases_pub::PrivTr` is only usable at visibility `pub(self)`
298+
--> $DIR/private-in-public.rs:100:5
299+
|
300+
LL | trait PrivTr {
301+
| ^^^^^^^^^^^^
302+
291303
warning: type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
292304
--> $DIR/private-in-public.rs:106:5
293305
|
@@ -301,76 +313,88 @@ LL | struct Priv;
301313
| ^^^^^^^^^^^
302314

303315
warning: type `Priv1` is more private than the item `aliases_priv::f1`
304-
--> $DIR/private-in-public.rs:133:5
316+
--> $DIR/private-in-public.rs:134:5
305317
|
306318
LL | pub fn f1(arg: PrivUseAlias) {}
307319
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f1` is reachable at visibility `pub(crate)`
308320
|
309321
note: but type `Priv1` 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 Priv1;
313325
| ^^^^^^^^^^^^
314326

315327
warning: type `Priv2` is more private than the item `aliases_priv::f2`
316-
--> $DIR/private-in-public.rs:134:5
328+
--> $DIR/private-in-public.rs:135:5
317329
|
318330
LL | pub fn f2(arg: PrivAlias) {}
319331
| ^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f2` is reachable at visibility `pub(crate)`
320332
|
321333
note: but type `Priv2` is only usable at visibility `pub(self)`
322-
--> $DIR/private-in-public.rs:119:5
334+
--> $DIR/private-in-public.rs:120:5
323335
|
324336
LL | struct Priv2;
325337
| ^^^^^^^^^^^^
326338

327339
warning: associated type `aliases_priv::PrivTr::Assoc` is more private than the item `aliases_priv::f3`
328-
--> $DIR/private-in-public.rs:135:5
340+
--> $DIR/private-in-public.rs:136:5
329341
|
330342
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
331343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f3` is reachable at visibility `pub(crate)`
332344
|
333345
note: but associated type `aliases_priv::PrivTr::Assoc` is only usable at visibility `pub(self)`
334-
--> $DIR/private-in-public.rs:129:9
346+
--> $DIR/private-in-public.rs:130:9
335347
|
336348
LL | type Assoc = Priv3;
337349
| ^^^^^^^^^^
338350

351+
warning: trait `aliases_priv::PrivTr` is more private than the item `aliases_priv::f3`
352+
--> $DIR/private-in-public.rs:136:5
353+
|
354+
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
355+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f3` is reachable at visibility `pub(crate)`
356+
|
357+
note: but trait `aliases_priv::PrivTr` is only usable at visibility `pub(self)`
358+
--> $DIR/private-in-public.rs:129:5
359+
|
360+
LL | trait PrivTr {
361+
| ^^^^^^^^^^^^
362+
339363
warning: type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
340-
--> $DIR/private-in-public.rs:135:5
364+
--> $DIR/private-in-public.rs:136:5
341365
|
342366
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
343367
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f3` is reachable at visibility `pub(crate)`
344368
|
345369
note: but type `aliases_priv::Priv` is only usable at visibility `pub(self)`
346-
--> $DIR/private-in-public.rs:116:5
370+
--> $DIR/private-in-public.rs:117:5
347371
|
348372
LL | struct Priv;
349373
| ^^^^^^^^^^^
350374

351375
warning: type `aliases_params::Priv` is more private than the item `aliases_params::f2`
352-
--> $DIR/private-in-public.rs:145:5
376+
--> $DIR/private-in-public.rs:147:5
353377
|
354378
LL | pub fn f2(arg: PrivAliasGeneric) {}
355379
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_params::f2` is reachable at visibility `pub(crate)`
356380
|
357381
note: but type `aliases_params::Priv` is only usable at visibility `pub(self)`
358-
--> $DIR/private-in-public.rs:141:5
382+
--> $DIR/private-in-public.rs:143:5
359383
|
360384
LL | struct Priv;
361385
| ^^^^^^^^^^^
362386

363387
warning: type `aliases_params::Priv` is more private than the item `aliases_params::f3`
364-
--> $DIR/private-in-public.rs:147:5
388+
--> $DIR/private-in-public.rs:149:5
365389
|
366390
LL | pub fn f3(arg: Result<u8>) {}
367391
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_params::f3` is reachable at visibility `pub(crate)`
368392
|
369393
note: but type `aliases_params::Priv` is only usable at visibility `pub(self)`
370-
--> $DIR/private-in-public.rs:141:5
394+
--> $DIR/private-in-public.rs:143:5
371395
|
372396
LL | struct Priv;
373397
| ^^^^^^^^^^^
374398

375-
warning: 31 warnings emitted
399+
warning: 33 warnings emitted
376400

0 commit comments

Comments
 (0)