Skip to content

Commit 4389a1c

Browse files
committed
Stop using hir_ty_to_ty in rustc_privacy
1 parent b998b51 commit 4389a1c

32 files changed

+191
-565
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4374,6 +4374,7 @@ dependencies = [
43744374
"rustc_middle",
43754375
"rustc_session",
43764376
"rustc_span",
4377+
"rustc_ty_utils",
43774378
"tracing",
43784379
]
43794380

compiler/rustc_privacy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ rustc_macros = { path = "../rustc_macros" }
1616
rustc_middle = { path = "../rustc_middle" }
1717
rustc_session = { path = "../rustc_session" }
1818
rustc_span = { path = "../rustc_span" }
19+
rustc_ty_utils = { path = "../rustc_ty_utils" }
1920
tracing = "0.1"
2021
# tidy-alphabetical-end

compiler/rustc_privacy/src/lib.rs

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hir as hir;
2121
use rustc_hir::def::{DefKind, Res};
2222
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, CRATE_DEF_ID};
2323
use rustc_hir::intravisit::{self, Visitor};
24-
use rustc_hir::{AssocItemKind, ForeignItemKind, ItemId, PatKind};
24+
use rustc_hir::{AssocItemKind, ForeignItemKind, ItemId, ItemKind, PatKind};
2525
use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility, Level};
2626
use rustc_middle::query::Providers;
2727
use rustc_middle::ty::GenericArgs;
@@ -173,6 +173,10 @@ where
173173
{
174174
type BreakTy = V::BreakTy;
175175

176+
fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
177+
self.visit_clause(p.as_clause().unwrap())
178+
}
179+
176180
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<V::BreakTy> {
177181
let tcx = self.def_id_visitor.tcx();
178182
// GenericArgs are not visited here because they are visited below
@@ -1076,6 +1080,14 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
10761080
}
10771081
}
10781082

