Skip to content

Commit 0afdf43

Browse files
committed
Auto merge of #70449 - ecstatic-morse:visit-body, r=oli-obk
Make `Visitor::visit_body` take a plain `&Body` `ReadOnlyBodyAndCache` has replaced `&Body` in many parts of the code base that don't care about basic block predecessors. This includes the MIR `Visitor` trait, which I suspect resulted in many unnecessary changes in #64736. This reverts part of that PR to reduce the number of places where we need to pass a `ReadOnlyBodyAndCache`. In the long term, we should either give `ReadOnlyBodyAndCache` more ergonomic name and replace all uses of `&mir::Body` with it at the cost of carrying an extra pointer everywhere, or use it only in places that actually need access to the predecessor cache. Perhaps there is an even nicer alternative. r? @Nashenas88
2 parents 4911572 + 538cdef commit 0afdf43

File tree

21 files changed

+33
-34
lines changed

21 files changed

+33
-34
lines changed

src/librustc/mir/visit.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ use rustc_span::Span;
6565
// variant argument) that does not require visiting, as in
6666
// `is_cleanup` above.
6767

68-
macro_rules! body_cache_type {
69-
(mut $a:lifetime, $tcx:lifetime) => {
68+
macro_rules! body_type {
69+
(mut $tcx:lifetime) => {
7070
&mut BodyAndCache<$tcx>
7171
};
72-
($a:lifetime, $tcx:lifetime) => {
73-
ReadOnlyBodyAndCache<$a, $tcx>
72+
($tcx:lifetime) => {
73+
&Body<$tcx>
7474
};
7575
}
7676

@@ -82,7 +82,7 @@ macro_rules! make_mir_visitor {
8282

8383
fn visit_body(
8484
&mut self,
85-
body: body_cache_type!($($mutability)? '_, 'tcx)
85+
body: body_type!($($mutability)? 'tcx)
8686
) {
8787
self.super_body(body);
8888
}
@@ -254,7 +254,7 @@ macro_rules! make_mir_visitor {
254254

255255
fn super_body(
256256
&mut self,
257-
$($mutability)? body: body_cache_type!($($mutability)? '_, 'tcx)
257+
$($mutability)? body: body_type!($($mutability)? 'tcx)
258258
) {
259259
let span = body.span;
260260
if let Some(yield_ty) = &$($mutability)? body.yield_ty {
@@ -819,7 +819,7 @@ macro_rules! make_mir_visitor {
819819

820820
fn visit_location(
821821
&mut self,
822-
body: body_cache_type!($($mutability)? '_, 'tcx),
822+
body: body_type!($($mutability)? 'tcx),
823823
location: Location
824824
) {
825825
let basic_block = & $($mutability)? body[location.block];

src/librustc_codegen_ssa/mir/analyze.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
2020
let mir = fx.mir;
2121
let mut analyzer = LocalAnalyzer::new(fx);
2222

23-
analyzer.visit_body(mir);
23+
analyzer.visit_body(&mir);
2424

2525
for (local, decl) in mir.local_decls.iter_enumerated() {
2626
let ty = fx.monomorphize(&decl.ty);

src/librustc_mir/borrow_check/borrow_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl LocalsStateAtExit {
107107
LocalsStateAtExit::AllAreInvalidated
108108
} else {
109109
let mut has_storage_dead = HasStorageDead(BitSet::new_empty(body.local_decls.len()));
110-
has_storage_dead.visit_body(body);
110+
has_storage_dead.visit_body(&body);
111111
let mut has_storage_dead_or_moved = has_storage_dead.0;
112112
for move_out in &move_data.moves {
113113
if let Some(index) = move_data.base_local(move_out.path) {

src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15591559
}
15601560
}
15611561
let mut visitor = FakeReadCauseFinder { place, cause: None };
1562-
visitor.visit_body(self.body);
1562+
visitor.visit_body(&self.body);
15631563
match visitor.cause {
15641564
Some(FakeReadCause::ForMatchGuard) => Some("match guard"),
15651565
Some(FakeReadCause::ForIndex) => Some("indexing expression"),

src/librustc_mir/borrow_check/invalidation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(super) fn generate_invalidates<'tcx>(
3737
body: &body,
3838
dominators,
3939
};
40-
ig.visit_body(body);
40+
ig.visit_body(&body);
4141
}
4242
}
4343

src/librustc_mir/borrow_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ fn do_mir_borrowck<'a, 'tcx>(
299299
}
300300

301301
dataflow::visit_results(
302-
&*body,
303-
traversal::reverse_postorder(&*body).map(|(bb, _)| bb),
302+
&body,
303+
traversal::reverse_postorder(&body).map(|(bb, _)| bb),
304304
&results,
305305
&mut mbcx,
306306
);

src/librustc_mir/borrow_check/type_check/liveness/local_use_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl LocalUseMap {
8181
live_locals.iter().for_each(|&local| locals_with_use_data[local] = true);
8282

8383
LocalUseMapBuild { local_use_map: &mut local_use_map, elements, locals_with_use_data }
84-
.visit_body(body);
84+
.visit_body(&body);
8585

8686
local_use_map
8787
}

src/librustc_mir/borrow_check/type_check/liveness/polonius.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub(super) fn populate_access_facts(
101101
location_table,
102102
move_data,
103103
};
104-
extractor.visit_body(body);
104+
extractor.visit_body(&body);
105105

106106
facts.var_dropped_at.extend(
107107
dropped_at.iter().map(|&(local, location)| (local, location_table.mid_index(location))),

src/librustc_mir/borrow_check/type_check/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ fn type_check_internal<'a, 'tcx, R>(
210210
);
211211
let errors_reported = {
212212
let mut verifier = TypeVerifier::new(&mut checker, *body, promoted);
213-
verifier.visit_body(body);
213+
verifier.visit_body(&body);
214214
verifier.errors_reported
215215
};
216216

@@ -435,7 +435,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
435435
}
436436
}
437437

438-
fn visit_body(&mut self, body: ReadOnlyBodyAndCache<'_, 'tcx>) {
438+
fn visit_body(&mut self, body: &Body<'tcx>) {
439439
self.sanitize_type(&"return type", body.return_ty());
440440
for local_decl in &body.local_decls {
441441
self.sanitize_type(local_decl, local_decl.ty);
@@ -563,7 +563,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
563563

564564
swap_constraints(self);
565565

566-
self.visit_body(promoted_body);
566+
self.visit_body(&promoted_body);
567567

568568
if !self.errors_reported {
569569
// if verifier failed, don't do further checks to avoid ICEs

src/librustc_mir/borrow_check/used_muts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
3232
never_initialized_mut_locals: &mut never_initialized_mut_locals,
3333
mbcx: self,
3434
};
35-
visitor.visit_body(visitor.mbcx.body);
35+
visitor.visit_body(&visitor.mbcx.body);
3636
}
3737

3838
// Take the union of the existed `used_mut` set with those variables we've found were

src/librustc_mir/const_eval/eval_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ pub fn const_eval_raw_provider<'tcx>(
307307
);
308308

309309
let res = ecx.load_mir(cid.instance.def, cid.promoted);
310-
res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, *body))
310+
res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, &body))
311311
.and_then(|place| {
312312
Ok(RawConst { alloc_id: place.ptr.assert_ptr().alloc_id, ty: place.layout.ty })
313313
})

src/librustc_mir/dataflow/impls/storage_liveness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'mir, 'tcx> MaybeRequiresStorage<'mir, 'tcx> {
8383
) -> Self {
8484
MaybeRequiresStorage {
8585
body,
86-
borrowed_locals: RefCell::new(ResultsRefCursor::new(*body, borrowed_locals)),
86+
borrowed_locals: RefCell::new(ResultsRefCursor::new(&body, borrowed_locals)),
8787
}
8888
}
8989
}
@@ -250,7 +250,7 @@ impl<'mir, 'tcx> MaybeRequiresStorage<'mir, 'tcx> {
250250
/// Kill locals that are fully moved and have not been borrowed.
251251
fn check_for_move(&self, trans: &mut impl GenKill<Local>, loc: Location) {
252252
let mut visitor = MoveVisitor { trans, borrowed_locals: &self.borrowed_locals };
253-
visitor.visit_location(self.body, loc);
253+
visitor.visit_location(&self.body, loc);
254254
}
255255
}
256256

src/librustc_mir/monomorphize/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ fn collect_neighbours<'tcx>(
11621162
debug!("collect_neighbours: {:?}", instance.def_id());
11631163
let body = tcx.instance_mir(instance.def);
11641164

1165-
MirNeighborCollector { tcx, body: &body, output, instance }.visit_body(body);
1165+
MirNeighborCollector { tcx, body: &body, output, instance }.visit_body(&body);
11661166
}
11671167

11681168
fn def_id_to_string(tcx: TyCtxt<'_>, def_id: DefId) -> String {

src/librustc_mir/transform/check_consts/validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Validator<'a, 'mir, 'tcx> {
183183
self.check_op_spanned(ops::Loop, body.span);
184184
}
185185

186-
self.visit_body(body);
186+
self.visit_body(&body);
187187

188188
// Ensure that the end result is `Sync` in a non-thread local `static`.
189189
let should_check_for_sync =

src/librustc_mir/transform/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: DefId) -> UnsafetyCheckResult
507507
// mir_built ensures that body has a computed cache, so we don't (and can't) attempt to
508508
// recompute it here.
509509
let body = body.unwrap_read_only();
510-
checker.visit_body(body);
510+
checker.visit_body(&body);
511511

512512
check_unused_unsafe(tcx, def_id, &checker.used_unsafe, &mut checker.inherited_blocks);
513513
UnsafetyCheckResult {

src/librustc_mir/transform/const_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ impl CanConstProp {
778778
trace!("local {:?} can't be const propagated because it's not a temporary", local);
779779
}
780780
}
781-
cpv.visit_body(body);
781+
cpv.visit_body(&body);
782782
cpv.can_const_prop
783783
}
784784
}

src/librustc_mir/transform/generator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ fn locals_live_across_suspend_points(
469469
// Find the MIR locals which do not use StorageLive/StorageDead statements.
470470
// The storage of these locals are always live.
471471
let mut ignored = StorageIgnored(BitSet::new_filled(body.local_decls.len()));
472-
ignored.visit_body(body);
472+
ignored.visit_body(&body);
473473

474474
// Calculate the MIR locals which have been previously
475475
// borrowed (even if they are still active).

src/librustc_mir/transform/instcombine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
2626
let optimizations = {
2727
let read_only_cache = read_only!(body);
2828
let mut optimization_finder = OptimizationFinder::new(body, tcx);
29-
optimization_finder.visit_body(read_only_cache);
29+
optimization_finder.visit_body(&read_only_cache);
3030
optimization_finder.optimizations
3131
};
3232

src/librustc_mir/transform/simplify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
309309
let locals = {
310310
let read_only_cache = read_only!(body);
311311
let mut marker = DeclMarker { locals: BitSet::new_empty(body.local_decls.len()), body };
312-
marker.visit_body(read_only_cache);
312+
marker.visit_body(&read_only_cache);
313313
// Return pointer and arguments are always live
314314
marker.locals.insert(RETURN_PLACE);
315315
for arg in body.args_iter() {

src/librustc_mir/util/collect_writes.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use rustc::mir::visit::PlaceContext;
22
use rustc::mir::visit::Visitor;
3-
use rustc::mir::ReadOnlyBodyAndCache;
4-
use rustc::mir::{Local, Location};
3+
use rustc::mir::{Body, Local, Location};
54

65
crate trait FindAssignments {
76
// Finds all statements that assign directly to local (i.e., X = ...)
87
// and returns their locations.
98
fn find_assignments(&self, local: Local) -> Vec<Location>;
109
}
1110

12-
impl<'a, 'tcx> FindAssignments for ReadOnlyBodyAndCache<'a, 'tcx> {
11+
impl<'tcx> FindAssignments for Body<'tcx> {
1312
fn find_assignments(&self, local: Local) -> Vec<Location> {
1413
let mut visitor = FindLocalAssignmentVisitor { needle: local, locations: vec![] };
15-
visitor.visit_body(*self);
14+
visitor.visit_body(self);
1615
visitor.locations
1716
}
1817
}

src/librustc_mir/util/def_use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl DefUseAnalysis {
3838
var_debug_info_index: 0,
3939
in_var_debug_info: false,
4040
};
41-
finder.visit_body(body);
41+
finder.visit_body(&body);
4242
self.info = finder.info
4343
}
4444

0 commit comments

Comments
 (0)