Skip to content

Commit 63849b6

Browse files
committed
remove ItemLikeVisitor impl for InherentCollect
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
1 parent c27f308 commit 63849b6

File tree

4 files changed

+84
-78
lines changed

4 files changed

+84
-78
lines changed

compiler/rustc_typeck/src/coherence/inherent_impls.rs

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
1010
use rustc_errors::struct_span_err;
1111
use rustc_hir as hir;
12+
use rustc_hir::def::DefKind;
1213
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
13-
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1414
use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams};
1515
use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt};
1616
use rustc_span::symbol::sym;
@@ -19,7 +19,9 @@ use rustc_span::Span;
1919
/// On-demand query: yields a map containing all types mapped to their inherent impls.
2020
pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls {
2121
let mut collect = InherentCollect { tcx, impls_map: Default::default() };
22-
tcx.hir().visit_all_item_likes(&mut collect);
22+
for id in tcx.hir().items() {
23+
collect.check_item(id);
24+
}
2325
collect.impls_map
2426
}
2527

@@ -46,79 +48,6 @@ struct InherentCollect<'tcx> {
4648
impls_map: CrateInherentImpls,
4749
}
4850

49-
impl<'tcx> ItemLikeVisitor<'_> for InherentCollect<'tcx> {
50-
fn visit_item(&mut self, item: &hir::Item<'_>) {
51-
let hir::ItemKind::Impl(hir::Impl { of_trait: None, self_ty: ty, ref items, .. }) = item.kind else {
52-
return;
53-
};
54-
55-
let self_ty = self.tcx.type_of(item.def_id);
56-
match *self_ty.kind() {
57-
ty::Adt(def, _) => {
58-
self.check_def_id(item, self_ty, def.did());
59-
}
60-
ty::Foreign(did) => {
61-
self.check_def_id(item, self_ty, did);
62-
}
63-
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => {
64-
self.check_def_id(item, self_ty, data.principal_def_id().unwrap());
65-
}
66-
ty::Dynamic(..) => {
67-
struct_span_err!(
68-
self.tcx.sess,
69-
ty.span,
70-
E0785,
71-
"cannot define inherent `impl` for a dyn auto trait"
72-
)
73-
.span_label(ty.span, "impl requires at least one non-auto trait")
74-
.note("define and implement a new trait or type instead")
75-
.emit();
76-
}
77-
ty::Bool
78-
| ty::Char
79-
| ty::Int(_)
80-
| ty::Uint(_)
81-
| ty::Float(_)
82-
| ty::Str
83-
| ty::Array(..)
84-
| ty::Slice(_)
85-
| ty::RawPtr(_)
86-
| ty::Ref(..)
87-
| ty::Never
88-
| ty::Tuple(..) => self.check_primitive_impl(item.def_id, self_ty, items, ty.span),
89-
ty::FnPtr(_) | ty::Projection(..) | ty::Opaque(..) | ty::Param(_) => {
90-
let mut err = struct_span_err!(
91-
self.tcx.sess,
92-
ty.span,
93-
E0118,
94-
"no nominal type found for inherent implementation"
95-
);
96-
97-
err.span_label(ty.span, "impl requires a nominal type")
98-
.note("either implement a trait on it or create a newtype to wrap it instead");
99-
100-
err.emit();
101-
}
102-
ty::FnDef(..)
103-
| ty::Closure(..)
104-
| ty::Generator(..)
105-
| ty::GeneratorWitness(..)
106-
| ty::Bound(..)
107-
| ty::Placeholder(_)
108-
| ty::Infer(_) => {
109-
bug!("unexpected impl self type of impl: {:?} {:?}", item.def_id, self_ty);
110-
}
111-
ty::Error(_) => {}
112-
}
113-
}
114-
115-
fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
116-
117-
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
118-
119-
fn visit_foreign_item(&mut self, _foreign_item: &hir::ForeignItem<'_>) {}
120-
}
121-
12251
const INTO_CORE: &str = "consider moving this inherent impl into `core` if possible";
12352
const INTO_DEFINING_CRATE: &str =
12453
"consider moving this inherent impl into the crate defining the type if possible";
@@ -246,4 +175,74 @@ impl<'tcx> InherentCollect<'tcx> {
246175
bug!("unexpected primitive type: {:?}", ty);
247176
}
248177
}
178+
179+
fn check_item(&mut self, id: hir::ItemId) {
180+
if !matches!(self.tcx.hir().def_kind(id.def_id), DefKind::Impl) {
181+
return;
182+
}
183+
184+
let item = self.tcx.hir().item(id);
185+
let hir::ItemKind::Impl(hir::Impl { of_trait: None, self_ty: ty, ref items, .. }) = item.kind else {
186+
return;
187+
};
188+
189+
let self_ty = self.tcx.type_of(item.def_id);
190+
match *self_ty.kind() {
191+
ty::Adt(def, _) => {
192+
self.check_def_id(item, self_ty, def.did());
193+
}
194+
ty::Foreign(did) => {
195+
self.check_def_id(item, self_ty, did);
196+
}
197+
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => {
198+
self.check_def_id(item, self_ty, data.principal_def_id().unwrap());
199+
}
200+
ty::Dynamic(..) => {
201+
struct_span_err!(
202+
self.tcx.sess,
203+
ty.span,
204+
E0785,
205+
"cannot define inherent `impl` for a dyn auto trait"
206+
)
207+
.span_label(ty.span, "impl requires at least one non-auto trait")
208+
.note("define and implement a new trait or type instead")
209+
.emit();
210+
}
211+
ty::Bool
212+
| ty::Char
213+
| ty::Int(_)
214+
| ty::Uint(_)
215+
| ty::Float(_)
216+
| ty::Str
217+
| ty::Array(..)
218+
| ty::Slice(_)
219+
| ty::RawPtr(_)
220+
| ty::Ref(..)
221+
| ty::Never
222+
| ty::Tuple(..) => self.check_primitive_impl(item.def_id, self_ty, items, ty.span),
223+
ty::FnPtr(_) | ty::Projection(..) | ty::Opaque(..) | ty::Param(_) => {
224+
let mut err = struct_span_err!(
225+
self.tcx.sess,
226+
ty.span,
227+
E0118,
228+
"no nominal type found for inherent implementation"
229+
);
230+
231+
err.span_label(ty.span, "impl requires a nominal type")
232+
.note("either implement a trait on it or create a newtype to wrap it instead");
233+
234+
err.emit();
235+
}
236+
ty::FnDef(..)
237+
| ty::Closure(..)
238+
| ty::Generator(..)
239+
| ty::GeneratorWitness(..)
240+
| ty::Bound(..)
241+
| ty::Placeholder(_)
242+
| ty::Infer(_) => {
243+
bug!("unexpected impl self type of impl: {:?} {:?}", item.def_id, self_ty);
244+
}
245+
ty::Error(_) => {}
246+
}
247+
}
249248
}