1083+
impl<'tcx> rustc_ty_utils::sig_types::SpannedTypeVisitor<'tcx> for TypePrivacyVisitor<'tcx> {
1084+
type BreakTy = ();
1085+
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> ControlFlow<()> {
1086+
self.span = span;
1087+
value.visit_with(&mut self.skeleton())
1088+
}
1089+
}
1090+
10791091
impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
10801092
fn visit_nested_body(&mut self, body_id: hir::BodyId) {
10811093
let old_maybe_typeck_results =
@@ -1086,71 +1098,39 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
10861098

10871099
fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'tcx>) {
10881100
self.span = hir_ty.span;
1089-
if let Some(typeck_results) = self.maybe_typeck_results {
1090-
// Types in bodies.
1091-
if self.visit(typeck_results.node_type(hir_ty.hir_id)).is_break() {
1092-
return;
1093-
}
1094-
} else {
1095-
// Types in signatures.
1096-
// FIXME: This is very ineffective. Ideally each HIR type should be converted
1097-
// into a semantic type only once and the result should be cached somehow.
1098-
if self.visit(rustc_hir_analysis::hir_ty_to_ty(self.tcx, hir_ty)).is_break() {
1099-
return;
1100-
}
1101+
if self
1102+
.visit(
1103+
self.maybe_typeck_results
1104+
.unwrap_or_else(|| span_bug!(hir_ty.span, "`hir::Ty` outside of a body"))
1105+
.node_type(hir_ty.hir_id),
1106+
)
1107+
.is_break()
1108+
{
1109+
return;
11011110
}
11021111

11031112
intravisit::walk_ty(self, hir_ty);
11041113
}
11051114

11061115
fn visit_infer(&mut self, inf: &'tcx hir::InferArg) {
11071116
self.span = inf.span;
1108-
if let Some(typeck_results) = self.maybe_typeck_results {
1109-
if let Some(ty) = typeck_results.node_type_opt(inf.hir_id) {
1110-
if self.visit(ty).is_break() {
1111-
return;
1112-
}
1113-
} else {
1114-
// FIXME: check types of const infers here.
1117+
if let Some(ty) = self
1118+
.maybe_typeck_results
1119+
.unwrap_or_else(|| span_bug!(inf.span, "`hir::InferArg` outside of a body"))
1120+
.node_type_opt(inf.hir_id)
1121+
{
1122+
if self.visit(ty).is_break() {
1123+
return;
11151124
}
11161125
} else {
1117-
span_bug!(self.span, "`hir::InferArg` outside of a body");
1126+
// FIXME: check types of const infers here.
11181127
}
11191128
intravisit::walk_inf(self, inf);
11201129
}
11211130

11221131
fn visit_trait_ref(&mut self, trait_ref: &'tcx hir::TraitRef<'tcx>) {
11231132
self.span = trait_ref.path.span;
1124-
if self.maybe_typeck_results.is_some() {
1125-
// Privacy of traits in bodies is checked as a part of trait object types.
1126-
} else {
1127-
let bounds = rustc_hir_analysis::hir_trait_to_predicates(
1128-
self.tcx,
1129-
trait_ref,
1130-
// NOTE: This isn't really right, but the actual type doesn't matter here. It's
1131-
// just required by `ty::TraitRef`.
1132-
self.tcx.types.never,
1133-
);
1134-
1135-
for (clause, _) in bounds.clauses() {
1136-
match clause.kind().skip_binder() {
1137-
ty::ClauseKind::Trait(trait_predicate) => {
1138-
if self.visit_trait(trait_predicate.trait_ref).is_break() {
1139-
return;
1140-
}
1141-
}
1142-
ty::ClauseKind::Projection(proj_predicate) => {
1143-
let term = self.visit(proj_predicate.term);
1144-
if term.is_break()
1145-
|| self.visit_projection_ty(proj_predicate.projection_ty).is_break()
1146-
{
1147-
return;
1148-
}
1149-
}
1150-
_ => {}
1151-
}
1152-
}
1153-
}
1133+
// Privacy of traits in bodies is checked as a part of trait object types.
11541134

11551135
intravisit::walk_trait_ref(self, trait_ref);
11561136
}
@@ -1727,7 +1707,26 @@ fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
17271707
// inferred types of expressions and patterns.
17281708
let span = tcx.def_span(module_def_id);
17291709
let mut visitor = TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results: None, span };
1730-
tcx.hir().visit_item_likes_in_module(module_def_id, &mut visitor);
1710+
1711+
let module = tcx.hir_module_items(module_def_id);
1712+
for def_id in module.definitions() {
1713+
rustc_ty_utils::sig_types::walk_types(tcx, def_id, &mut visitor);
1714+
1715+
if let Some(body_id) = tcx.hir().maybe_body_owned_by(def_id) {
1716+
visitor.visit_nested_body(body_id);
1717+
}
1718+
}
1719+
1720+
for id in module.items() {
1721+
if let ItemKind::Impl(i) = tcx.hir().item(id).kind {
1722+
if let Some(item) = i.of_trait {
1723+
let trait_ref = tcx.impl_trait_ref(id.owner_id.def_id).unwrap();
1724+
let trait_ref = trait_ref.instantiate_identity();
1725+
visitor.span = item.path.span;
1726+
visitor.visit_def_id(trait_ref.def_id, "trait", &trait_ref.print_only_trait_path());
1727+
}
1728+
}
1729+
}
17311730
}
17321731

17331732
fn effective_visibilities(tcx: TyCtxt<'_>, (): ()) -> &EffectiveVisibilities {

compiler/rustc_ty_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mod layout_sanity_check;
3737
mod needs_drop;
3838
mod opaque_types;
3939
mod representability;
40-
mod sig_types;
40+
pub mod sig_types;
4141
mod structural_match;
4242
mod ty;
4343

compiler/rustc_ty_utils/src/sig_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::{self, TyCtxt};
88
use rustc_span::Span;
99
use rustc_type_ir::visit::TypeVisitable;
1010

11-
pub(crate) trait SpannedTypeVisitor<'tcx> {
11+
pub trait SpannedTypeVisitor<'tcx> {
1212
type BreakTy = !;
1313
fn visit(
1414
&mut self,
@@ -17,7 +17,7 @@ pub(crate) trait SpannedTypeVisitor<'tcx> {
1717
) -> ControlFlow<Self::BreakTy>;
1818
}
1919

20-
pub(crate) fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
20+
pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
2121
tcx: TyCtxt<'tcx>,
2222
item: LocalDefId,
2323
visitor: &mut V,

tests/ui/dyn-keyword/dyn-2018-edition-lint.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ fn function(x: &SomeTrait, y: Box<SomeTrait>) {
66
//~| WARN this is accepted in the current edition
77
//~| ERROR trait objects without an explicit `dyn` are deprecated
88
//~| WARN this is accepted in the current edition
9-
//~| ERROR trait objects without an explicit `dyn` are deprecated
10-
//~| WARN this is accepted in the current edition
11-
//~| ERROR trait objects without an explicit `dyn` are deprecated
12-
//~| WARN this is accepted in the current edition
13-
//~| ERROR trait objects without an explicit `dyn` are deprecated
14-
//~| WARN this is accepted in the current edition
15-
//~| ERROR trait objects without an explicit `dyn` are deprecated
16-
//~| WARN this is accepted in the current edition
179
let _x: &SomeTrait = todo!();
1810
//~^ ERROR trait objects without an explicit `dyn` are deprecated
1911
//~| WARN this is accepted in the current edition

tests/ui/dyn-keyword/dyn-2018-edition-lint.stderr

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
3030
| +++
3131

3232
error: trait objects without an explicit `dyn` are deprecated
33-
--> $DIR/dyn-2018-edition-lint.rs:17:14
33+
--> $DIR/dyn-2018-edition-lint.rs:9:14
3434
|
3535
LL | let _x: &SomeTrait = todo!();
3636
| ^^^^^^^^^
@@ -42,61 +42,5 @@ help: use `dyn`
4242
LL | let _x: &dyn SomeTrait = todo!();
4343
| +++
4444

45-
error: trait objects without an explicit `dyn` are deprecated
46-
--> $DIR/dyn-2018-edition-lint.rs:4:17
47-
|
48-
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
49-
| ^^^^^^^^^
50-
|
51-
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
52-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
53-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
54-
help: use `dyn`
55-
|
56-
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
57-
| +++
58-
59-
error: trait objects without an explicit `dyn` are deprecated
60-
--> $DIR/dyn-2018-edition-lint.rs:4:17
61-
|
62-
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
63-
| ^^^^^^^^^
64-
|
65-
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
66-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
67-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
68-
help: use `dyn`
69-
|
70-
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
71-
| +++
72-
73-
error: trait objects without an explicit `dyn` are deprecated
74-
--> $DIR/dyn-2018-edition-lint.rs:4:35
75-
|
76-
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
77-
| ^^^^^^^^^
78-
|
79-
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
80-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
81-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
82-
help: use `dyn`
83-
|
84-
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
85-
| +++
86-
87-
error: trait objects without an explicit `dyn` are deprecated
88-
--> $DIR/dyn-2018-edition-lint.rs:4:35
89-
|
90-
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
91-
| ^^^^^^^^^
92-
|
93-
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
94-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
95-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
96-
help: use `dyn`
97-
|
98-
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
99-
| +++
100-
101-
error: aborting due to 7 previous errors
45+
error: aborting due to 3 previous errors
10246

tests/ui/impl-trait/fresh-lifetime-from-bare-trait-obj-114664.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
fn ice() -> impl AsRef<Fn(&())> {
66
//~^ WARN trait objects without an explicit `dyn` are deprecated
7-
//~| WARN trait objects without an explicit `dyn` are deprecated
8-
//~| WARN trait objects without an explicit `dyn` are deprecated
9-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
10-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
117
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
128
Foo
139
}

tests/ui/impl-trait/fresh-lifetime-from-bare-trait-obj-114664.stderr

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,5 @@ help: use `dyn`
1212
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
1313
| +++
1414

15-
warning: trait objects without an explicit `dyn` are deprecated
16-
--> $DIR/fresh-lifetime-from-bare-trait-obj-114664.rs:5:24
17-
|
18-
LL | fn ice() -> impl AsRef<Fn(&())> {
19-
| ^^^^^^^
20-
|
21-
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
22-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
23-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
24-
help: use `dyn`
25-
|
26-
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
27-
| +++
28-
29-
warning: trait objects without an explicit `dyn` are deprecated
30-
--> $DIR/fresh-lifetime-from-bare-trait-obj-114664.rs:5:24
31-
|
32-
LL | fn ice() -> impl AsRef<Fn(&())> {
33-
| ^^^^^^^
34-
|
35-
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
36-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
37-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
38-
help: use `dyn`
39-
|
40-
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
41-
| +++
42-
43-
warning: 3 warnings emitted
15+
warning: 1 warning emitted
4416

tests/ui/lint/force-warn/allowed-group-warn-by-default-lint.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ pub trait SomeTrait {}
1010
pub fn function(_x: Box<SomeTrait>) {}
1111
//~^ WARN trait objects without an explicit `dyn` are deprecated
1212
//~| WARN this is accepted in the current edition
13-
//~| WARN trait objects without an explicit `dyn` are deprecated
14-
//~| WARN this is accepted in the current edition
15-
//~| WARN trait objects without an explicit `dyn` are deprecated
16-
//~| WARN this is accepted in the current edition
1713

1814
fn main() {}

tests/ui/lint/force-warn/allowed-group-warn-by-default-lint.stderr

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,5 @@ help: use `dyn`
1212
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
1313
| +++
1414

15-
warning: trait objects without an explicit `dyn` are deprecated
16-
--> $DIR/allowed-group-warn-by-default-lint.rs:10:25
17-
|
18-
LL | pub fn function(_x: Box<SomeTrait>) {}
19-
| ^^^^^^^^^
20-
|
21-
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
22-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
23-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
24-
help: use `dyn`
25-
|
26-
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
27-
| +++
28-
29-
warning: trait objects without an explicit `dyn` are deprecated
30-
--> $DIR/allowed-group-warn-by-default-lint.rs:10:25
31-
|
32-
LL | pub fn function(_x: Box<SomeTrait>) {}
33-
| ^^^^^^^^^
34-
|
35-
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
36-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
37-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
38-
help: use `dyn`
39-
|
40-
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
41-
| +++
42-
43-
warning: 3 warnings emitted
15+
warning: 1 warning emitted
4416

tests/ui/lint/force-warn/cap-lints-allow.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,5 @@ pub trait SomeTrait {}
88
pub fn function(_x: Box<SomeTrait>) {}
99
//~^ WARN trait objects without an explicit `dyn` are deprecated
1010
//~| WARN this is accepted in the current edition
11-
//~| WARN trait objects without an explicit `dyn` are deprecated
12-
//~| WARN this is accepted in the current edition
13-
//~| WARN trait objects without an explicit `dyn` are deprecated
14-
//~| WARN this is accepted in the current edition
1511

1612
fn main() {}

0 commit comments

Comments
 (0)