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 b68fd24

Browse files
committedSep 7, 2024
privacy: normalize types before visiting
Ref #126076 (comment)
1 parent 5429bc8 commit b68fd24

File tree

7 files changed

+28
-39
lines changed

7 files changed

+28
-39
lines changed
 

‎Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -4244,10 +4244,12 @@ dependencies = [
42444244
"rustc_errors",
42454245
"rustc_fluent_macro",
42464246
"rustc_hir",
4247+
"rustc_infer",
42474248
"rustc_macros",
42484249
"rustc_middle",
42494250
"rustc_session",
42504251
"rustc_span",
4252+
"rustc_trait_selection",
42514253
"rustc_ty_utils",
42524254
"tracing",
42534255
]

‎compiler/rustc_privacy/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ rustc_data_structures = { path = "../rustc_data_structures" }
1111
rustc_errors = { path = "../rustc_errors" }
1212
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1313
rustc_hir = { path = "../rustc_hir" }
14+
rustc_infer = { path = "../rustc_infer" }
1415
rustc_macros = { path = "../rustc_macros" }
1516
rustc_middle = { path = "../rustc_middle" }
1617
rustc_session = { path = "../rustc_session" }
1718
rustc_span = { path = "../rustc_span" }
19+
rustc_trait_selection = { path = "../rustc_trait_selection" }
1820
rustc_ty_utils = { path = "../rustc_ty_utils" }
1921
tracing = "0.1"
2022
# tidy-alphabetical-end

‎compiler/rustc_privacy/src/lib.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ use rustc_session::lint;
4040
use rustc_span::hygiene::Transparency;
4141
use rustc_span::symbol::{kw, sym, Ident};
4242
use rustc_span::Span;
43+
use rustc_trait_selection::infer::TyCtxtInferExt;
44+
use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
4345
use tracing::debug;
4446
use {rustc_attr as attr, rustc_hir as hir};
4547

@@ -1309,15 +1311,9 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
13091311
self.in_primary_interface = true;
13101312
let ty = self.tcx.type_of(self.item_def_id).instantiate_identity();
13111313

1312-
// If `in_assoc_ty`, attempt to normalize `ty`.
1313-
// Ideally, we would normalize in all circumstances, but doing so
1314-
// currently causes some unexpected type errors.
1315-
let maybe_normalized_ty = if self.in_assoc_ty {
1316-
let param_env = self.tcx.param_env(self.item_def_id);
1317-
self.tcx.try_normalize_erasing_regions(param_env, ty).ok()
1318-
} else {
1319-
None
1320-
};
1314+
// Attempt to normalize `ty`
1315+
let param_env = self.tcx.param_env(self.item_def_id);
1316+
let maybe_normalized_ty = try_normalize(self.tcx, param_env, ty);
13211317

13221318
self.visit(maybe_normalized_ty.unwrap_or(ty));
13231319
self
@@ -1780,3 +1776,17 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
17801776
checker.check_item(id);
17811777
}
17821778
}
1779+
1780+
/// Attempts to deeply normalize `ty`.
1781+
fn try_normalize<'tcx>(
1782+
tcx: TyCtxt<'tcx>,
1783+
param_env: ty::ParamEnv<'tcx>,
1784+
ty: Ty<'tcx>,
1785+
) -> Result<Ty<'tcx>, ()> {
1786+
let infcx = tcx.infer_ctxt().with_next_trait_solver(true).build();
1787+
let ocx = ObligationCtxt::new(&infcx);
1788+
let cause = ObligationCause::dummy();
1789+
let Ok(ty) = ocx.deeply_normalize(&cause, param_env, ty) else { return Err(()) };
1790+
let errors = ocx.select_all_or_error();
1791+
if errors.is_empty() { Ok(ty) } else { Err(()) }
1792+
}

‎src/tools/tidy/src/issues.txt

-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,6 @@ ui/const-generics/issues/issue-80062.rs
623623
ui/const-generics/issues/issue-80375.rs
624624
ui/const-generics/issues/issue-82956.rs
625625
ui/const-generics/issues/issue-83249.rs
626-
ui/const-generics/issues/issue-83288.rs
627626
ui/const-generics/issues/issue-83466.rs
628627
ui/const-generics/issues/issue-83765.rs
629628
ui/const-generics/issues/issue-84659.rs

‎tests/ui/const-generics/issues/issue-83288.rs ‎tests/crashes/83288.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ build-pass
1+
//@ known-bug: rust-lang/rust#83288
22

33
#![allow(incomplete_features)]
44
#![feature(generic_const_exprs)]

‎tests/ui/associated-inherent-types/private-in-public.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#![crate_type = "lib"]
66

77
pub type PubAlias0 = PubTy::PrivAssocTy;
8-
//~^ WARNING associated type `PubTy::PrivAssocTy` is more private than the item `PubAlias0`
8+
99
pub type PubAlias1 = PrivTy::PubAssocTy;
10-
//~^ WARNING type `PrivTy` is more private than the item `PubAlias1`
10+
1111
pub type PubAlias2 = PubTy::PubAssocTy<PrivTy>;
1212
//~^ WARNING type `PrivTy` is more private than the item `PubAlias2`
1313

Original file line numberDiff line numberDiff line change
@@ -1,28 +1,3 @@
1-
warning: associated type `PubTy::PrivAssocTy` is more private than the item `PubAlias0`
2-
--> $DIR/private-in-public.rs:7:1
3-
|
4-
LL | pub type PubAlias0 = PubTy::PrivAssocTy;
5-
| ^^^^^^^^^^^^^^^^^^ type alias `PubAlias0` is reachable at visibility `pub`
6-
|
7-
note: but associated type `PubTy::PrivAssocTy` is only usable at visibility `pub(crate)`
8-
--> $DIR/private-in-public.rs:16:5
9-
|
10-
LL | type PrivAssocTy = ();
11-
| ^^^^^^^^^^^^^^^^
12-
= note: `#[warn(private_interfaces)]` on by default
13-
14-
warning: type `PrivTy` is more private than the item `PubAlias1`
15-
--> $DIR/private-in-public.rs:9:1
16-
|
17-
LL | pub type PubAlias1 = PrivTy::PubAssocTy;
18-
| ^^^^^^^^^^^^^^^^^^ type alias `PubAlias1` is reachable at visibility `pub`
19-
|
20-
note: but type `PrivTy` is only usable at visibility `pub(crate)`
21-
--> $DIR/private-in-public.rs:20:1
22-
|
23-
LL | struct PrivTy;
24-
| ^^^^^^^^^^^^^
25-
261
warning: type `PrivTy` is more private than the item `PubAlias2`
272
--> $DIR/private-in-public.rs:11:1
283
|
@@ -34,6 +9,7 @@ note: but type `PrivTy` is only usable at visibility `pub(crate)`
349
|
3510
LL | struct PrivTy;
3611
| ^^^^^^^^^^^^^
12+
= note: `#[warn(private_interfaces)]` on by default
3713

38-
warning: 3 warnings emitted
14+
warning: 1 warning emitted
3915

0 commit comments

Comments
 (0)
Failed to load comments.