Skip to content

Commit 311e8d3

Browse files
committed
Remove MirVisitable.
The `MirVisitable` trait is just a complicated way to visit either a statement or a terminator. (And its impl for `Terminator` is unused.) It has a single use. This commit removes it, replacing it with an if/else, which is shorter and simpler.
1 parent cee430b commit 311e8d3

File tree

3 files changed

+18
-43
lines changed

3 files changed

+18
-43
lines changed

compiler/rustc_borrowck/src/diagnostics/find_use.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::VecDeque;
22

33
use rustc_data_structures::fx::FxIndexSet;
4-
use rustc_middle::mir::visit::{MirVisitable, PlaceContext, Visitor};
4+
use rustc_middle::mir::visit::{PlaceContext, Visitor};
55
use rustc_middle::mir::{self, Body, Local, Location};
66
use rustc_middle::ty::{RegionVid, TyCtxt};
77

@@ -45,7 +45,22 @@ impl<'a, 'tcx> UseFinder<'a, 'tcx> {
4545

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

48-
match self.def_use(p, block_data.visitable(p.statement_index)) {
48+
let mut visitor = DefUseVisitor {
49+
body: self.body,
50+
tcx: self.tcx,
51+
region_vid: self.region_vid,
52+
def_use_result: None,
53+
};
54+
55+
let is_statement = p.statement_index < block_data.statements.len();
56+
57+
if is_statement {
58+
visitor.visit_statement(&block_data.statements[p.statement_index], p);
59+
} else {
60+
visitor.visit_terminator(block_data.terminator.as_ref().unwrap(), p);
61+
}
62+
63+
match visitor.def_use_result {
4964
Some(DefUseResult::Def) => {}
5065

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

5974
None => {
60-
if p.statement_index < block_data.statements.len() {
75+
if is_statement {
6176
queue.push_back(p.successor_within_block());
6277
} else {
6378
queue.extend(
@@ -77,19 +92,6 @@ impl<'a, 'tcx> UseFinder<'a, 'tcx> {
7792

7893
None
7994
}
80-
81-
fn def_use(&self, location: Location, thing: &dyn MirVisitable<'tcx>) -> Option<DefUseResult> {
82-
let mut visitor = DefUseVisitor {
83-
body: self.body,
84-
tcx: self.tcx,
85-
region_vid: self.region_vid,
86-
def_use_result: None,
87-
};
88-
89-
thing.apply(location, &mut visitor);
90-
91-
visitor.def_use_result
92-
}
9395
}
9496

9597
struct DefUseVisitor<'a, 'tcx> {

compiler/rustc_middle/src/mir/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use tracing::{debug, trace};
3232
pub use self::query::*;
3333
use self::visit::TyContext;
3434
use crate::mir::interpret::{AllocRange, Scalar};
35-
use crate::mir::visit::MirVisitable;
3635
use crate::ty::codec::{TyDecoder, TyEncoder};
3736
use crate::ty::print::{FmtPrinter, Printer, pretty_print_const, with_no_trimmed_paths};
3837
use crate::ty::visit::TypeVisitableExt;
@@ -1364,10 +1363,6 @@ impl<'tcx> BasicBlockData<'tcx> {
13641363
self.terminator.as_mut().expect("invalid terminator state")
13651364
}
13661365

1367-
pub fn visitable(&self, index: usize) -> &dyn MirVisitable<'tcx> {
1368-
if index < self.statements.len() { &self.statements[index] } else { &self.terminator }
1369-
}
1370-
13711366
/// Does the block have no statements and an unreachable terminator?
13721367
#[inline]
13731368
pub fn is_empty_unreachable(&self) -> bool {

compiler/rustc_middle/src/mir/visit.rs

-22
Original file line numberDiff line numberDiff line change
@@ -1265,28 +1265,6 @@ macro_rules! visit_place_fns {
12651265
make_mir_visitor!(Visitor,);
12661266
make_mir_visitor!(MutVisitor, mut);
12671267

1268-
pub trait MirVisitable<'tcx> {
1269-
fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
1270-
}
1271-
1272-
impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
1273-
fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1274-
visitor.visit_statement(self, location)
1275-
}
1276-
}
1277-
1278-
impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
1279-
fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1280-
visitor.visit_terminator(self, location)
1281-
}
1282-
}
1283-
1284-
impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
1285-
fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>) {
1286-
visitor.visit_terminator(self.as_ref().unwrap(), location)
1287-
}
1288-
}
1289-
12901268
/// Extra information passed to `visit_ty` and friends to give context
12911269
/// about where the type etc appears.
12921270
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]

0 commit comments

Comments
 (0)