Skip to content

[WIP] polonius: adapt to the new fact format #68993

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/librustc_mir/borrow_check/facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ impl AllFactsExt for AllFacts {
killed,
outlives,
invalidates,
var_used,
var_defined,
var_drop_used,
var_uses_region,
var_drops_region,
child,
path_belongs_to_var,
initialized_at,
moved_out_at,
path_accessed_at,
var_used_at,
var_defined_at,
var_dropped_at,
use_of_var_derefs_origin,
drop_of_var_derefs_origin,
child_path,
path_is_var,
path_assigned_at_base,
path_moved_at_base,
path_accessed_at_base,
known_subset,
])
}
Expand Down
41 changes: 30 additions & 11 deletions src/librustc_mir/borrow_check/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ fn populate_polonius_move_facts(
body: &Body<'_>,
) {
all_facts
.path_belongs_to_var
.extend(move_data.rev_lookup.iter_locals_enumerated().map(|(v, &m)| (m, v)));
.path_is_var
.extend(
move_data
.rev_lookup
.iter_locals_enumerated()
.map(|(v, &m)| (m, v)));

for (child, move_path) in move_data.move_paths.iter_enumerated() {
all_facts
.child
.extend(move_path.parents(&move_data.move_paths).iter().map(|&parent| (child, parent)));
if let Some(parent) = move_path.parent {
all_facts.child_path.push((child, parent));
};
}

let fn_entry_start = location_table.start_index(Location {block: BasicBlock::from_u32(0u32), statement_index: 0 });

// initialized_at
for init in move_data.inits.iter() {
match init.location {
Expand All @@ -113,29 +119,42 @@ fn populate_polonius_move_facts(
// the successors, but not in the unwind block.
let first_statement = Location { block: successor, statement_index: 0 };
all_facts
.initialized_at
.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.initialized_at.push((init.path, location_table.mid_index(location)));
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);
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)));
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
.moved_out_at
.extend(move_data.moves.iter().map(|mo| (mo.path, location_table.mid_index(mo.source))));
.path_moved_at_base
.extend(
move_data
.moves
.iter()
.map(|mo| (mo.path, location_table.mid_index(mo.source))));
}

/// Computes the (non-lexical) regions from the input MIR.
Expand Down
42 changes: 21 additions & 21 deletions src/librustc_mir/borrow_check/type_check/liveness/polonius.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ type VarPointRelation = Vec<(Local, LocationIndex)>;
type PathPointRelation = Vec<(MovePathIndex, LocationIndex)>;

struct UseFactsExtractor<'me> {
var_defined: &'me mut VarPointRelation,
var_used: &'me mut VarPointRelation,
var_defined_at: &'me mut VarPointRelation,
var_used_at: &'me mut VarPointRelation,
location_table: &'me LocationTable,
var_drop_used: &'me mut Vec<(Local, Location)>,
var_dropped_at: &'me mut VarPointRelation,
move_data: &'me MoveData<'me>,
path_accessed_at: &'me mut PathPointRelation,
path_accessed_at_base: &'me mut PathPointRelation,
}

// A Visitor to walk through the MIR and extract point-wise facts
Expand All @@ -28,22 +28,22 @@ impl UseFactsExtractor<'_> {

fn insert_def(&mut self, local: Local, location: Location) {
debug!("UseFactsExtractor::insert_def()");
self.var_defined.push((local, self.location_to_index(location)));
self.var_defined_at.push((local, self.location_to_index(location)));
}

fn insert_use(&mut self, local: Local, location: Location) {
debug!("UseFactsExtractor::insert_use()");
self.var_used.push((local, self.location_to_index(location)));
self.var_used_at.push((local, self.location_to_index(location)));
}

fn insert_drop_use(&mut self, local: Local, location: Location) {
debug!("UseFactsExtractor::insert_drop_use()");
self.var_drop_used.push((local, location));
self.var_dropped_at.push((local, self.location_to_index(location)));
}

fn insert_path_access(&mut self, path: MovePathIndex, location: Location) {
debug!("UseFactsExtractor::insert_path_access({:?}, {:?})", path, location);
self.path_accessed_at.push((path, self.location_to_index(location)));
self.path_accessed_at_base.push((path, self.location_table.start_index(location)));
}

fn place_to_mpi(&self, place: &Place<'_>) -> Option<MovePathIndex> {
Expand Down Expand Up @@ -88,51 +88,51 @@ pub(super) fn populate_access_facts(
body: ReadOnlyBodyAndCache<'_, 'tcx>,
location_table: &LocationTable,
move_data: &MoveData<'_>,
drop_used: &mut Vec<(Local, Location)>,
dropped_at: &mut Vec<(Local, Location)>,
) {
debug!("populate_access_facts()");

if let Some(facts) = typeck.borrowck_context.all_facts.as_mut() {
let mut extractor = UseFactsExtractor {
var_defined: &mut facts.var_defined,
var_used: &mut facts.var_used,
var_drop_used: drop_used,
path_accessed_at: &mut facts.path_accessed_at,
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,
};
extractor.visit_body(body);

facts.var_drop_used.extend(
drop_used.iter().map(|&(local, location)| (local, location_table.mid_index(location))),
facts.var_dropped_at.extend(
dropped_at.iter().map(|&(local, location)| (local, location_table.mid_index(location))),
);

for (local, local_decl) in body.local_decls.iter_enumerated() {
debug!("add var_uses_regions facts - local={:?}, type={:?}", local, local_decl.ty);
debug!("add use_of_var_derefs_origin facts - local={:?}, type={:?}", local, local_decl.ty);
let _prof_timer = typeck.infcx.tcx.prof.generic_activity("polonius_fact_generation");
let universal_regions = &typeck.borrowck_context.universal_regions;
typeck.infcx.tcx.for_each_free_region(&local_decl.ty, |region| {
let region_vid = universal_regions.to_region_vid(region);
facts.var_uses_region.push((local, region_vid));
facts.use_of_var_derefs_origin.push((local, region_vid));
});
}
}
}

// For every potentially drop()-touched region `region` in `local`'s type
// (`kind`), emit a Polonius `var_drops_region(local, region)` fact.
pub(super) fn add_var_drops_regions(
// (`kind`), emit a Polonius `use_of_var_derefs_origin(local, origin)` fact.
pub(super) fn add_drop_of_var_derefs_origin(
typeck: &mut TypeChecker<'_, 'tcx>,
local: Local,
kind: &GenericArg<'tcx>,
) {
debug!("add_var_drops_region(local={:?}, kind={:?}", local, kind);
debug!("add_drop_of_var_derefs_origin(local={:?}, kind={:?}", local, kind);
if let Some(facts) = typeck.borrowck_context.all_facts.as_mut() {
let _prof_timer = typeck.infcx.tcx.prof.generic_activity("polonius_fact_generation");
let universal_regions = &typeck.borrowck_context.universal_regions;
typeck.infcx.tcx.for_each_free_region(kind, |drop_live_region| {
let region_vid = universal_regions.to_region_vid(drop_live_region);
facts.var_drops_region.push((local, region_vid));
facts.drop_of_var_derefs_origin.push((local, region_vid));
});
}
}
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/type_check/liveness/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ impl LivenessContext<'_, '_, '_, 'tcx> {
for &kind in &drop_data.dropck_result.kinds {
Self::make_all_regions_live(self.elements, &mut self.typeck, kind, live_at);

polonius::add_var_drops_regions(&mut self.typeck, dropped_local, &kind);
polonius::add_drop_of_var_derefs_origin(&mut self.typeck, dropped_local, &kind);
}
}

Expand Down