compiler/rustc_typeck/src/outlives/test.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ pub fn test_inferred_outlives(tcx: TyCtxt<'_>) {
88
// attribute and report an error with various results if found.
99
if tcx.has_attr(id.def_id.to_def_id(), sym::rustc_outlives) {
1010
let inferred_outlives_of = tcx.inferred_outlives_of(id.def_id);
11-
struct_span_err!(tcx.sess, tcx.hir().span(id.hir_id()), E0640, "{:?}", inferred_outlives_of).emit();
11+
struct_span_err!(
12+
tcx.sess,
13+
tcx.hir().span(id.hir_id()),
14+
E0640,
15+
"{:?}",
16+
inferred_outlives_of
17+
)
18+
.emit();
1219
}
1320
}
1421
}

src/test/ui/dep-graph/dep-graph-trait-impl-two-traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod x {
2929
mod y {
3030
use {Foo, Bar};
3131

32-
#[rustc_then_this_would_need(typeck)] //~ ERROR no path
32+
#[rustc_then_this_would_need(typeck)] //~ ERROR OK
3333
pub fn call_bar() {
3434
char::bar('a');
3535
}

src/test/ui/dep-graph/dep-graph-trait-impl-two-traits.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: no path from `x::<impl Foo for char>` to `typeck`
1+
error: OK
22
--> $DIR/dep-graph-trait-impl-two-traits.rs:32:5
33
|
44
LL | #[rustc_then_this_would_need(typeck)]

0 commit comments

Comments
 (0)