Skip to content

Commit 83bd12c

Browse files
committed
Only inspect user-written predicates for privacy concerns
1 parent 769ab55 commit 83bd12c

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

Diff for: compiler/rustc_ty_utils/src/sig_types.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub trait SpannedTypeVisitor<'tcx> {
1313
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> Self::Result;
1414
}
1515

16+
#[instrument(level = "trace", skip(tcx, visitor))]
1617
pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
1718
tcx: TyCtxt<'tcx>,
1819
item: LocalDefId,
@@ -36,7 +37,7 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
3637
for (hir, ty) in hir_sig.inputs.iter().zip(ty_sig.inputs().iter()) {
3738
try_visit!(visitor.visit(hir.span, ty.map_bound(|x| *x)));
3839
}
39-
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
40+
for (pred, span) in tcx.explicit_predicates_of(item).instantiate_identity(tcx) {
4041
try_visit!(visitor.visit(span, pred));
4142
}
4243
}
@@ -54,7 +55,7 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
5455
// Associated types in traits don't necessarily have a type that we can visit
5556
try_visit!(visitor.visit(ty.span, tcx.type_of(item).instantiate_identity()));
5657
}
57-
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
58+
for (pred, span) in tcx.explicit_predicates_of(item).instantiate_identity(tcx) {
5859
try_visit!(visitor.visit(span, pred));
5960
}
6061
}
@@ -76,7 +77,7 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
7677
let ty = field.ty(tcx, args);
7778
try_visit!(visitor.visit(span, ty));
7879
}
79-
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
80+
for (pred, span) in tcx.explicit_predicates_of(item).instantiate_identity(tcx) {
8081
try_visit!(visitor.visit(span, pred));
8182
}
8283
}
@@ -95,12 +96,12 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
9596
_ => tcx.def_span(item),
9697
};
9798
try_visit!(visitor.visit(span, tcx.type_of(item).instantiate_identity()));
98-
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
99+
for (pred, span) in tcx.explicit_predicates_of(item).instantiate_identity(tcx) {
99100
try_visit!(visitor.visit(span, pred));
100101
}
101102
}
102103
DefKind::TraitAlias | DefKind::Trait => {
103-
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
104+
for (pred, span) in tcx.explicit_predicates_of(item).instantiate_identity(tcx) {
104105
try_visit!(visitor.visit(span, pred));
105106
}
106107
}

Diff for: tests/ui/privacy/generic_struct_field_projection.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
//! To determine all the types that need to be private when looking at `Struct`, we
2-
//! invoke `predicates_of` to also look at types in `where` bounds.
2+
//! used to invoke `predicates_of` to also look at types in `where` bounds.
33
//! Unfortunately this also computes the inferred outlives bounds, which means for
44
//! every field we check that if it is of type `&'a T` then `T: 'a` and if it is of
55
//! struct type, we check that the struct satisfies its lifetime parameters by looking
66
//! at its inferred outlives bounds. This means we end up with a `<Foo as Trait>::Assoc: 'a`
77
//! in the outlives bounds of `Struct`. While this is trivially provable, privacy
8-
//! only sees `Foo` and `Trait` and determins that `Foo` is private and then errors.
8+
//! only sees `Foo` and `Trait` and determines that `Foo` is private and then errors.
9+
//! So now we invoke `explicit_predicates_of` to make sure we only care about user-written
10+
//! predicates.
11+
12+
//@ check-pass
913

1014
mod baz {
1115
struct Foo;
@@ -20,7 +24,6 @@ mod baz {
2024

2125
pub struct Bar<'a, T: Trait> {
2226
source: &'a T::Assoc,
23-
//~^ ERROR: type `Foo` is private
2427
}
2528

2629
pub struct Baz<'a> {

Diff for: tests/ui/privacy/generic_struct_field_projection.stderr

-8
This file was deleted.

0 commit comments

Comments
 (0)