Skip to content
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

MIR visitor tweaks #137266

Merged
merged 3 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions compiler/rustc_borrowck/src/diagnostics/find_use.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::VecDeque;

use rustc_data_structures::fx::FxIndexSet;
use rustc_middle::mir::visit::{MirVisitable, PlaceContext, Visitor};
use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_middle::mir::{self, Body, Local, Location};
use rustc_middle::ty::{RegionVid, TyCtxt};

Expand Down Expand Up @@ -45,7 +45,22 @@ impl<'a, 'tcx> UseFinder<'a, 'tcx> {

let block_data = &self.body[p.block];

match self.def_use(p, block_data.visitable(p.statement_index)) {
let mut visitor = DefUseVisitor {
body: self.body,
tcx: self.tcx,
region_vid: self.region_vid,
def_use_result: None,
};

let is_statement = p.statement_index < block_data.statements.len();

if is_statement {
visitor.visit_statement(&block_data.statements[p.statement_index], p);
} else {
visitor.visit_terminator(block_data.terminator.as_ref().unwrap(), p);
}

match visitor.def_use_result {
Some(DefUseResult::Def) => {}

Some(DefUseResult::UseLive { local }) => {
Expand All @@ -57,7 +72,7 @@ impl<'a, 'tcx> UseFinder<'a, 'tcx> {
}

None => {
if p.statement_index < block_data.statements.len() {
if is_statement {
queue.push_back(p.successor_within_block());
} else {
queue.extend(
Expand All @@ -77,19 +92,6 @@ impl<'a, 'tcx> UseFinder<'a, 'tcx> {

None
}

fn def_use(&self, location: Location, thing: &dyn MirVisitable<'tcx>) -> Option<DefUseResult> {
let mut visitor = DefUseVisitor {
body: self.body,
tcx: self.tcx,
region_vid: self.region_vid,
def_use_result: None,
};

thing.apply(location, &mut visitor);

visitor.def_use_result
}
}

struct DefUseVisitor<'a, 'tcx> {
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use tracing::{debug, trace};
pub use self::query::*;
use self::visit::TyContext;
use crate::mir::interpret::{AllocRange, Scalar};
use crate::mir::visit::MirVisitable;
use crate::ty::codec::{TyDecoder, TyEncoder};
use crate::ty::print::{FmtPrinter, Printer, pretty_print_const, with_no_trimmed_paths};
use crate::ty::visit::TypeVisitableExt;
Expand Down Expand Up @@ -1364,10 +1363,6 @@ impl<'tcx> BasicBlockData<'tcx> {
self.terminator.as_mut().expect("invalid terminator state")
}

pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
if index < self.statements.len() { &self.statements[index] } else { &self.terminator }
}

/// Does the block have no statements and an unreachable terminator?
#[inline]
pub fn is_empty_unreachable(&self) -> bool {
Expand Down
Loading
Loading