Skip to content

Commit d2840d2

Browse files
committed
add mapping from DefKind to Target and remove more ItemLikeVisitor impls
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
1 parent df10715 commit d2840d2

File tree

3 files changed

+60
-62
lines changed

3 files changed

+60
-62
lines changed

compiler/rustc_hir/src/target.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use crate::hir;
88
use crate::{Item, ItemKind, TraitItem, TraitItemKind};
99

10+
use crate::def::DefKind;
1011
use std::fmt::{self, Display};
1112

1213
#[derive(Copy, Clone, PartialEq, Debug)]
@@ -130,6 +131,29 @@ impl Target {
130131
}
131132
}
132133

134+
pub fn from_def_kind(def_kind: DefKind) -> Target {
135+
match def_kind {
136+
DefKind::ExternCrate => Target::ExternCrate,
137+
DefKind::Use => Target::Use,
138+
DefKind::Static(..) => Target::Static,
139+
DefKind::Const => Target::Const,
140+
DefKind::Fn => Target::Fn,
141+
DefKind::Macro(..) => Target::MacroDef,
142+
DefKind::Mod => Target::Mod,
143+
DefKind::ForeignMod => Target::ForeignMod,
144+
DefKind::GlobalAsm => Target::GlobalAsm,
145+
DefKind::TyAlias => Target::TyAlias,
146+
DefKind::OpaqueTy => Target::OpaqueTy,
147+
DefKind::Enum => Target::Enum,
148+
DefKind::Struct => Target::Struct,
149+
DefKind::Union => Target::Union,
150+
DefKind::Trait => Target::Trait,
151+
DefKind::TraitAlias => Target::TraitAlias,
152+
DefKind::Impl => Target::Impl,
153+
_ => panic!("impossible case reached"),
154+
}
155+
}
156+
133157
pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
134158
match trait_item.kind {
135159
TraitItemKind::Const(..) => Target::AssocConst,

compiler/rustc_passes/src/lang_items.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::weak_lang_items;
1212

1313
use rustc_errors::{pluralize, struct_span_err};
1414
use rustc_hir as hir;
15+
use rustc_hir::def::DefKind;
1516
use rustc_hir::def_id::DefId;
16-
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1717
use rustc_hir::lang_items::{extract, GenericRequirement, ITEM_REFS};
1818
use rustc_hir::{HirId, LangItem, LanguageItems, Target};
1919
use rustc_middle::ty::TyCtxt;
@@ -27,28 +27,6 @@ struct LanguageItemCollector<'tcx> {
2727
tcx: TyCtxt<'tcx>,
2828
}
2929

