Skip to content

Commit 1c188eb

Browse files
committed
Auto merge of #132062 - nnethercote:still-more-rustc_mir_dataflow-cleanups, r=<try>
Still more `rustc_mir_dataflow` cleanups A sequel to #118203, #118230, #118638, and #131481. I'm pleased with this PR. It cleans up a few things that have been bugging me for a while. It took quite some effort to find these improvements. r? `@cjgillot`
2 parents 4f2f477 + 9e735ca commit 1c188eb

28 files changed

+407
-445
lines changed

compiler/rustc_borrowck/src/dataflow.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<'a, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'tcx> {
4444
}
4545

4646
fn reconstruct_before_statement_effect(
47-
&mut self,
47+
&self,
4848
state: &mut Self::Domain,
4949
stmt: &mir::Statement<'tcx>,
5050
loc: Location,
@@ -55,7 +55,7 @@ impl<'a, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'tcx> {
5555
}
5656

5757
fn reconstruct_statement_effect(
58-
&mut self,
58+
&self,
5959
state: &mut Self::Domain,
6060
stmt: &mir::Statement<'tcx>,
6161
loc: Location,
@@ -66,7 +66,7 @@ impl<'a, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'tcx> {
6666
}
6767

6868
fn reconstruct_before_terminator_effect(
69-
&mut self,
69+
&self,
7070
state: &mut Self::Domain,
7171
term: &mir::Terminator<'tcx>,
7272
loc: Location,
@@ -77,7 +77,7 @@ impl<'a, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'tcx> {
7777
}
7878

7979
fn reconstruct_terminator_effect(
80-
&mut self,
80+
&self,
8181
state: &mut Self::Domain,
8282
term: &mir::Terminator<'tcx>,
8383
loc: Location,
@@ -511,7 +511,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
511511
}
512512

513513
fn apply_before_statement_effect(
514-
&mut self,
514+
&self,
515515
trans: &mut Self::Domain,
516516
_statement: &mir::Statement<'tcx>,
517517
location: Location,
@@ -520,7 +520,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
520520
}
521521

522522
fn apply_statement_effect(
523-
&mut self,
523+
&self,
524524
trans: &mut Self::Domain,
525525
stmt: &mir::Statement<'tcx>,
526526
location: Location,
@@ -568,7 +568,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
568568
}
569569

570570
fn apply_before_terminator_effect(
571-
&mut self,
571+
&self,
572572
trans: &mut Self::Domain,
573573
_terminator: &mir::Terminator<'tcx>,
574574
location: Location,
@@ -577,7 +577,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
577577
}
578578

579579
fn apply_terminator_effect<'mir>(
580-
&mut self,
580+
&self,
581581
trans: &mut Self::Domain,
582582
terminator: &'mir mir::Terminator<'tcx>,
583583
_location: Location,

compiler/rustc_borrowck/src/lib.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ fn do_mir_borrowck<'tcx>(
193193
.map(|(idx, body)| (idx, MoveData::gather_moves(body, tcx, |_| true)));
194194

195195
let mut flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
196-
.into_engine(tcx, body)
197-
.pass_name("borrowck")
198-
.iterate_to_fixpoint()
196+
.iterate_to_fixpoint(tcx, body, Some("borrowck"))
199197
.into_results_cursor(body);
200198

201199
let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(def).is_fn_or_closure();
@@ -243,18 +241,21 @@ fn do_mir_borrowck<'tcx>(
243241
// usage significantly on some benchmarks.
244242
drop(flow_inits);
245243

246-
let flow_borrows = Borrows::new(tcx, body, &regioncx, &borrow_set)
247-
.into_engine(tcx, body)
248-
.pass_name("borrowck")
249-
.iterate_to_fixpoint();
250-
let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data)
251-
.into_engine(tcx, body)
252-
.pass_name("borrowck")
253-
.iterate_to_fixpoint();
254-
let flow_ever_inits = EverInitializedPlaces::new(body, &move_data)
255-
.into_engine(tcx, body)
256-
.pass_name("borrowck")
257-
.iterate_to_fixpoint();
244+
let flow_borrows = Borrows::new(tcx, body, &regioncx, &borrow_set).iterate_to_fixpoint(
245+
tcx,
246+
body,
247+
Some("borrowck"),
248+
);
249+
let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data).iterate_to_fixpoint(
250+
tcx,
251+
body,
252+
Some("borrowck"),
253+
);
254+
let flow_ever_inits = EverInitializedPlaces::new(body, &move_data).iterate_to_fixpoint(
255+
tcx,
256+
body,
257+
Some("borrowck"),
258+
);
258259

259260
let movable_coroutine =
260261
// The first argument is the coroutine type passed by value
@@ -602,7 +603,7 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>
602603

603604
fn visit_statement_before_primary_effect(
604605
&mut self,
605-
_results: &mut R,
606+
_results: &R,
606607
state: &BorrowckDomain<'a, 'tcx>,
607608
stmt: &'a Statement<'tcx>,
608609
location: Location,
@@ -672,7 +673,7 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>
672673

673674
fn visit_terminator_before_primary_effect(
674675
&mut self,
675-
_results: &mut R,
676+
_results: &R,
676677
state: &BorrowckDomain<'a, 'tcx>,
677678
term: &'a Terminator<'tcx>,
678679
loc: Location,
@@ -785,7 +786,7 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>
785786

786787
fn visit_terminator_after_primary_effect(
787788
&mut self,
788-
_results: &mut R,
789+
_results: &R,
789790
state: &BorrowckDomain<'a, 'tcx>,
790791
term: &'a Terminator<'tcx>,
791792
loc: Location,

compiler/rustc_const_eval/src/check_consts/check.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
6363
let ConstCx { tcx, body, .. } = *ccx;
6464

6565
FlowSensitiveAnalysis::new(NeedsDrop, ccx)
66-
.into_engine(tcx, body)
67-
.iterate_to_fixpoint()
66+
.iterate_to_fixpoint(tcx, body, None)
6867
.into_results_cursor(body)
6968
});
7069

@@ -93,8 +92,7 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
9392
let ConstCx { tcx, body, .. } = *ccx;
9493

9594
FlowSensitiveAnalysis::new(NeedsNonConstDrop, ccx)
96-
.into_engine(tcx, body)
97-
.iterate_to_fixpoint()
95+
.iterate_to_fixpoint(tcx, body, None)
9896
.into_results_cursor(body)
9997
});
10098

