about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRémy Rakic <remy.rakic+github@gmail.com>2024-12-11 14:09:08 +0000
committerRémy Rakic <remy.rakic+github@gmail.com>2024-12-15 14:44:07 +0000
commit1740a5f84a19da866915e53f1dc05a9f929b4b37 (patch)
tree10177064e783d1d32aba9ee607bd8501295a008d
parent5486857448ad37a90ed1d75c87fac8adb9884894 (diff)
downloadrust-1740a5f84a19da866915e53f1dc05a9f929b4b37.tar.gz
rust-1740a5f84a19da866915e53f1dc05a9f929b4b37.zip
simplify `emit_access_facts` and fact generation
- integrate it within existing fact generation instead of being called
  in typeck
- simplify access fact extraction
- also remove single use fact emit functions in root fact generation
-rw-r--r--compiler/rustc_borrowck/src/polonius/legacy/accesses.rs33
-rw-r--r--compiler/rustc_borrowck/src/polonius/legacy/mod.rs38
-rw-r--r--compiler/rustc_borrowck/src/type_check/mod.rs8
3 files changed, 21 insertions, 58 deletions
diff --git a/compiler/rustc_borrowck/src/polonius/legacy/accesses.rs b/compiler/rustc_borrowck/src/polonius/legacy/accesses.rs
index 4266aba9cc5..b4c39567da1 100644
--- a/compiler/rustc_borrowck/src/polonius/legacy/accesses.rs
+++ b/compiler/rustc_borrowck/src/polonius/legacy/accesses.rs
@@ -1,7 +1,7 @@
 use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
 use rustc_middle::mir::{Body, Local, Location, Place};
 use rustc_middle::ty::TyCtxt;
-use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
+use rustc_mir_dataflow::move_paths::{LookupResult, MoveData};
 use tracing::debug;
 
 use crate::def_use::{self, DefUse};
@@ -9,28 +9,16 @@ use crate::facts::AllFacts;
 use crate::location::{LocationIndex, LocationTable};
 use crate::universal_regions::UniversalRegions;
 
-type VarPointRelation = Vec<(Local, LocationIndex)>;
-type PathPointRelation = Vec<(MovePathIndex, LocationIndex)>;
-
 /// Emit polonius facts for variable defs, uses, drops, and path accesses.
 pub(crate) fn emit_access_facts<'tcx>(
+    facts: &mut AllFacts,
     tcx: TyCtxt<'tcx>,
     body: &Body<'tcx>,
     move_data: &MoveData<'tcx>,
     universal_regions: &UniversalRegions<'tcx>,
     location_table: &LocationTable,
-    all_facts: &mut Option<AllFacts>,
 ) {
-    let Some(facts) = all_facts.as_mut() else { return };
-    let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
-    let mut extractor = AccessFactsExtractor {
-        var_defined_at: &mut facts.var_defined_at,
-        var_used_at: &mut facts.var_used_at,
-        var_dropped_at: &mut facts.var_dropped_at,
-        path_accessed_at_base: &mut facts.path_accessed_at_base,
-        location_table,
-        move_data,
-    };
+    let mut extractor = AccessFactsExtractor { facts, move_data, location_table };
     extractor.visit_body(body);
 
     for (local, local_decl) in body.local_decls.iter_enumerated() {
@@ -44,12 +32,9 @@ pub(crate) fn emit_access_facts<'tcx>(
 
 /// MIR visitor extracting point-wise facts about accesses.
 struct AccessFactsExtractor<'a, 'tcx> {
-    var_defined_at: &'a mut VarPointRelation,
-    var_used_at: &'a mut VarPointRelation,
-    location_table: &'a LocationTable,
-    var_dropped_at: &'a mut VarPointRelation,
+    facts: &'a mut AllFacts,
     move_data: &'a MoveData<'tcx>,
-    path_accessed_at_base: &'a mut PathPointRelation,
+    location_table: &'a LocationTable,
 }
 
 impl<'tcx> AccessFactsExtractor<'_, 'tcx> {
@@ -63,15 +48,15 @@ impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
         match def_use::categorize(context) {
             Some(DefUse::Def) => {
                 debug!("AccessFactsExtractor - emit def");
-                self.var_defined_at.push((local, self.location_to_index(location)));
+                self.facts.var_defined_at.push((local, self.location_to_index(location)));
             }
             Some(DefUse::Use) => {
                 debug!("AccessFactsExtractor - emit use");
-                self.var_used_at.push((local, self.location_to_index(location)));
+                self.facts.var_used_at.push((local, self.location_to_index(location)));
             }
             Some(DefUse::Drop) => {
                 debug!("AccessFactsExtractor - emit drop");
-                self.var_dropped_at.push((local, self.location_to_index(location)));
+                self.facts.var_dropped_at.push((local, self.location_to_index(location)));
             }
             _ => (),
         }
@@ -91,7 +76,7 @@ impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
                     }
                 };
                 debug!("AccessFactsExtractor - emit path access ({path:?}, {location:?})");
