|
| 1 | +use rustc_hir::def_id::DefId; |
| 2 | +use rustc_hir::{HirId, HirIdSet}; |
| 3 | +use rustc_middle::hir::place::{PlaceBase, PlaceWithHirId}; |
| 4 | +use rustc_middle::mir::FakeReadCause; |
| 5 | +use rustc_middle::query::Providers; |
| 6 | +use rustc_middle::ty::{self, TyCtxt}; |
| 7 | +use tracing::instrument; |
| 8 | + |
| 9 | +use crate::expr_use_visitor::{Delegate, ExprUseVisitor}; |
| 10 | + |
| 11 | +#[derive(Default)] |
| 12 | +struct ExtractConsumingNodeDelegate { |
| 13 | + nodes: HirIdSet, |
| 14 | +} |
| 15 | + |
| 16 | +impl<'tcx> Delegate<'tcx> for ExtractConsumingNodeDelegate { |
| 17 | + #[instrument(level = "debug", skip(self))] |
| 18 | + fn consume(&mut self, place_with_id: &PlaceWithHirId<'tcx>, _: HirId) { |
| 19 | + match place_with_id.place.base { |
| 20 | + PlaceBase::Rvalue => { |
| 21 | + self.nodes.insert(place_with_id.hir_id); |
| 22 | + } |
| 23 | + PlaceBase::Local(id) => { |
| 24 | + self.nodes.insert(id); |
| 25 | + } |
| 26 | + PlaceBase::Upvar(upvar) => { |
| 27 | + self.nodes.insert(upvar.var_path.hir_id); |
| 28 | + } |
| 29 | + PlaceBase::StaticItem => {} |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + fn borrow( |
| 34 | + &mut self, |
| 35 | + _place_with_id: &PlaceWithHirId<'tcx>, |
| 36 | + _diag_expr_id: HirId, |
| 37 | + _bk: ty::BorrowKind, |
| 38 | + ) { |
| 39 | + // do nothing |
| 40 | + } |
| 41 | + |
| 42 | + fn mutate(&mut self, _assignee_place: &PlaceWithHirId<'tcx>, _diag_expr_id: HirId) { |
| 43 | + // do nothing |
| 44 | + } |
| 45 | + |
| 46 | + fn fake_read( |
| 47 | + &mut self, |
| 48 | + _place_with_id: &PlaceWithHirId<'tcx>, |
| 49 | + _cause: FakeReadCause, |
| 50 | + _diag_expr_id: HirId, |
| 51 | + ) { |
| 52 | + // do nothing |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn extract_tail_expr_consuming_nodes<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx HirIdSet { |
| 57 | + let hir = tcx.hir(); |
| 58 | + let body = hir.body_owned_by(def_id.expect_local()); |
| 59 | + let mut delegate = ExtractConsumingNodeDelegate::default(); |
| 60 | + let euv = ExprUseVisitor::new((tcx, def_id.expect_local()), &mut delegate); |
| 61 | + let _ = euv.walk_expr(body.value); |
| 62 | + tcx.arena.alloc(delegate.nodes) |
| 63 | +} |
| 64 | + |
| 65 | +pub(crate) fn provide(providers: &mut Providers) { |
| 66 | + providers.extract_tail_expr_consuming_nodes = extract_tail_expr_consuming_nodes; |
| 67 | +} |
0 commit comments