@@ -123,8 +121,7 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
123121
let ConstCx { tcx, body, .. } = *ccx;
124122

125123
FlowSensitiveAnalysis::new(HasMutInterior, ccx)
126-
.into_engine(tcx, body)
127-
.iterate_to_fixpoint()
124+
.iterate_to_fixpoint(tcx, body, None)
128125
.into_results_cursor(body)
129126
});
130127

@@ -239,8 +236,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
239236
let always_live_locals = &always_storage_live_locals(&ccx.body);
240237
let mut maybe_storage_live =
241238
MaybeStorageLive::new(Cow::Borrowed(always_live_locals))
242-
.into_engine(ccx.tcx, &ccx.body)
243-
.iterate_to_fixpoint()
239+
.iterate_to_fixpoint(ccx.tcx, &ccx.body, None)
244240
.into_results_cursor(&ccx.body);
245241

246242
// And then check all `Return` in the MIR, and if a local is "maybe live" at a

compiler/rustc_const_eval/src/check_consts/resolver.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ where
330330
}
331331

332332
fn apply_statement_effect(
333-
&mut self,
333+
&self,
334334
state: &mut Self::Domain,
335335
statement: &mir::Statement<'tcx>,
336336
location: Location,
@@ -339,7 +339,7 @@ where
339339
}
340340

341341
fn apply_terminator_effect<'mir>(
342-
&mut self,
342+
&self,
343343
state: &mut Self::Domain,
344344
terminator: &'mir mir::Terminator<'tcx>,
345345
location: Location,
@@ -349,7 +349,7 @@ where
349349
}
350350

