summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_borrowck/src/polonius
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /compiler/rustc_borrowck/src/polonius
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_borrowck/src/polonius')
-rw-r--r--compiler/rustc_borrowck/src/polonius/loan_invalidations.rs424
-rw-r--r--compiler/rustc_borrowck/src/polonius/loan_kills.rs147
-rw-r--r--compiler/rustc_borrowck/src/polonius/mod.rs188
3 files changed, 759 insertions, 0 deletions
diff --git a/compiler/rustc_borrowck/src/polonius/loan_invalidations.rs b/compiler/rustc_borrowck/src/polonius/loan_invalidations.rs
new file mode 100644
index 000000000..232bd7418
--- /dev/null
+++ b/compiler/rustc_borrowck/src/polonius/loan_invalidations.rs
@@ -0,0 +1,424 @@
+#![deny(rustc::untranslatable_diagnostic)]
+#![deny(rustc::diagnostic_outside_of_impl)]
+use rustc_data_structures::graph::dominators::Dominators;
+use rustc_middle::mir::visit::Visitor;
+use rustc_middle::mir::{self, BasicBlock, Body, Location, NonDivergingIntrinsic, Place, Rvalue};
+use rustc_middle::mir::{BorrowKind, Mutability, Operand};
+use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
+use rustc_middle::mir::{Statement, StatementKind};
+use rustc_middle::ty::TyCtxt;
+
+use crate::{
+ borrow_set::BorrowSet, facts::AllFacts, location::LocationTable, path_utils::*, AccessDepth,
+ Activation, ArtificialField, BorrowIndex, Deep, LocalMutationIsAllowed, Read, ReadKind,
+ ReadOrWrite, Reservation, Shallow, Write, WriteKind,
+};
+
+/// Emit `loan_invalidated_at` facts.
+pub(super) fn emit_loan_invalidations<'tcx>(
+ tcx: TyCtxt<'tcx>,
+ all_facts: &mut AllFacts,
+ location_table: &LocationTable,
+ body: &Body<'tcx>,
+ borrow_set: &BorrowSet<'tcx>,
+) {
+ let dominators = body.basic_blocks.dominators();
+ let mut visitor =
+ LoanInvalidationsGenerator { all_facts, borrow_set, tcx, location_table, body, dominators };
+ visitor.visit_body(body);
+}
+
+struct LoanInvalidationsGenerator<'cx, 'tcx> {
+ tcx: TyCtxt<'tcx>,
+ all_facts: &'cx mut AllFacts,
+ location_table: &'cx LocationTable,
+ body: &'cx Body<'tcx>,
+ dominators: &'cx Dominators<BasicBlock>,
+ borrow_set: &'cx BorrowSet<'tcx>,
+}
+
+/// Visits the whole MIR and generates `invalidates()` facts.
+/// Most of the code implementing this was stolen from `borrow_check/mod.rs`.
+impl<'cx, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'cx, 'tcx> {
+ fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
+ self.check_activations(location);
+
+ match &statement.kind {
+ StatementKind::Assign(box (lhs, rhs)) => {
+ self.consume_rvalue(location, rhs);
+
+ self.mutate_place(location, *lhs, Shallow(None));
+ }
+ StatementKind::FakeRead(box (_, _)) => {
+ // Only relevant for initialized/liveness/safety checks.
+ }
+ StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => {
+ self.consume_operand(location, op);
+ }
+ StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(mir::CopyNonOverlapping {
+ src,
+ dst,
+ count,
+ })) => {
+ self.consume_operand(location, src);
+ self.consume_operand(location, dst);
+ self.consume_operand(location, count);
+ }
+ // Only relevant for mir typeck
+ StatementKind::AscribeUserType(..)
+ // Only relevant for liveness and unsafeck
+ | StatementKind::PlaceMention(..)
+ // Doesn't have any language semantics
+ | StatementKind::Coverage(..)
+ // Does not actually affect borrowck
+ | StatementKind::StorageLive(..) => {}
+ StatementKind::StorageDead(local) => {
+ self.access_place(
+ location,
+ Place::from(*local),
+ (Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
+ LocalMutationIsAllowed::Yes,
+ );
+ }
+ StatementKind::ConstEvalCounter
+ | StatementKind::Nop
+ | StatementKind::Retag { .. }
+ | StatementKind::Deinit(..)
+ | StatementKind::SetDiscriminant { .. } => {
+ bug!("Statement not allowed in this MIR phase")
+ }
+ }
+
+ self.super_statement(statement, location);
+ }
+
+ fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
+ self.check_activations(location);
+
+ match &terminator.kind {
+ TerminatorKind::SwitchInt { discr, targets: _ } => {
+ self.consume_operand(location, discr);
+ }
+ TerminatorKind::Drop { place: drop_place, target: _, unwind: _, replace } => {
+ let write_kind =
+ if *replace { WriteKind::Replace } else { WriteKind::StorageDeadOrDrop };
+ self.access_place(
+ location,
+ *drop_place,
+ (AccessDepth::Drop, Write(write_kind)),
+ LocalMutationIsAllowed::Yes,
+ );
+ }
+ TerminatorKind::Call {
+ func,
+ args,
+ destination,
+ target: _,
+ unwind: _,
+ call_source: _,
+ fn_span: _,
+ } => {
+ self.consume_operand(location, func);
+ for arg in args {
+ self.consume_operand(location, arg);
+ }
+ self.mutate_place(location, *destination, Deep);
+ }
+ TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {
+ self.consume_operand(location, cond);
+ use rustc_middle::mir::AssertKind;
+ if let AssertKind::BoundsCheck { len, index } = &**msg {
+ self.consume_operand(location, len);
+ self.consume_operand(location, index);
+ }
+ }
+ TerminatorKind::Yield { value, resume, resume_arg, drop: _ } => {
+ self.consume_operand(location, value);
+
+ // Invalidate all borrows of local places
+ let borrow_set = self.borrow_set;
+ let resume = self.location_table.start_index(resume.start_location());
+ for (i, data) in borrow_set.iter_enumerated() {
+ if borrow_of_local_data(data.borrowed_place) {
+ self.all_facts.loan_invalidated_at.push((resume, i));
+ }
+ }
+
+ self.mutate_place(location, *resume_arg, Deep);
+ }
+ TerminatorKind::UnwindResume
+ | TerminatorKind::Return
+ | TerminatorKind::CoroutineDrop => {
+ // Invalidate all borrows of local places
+ let borrow_set = self.borrow_set;
+ let start = self.location_table.start_index(location);
+ for (i, data) in borrow_set.iter_enumerated() {
+ if borrow_of_local_data(data.borrowed_place) {
+ self.all_facts.loan_invalidated_at.push((start, i));
+ }
+ }
+ }
+ TerminatorKind::InlineAsm {
+ template: _,
+ operands,
+ options: _,
+ line_spans: _,
+ destination: _,
+ unwind: _,
+ } => {
+ for op in operands {
+ match op {
+ InlineAsmOperand::In { reg: _, value } => {
+ self.consume_operand(location, value);
+ }
+ InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
+ if let &Some(place) = place {
+ self.mutate_place(location, place, Shallow(None));
+ }
+ }
+ InlineAsmOperand::InOut { reg: _, late: _, in_value, out_place } => {
+ self.consume_operand(location, in_value);
+ if let &Some(out_place) = out_place {
+ self.mutate_place(location, out_place, Shallow(None));
+ }
+ }
+ InlineAsmOperand::Const { value: _ }
+ | InlineAsmOperand::SymFn { value: _ }
+ | InlineAsmOperand::SymStatic { def_id: _ } => {}
+ }
+ }
+ }
+ TerminatorKind::Goto { target: _ }
+ | TerminatorKind::UnwindTerminate(_)
+ | TerminatorKind::Unreachable
+ | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
+ | TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {
+ // no data used, thus irrelevant to borrowck
+ }
+ }
+
+ self.super_terminator(terminator, location);
+ }
+}
+
+impl<'cx, 'tcx> LoanInvalidationsGenerator<'cx, 'tcx> {
+ /// Simulates mutation of a place.
+ fn mutate_place(&mut self, location: Location, place: Place<'tcx>, kind: AccessDepth) {
+ self.access_place(
+ location,
+ place,
+ (kind, Write(WriteKind::Mutate)),
+ LocalMutationIsAllowed::ExceptUpvars,
+ );
+ }
+
+ /// Simulates consumption of an operand.
+ fn consume_operand(&mut self, location: Location, operand: &Operand<'tcx>) {
+ match *operand {
+ Operand::Copy(place) => {
+ self.access_place(
+ location,
+ place,
+ (Deep, Read(ReadKind::Copy)),
+ LocalMutationIsAllowed::No,
+ );
+ }
+ Operand::Move(place) => {
+ self.access_place(
+ location,
+ place,
+ (Deep, Write(WriteKind::Move)),
+ LocalMutationIsAllowed::Yes,
+ );
+ }
+ Operand::Constant(_) => {}
+ }
+ }
+
+ // Simulates consumption of an rvalue
+ fn consume_rvalue(&mut self, location: Location, rvalue: &Rvalue<'tcx>) {
+ match rvalue {
+ &Rvalue::Ref(_ /*rgn*/, bk, place) => {
+ let access_kind = match bk {
+ BorrowKind::Fake => {
+ (Shallow(Some(ArtificialField::FakeBorrow)), Read(ReadKind::Borrow(bk)))
+ }
+ BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
+ BorrowKind::Mut { .. } => {
+ let wk = WriteKind::MutableBorrow(bk);
+ if allow_two_phase_borrow(bk) {
+ (Deep, Reservation(wk))
+ } else {
+ (Deep, Write(wk))
+ }
+ }
+ };
+
+ self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
+ }
+
+ &Rvalue::AddressOf(mutability, place) => {
+ let access_kind = match mutability {
+ Mutability::Mut => (
+ Deep,
+ Write(WriteKind::MutableBorrow(BorrowKind::Mut {
+ kind: mir::MutBorrowKind::Default,
+ })),
+ ),
+ Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
+ };
+
+ self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
+ }
+
+ Rvalue::ThreadLocalRef(_) => {}
+
+ Rvalue::Use(operand)
+ | Rvalue::Repeat(operand, _)
+ | Rvalue::UnaryOp(_ /*un_op*/, operand)
+ | Rvalue::Cast(_ /*cast_kind*/, operand, _ /*ty*/)
+ | Rvalue::ShallowInitBox(operand, _ /*ty*/) => self.consume_operand(location, operand),
+
+ &Rvalue::CopyForDeref(place) => {
+ let op = &Operand::Copy(place);
+ self.consume_operand(location, op);
+ }
+
+ &(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
+ let af = match rvalue {
+ Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
+ Rvalue::Discriminant(..) => None,
+ _ => unreachable!(),
+ };
+ self.access_place(
+ location,
+ place,
+ (Shallow(af), Read(ReadKind::Copy)),
+ LocalMutationIsAllowed::No,
+ );
+ }
+
+ Rvalue::BinaryOp(_bin_op, box (operand1, operand2))
+ | Rvalue::CheckedBinaryOp(_bin_op, box (operand1, operand2)) => {
+ self.consume_operand(location, operand1);
+ self.consume_operand(location, operand2);
+ }
+
+ Rvalue::NullaryOp(_op, _ty) => {}
+
+ Rvalue::Aggregate(_, operands) => {
+ for operand in operands {
+ self.consume_operand(location, operand);
+ }
+ }
+ }
+ }
+
+ /// Simulates an access to a place.
+ fn access_place(
+ &mut self,
+ location: Location,
+ place: Place<'tcx>,
+ kind: (AccessDepth, ReadOrWrite),
+ _is_local_mutation_allowed: LocalMutationIsAllowed,
+ ) {
+ let (sd, rw) = kind;
+ // note: not doing check_access_permissions checks because they don't generate invalidates
+ self.check_access_for_conflict(location, place, sd, rw);
+ }
+
+ fn check_access_for_conflict(
+ &mut self,
+ location: Location,
+ place: Place<'tcx>,
+ sd: AccessDepth,
+ rw: ReadOrWrite,
+ ) {
+ debug!(
+ "check_access_for_conflict(location={:?}, place={:?}, sd={:?}, rw={:?})",
+ location, place, sd, rw,
+ );
+ each_borrow_involving_path(
+ self,
+ self.tcx,
+ self.body,
+ location,
+ (sd, place),
+ self.borrow_set,
+ |_| true,
+ |this, borrow_index, borrow| {
+ match (rw, borrow.kind) {
+ // Obviously an activation is compatible with its own
+ // reservation (or even prior activating uses of same
+ // borrow); so don't check if they interfere.
+ //
+ // NOTE: *reservations* do conflict with themselves;
+ // thus aren't injecting unsoundness w/ this check.)
+ (Activation(_, activating), _) if activating == borrow_index => {
+ // Activating a borrow doesn't generate any invalidations, since we
+ // have already taken the reservation
+ }
+
+ (Read(_), BorrowKind::Fake | BorrowKind::Shared)
+ | (Read(ReadKind::Borrow(BorrowKind::Fake)), BorrowKind::Mut { .. }) => {
+ // Reads don't invalidate shared or shallow borrows
+ }
+
+ (Read(_), BorrowKind::Mut { .. }) => {
+ // Reading from mere reservations of mutable-borrows is OK.
+ if !is_active(this.dominators, borrow, location) {
+ // If the borrow isn't active yet, reads don't invalidate it
+ assert!(allow_two_phase_borrow(borrow.kind));
+ return Control::Continue;
+ }
+
+ // Unique and mutable borrows are invalidated by reads from any
+ // involved path
+ this.emit_loan_invalidated_at(borrow_index, location);
+ }
+
+ (Reservation(_) | Activation(_, _) | Write(_), _) => {
+ // unique or mutable borrows are invalidated by writes.
+ // Reservations count as writes since we need to check
+ // that activating the borrow will be OK
+ // FIXME(bob_twinkles) is this actually the right thing to do?
+ this.emit_loan_invalidated_at(borrow_index, location);
+ }
+ }
+ Control::Continue
+ },
+ );
+ }
+
+ /// Generates a new `loan_invalidated_at(L, B)` fact.
+ fn emit_loan_invalidated_at(&mut self, b: BorrowIndex, l: Location) {
+ let lidx = self.location_table.start_index(l);
+ self.all_facts.loan_invalidated_at.push((lidx, b));
+ }
+
+ fn check_activations(&mut self, location: Location) {
+ // Two-phase borrow support: For each activation that is newly
+ // generated at this statement, check if it interferes with
+ // another borrow.
+ for &borrow_index in self.borrow_set.activations_at_location(location) {
+ let borrow = &self.borrow_set[borrow_index];
+
+ // only mutable borrows should be 2-phase
+ assert!(match borrow.kind {
+ BorrowKind::Shared | BorrowKind::Fake => false,
+ BorrowKind::Mut { .. } => true,
+ });
+
+ self.access_place(
+ location,
+ borrow.borrowed_place,
+ (Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),
+ LocalMutationIsAllowed::No,
+ );
+
+ // We do not need to call `check_if_path_or_subpath_is_moved`
+ // again, as we already called it when we made the
+ // initial reservation.
+ }
+ }
+}
diff --git a/compiler/rustc_borrowck/src/polonius/loan_kills.rs b/compiler/rustc_borrowck/src/polonius/loan_kills.rs
new file mode 100644
index 000000000..5df943837
--- /dev/null
+++ b/compiler/rustc_borrowck/src/polonius/loan_kills.rs
@@ -0,0 +1,147 @@
+#![deny(rustc::untranslatable_diagnostic)]
+#![deny(rustc::diagnostic_outside_of_impl)]
+use rustc_middle::mir::visit::Visitor;
+use rustc_middle::mir::{
+ Body, Local, Location, Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind,
+ Terminator, TerminatorKind,
+};
+use rustc_middle::ty::TyCtxt;
+
+use crate::{borrow_set::BorrowSet, facts::AllFacts, location::LocationTable, places_conflict};
+
+/// Emit `loan_killed_at` and `cfg_edge` facts at the same time.
+pub(super) fn emit_loan_kills<'tcx>(
+ tcx: TyCtxt<'tcx>,
+ all_facts: &mut AllFacts,
+ location_table: &LocationTable,
+ body: &Body<'tcx>,
+ borrow_set: &BorrowSet<'tcx>,
+) {
+ let mut visitor = LoanKillsGenerator { borrow_set, tcx, location_table, all_facts, body };
+ for (bb, data) in body.basic_blocks.iter_enumerated() {
+ visitor.visit_basic_block_data(bb, data);
+ }
+}
+
+struct LoanKillsGenerator<'cx, 'tcx> {
+ tcx: TyCtxt<'tcx>,
+ all_facts: &'cx mut AllFacts,
+ location_table: &'cx LocationTable,
+ borrow_set: &'cx BorrowSet<'tcx>,
+ body: &'cx Body<'tcx>,
+}
+
+impl<'cx, 'tcx> Visitor<'tcx> for LoanKillsGenerator<'cx, 'tcx> {
+ fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
+ // Also record CFG facts here.
+ self.all_facts.cfg_edge.push((
+ self.location_table.start_index(location),
+ self.location_table.mid_index(location),
+ ));
+
+ self.all_facts.cfg_edge.push((
+ self.location_table.mid_index(location),
+ self.location_table.start_index(location.successor_within_block()),
+ ));
+
+ // If there are borrows on this now dead local, we need to record them as `killed`.
+ if let StatementKind::StorageDead(local) = statement.kind {
+ self.record_killed_borrows_for_local(local, location);
+ }
+
+ self.super_statement(statement, location);
+ }
+
+ fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
+ // When we see `X = ...`, then kill borrows of
+ // `(*X).foo` and so forth.
+ self.record_killed_borrows_for_place(*place, location);
+ self.super_assign(place, rvalue, location);
+ }
+
+ fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
+ // Also record CFG facts here.
+ self.all_facts.cfg_edge.push((
+ self.location_table.start_index(location),
+ self.location_table.mid_index(location),
+ ));
+
+ let successor_blocks = terminator.successors();
+ self.all_facts.cfg_edge.reserve(successor_blocks.size_hint().0);
+ for successor_block in successor_blocks {
+ self.all_facts.cfg_edge.push((
+ self.location_table.mid_index(location),
+ self.location_table.start_index(successor_block.start_location()),
+ ));
+ }
+
+ // A `Call` terminator's return value can be a local which has borrows,
+ // so we need to record those as `killed` as well.
+ if let TerminatorKind::Call { destination, .. } = terminator.kind {
+ self.record_killed_borrows_for_place(destination, location);
+ }
+
+ self.super_terminator(terminator, location);
+ }
+}
+
+impl<'tcx> LoanKillsGenerator<'_, 'tcx> {
+ /// Records the borrows on the specified place as `killed`. For example, when assigning to a
+ /// local, or on a call's return destination.
+ fn record_killed_borrows_for_place(&mut self, place: Place<'tcx>, location: Location) {
+ // Depending on the `Place` we're killing:
+ // - if it's a local, or a single deref of a local,
+ // we kill all the borrows on the local.
+ // - if it's a deeper projection, we have to filter which
+ // of the borrows are killed: the ones whose `borrowed_place`
+ // conflicts with the `place`.
+ match place.as_ref() {
+ PlaceRef { local, projection: &[] }
+ | PlaceRef { local, projection: &[ProjectionElem::Deref] } => {
+ debug!(
+ "Recording `killed` facts for borrows of local={:?} at location={:?}",
+ local, location
+ );
+
+ self.record_killed_borrows_for_local(local, location);
+ }
+
+ PlaceRef { local, projection: &[.., _] } => {
+ // Kill conflicting borrows of the innermost local.
+ debug!(
+ "Recording `killed` facts for borrows of \
+ innermost projected local={:?} at location={:?}",
+ local, location
+ );
+
+ if let Some(borrow_indices) = self.borrow_set.local_map.get(&local) {
+ for &borrow_index in borrow_indices {
+ let places_conflict = places_conflict::places_conflict(
+ self.tcx,
+ self.body,
+ self.borrow_set[borrow_index].borrowed_place,
+ place,
+ places_conflict::PlaceConflictBias::NoOverlap,
+ );
+
+ if places_conflict {
+ let location_index = self.location_table.mid_index(location);
+ self.all_facts.loan_killed_at.push((borrow_index, location_index));
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /// Records the borrows on the specified local as `killed`.
+ fn record_killed_borrows_for_local(&mut self, local: Local, location: Location) {
+ if let Some(borrow_indices) = self.borrow_set.local_map.get(&local) {
+ let location_index = self.location_table.mid_index(location);
+ self.all_facts.loan_killed_at.reserve(borrow_indices.len());
+ for &borrow_index in borrow_indices {
+ self.all_facts.loan_killed_at.push((borrow_index, location_index));
+ }
+ }
+ }
+}
diff --git a/compiler/rustc_borrowck/src/polonius/mod.rs b/compiler/rustc_borrowck/src/polonius/mod.rs
new file mode 100644
index 000000000..40126d50d
--- /dev/null
+++ b/compiler/rustc_borrowck/src/polonius/mod.rs
@@ -0,0 +1,188 @@
+//! Functions dedicated to fact generation for the `-Zpolonius=legacy` datalog implementation.
+//!
+//! Will be removed in the future, once the in-tree `-Zpolonius=next` implementation reaches feature
+//! parity.
+
+use rustc_middle::mir::{Body, LocalKind, Location, START_BLOCK};
+use rustc_middle::ty::TyCtxt;
+use rustc_mir_dataflow::move_paths::{InitKind, InitLocation, MoveData};
+
+use crate::borrow_set::BorrowSet;
+use crate::facts::AllFacts;
+use crate::location::LocationTable;
+use crate::type_check::free_region_relations::UniversalRegionRelations;
+use crate::universal_regions::UniversalRegions;
+
+mod loan_invalidations;
+mod loan_kills;
+
+/// 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
+///
+/// The rest of the facts are emitted during typeck and liveness.
+pub(crate) fn emit_facts<'tcx>(
+ all_facts: &mut Option<AllFacts>,
+ tcx: TyCtxt<'tcx>,
+ location_table: &LocationTable,
+ body: &Body<'tcx>,
+ borrow_set: &BorrowSet<'tcx>,
+ move_data: &MoveData<'_>,
+ universal_regions: &UniversalRegions<'_>,
+ universal_region_relations: &UniversalRegionRelations<'_>,
+) {
+ let Some(all_facts) = all_facts else {
+ // We don't do anything if there are no facts to fill.
+ return;
+ };
+ 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_regions,
+ &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);
+}
+
+/// Emit facts needed for move/init analysis: moves and assignments.
+fn emit_move_facts(
+ all_facts: &mut AllFacts,
+ move_data: &MoveData<'_>,
+ location_table: &LocationTable,
+ body: &Body<'_>,
+) {
+ all_facts
+ .path_is_var
+ .extend(move_data.rev_lookup.iter_locals_enumerated().map(|(l, r)| (r, l)));
+
+ for (child, move_path) in move_data.move_paths.iter_enumerated() {
+ if let Some(parent) = move_path.parent {
+ all_facts.child_path.push((child, parent));
+ }
+ }
+
+ let fn_entry_start =
+ location_table.start_index(Location { block: START_BLOCK, statement_index: 0 });
+
+ // initialized_at
+ for init in move_data.inits.iter() {
+ match init.location {
+ InitLocation::Statement(location) => {
+ let block_data = &body[location.block];
+ let is_terminator = location.statement_index == block_data.statements.len();
+
+ if is_terminator && init.kind == InitKind::NonPanicPathOnly {
+ // We are at the terminator of an init that has a panic path,
+ // and where the init should not happen on panic
+
+ for successor in block_data.terminator().successors() {
+ if body[successor].is_cleanup {
+ continue;
+ }
+
+ // The initialization happened in (or rather, when arriving at)
+ // the successors, but not in the unwind block.
+ let first_statement = Location { block: successor, statement_index: 0 };
+ all_facts
+ .path_assigned_at_base
+ .push((init.path, location_table.start_index(first_statement)));
+ }
+ } else {
+ // In all other cases, the initialization just happens at the
+ // midpoint, like any other effect.
+ all_facts
+ .path_assigned_at_base
+ .push((init.path, location_table.mid_index(location)));
+ }
+ }
+ // Arguments are initialized on function entry
+ InitLocation::Argument(local) => {
+ assert!(body.local_kind(local) == LocalKind::Arg);
+ all_facts.path_assigned_at_base.push((init.path, fn_entry_start));
+ }
+ }
+ }
+
+ for (local, path) in move_data.rev_lookup.iter_locals_enumerated() {
+ if body.local_kind(local) != LocalKind::Arg {
+ // Non-arguments start out deinitialised; we simulate this with an
+ // initial move:
+ all_facts.path_moved_at_base.push((path, fn_entry_start));
+ }
+ }
+
+ // moved_out_at
+ // deinitialisation is assumed to always happen!
+ all_facts
+ .path_moved_at_base
+ .extend(move_data.moves.iter().map(|mo| (mo.path, location_table.mid_index(mo.source))));
+}
+
+/// Emit universal regions facts, and their relations.
+fn emit_universal_region_facts(
+ all_facts: &mut AllFacts,
+ borrow_set: &BorrowSet<'_>,
+ universal_regions: &UniversalRegions<'_>,
+ universal_region_relations: &UniversalRegionRelations<'_>,
+) {
+ // 1: universal regions are modeled in Polonius as a pair:
+ // - the universal region vid itself.
+ // - a "placeholder loan" associated to this universal region. Since they don't exist in
+ // the `borrow_set`, their `BorrowIndex` are synthesized as the universal region index
+ // added to the existing number of loans, as if they succeeded them in the set.
+ //
+ all_facts.universal_region.extend(universal_regions.universal_regions());
+ let borrow_count = borrow_set.len();
+ debug!(
+ "emit_universal_region_facts: polonius placeholders, num_universals={}, borrow_count={}",
+ universal_regions.len(),
+ borrow_count
+ );
+
+ for universal_region in universal_regions.universal_regions() {
+ let universal_region_idx = universal_region.index();
+ let placeholder_loan_idx = borrow_count + universal_region_idx;
+ all_facts.placeholder.push((universal_region, placeholder_loan_idx.into()));
+ }
+
+ // 2: the universal region relations `outlives` constraints are emitted as
+ // `known_placeholder_subset` facts.
+ for (fr1, fr2) in universal_region_relations.known_outlives() {
+ if fr1 != fr2 {
+ debug!(
+ "emit_universal_region_facts: emitting polonius `known_placeholder_subset` \
+ fr1={:?}, fr2={:?}",
+ fr1, fr2
+ );
+ all_facts.known_placeholder_subset.push((fr1, fr2));
+ }
+ }
+}
+
+/// 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);
+}