Skip to content

Commit 7c19744

Browse files
committed
Limit caching to ADT types
1 parent f3b84c5 commit 7c19744

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

compiler/rustc_privacy/src/lib.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,17 @@ where
185185
let tcx = self.def_id_visitor.tcx();
186186
// GenericArgs are not visited here because they are visited below
187187
// in `super_visit_with`.
188-
match *ty.kind() {
188+
let ty_kind = *ty.kind();
189+
match ty_kind {
189190
ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), ..)
190191
| ty::Foreign(def_id)
191192
| ty::FnDef(def_id, ..)
192193
| ty::Closure(def_id, ..)
193194
| ty::CoroutineClosure(def_id, ..)
194195
| ty::Coroutine(def_id, ..) => {
195-
if !self.visited_tys.insert(ty) {
196+
if let ty::Adt(..) = ty_kind
197+
&& !self.visited_tys.insert(ty)
198+
{
196199
return V::Result::output();
197200
}
198201
try_visit!(self.def_id_visitor.visit_def_id(def_id, "type", &ty));
@@ -202,7 +205,7 @@ where
202205
// Default type visitor doesn't visit signatures of fn types.
203206
// Something like `fn() -> Priv {my_func}` is considered a private type even if
204207
// `my_func` is public, so we need to visit signatures.
205-
if let ty::FnDef(..) = ty.kind() {
208+
if let ty::FnDef(..) = ty_kind {
206209
// FIXME: this should probably use `args` from `FnDef`
207210
try_visit!(tcx.fn_sig(def_id).instantiate_identity().visit_with(self));
208211
}
@@ -217,9 +220,6 @@ where
217220
}
218221
}
219222
ty::Alias(kind @ (ty::Inherent | ty::Free | ty::Projection), data) => {
220-
if !self.visited_tys.insert(ty) {
221-
return V::Result::output();
222-
}
223223
if self.def_id_visitor.skip_assoc_tys() {
224224
// Visitors searching for minimal visibility/reachability want to
225225
// conservatively approximate associated types like `Type::Alias`
@@ -251,9 +251,6 @@ where
251251
};
252252
}
253253
ty::Dynamic(predicates, ..) => {
254-
if !self.visited_tys.insert(ty) {
255-
return V::Result::output();
256-
}
257254
// All traits in the list are considered the "primary" part of the type
258255
// and are visited by shallow visitors.
259256
for predicate in predicates {
@@ -290,7 +287,7 @@ where
290287
| ty::Str
291288
| ty::Never
292289
| ty::Bound(..)
293-
| ty::Param(..) => return V::Result::output(),
290+
| ty::Param(..) => {}
294291

295292
// These types don't have their own def-ids (but may have subcomponents
296293
// with def-ids that should be visited recursively).
@@ -303,11 +300,7 @@ where
303300
| ty::FnPtr(..)
304301
| ty::UnsafeBinder(_)
305302
| ty::Error(_)
306-
| ty::CoroutineWitness(..) => {
307-
if !self.visited_tys.insert(ty) {
308-
return V::Result::output();
309-
}
310-
}
303+
| ty::CoroutineWitness(..) => {}
311304
ty::Placeholder(..) | ty::Infer(..) => {
312305
bug!("unexpected type: {:?}", ty)
313306
}

tests/ui/const-generics/generic_const_exprs/eval-privacy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ where
1616
{
1717
type AssocTy = Const<{ my_const_fn(U) }>;
1818
//~^ ERROR private type
19+
//~| ERROR private type
1920
fn assoc_fn() -> Self::AssocTy {
2021
Const
2122
}

tests/ui/const-generics/generic_const_exprs/eval-privacy.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ LL | type AssocTy = Const<{ my_const_fn(U) }>;
77
LL | const fn my_const_fn(val: u8) -> u8 {
88
| ----------------------------------- `fn(u8) -> u8 {my_const_fn}` declared as private
99

10-
error: aborting due to 1 previous error
10+
error[E0446]: private type `fn(u8) -> u8 {my_const_fn}` in public interface
11+
--> $DIR/eval-privacy.rs:17:5
12+
|
13+
LL | type AssocTy = Const<{ my_const_fn(U) }>;
14+
| ^^^^^^^^^^^^ can't leak private type
15+
...
16+
LL | const fn my_const_fn(val: u8) -> u8 {
17+
| ----------------------------------- `fn(u8) -> u8 {my_const_fn}` declared as private
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
21+
error: aborting due to 2 previous errors
1122

1223
For more information about this error, try `rustc --explain E0446`.

tests/ui/privacy/where-priv-type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ where
6565
{
6666
type AssocTy = Const<{ my_const_fn(U) }>;
6767
//~^ ERROR private type
68+
//~| ERROR private type
6869
fn assoc_fn() -> Self::AssocTy {
6970
Const
7071
}

tests/ui/privacy/where-priv-type.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ LL | type AssocTy = Const<{ my_const_fn(U) }>;
7777
LL | const fn my_const_fn(val: u8) -> u8 {
7878
| ----------------------------------- `fn(u8) -> u8 {my_const_fn}` declared as private
7979

80-
error: aborting due to 1 previous error; 5 warnings emitted
80+
error[E0446]: private type `fn(u8) -> u8 {my_const_fn}` in public interface
81+
--> $DIR/where-priv-type.rs:66:5
82+
|
83+
LL | type AssocTy = Const<{ my_const_fn(U) }>;
84+
| ^^^^^^^^^^^^ can't leak private type
85+
...
86+
LL | const fn my_const_fn(val: u8) -> u8 {
87+
| ----------------------------------- `fn(u8) -> u8 {my_const_fn}` declared as private
88+
|
89+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
90+
91+
error: aborting due to 2 previous errors; 5 warnings emitted
8192

8293
For more information about this error, try `rustc --explain E0446`.

0 commit comments

Comments
 (0)