351351
fn apply_call_return_effect(
352-
&mut self,
352+
&self,
353353
state: &mut Self::Domain,
354354
block: BasicBlock,
355355
return_places: CallReturnPlaces<'_, 'tcx>,

compiler/rustc_mir_dataflow/src/framework/cursor.rs

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
//! Random access inspection of the results of a dataflow analysis.
22
33
use std::cmp::Ordering;
4+
use std::ops::Deref;
45

56
#[cfg(debug_assertions)]
67
use rustc_index::bit_set::BitSet;
78
use rustc_middle::mir::{self, BasicBlock, Location};
89

910
use super::{Analysis, Direction, Effect, EffectIndex, Results};
1011

12+
/// Some `ResultsCursor`s want to own a `Results`, and some want to borrow a `Results`. This type
13+
/// allows either. We could use `Cow` but that would require `Results` and `A` to impl `Clone`. So
14+
/// this is a greatly cut-down alternative to `Cow`.
15+
pub enum ResultsHandle<'a, 'tcx, A>
16+
where
17+
A: Analysis<'tcx>,
18+
{
19+
Borrowed(&'a Results<'tcx, A>),
20+
Owned(Results<'tcx, A>),
21+
}
22+
23+
impl<'tcx, A> Deref for ResultsHandle<'_, 'tcx, A>
24+
where
25+
A: Analysis<'tcx>,
26+
{
27+
type Target = Results<'tcx, A>;
28+
29+
fn deref(&self) -> &Results<'tcx, A> {
30+
match self {
31+
ResultsHandle::Borrowed(borrowed) => borrowed,
32+
ResultsHandle::Owned(owned) => owned,
33+
}
34+
}
35+
}
36+
1137
/// Allows random access inspection of the results of a dataflow analysis.
1238
///
1339
/// This cursor only has linear performance within a basic block when its statements are visited in
@@ -19,7 +45,7 @@ where
1945
A: Analysis<'tcx>,
2046
{
2147
body: &'mir mir::Body<'tcx>,
22-
results: Results<'tcx, A>,
48+
results: ResultsHandle<'mir, 'tcx, A>,
2349
state: A::Domain,
2450

2551
pos: CursorPosition,
@@ -47,13 +73,8 @@ where
4773
self.body
4874
}
4975

50-
/// Unwraps this cursor, returning the underlying `Results`.
51-
pub fn into_results(self) -> Results<'tcx, A> {
52-
self.results
53-
}
54-
5576
/// Returns a new cursor that can inspect `results`.
56-
pub fn new(body: &'mir mir::Body<'tcx>, results: Results<'tcx, A>) -> Self {
77+
pub fn new(body: &'mir mir::Body<'tcx>, results: ResultsHandle<'mir, 'tcx, A>) -> Self {
5778
let bottom_value = results.analysis.bottom_value(body);
5879
ResultsCursor {
5980
body,
@@ -83,21 +104,11 @@ where
83104
&self.results
84105
}
85106

86-
/// Returns the underlying `Results`.
87-
pub fn mut_results(&mut self) -> &mut Results<'tcx, A> {
88-
&mut self.results
89-
}
90-
91107
/// Returns the `Analysis` used to generate the underlying `Results`.
92108
pub fn analysis(&self) -> &A {
93109
&self.results.analysis
94110
}
95111

96-
/// Returns the `Analysis` used to generate the underlying `Results`.
97-
pub fn mut_analysis(&mut self) -> &mut A {
98-
&mut self.results.analysis
99-
}
100-
101112
/// Resets the cursor to hold the entry set for the given basic block.
102113
///
103114
/// For forward dataflow analyses, this is the dataflow state prior to the first statement.
@@ -199,7 +210,7 @@ where
199210
let target_effect_index = effect.at_index(target.statement_index);
200211

201212
A::Direction::apply_effects_in_range(
202-
&mut self.results.analysis,
213+
&self.results.analysis,
203214
&mut self.state,
204215
target.block,
205216
block_data,
@@ -214,8 +225,8 @@ where
214225
///
215226
/// This can be used, e.g., to apply the call return effect directly to the cursor without
216227
/// creating an extra copy of the dataflow state.
217-
pub fn apply_custom_effect(&mut self, f: impl FnOnce(&mut A, &mut A::Domain)) {
218-
f(&mut self.results.analysis, &mut self.state);
228+
pub fn apply_custom_effect(&mut self, f: impl FnOnce(&A, &mut A::Domain)) {
229+
f(&self.results.analysis, &mut self.state);
219230
self.state_needs_reset = true;
220231
}
221232
}

0 commit comments

Comments
 (0)