remove HirVisitor

Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
This commit is contained in:
Miguel Guarniz 2022-05-07 15:27:50 -04:00
parent 885b90bb34
commit 0b7dd95475
2 changed files with 31 additions and 43 deletions

View File

@ -16,7 +16,7 @@
fn check_for_debugger_visualizer<'tcx>(
tcx: TyCtxt<'tcx>,
hir_id: HirId,
debugger_visualizers: &mut FxHashSet<DebuggerVisualizerFile>
debugger_visualizers: &mut FxHashSet<DebuggerVisualizerFile>,
) {
let attrs = tcx.hir().attrs(hir_id);
for attr in attrs {
@ -51,8 +51,7 @@ fn check_for_debugger_visualizer<'tcx>(
let contents = match std::fs::read(&file) {
Ok(contents) => contents,
Err(err) => {
tcx
.sess
tcx.sess
.struct_span_err(
attr.span,
&format!(
@ -71,12 +70,8 @@ fn check_for_debugger_visualizer<'tcx>(
DebuggerVisualizerType::Natvis,
));
} else {
tcx
.sess
.struct_span_err(
attr.span,
&format!("{} is not a valid file", file.display()),
)
tcx.sess
.struct_span_err(attr.span, &format!("{} is not a valid file", file.display()))
.emit();
}
}
@ -101,7 +96,6 @@ fn debugger_visualizers<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> Vec<Debugger
// Collect debugger visualizers on the crate attributes.
check_for_debugger_visualizer(tcx, CRATE_HIR_ID, &mut debugger_visualizers);
// Extract out the found debugger_visualizer items.
let mut visualizers = debugger_visualizers.into_iter().collect::<Vec<_>>();

View File

@ -21,7 +21,7 @@
use rustc_borrowck::consumers::BodyWithBorrowckFacts;
use rustc_driver::Compilation;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::def::DefKind;
use rustc_interface::interface::Compiler;
use rustc_interface::{Config, Queries};
use rustc_middle::ty::query::query_values::mir_borrowck;
@ -65,11 +65,34 @@ fn after_analysis<'tcx>(
queries.global_ctxt().unwrap().peek_mut().enter(|tcx| {
// Collect definition ids of MIR bodies.
let hir = tcx.hir();
let mut visitor = HirVisitor { bodies: Vec::new() };
hir.visit_all_item_likes(&mut visitor);
let mut bodies = Vec::new();
let crate_items = tcx.hir_crate_items(());
for id in crate_items.items() {
if matches!(tcx.def_kind(id.def_id), DefKind::Fn) {
bodies.push(id.def_id);
}
}
for id in crate_items.trait_items() {
if matches!(tcx.def_kind(id.def_id), DefKind::AssocFn) {
let trait_item = hir.trait_item(id);
if let rustc_hir::TraitItemKind::Fn(_, trait_fn) = &trait_item.kind {
if let rustc_hir::TraitFn::Provided(_) = trait_fn {
bodies.push(trait_item.def_id);
}
}
}
}
for id in crate_items.impl_items() {
if matches!(tcx.def_kind(id.def_id), DefKind::AssocFn) {
bodies.push(id.def_id);
}
}
// Trigger borrow checking of all bodies.
for def_id in visitor.bodies {
for def_id in bodies {
let _ = tcx.optimized_mir(def_id);
}
@ -121,35 +144,6 @@ fn mir_borrowck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> mir_borrowck<'tc
original_mir_borrowck(tcx, def_id)
}
/// Visitor that collects all body definition ids mentioned in the program.
struct HirVisitor {
bodies: Vec<LocalDefId>,
}
impl<'tcx> ItemLikeVisitor<'tcx> for HirVisitor {
fn visit_item(&mut self, item: &rustc_hir::Item) {
if let rustc_hir::ItemKind::Fn(..) = item.kind {
self.bodies.push(item.def_id);
}
}
fn visit_trait_item(&mut self, trait_item: &rustc_hir::TraitItem) {
if let rustc_hir::TraitItemKind::Fn(_, trait_fn) = &trait_item.kind {
if let rustc_hir::TraitFn::Provided(_) = trait_fn {
self.bodies.push(trait_item.def_id);
}
}
}
fn visit_impl_item(&mut self, impl_item: &rustc_hir::ImplItem) {
if let rustc_hir::ImplItemKind::Fn(..) = impl_item.kind {
self.bodies.push(impl_item.def_id);
}
}
fn visit_foreign_item(&mut self, _foreign_item: &rustc_hir::ForeignItem) {}
}
/// Pull MIR bodies stored in the thread-local.
fn get_bodies<'tcx>(tcx: TyCtxt<'tcx>) -> Vec<(String, BodyWithBorrowckFacts<'tcx>)> {
MIR_BODIES.with(|state| {