-                self.path_accessed_at_base.push((path, self.location_to_index(location)));
+                self.facts.path_accessed_at_base.push((path, self.location_to_index(location)));
             }
 
             _ => {}
diff --git a/compiler/rustc_borrowck/src/polonius/legacy/mod.rs b/compiler/rustc_borrowck/src/polonius/legacy/mod.rs
index 15988fad57f..d1363d98c88 100644
--- a/compiler/rustc_borrowck/src/polonius/legacy/mod.rs
+++ b/compiler/rustc_borrowck/src/polonius/legacy/mod.rs
@@ -23,14 +23,14 @@ mod accesses;
 mod loan_invalidations;
 mod loan_kills;
 
-pub(crate) use accesses::emit_access_facts;
-
 /// When requested, emit most of the facts needed by polonius:
 /// - moves and assignments
 /// - universal regions and their relations
 /// - CFG points and edges
 /// - loan kills
 /// - loan invalidations
+/// - access facts such as variable definitions, uses, drops, and path accesses
+/// - outlives constraints
 ///
 /// The rest of the facts are emitted during typeck and liveness.
 pub(crate) fn emit_facts<'tcx>(
@@ -49,8 +49,16 @@ pub(crate) fn emit_facts<'tcx>(
     let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
     emit_move_facts(all_facts, move_data, location_table, body);
     emit_universal_region_facts(all_facts, borrow_set, universal_region_relations);
-    emit_cfg_and_loan_kills_facts(all_facts, tcx, location_table, body, borrow_set);
-    emit_loan_invalidations_facts(all_facts, tcx, location_table, body, borrow_set);
+    loan_kills::emit_loan_kills(tcx, all_facts, location_table, body, borrow_set);
+    loan_invalidations::emit_loan_invalidations(tcx, all_facts, location_table, body, borrow_set);
+    accesses::emit_access_facts(
+        all_facts,
+        tcx,
+        body,
+        move_data,
+        &universal_region_relations.universal_regions,
+        location_table,
+    );
 }
 
 /// Emit facts needed for move/init analysis: moves and assignments.
@@ -170,28 +178,6 @@ fn emit_universal_region_facts(
     }
 }
 
-/// Emit facts about loan invalidations.
-fn emit_loan_invalidations_facts<'tcx>(
-    all_facts: &mut AllFacts,
-    tcx: TyCtxt<'tcx>,
-    location_table: &LocationTable,
-    body: &Body<'tcx>,
-    borrow_set: &BorrowSet<'tcx>,
-) {
-    loan_invalidations::emit_loan_invalidations(tcx, all_facts, location_table, body, borrow_set);
-}
-
-/// Emit facts about CFG points and edges, as well as locations where loans are killed.
-fn emit_cfg_and_loan_kills_facts<'tcx>(
-    all_facts: &mut AllFacts,
-    tcx: TyCtxt<'tcx>,
-    location_table: &LocationTable,
-    body: &Body<'tcx>,
-    borrow_set: &BorrowSet<'tcx>,
-) {
-    loan_kills::emit_loan_kills(tcx, all_facts, location_table, body, borrow_set);
-}
-
 /// For every potentially drop()-touched region `region` in `local`'s type
 /// (`kind`), emit a `drop_of_var_derefs_origin(local, origin)` fact.
 pub(crate) fn emit_drop_facts<'tcx>(
diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs
index 2820d1d2304..94ef491814d 100644
--- a/compiler/rustc_borrowck/src/type_check/mod.rs
+++ b/compiler/rustc_borrowck/src/type_check/mod.rs
@@ -181,14 +181,6 @@ pub(crate) fn type_check<'a, 'tcx>(
 
     liveness::generate(&mut checker, body, &elements, flow_inits, move_data);
 
-    polonius::legacy::emit_access_facts(
-        infcx.tcx,
-        body,
-        move_data,
-        &universal_region_relations.universal_regions,
-        location_table,
-        checker.all_facts,
-    );
     polonius::legacy::emit_outlives_facts(
         infcx.tcx,
         checker.constraints,