-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Extend Polonius fact generation for (some) move tracking #62800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2adb246
996ba93
6b09477
9f39e00
6568b08
9cd1a11
560ef6d
28312b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,14 @@ use crate::borrow_check::nll::facts::AllFactsExt; | |
use crate::borrow_check::nll::type_check::{MirTypeckResults, MirTypeckRegionConstraints}; | ||
use crate::borrow_check::nll::region_infer::values::RegionValueElements; | ||
use crate::dataflow::indexes::BorrowIndex; | ||
use crate::dataflow::move_paths::{MoveData, MovePathIndex}; | ||
use crate::dataflow::move_paths::{InitLocation, MoveData, MovePathIndex, InitKind}; | ||
use crate::dataflow::FlowAtLocation; | ||
use crate::dataflow::MaybeInitializedPlaces; | ||
use crate::transform::MirSource; | ||
use crate::borrow_check::Upvar; | ||
use rustc::hir::def_id::DefId; | ||
use rustc::infer::InferCtxt; | ||
use rustc::mir::{ClosureOutlivesSubject, ClosureRegionRequirements, Local, Body, Promoted}; | ||
use rustc::mir::{ClosureOutlivesSubject, ClosureRegionRequirements, Local, Location, Body, LocalKind, BasicBlock, Promoted}; | ||
use rustc::ty::{self, RegionKind, RegionVid}; | ||
use rustc_data_structures::indexed_vec::IndexVec; | ||
use rustc_errors::Diagnostic; | ||
|
@@ -69,6 +69,61 @@ pub(in crate::borrow_check) fn replace_regions_in_mir<'cx, 'tcx>( | |
universal_regions | ||
} | ||
|
||
|
||
// This function populates an AllFacts instance with base facts related to | ||
// MovePaths and needed for the move analysis. | ||
fn populate_polonius_move_facts(all_facts: &mut AllFacts, move_data: &MoveData<'_>, location_table: &LocationTable, body: &Body<'_>) { | ||
all_facts.var_starts_path.extend(move_data.rev_lookup.iter_locals_enumerated().map(|(v, &m)| (v, m))); | ||
|
||
for (idx, move_path) in move_data.move_paths.iter_enumerated() { | ||
all_facts.parent.extend(move_path.parents(&move_data.move_paths).iter().map(|&parent| (parent, idx))); | ||
} | ||
|
||
// 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.initialized_at.push((init.path, location_table.start_index(first_statement))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting quirk. I guess this makes sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just say I scratched my head for quite some time before I figured out why this happened. The results were completely wrong otherwise. If you are curious about the details, there's a ton of logs on Zulip where I figure this out in the initialization topic. |
||
} | ||
|
||
} else { | ||
// In all other cases, the initialization just happens at the | ||
// midpoint, like any other effect. | ||
all_facts.initialized_at.push((init.path, location_table.mid_index(location))); | ||
} | ||
}, | ||
// Arguments are initialized on function entry | ||
InitLocation::Argument(local) => { | ||
assert!(body.local_kind(local) == LocalKind::Arg); | ||
let fn_entry = Location {block: BasicBlock::from_u32(0u32), statement_index: 0 }; | ||
all_facts.initialized_at.push((init.path, location_table.start_index(fn_entry))); | ||
|
||
} | ||
} | ||
} | ||
|
||
|
||
// moved_out_at | ||
// deinitialisation is assumed to always happen! | ||
all_facts.moved_out_at.extend(move_data.moves.iter().map(|mo| (mo.path, location_table.mid_index(mo.source)))); | ||
} | ||
|
||
/// Computes the (non-lexical) regions from the input MIR. | ||
/// | ||
/// This may result in errors being reported. | ||
|
@@ -123,6 +178,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>( | |
all_facts | ||
.universal_region | ||
.extend(universal_regions.universal_regions()); | ||
populate_polonius_move_facts(all_facts, move_data, location_table, body); | ||
} | ||
|
||
// Create the region inference context, taking ownership of the | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as noted in the polonius PR, ultimately I think we should leave the transitive computation here to polonius. But I guess I don't wnat to do that in this PR.