Skip to content

Commit

Permalink
rename AllFacts to PoloniusFacts
Browse files Browse the repository at this point in the history
This is another strangely named struct (and associated fields) that is
hard to see was related to datalog polonius.
  • Loading branch information
lqd committed Jan 8, 2025
1 parent 3a1a621 commit 36ea00c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 45 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/consumers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use super::dataflow::{BorrowIndex, Borrows, calculate_borrows_out_of_scope_a
pub use super::place_ext::PlaceExt;
pub use super::places_conflict::{PlaceConflictBias, places_conflict};
pub use super::polonius::legacy::{
AllFacts as PoloniusInput, PoloniusLocationTable, PoloniusOutput, PoloniusRegionVid,
PoloniusFacts as PoloniusInput, PoloniusLocationTable, PoloniusOutput, PoloniusRegionVid,
RichLocation, RustcFacts,
};
pub use super::region_infer::RegionInferenceContext;
Expand Down
22 changes: 12 additions & 10 deletions compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ use crate::borrow_set::BorrowSet;
use crate::consumers::ConsumerOptions;
use crate::diagnostics::{BorrowckDiagnosticsBuffer, RegionErrors};
use crate::polonius::LocalizedOutlivesConstraintSet;
use crate::polonius::legacy::{AllFacts, AllFactsExt, PoloniusLocationTable, PoloniusOutput};
use crate::polonius::legacy::{
PoloniusFacts, PoloniusFactsExt, PoloniusLocationTable, PoloniusOutput,
};
use crate::region_infer::RegionInferenceContext;
use crate::type_check::{self, MirTypeckResults};
use crate::universal_regions::UniversalRegions;
Expand All @@ -39,7 +41,7 @@ use crate::{BorrowckInferCtxt, polonius, renumber};
pub(crate) struct NllOutput<'tcx> {
pub regioncx: RegionInferenceContext<'tcx>,
pub opaque_type_values: FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>>,
pub polonius_input: Option<Box<AllFacts>>,
pub polonius_input: Option<Box<PoloniusFacts>>,
pub polonius_output: Option<Box<PoloniusOutput>>,
pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>,
pub nll_errors: RegionErrors<'tcx>,
Expand Down Expand Up @@ -91,8 +93,8 @@ pub(crate) fn compute_regions<'a, 'tcx>(
|| is_polonius_legacy_enabled;
let polonius_output = consumer_options.map(|c| c.polonius_output()).unwrap_or_default()
|| is_polonius_legacy_enabled;
let mut all_facts =
(polonius_input || AllFacts::enabled(infcx.tcx)).then_some(AllFacts::default());
let mut polonius_facts =
(polonius_input || PoloniusFacts::enabled(infcx.tcx)).then_some(PoloniusFacts::default());

let location_map = Rc::new(DenseLocationMap::new(body));

Expand All @@ -109,7 +111,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
universal_regions,
location_table,
borrow_set,
&mut all_facts,
&mut polonius_facts,
flow_inits,
move_data,
Rc::clone(&location_map),
Expand All @@ -122,7 +124,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(

// If requested, emit legacy polonius facts.
polonius::legacy::emit_facts(
&mut all_facts,
&mut polonius_facts,
infcx.tcx,
location_table,
body,
Expand All @@ -147,13 +149,13 @@ pub(crate) fn compute_regions<'a, 'tcx>(
});

// If requested: dump NLL facts, and run legacy polonius analysis.
let polonius_output = all_facts.as_ref().and_then(|all_facts| {
let polonius_output = polonius_facts.as_ref().and_then(|polonius_facts| {
if infcx.tcx.sess.opts.unstable_opts.nll_facts {
let def_id = body.source.def_id();
let def_path = infcx.tcx.def_path(def_id);
let dir_path = PathBuf::from(&infcx.tcx.sess.opts.unstable_opts.nll_facts_dir)
.join(def_path.to_filename_friendly_no_crate());
all_facts.write_to_dir(dir_path, location_table).unwrap();
polonius_facts.write_to_dir(dir_path, location_table).unwrap();
}

if polonius_output {
Expand All @@ -162,7 +164,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
let algorithm = Algorithm::from_str(&algorithm).unwrap();
debug!("compute_regions: using polonius algorithm {:?}", algorithm);
let _prof_timer = infcx.tcx.prof.generic_activity("polonius_analysis");
Some(Box::new(Output::compute(all_facts, algorithm, false)))
Some(Box::new(Output::compute(polonius_facts, algorithm, false)))
} else {
None
}
Expand All @@ -182,7 +184,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
NllOutput {
regioncx,
opaque_type_values: remapped_opaque_tys,
polonius_input: all_facts.map(Box::new),
polonius_input: polonius_facts.map(Box::new),
polonius_output,
opt_closure_req: closure_region_requirements,
nll_errors,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/polonius/legacy/accesses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData};
use tracing::debug;

use super::{AllFacts, LocationIndex, PoloniusLocationTable};
use super::{LocationIndex, PoloniusFacts, PoloniusLocationTable};
use crate::def_use::{self, DefUse};
use crate::universal_regions::UniversalRegions;

/// Emit polonius facts for variable defs, uses, drops, and path accesses.
pub(crate) fn emit_access_facts<'tcx>(
tcx: TyCtxt<'tcx>,
facts: &mut AllFacts,
facts: &mut PoloniusFacts,
body: &Body<'tcx>,
location_table: &PoloniusLocationTable,
move_data: &MoveData<'tcx>,
Expand All @@ -31,7 +31,7 @@ pub(crate) fn emit_access_facts<'tcx>(

/// MIR visitor extracting point-wise facts about accesses.
struct AccessFactsExtractor<'a, 'tcx> {
facts: &'a mut AllFacts,
facts: &'a mut PoloniusFacts,
move_data: &'a MoveData<'tcx>,
location_table: &'a PoloniusLocationTable,
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_borrowck/src/polonius/legacy/facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs::{self, File};
use std::io::Write;
use std::path::Path;

use polonius_engine::{AllFacts as PoloniusFacts, Atom, Output};
use polonius_engine::{AllFacts, Atom, Output};
use rustc_macros::extension;
use rustc_middle::mir::Local;
use rustc_middle::ty::{RegionVid, TyCtxt};
Expand Down Expand Up @@ -49,11 +49,11 @@ impl polonius_engine::FactTypes for RustcFacts {
type Path = MovePathIndex;
}

pub type AllFacts = PoloniusFacts<RustcFacts>;
pub type PoloniusFacts = AllFacts<RustcFacts>;

#[extension(pub(crate) trait AllFactsExt)]
impl AllFacts {
/// Returns `true` if there is a need to gather `AllFacts` given the
#[extension(pub(crate) trait PoloniusFactsExt)]
impl PoloniusFacts {
/// Returns `true` if there is a need to gather `PoloniusFacts` given the
/// current `-Z` flags.
fn enabled(tcx: TyCtxt<'_>) -> bool {
tcx.sess.opts.unstable_opts.nll_facts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_middle::mir::{
use rustc_middle::ty::TyCtxt;
use tracing::debug;

use super::{AllFacts, PoloniusLocationTable};
use super::{PoloniusFacts, PoloniusLocationTable};
use crate::borrow_set::BorrowSet;
use crate::path_utils::*;
use crate::{
Expand All @@ -22,7 +22,7 @@ use crate::{
/// Emit `loan_invalidated_at` facts.
pub(super) fn emit_loan_invalidations<'tcx>(
tcx: TyCtxt<'tcx>,
facts: &mut AllFacts,
facts: &mut PoloniusFacts,
body: &Body<'tcx>,
location_table: &PoloniusLocationTable,
borrow_set: &BorrowSet<'tcx>,
Expand All @@ -35,7 +35,7 @@ pub(super) fn emit_loan_invalidations<'tcx>(

struct LoanInvalidationsGenerator<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
facts: &'a mut AllFacts,
facts: &'a mut PoloniusFacts,
body: &'a Body<'tcx>,
location_table: &'a PoloniusLocationTable,
dominators: &'a Dominators<BasicBlock>,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/polonius/legacy/loan_kills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use rustc_middle::mir::{
use rustc_middle::ty::TyCtxt;
use tracing::debug;

use super::{AllFacts, PoloniusLocationTable};
use super::{PoloniusFacts, PoloniusLocationTable};
use crate::borrow_set::BorrowSet;
use crate::places_conflict;

/// Emit `loan_killed_at` and `cfg_edge` facts at the same time.
pub(super) fn emit_loan_kills<'tcx>(
tcx: TyCtxt<'tcx>,
facts: &mut AllFacts,
facts: &mut PoloniusFacts,
body: &Body<'tcx>,
location_table: &PoloniusLocationTable,
borrow_set: &BorrowSet<'tcx>,
Expand All @@ -26,7 +26,7 @@ pub(super) fn emit_loan_kills<'tcx>(

struct LoanKillsGenerator<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
facts: &'a mut AllFacts,
facts: &'a mut PoloniusFacts,
location_table: &'a PoloniusLocationTable,
borrow_set: &'a BorrowSet<'tcx>,
body: &'a Body<'tcx>,
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_borrowck/src/polonius/legacy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use self::facts::*;
///
/// The rest of the facts are emitted during typeck and liveness.
pub(crate) fn emit_facts<'tcx>(
all_facts: &mut Option<AllFacts>,
facts: &mut Option<PoloniusFacts>,
tcx: TyCtxt<'tcx>,
location_table: &PoloniusLocationTable,
body: &Body<'tcx>,
Expand All @@ -45,7 +45,7 @@ pub(crate) fn emit_facts<'tcx>(
universal_region_relations: &UniversalRegionRelations<'tcx>,
constraints: &MirTypeckRegionConstraints<'tcx>,
) {
let Some(facts) = all_facts else {
let Some(facts) = facts else {
// We don't do anything if there are no facts to fill.
return;
};
Expand All @@ -67,7 +67,7 @@ pub(crate) fn emit_facts<'tcx>(

/// Emit facts needed for move/init analysis: moves and assignments.
fn emit_move_facts(
facts: &mut AllFacts,
facts: &mut PoloniusFacts,
body: &Body<'_>,
location_table: &PoloniusLocationTable,
move_data: &MoveData<'_>,
Expand Down Expand Up @@ -139,7 +139,7 @@ fn emit_move_facts(

/// Emit universal regions facts, and their relations.
fn emit_universal_region_facts(
facts: &mut AllFacts,
facts: &mut PoloniusFacts,
borrow_set: &BorrowSet<'_>,
universal_region_relations: &UniversalRegionRelations<'_>,
) {
Expand Down Expand Up @@ -187,10 +187,10 @@ pub(crate) fn emit_drop_facts<'tcx>(
local: Local,
kind: &GenericArg<'tcx>,
universal_regions: &UniversalRegions<'tcx>,
all_facts: &mut Option<AllFacts>,
facts: &mut Option<PoloniusFacts>,
) {
debug!("emit_drop_facts(local={:?}, kind={:?}", local, kind);
let Some(facts) = all_facts.as_mut() else { return };
let Some(facts) = facts.as_mut() else { return };
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
tcx.for_each_free_region(kind, |drop_live_region| {
let region_vid = universal_regions.to_region_vid(drop_live_region);
Expand All @@ -201,7 +201,7 @@ pub(crate) fn emit_drop_facts<'tcx>(
/// Emit facts about the outlives constraints: the `subset` base relation, i.e. not a transitive
/// closure.
fn emit_outlives_facts<'tcx>(
facts: &mut AllFacts,
facts: &mut PoloniusFacts,
location_table: &PoloniusLocationTable,
constraints: &MirTypeckRegionConstraints<'tcx>,
) {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/type_check/liveness/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
fn add_extra_drop_facts(&mut self, relevant_live_locals: &[Local]) {
// This collect is more necessary than immediately apparent
// because these facts go into `add_drop_live_facts_for()`,
// which also writes to `all_facts`, and so this is genuinely
// which also writes to `polonius_facts`, and so this is genuinely
// a simultaneous overlapping mutable borrow.
// FIXME for future hackers: investigate whether this is
// actually necessary; these facts come from Polonius
// and probably maybe plausibly does not need to go back in.
// It may be necessary to just pick out the parts of
// `add_drop_live_facts_for()` that make sense.
let Some(facts) = self.cx.typeck.all_facts.as_ref() else { return };
let Some(facts) = self.cx.typeck.polonius_facts.as_ref() else { return };
let facts_to_add: Vec<_> = {
let relevant_live_locals: FxIndexSet<_> =
relevant_live_locals.iter().copied().collect();
Expand Down Expand Up @@ -583,7 +583,7 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
dropped_local,
&kind,
self.typeck.universal_regions,
self.typeck.all_facts,
self.typeck.polonius_facts,
);
}
}
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use crate::constraints::{OutlivesConstraint, OutlivesConstraintSet};
use crate::diagnostics::UniverseInfo;
use crate::member_constraints::MemberConstraintSet;
use crate::polonius::PoloniusContext;
use crate::polonius::legacy::{AllFacts, PoloniusLocationTable};
use crate::polonius::legacy::{PoloniusFacts, PoloniusLocationTable};
use crate::region_infer::TypeTest;
use crate::region_infer::values::{LivenessValues, PlaceholderIndex, PlaceholderIndices};
use crate::renumber::RegionCtxt;
Expand Down Expand Up @@ -100,7 +100,7 @@ mod relate_tys;
/// - `universal_regions` -- the universal regions from `body`s function signature
/// - `location_table` -- for datalog polonius, the map between `Location`s and `RichLocation`s
/// - `borrow_set` -- information about borrows occurring in `body`
/// - `all_facts` -- when using Polonius, this is the generated set of Polonius facts
/// - `polonius_facts` -- when using Polonius, this is the generated set of Polonius facts
/// - `flow_inits` -- results of a maybe-init dataflow analysis
/// - `move_data` -- move-data constructed when performing the maybe-init dataflow analysis
/// - `location_map` -- map between MIR `Location` and `PointIndex`
Expand All @@ -111,7 +111,7 @@ pub(crate) fn type_check<'a, 'tcx>(
universal_regions: UniversalRegions<'tcx>,
location_table: &PoloniusLocationTable,
borrow_set: &BorrowSet<'tcx>,
all_facts: &mut Option<AllFacts>,
polonius_facts: &mut Option<PoloniusFacts>,
flow_inits: ResultsCursor<'a, 'tcx, MaybeInitializedPlaces<'a, 'tcx>>,
move_data: &MoveData<'tcx>,
location_map: Rc<DenseLocationMap>,
Expand Down Expand Up @@ -165,7 +165,7 @@ pub(crate) fn type_check<'a, 'tcx>(
reported_errors: Default::default(),
universal_regions: &universal_region_relations.universal_regions,
location_table,
all_facts,
polonius_facts,
borrow_set,
constraints: &mut constraints,
polonius_context: &mut polonius_context,
Expand Down Expand Up @@ -495,14 +495,14 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {

// Use new sets of constraints and closure bounds so that we can
// modify their locations.
let all_facts = &mut None;
let polonius_facts = &mut None;
let mut constraints = Default::default();
let mut liveness_constraints =
LivenessValues::without_specific_points(Rc::new(DenseLocationMap::new(promoted_body)));
// Don't try to add borrow_region facts for the promoted MIR

let mut swap_constraints = |this: &mut Self| {
mem::swap(this.typeck.all_facts, all_facts);
mem::swap(this.typeck.polonius_facts, polonius_facts);
mem::swap(&mut this.typeck.constraints.outlives_constraints, &mut constraints);
mem::swap(&mut this.typeck.constraints.liveness_constraints, &mut liveness_constraints);
};
Expand Down Expand Up @@ -561,7 +561,7 @@ struct TypeChecker<'a, 'tcx> {
reported_errors: FxIndexSet<(Ty<'tcx>, Span)>,
universal_regions: &'a UniversalRegions<'tcx>,
location_table: &'a PoloniusLocationTable,
all_facts: &'a mut Option<AllFacts>,
polonius_facts: &'a mut Option<PoloniusFacts>,
borrow_set: &'a BorrowSet<'tcx>,
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
/// When using `-Zpolonius=next`, the helper data used to create polonius constraints.
Expand Down Expand Up @@ -2327,18 +2327,18 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
borrowed_place: &Place<'tcx>,
) {
// These constraints are only meaningful during borrowck:
let Self { borrow_set, location_table, all_facts, constraints, .. } = self;
let Self { borrow_set, location_table, polonius_facts, constraints, .. } = self;

// In Polonius mode, we also push a `loan_issued_at` fact
// linking the loan to the region (in some cases, though,
// there is no loan associated with this borrow expression --
// that occurs when we are borrowing an unsafe place, for
// example).
if let Some(all_facts) = all_facts {
if let Some(polonius_facts) = polonius_facts {
let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
if let Some(borrow_index) = borrow_set.get_index_of(&location) {
let region_vid = borrow_region.as_var();
all_facts.loan_issued_at.push((
polonius_facts.loan_issued_at.push((
region_vid.into(),
borrow_index,
location_table.mid_index(location),
Expand Down

0 comments on commit 36ea00c

Please sign in to comment.