diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 3b66142eb2cec..83a9827e8f81f 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -702,12 +702,12 @@ struct MirBorrowckCtxt<'a, 'infcx, 'tcx> { // 2. loans made in overlapping scopes do not conflict // 3. assignments do not affect things loaned out as immutable // 4. moves do not affect things loaned out in any way -impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> { +impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> { fn visit_after_early_statement_effect( &mut self, _results: &mut Results<'tcx, Borrowck<'a, 'tcx>>, state: &BorrowckDomain, - stmt: &'a Statement<'tcx>, + stmt: &Statement<'tcx>, location: Location, ) { debug!("MirBorrowckCtxt::process_statement({:?}, {:?}): {:?}", location, stmt, state); @@ -783,7 +783,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt< &mut self, _results: &mut Results<'tcx, Borrowck<'a, 'tcx>>, state: &BorrowckDomain, - term: &'a Terminator<'tcx>, + term: &Terminator<'tcx>, loc: Location, ) { debug!("MirBorrowckCtxt::process_terminator({:?}, {:?}): {:?}", loc, term, state); @@ -896,7 +896,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt< &mut self, _results: &mut Results<'tcx, Borrowck<'a, 'tcx>>, state: &BorrowckDomain, - term: &'a Terminator<'tcx>, + term: &Terminator<'tcx>, loc: Location, ) { let span = term.source_info.span; @@ -1363,7 +1363,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> { fn consume_rvalue( &mut self, location: Location, - (rvalue, span): (&'a Rvalue<'tcx>, Span), + (rvalue, span): (&Rvalue<'tcx>, Span), state: &BorrowckDomain, ) { match rvalue { @@ -1636,7 +1636,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> { fn consume_operand( &mut self, location: Location, - (operand, span): (&'a Operand<'tcx>, Span), + (operand, span): (&Operand<'tcx>, Span), state: &BorrowckDomain, ) { match *operand { diff --git a/compiler/rustc_mir_dataflow/src/framework/cursor.rs b/compiler/rustc_mir_dataflow/src/framework/cursor.rs index c46ae9775cf68..d5005768b80bd 100644 --- a/compiler/rustc_mir_dataflow/src/framework/cursor.rs +++ b/compiler/rustc_mir_dataflow/src/framework/cursor.rs @@ -114,26 +114,11 @@ where self.reachable_blocks.insert_all() } - /// Returns the underlying `Results`. - pub fn results(&self) -> &Results<'tcx, A> { - &self.results - } - - /// Returns the underlying `Results`. - pub fn mut_results(&mut self) -> &mut Results<'tcx, A> { - &mut self.results - } - /// Returns the `Analysis` used to generate the underlying `Results`. pub fn analysis(&self) -> &A { &self.results.analysis } - /// Returns the `Analysis` used to generate the underlying `Results`. - pub fn mut_analysis(&mut self) -> &mut A { - &mut self.results.analysis - } - /// Resets the cursor to hold the entry set for the given basic block. /// /// For forward dataflow analyses, this is the dataflow state prior to the first statement. diff --git a/compiler/rustc_mir_dataflow/src/framework/direction.rs b/compiler/rustc_mir_dataflow/src/framework/direction.rs index 3d7f9e2d8e71b..b8c26dad59f41 100644 --- a/compiler/rustc_mir_dataflow/src/framework/direction.rs +++ b/compiler/rustc_mir_dataflow/src/framework/direction.rs @@ -43,7 +43,7 @@ pub trait Direction { block: BasicBlock, block_data: &'mir mir::BasicBlockData<'tcx>, results: &mut Results<'tcx, A>, - vis: &mut impl ResultsVisitor<'mir, 'tcx, A>, + vis: &mut impl ResultsVisitor<'tcx, A>, ) where A: Analysis<'tcx>; } @@ -212,7 +212,7 @@ impl Direction for Backward { block: BasicBlock, block_data: &'mir mir::BasicBlockData<'tcx>, results: &mut Results<'tcx, A>, - vis: &mut impl ResultsVisitor<'mir, 'tcx, A>, + vis: &mut impl ResultsVisitor<'tcx, A>, ) where A: Analysis<'tcx>, { @@ -394,7 +394,7 @@ impl Direction for Forward { block: BasicBlock, block_data: &'mir mir::BasicBlockData<'tcx>, results: &mut Results<'tcx, A>, - vis: &mut impl ResultsVisitor<'mir, 'tcx, A>, + vis: &mut impl ResultsVisitor<'tcx, A>, ) where A: Analysis<'tcx>, { diff --git a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs index 95f488a925b2c..b5e9a0b893247 100644 --- a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs +++ b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs @@ -201,11 +201,12 @@ struct Formatter<'mir, 'tcx, A> where A: Analysis<'tcx>, { + body: &'mir Body<'tcx>, // The `RefCell` is used because `::node_label` - // takes `&self`, but it needs to modify the cursor. This is also the + // takes `&self`, but it needs to modify the results. This is also the // reason for the `Formatter`/`BlockFormatter` split; `BlockFormatter` has // the operations that involve the mutation, i.e. within the `borrow_mut`. - cursor: RefCell>, + results: RefCell<&'mir mut Results<'tcx, A>>, style: OutputStyle, reachable: DenseBitSet, } @@ -220,11 +221,7 @@ where style: OutputStyle, ) -> Self { let reachable = traversal::reachable_as_bitset(body); - Formatter { cursor: results.as_results_cursor(body).into(), style, reachable } - } - - fn body(&self) -> &'mir Body<'tcx> { - self.cursor.borrow().body() + Formatter { body, results: results.into(), style, reachable } } } @@ -253,7 +250,7 @@ where type Edge = CfgEdge; fn graph_id(&self) -> dot::Id<'_> { - let name = graphviz_safe_def_name(self.body().source.def_id()); + let name = graphviz_safe_def_name(self.body.source.def_id()); dot::Id::new(format!("graph_for_def_id_{name}")).unwrap() } @@ -262,10 +259,16 @@ where } fn node_label(&self, block: &Self::Node) -> dot::LabelText<'_> { - let mut cursor = self.cursor.borrow_mut(); - let mut fmt = - BlockFormatter { cursor: &mut cursor, style: self.style, bg: Background::Light }; - let label = fmt.write_node_label(*block).unwrap(); + let mut results = self.results.borrow_mut(); + + let diffs = StateDiffCollector::run(self.body, *block, *results, self.style); + + let mut fmt = BlockFormatter { + cursor: results.as_results_cursor(self.body), + style: self.style, + bg: Background::Light, + }; + let label = fmt.write_node_label(*block, diffs).unwrap(); dot::LabelText::html(String::from_utf8(label).unwrap()) } @@ -275,7 +278,7 @@ where } fn edge_label(&self, e: &Self::Edge) -> dot::LabelText<'_> { - let label = &self.body()[e.source].terminator().kind.fmt_successor_labels()[e.index]; + let label = &self.body[e.source].terminator().kind.fmt_successor_labels()[e.index]; dot::LabelText::label(label.clone()) } } @@ -288,7 +291,7 @@ where type Edge = CfgEdge; fn nodes(&self) -> dot::Nodes<'_, Self::Node> { - self.body() + self.body .basic_blocks .indices() .filter(|&idx| self.reachable.contains(idx)) @@ -297,10 +300,10 @@ where } fn edges(&self) -> dot::Edges<'_, Self::Edge> { - let body = self.body(); - body.basic_blocks + self.body + .basic_blocks .indices() - .flat_map(|bb| dataflow_successors(body, bb)) + .flat_map(|bb| dataflow_successors(self.body, bb)) .collect::>() .into() } @@ -310,20 +313,20 @@ where } fn target(&self, edge: &Self::Edge) -> Self::Node { - self.body()[edge.source].terminator().successors().nth(edge.index).unwrap() + self.body[edge.source].terminator().successors().nth(edge.index).unwrap() } } -struct BlockFormatter<'a, 'mir, 'tcx, A> +struct BlockFormatter<'mir, 'tcx, A> where A: Analysis<'tcx>, { - cursor: &'a mut ResultsCursor<'mir, 'tcx, A>, + cursor: ResultsCursor<'mir, 'tcx, A>, bg: Background, style: OutputStyle, } -impl<'tcx, A> BlockFormatter<'_, '_, 'tcx, A> +impl<'tcx, A> BlockFormatter<'_, 'tcx, A> where A: Analysis<'tcx>, A::Domain: DebugWithContext, @@ -336,7 +339,11 @@ where bg } - fn write_node_label(&mut self, block: BasicBlock) -> io::Result> { + fn write_node_label( + &mut self, + block: BasicBlock, + diffs: StateDiffCollector, + ) -> io::Result> { use std::io::Write; // Sample output: @@ -392,7 +399,7 @@ where self.write_row_with_full_state(w, "", "(on start)")?; // D + E: Statement and terminator transfer functions - self.write_statements_and_terminator(w, block)?; + self.write_statements_and_terminator(w, block, diffs)?; // F: State at end of block @@ -575,14 +582,8 @@ where &mut self, w: &mut impl io::Write, block: BasicBlock, + diffs: StateDiffCollector, ) -> io::Result<()> { - let diffs = StateDiffCollector::run( - self.cursor.body(), - block, - self.cursor.mut_results(), - self.style, - ); - let mut diffs_before = diffs.before.map(|v| v.into_iter()); let mut diffs_after = diffs.after.into_iter(); @@ -709,7 +710,7 @@ impl StateDiffCollector { } } -impl<'tcx, A> ResultsVisitor<'_, 'tcx, A> for StateDiffCollector +impl<'tcx, A> ResultsVisitor<'tcx, A> for StateDiffCollector where A: Analysis<'tcx>, A::Domain: DebugWithContext, diff --git a/compiler/rustc_mir_dataflow/src/framework/results.rs b/compiler/rustc_mir_dataflow/src/framework/results.rs index 8e2c3afddb352..93dfc06a878aa 100644 --- a/compiler/rustc_mir_dataflow/src/framework/results.rs +++ b/compiler/rustc_mir_dataflow/src/framework/results.rs @@ -47,7 +47,7 @@ where &mut self, body: &'mir Body<'tcx>, blocks: impl IntoIterator, - vis: &mut impl ResultsVisitor<'mir, 'tcx, A>, + vis: &mut impl ResultsVisitor<'tcx, A>, ) { visit_results(body, blocks, self, vis) } @@ -55,7 +55,7 @@ where pub fn visit_reachable_with<'mir>( &mut self, body: &'mir Body<'tcx>, - vis: &mut impl ResultsVisitor<'mir, 'tcx, A>, + vis: &mut impl ResultsVisitor<'tcx, A>, ) { let blocks = traversal::reachable(body); visit_results(body, blocks.map(|(bb, _)| bb), self, vis) diff --git a/compiler/rustc_mir_dataflow/src/framework/visitor.rs b/compiler/rustc_mir_dataflow/src/framework/visitor.rs index a03aecee7be12..c9fdf46c4f5b2 100644 --- a/compiler/rustc_mir_dataflow/src/framework/visitor.rs +++ b/compiler/rustc_mir_dataflow/src/framework/visitor.rs @@ -8,7 +8,7 @@ pub fn visit_results<'mir, 'tcx, A>( body: &'mir mir::Body<'tcx>, blocks: impl IntoIterator, results: &mut Results<'tcx, A>, - vis: &mut impl ResultsVisitor<'mir, 'tcx, A>, + vis: &mut impl ResultsVisitor<'tcx, A>, ) where A: Analysis<'tcx>, { @@ -29,7 +29,7 @@ pub fn visit_results<'mir, 'tcx, A>( /// A visitor over the results of an `Analysis`. Use this when you want to inspect domain values in /// many or all locations; use `ResultsCursor` if you want to inspect domain values only in certain /// locations. -pub trait ResultsVisitor<'mir, 'tcx, A> +pub trait ResultsVisitor<'tcx, A> where A: Analysis<'tcx>, { @@ -40,7 +40,7 @@ where &mut self, _results: &mut Results<'tcx, A>, _state: &A::Domain, - _statement: &'mir mir::Statement<'tcx>, + _statement: &mir::Statement<'tcx>, _location: Location, ) { } @@ -50,7 +50,7 @@ where &mut self, _results: &mut Results<'tcx, A>, _state: &A::Domain, - _statement: &'mir mir::Statement<'tcx>, + _statement: &mir::Statement<'tcx>, _location: Location, ) { } @@ -60,7 +60,7 @@ where &mut self, _results: &mut Results<'tcx, A>, _state: &A::Domain, - _terminator: &'mir mir::Terminator<'tcx>, + _terminator: &mir::Terminator<'tcx>, _location: Location, ) { } @@ -72,7 +72,7 @@ where &mut self, _results: &mut Results<'tcx, A>, _state: &A::Domain, - _terminator: &'mir mir::Terminator<'tcx>, + _terminator: &mir::Terminator<'tcx>, _location: Location, ) { } diff --git a/compiler/rustc_mir_dataflow/src/points.rs b/compiler/rustc_mir_dataflow/src/points.rs index 5d2a78acbf526..21590ff1bbad4 100644 --- a/compiler/rustc_mir_dataflow/src/points.rs +++ b/compiler/rustc_mir_dataflow/src/points.rs @@ -120,12 +120,12 @@ struct Visitor<'a, N: Idx> { values: SparseIntervalMatrix, } -impl<'mir, 'tcx, A, N> ResultsVisitor<'mir, 'tcx, A> for Visitor<'_, N> +impl<'tcx, A, N> ResultsVisitor<'tcx, A> for Visitor<'_, N> where A: Analysis<'tcx, Domain = DenseBitSet>, N: Idx, { - fn visit_after_primary_statement_effect( + fn visit_after_primary_statement_effect<'mir>( &mut self, _results: &mut Results<'tcx, A>, state: &A::Domain, @@ -139,7 +139,7 @@ where }); } - fn visit_after_primary_terminator_effect( + fn visit_after_primary_terminator_effect<'mir>( &mut self, _results: &mut Results<'tcx, A>, state: &A::Domain, diff --git a/compiler/rustc_mir_transform/src/coroutine.rs b/compiler/rustc_mir_transform/src/coroutine.rs index 92d5e152f58e6..0eed46c72f993 100644 --- a/compiler/rustc_mir_transform/src/coroutine.rs +++ b/compiler/rustc_mir_transform/src/coroutine.rs @@ -875,14 +875,14 @@ struct StorageConflictVisitor<'a, 'tcx> { eligible_storage_live: DenseBitSet, } -impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, MaybeRequiresStorage<'a, 'tcx>> +impl<'a, 'tcx> ResultsVisitor<'tcx, MaybeRequiresStorage<'a, 'tcx>> for StorageConflictVisitor<'a, 'tcx> { fn visit_after_early_statement_effect( &mut self, _results: &mut Results<'tcx, MaybeRequiresStorage<'a, 'tcx>>, state: &DenseBitSet, - _statement: &'a Statement<'tcx>, + _statement: &Statement<'tcx>, loc: Location, ) { self.apply_state(state, loc); @@ -892,7 +892,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, MaybeRequiresStorage<'a, 'tcx>> &mut self, _results: &mut Results<'tcx, MaybeRequiresStorage<'a, 'tcx>>, state: &DenseBitSet, - _terminator: &'a Terminator<'tcx>, + _terminator: &Terminator<'tcx>, loc: Location, ) { self.apply_state(state, loc); diff --git a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs index 90173da17f0fc..a2103a004d2d1 100644 --- a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs +++ b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs @@ -958,13 +958,13 @@ fn try_write_constant<'tcx>( interp_ok(()) } -impl<'mir, 'tcx> ResultsVisitor<'mir, 'tcx, ConstAnalysis<'_, 'tcx>> for Collector<'_, 'tcx> { +impl<'tcx> ResultsVisitor<'tcx, ConstAnalysis<'_, 'tcx>> for Collector<'_, 'tcx> { #[instrument(level = "trace", skip(self, results, statement))] fn visit_after_early_statement_effect( &mut self, results: &mut Results<'tcx, ConstAnalysis<'_, 'tcx>>, state: &State>, - statement: &'mir Statement<'tcx>, + statement: &Statement<'tcx>, location: Location, ) { match &statement.kind { @@ -986,7 +986,7 @@ impl<'mir, 'tcx> ResultsVisitor<'mir, 'tcx, ConstAnalysis<'_, 'tcx>> for Collect &mut self, results: &mut Results<'tcx, ConstAnalysis<'_, 'tcx>>, state: &State>, - statement: &'mir Statement<'tcx>, + statement: &Statement<'tcx>, location: Location, ) { match statement.kind { @@ -1011,7 +1011,7 @@ impl<'mir, 'tcx> ResultsVisitor<'mir, 'tcx, ConstAnalysis<'_, 'tcx>> for Collect &mut self, results: &mut Results<'tcx, ConstAnalysis<'_, 'tcx>>, state: &State>, - terminator: &'mir Terminator<'tcx>, + terminator: &Terminator<'tcx>, location: Location, ) { OperandCollector {