-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Refactor HIR item-like traversal (part 1) #95655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b73b4de
0baf85e
28aa2dd
0d01ee9
3d6f4c8
df10715
d2840d2
f9781fd
cb10a9a
e2512f7
1c9ddd2
7ea034a
51ee3d4
f983d26
0b38596
a349fc4
a31632b
88108bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use crate::hir; | ||
use crate::{Item, ItemKind, TraitItem, TraitItemKind}; | ||
|
||
use crate::def::DefKind; | ||
use std::fmt::{self, Display}; | ||
|
||
#[derive(Copy, Clone, PartialEq, Debug)] | ||
|
@@ -130,6 +131,30 @@ impl Target { | |
} | ||
} | ||
|
||
// FIXME: For now, should only be used with def_kinds from ItemIds | ||
pub fn from_def_kind(def_kind: DefKind) -> Target { | ||
match def_kind { | ||
DefKind::ExternCrate => Target::ExternCrate, | ||
DefKind::Use => Target::Use, | ||
DefKind::Static(..) => Target::Static, | ||
DefKind::Const => Target::Const, | ||
DefKind::Fn => Target::Fn, | ||
DefKind::Macro(..) => Target::MacroDef, | ||
DefKind::Mod => Target::Mod, | ||
DefKind::ForeignMod => Target::ForeignMod, | ||
DefKind::GlobalAsm => Target::GlobalAsm, | ||
DefKind::TyAlias => Target::TyAlias, | ||
DefKind::OpaqueTy => Target::OpaqueTy, | ||
DefKind::Enum => Target::Enum, | ||
DefKind::Struct => Target::Struct, | ||
DefKind::Union => Target::Union, | ||
DefKind::Trait => Target::Trait, | ||
DefKind::TraitAlias => Target::TraitAlias, | ||
DefKind::Impl => Target::Impl, | ||
_ => panic!("impossible case reached"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it impossible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, this is an oversight. If this is used with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a comment so that users are aware of this until we merge |
||
} | ||
} | ||
|
||
pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target { | ||
match trait_item.kind { | ||
TraitItemKind::Const(..) => Target::AssocConst, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,19 @@ | ||
use rustc_hir as hir; | ||
use rustc_hir::itemlikevisit::ItemLikeVisitor; | ||
use rustc_hir::def::DefKind; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_session::cstore::ForeignModule; | ||
|
||
crate fn collect(tcx: TyCtxt<'_>) -> Vec<ForeignModule> { | ||
let mut collector = Collector { modules: Vec::new() }; | ||
tcx.hir().visit_all_item_likes(&mut collector); | ||
collector.modules | ||
} | ||
|
||
struct Collector { | ||
modules: Vec<ForeignModule>, | ||
} | ||
|
||
impl<'tcx> ItemLikeVisitor<'tcx> for Collector { | ||
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) { | ||
let hir::ItemKind::ForeignMod { items, .. } = it.kind else { | ||
return; | ||
}; | ||
|
||
let foreign_items = items.iter().map(|it| it.id.def_id.to_def_id()).collect(); | ||
self.modules.push(ForeignModule { foreign_items, def_id: it.def_id.to_def_id() }); | ||
let mut modules = Vec::new(); | ||
for id in tcx.hir().items() { | ||
if !matches!(tcx.hir().def_kind(id.def_id), DefKind::ForeignMod) { | ||
continue; | ||
} | ||
let item = tcx.hir().item(id); | ||
if let hir::ItemKind::ForeignMod { items, .. } = item.kind { | ||
let foreign_items = items.iter().map(|it| it.id.def_id.to_def_id()).collect(); | ||
modules.push(ForeignModule { foreign_items, def_id: id.def_id.to_def_id() }); | ||
} | ||
} | ||
|
||
fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem<'tcx>) {} | ||
fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem<'tcx>) {} | ||
fn visit_foreign_item(&mut self, _it: &'tcx hir::ForeignItem<'tcx>) {} | ||
modules | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great idea! I wonder if we should somehow merge
DefKind
andTarget
in a follow-up PR.