30-
impl<'v, 'tcx> ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> {
31-
fn visit_item(&mut self, item: &hir::Item<'_>) {
32-
self.check_for_lang(Target::from_item(item), item.hir_id());
33-
34-
if let hir::ItemKind::Enum(def, ..) = &item.kind {
35-
for variant in def.variants {
36-
self.check_for_lang(Target::Variant, variant.id);
37-
}
38-
}
39-
}
40-
41-
fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
42-
self.check_for_lang(Target::from_trait_item(trait_item), trait_item.hir_id())
43-
}
44-
45-
fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
46-
self.check_for_lang(target_from_impl_item(self.tcx, impl_item), impl_item.hir_id())
47-
}
48-
49-
fn visit_foreign_item(&mut self, _: &hir::ForeignItem<'_>) {}
50-
}
51-
5230
impl<'tcx> LanguageItemCollector<'tcx> {
5331
fn new(tcx: TyCtxt<'tcx>) -> LanguageItemCollector<'tcx> {
5432
LanguageItemCollector { tcx, items: LanguageItems::new() }
@@ -262,18 +240,28 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
262240
let crate_items = tcx.hir_crate_items(());
263241

264242
for id in crate_items.items() {
265-
let item = tcx.hir().item(id);
266-
collector.visit_item(item);
243+
collector.check_for_lang(Target::from_def_kind(tcx.hir().def_kind(id.def_id)), id.hir_id());
244+
245+
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Enum) {
246+
let item = tcx.hir().item(id);
247+
if let hir::ItemKind::Enum(def, ..) = &item.kind {
248+
for variant in def.variants {
249+
collector.check_for_lang(Target::Variant, variant.id);
250+
}
251+
}
252+
}
267253
}
268254

255+
// FIXME: avoid calling trait_item() when possible
269256
for id in crate_items.trait_items() {
270257
let item = tcx.hir().trait_item(id);
271-
collector.visit_trait_item(item);
258+
collector.check_for_lang(Target::from_trait_item(item), item.hir_id())
272259
}
273260

261+
// FIXME: avoid calling impl_item() when possible
274262
for id in crate_items.impl_items() {
275263
let item = tcx.hir().impl_item(id);
276-
collector.visit_impl_item(item);
264+
collector.check_for_lang(target_from_impl_item(tcx, item), item.hir_id())
277265
}
278266

279267
// Extract out the found lang items.

compiler/rustc_passes/src/weak_lang_items.rs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@
22
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_errors::struct_span_err;
5-
use rustc_hir as hir;
6-
use rustc_hir::intravisit::{self, Visitor};
75
use rustc_hir::lang_items::{self, LangItem};
86
use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
97
use rustc_middle::middle::lang_items::required;
108
use rustc_middle::ty::TyCtxt;
119
use rustc_session::config::CrateType;
12-
use rustc_span::symbol::Symbol;
13-
use rustc_span::Span;
14-
15-
struct Context<'a, 'tcx> {
16-
tcx: TyCtxt<'tcx>,
17-
items: &'a mut lang_items::LanguageItems,
18-
}
1910

2011
/// Checks the crate for usage of weak lang items, returning a vector of all the
2112
/// language items required by this crate, but not defined yet.
@@ -30,10 +21,28 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItem
3021
items.missing.push(LangItem::EhCatchTypeinfo);
3122
}
3223

33-
{
34-
let mut cx = Context { tcx, items };
35-
tcx.hir().visit_all_item_likes(&mut cx.as_deep_visitor());
24+
let crate_items = tcx.hir_crate_items(());
25+
for id in crate_items.foreign_items() {
26+
let attrs = tcx.hir().attrs(id.hir_id());
27+
let span = tcx.hir().span(id.hir_id());
28+
if let Some((lang_item, _)) = lang_items::extract(attrs) {
29+
if let Some(&item) = WEAK_ITEMS_REFS.get(&lang_item) {
30+
if items.require(item).is_err() {
31+
items.missing.push(item);
32+
}
33+
} else {
34+
struct_span_err!(
35+
tcx.sess,
36+
span,
37+
E0264,
38+
"unknown external lang item: `{}`",
39+
lang_item
40+
)
41+
.emit();
42+
}
43+
}
3644
}
45+
3746
verify(tcx, items);
3847
}
3948

@@ -80,26 +89,3 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
8089
}
8190
}
8291
}
83-
84-
impl<'a, 'tcx> Context<'a, 'tcx> {
85-
fn register(&mut self, name: Symbol, span: Span) {
86-
if let Some(&item) = WEAK_ITEMS_REFS.get(&name) {
87-
if self.items.require(item).is_err() {
88-
self.items.missing.push(item);
89-
}
90-
} else {
91-
struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name)
92-
.emit();
93-
}
94-
}
95-
}
96-
97-
impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
98-
fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
99-
let attrs = self.tcx.hir().attrs(i.hir_id());
100-
if let Some((lang_item, _)) = lang_items::extract(attrs) {
101-
self.register(lang_item, i.span);
102-
}
103-
intravisit::walk_foreign_item(self, i)
104-
}
105-
}

0 commit comments

Comments
 (0)