Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cc36e1e

Browse files
committedJun 6, 2024
privacy: normalize associated types before visiting
This permits associated types to reference private types and traits, so long as the normalized type does not itself violate type privacy. Fixes #45713
1 parent b14d8b2 commit cc36e1e

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed
 

‎compiler/rustc_privacy/src/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,19 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
12981298

12991299
fn ty(&mut self) -> &mut Self {
13001300
self.in_primary_interface = true;
1301-
self.visit(self.tcx.type_of(self.item_def_id).instantiate_identity());
1301+
let ty = self.tcx.type_of(self.item_def_id).instantiate_identity();
1302+
1303+
// If `in_assoc_ty`, attempt to normalize `ty`.
1304+
// Ideally, we would normalize in all circumstances, but doing so
1305+
// currently causes some unexpected type errors.
1306+
let maybe_normalized_ty = if self.in_assoc_ty {
1307+
let param_env = self.tcx.param_env(self.item_def_id);
1308+
self.tcx.try_normalize_erasing_regions(param_env, ty).ok()
1309+
} else {
1310+
None
1311+
};
1312+
1313+
self.visit(maybe_normalized_ty.unwrap_or(ty));
13021314
self
13031315
}
13041316

‎tests/ui/privacy/projections2.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ mod m {
1717

1818
impl Trait4 for u8 {
1919
type A<T: Trait> = <u8 as Trait3>::A<T>;
20-
//~^ ERROR: private associated type `Trait3::A` in public interface
21-
//~| ERROR: private trait `Trait3` in public interface
20+
//~^ ERROR: private type `Priv` in public interface
2221
}
2322
}
2423

‎tests/ui/privacy/projections2.stderr

+5-14
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,15 @@ LL | struct Priv;
1111
| ^^^^^^^^^^^
1212
= note: `#[warn(private_interfaces)]` on by default
1313

14-
error[E0446]: private associated type `Trait3::A` in public interface
14+
error[E0446]: private type `Priv` in public interface
1515
--> $DIR/projections2.rs:19:9
1616
|
17-
LL | type A<T: Trait>;
18-
| ---------------- `Trait3::A` declared as private
19-
...
20-
LL | type A<T: Trait> = <u8 as Trait3>::A<T>;
21-
| ^^^^^^^^^^^^^^^^ can't leak private associated type
22-
23-
error[E0446]: private trait `Trait3` in public interface
24-
--> $DIR/projections2.rs:19:9
25-
|
26-
LL | trait Trait3 {
27-
| ------------ `Trait3` declared as private
17+
LL | struct Priv;
18+
| ----------- `Priv` declared as private
2819
...
2920
LL | type A<T: Trait> = <u8 as Trait3>::A<T>;
30-
| ^^^^^^^^^^^^^^^^ can't leak private trait
21+
| ^^^^^^^^^^^^^^^^ can't leak private type
3122

32-
error: aborting due to 2 previous errors; 1 warning emitted
23+
error: aborting due to 1 previous error; 1 warning emitted
3324

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

0 commit comments

Comments
 (0)
Failed to load comments.