Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
- move constraints to an Option
- check `-Zpolonius=next` only once
- rewrite fixme comments to make the actionable part clear
  • Loading branch information
lqd committed Dec 18, 2024
1 parent c75c517 commit aeb3d10
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ fn do_mir_borrowck<'tcx>(
body,
&regioncx,
&borrow_set,
&localized_outlives_constraints,
localized_outlives_constraints,
&opt_closure_req,
);

Expand Down
17 changes: 7 additions & 10 deletions compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(crate) struct NllOutput<'tcx> {
pub nll_errors: RegionErrors<'tcx>,

/// When using `-Zpolonius=next`: the localized typeck and liveness constraints.
pub localized_outlives_constraints: LocalizedOutlivesConstraintSet,
pub localized_outlives_constraints: Option<LocalizedOutlivesConstraintSet>,
}

/// Rewrites the regions in the MIR to use NLL variables, also scraping out the set of universal
Expand Down Expand Up @@ -141,15 +141,12 @@ pub(crate) fn compute_regions<'a, 'tcx>(

// If requested for `-Zpolonius=next`, convert NLL constraints to localized outlives
// constraints.
let mut localized_outlives_constraints = LocalizedOutlivesConstraintSet::default();
if infcx.tcx.sess.opts.unstable_opts.polonius.is_next_enabled() {
polonius::create_localized_constraints(
&mut regioncx,
infcx.infcx.tcx,
body,
&mut localized_outlives_constraints,
);
}
let localized_outlives_constraints =
if infcx.tcx.sess.opts.unstable_opts.polonius.is_next_enabled() {
Some(polonius::create_localized_constraints(&mut regioncx, body))
} else {
None
};

// If requested: dump NLL facts, and run legacy polonius analysis.
let polonius_output = all_facts.as_ref().and_then(|all_facts| {
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_borrowck/src/polonius/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ pub(crate) fn dump_polonius_mir<'tcx>(
body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>,
borrow_set: &BorrowSet<'tcx>,
localized_outlives_constraints: &LocalizedOutlivesConstraintSet,
localized_outlives_constraints: Option<LocalizedOutlivesConstraintSet>,
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
) {
let tcx = infcx.tcx;
if !tcx.sess.opts.unstable_opts.polonius.is_next_enabled() {
return;
}

let localized_outlives_constraints = localized_outlives_constraints
.expect("missing localized constraints with `-Zpolonius=next`");

// We want the NLL extra comments printed by default in NLL MIR dumps (they were removed in
// #112346). Specifying `-Z mir-include-spans` on the CLI still has priority: for example,
// they're always disabled in mir-opt tests to make working with blessed dumps easier.
Expand All @@ -48,7 +51,7 @@ pub(crate) fn dump_polonius_mir<'tcx>(
regioncx,
closure_region_requirements,
borrow_set,
localized_outlives_constraints,
&localized_outlives_constraints,
pass_where,
out,
)
Expand Down
30 changes: 14 additions & 16 deletions compiler/rustc_borrowck/src/polonius/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub(crate) use dump::dump_polonius_mir;
pub(crate) mod legacy;

use rustc_middle::mir::{Body, Location};
use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::points::PointIndex;

use crate::RegionInferenceContext;
Expand All @@ -49,34 +48,31 @@ use crate::region_infer::values::LivenessValues;
use crate::type_check::Locations;
use crate::universal_regions::UniversalRegions;

/// When using `-Zpolonius=next`, fills the given constraint set by:
/// Creates a constraint set for `-Zpolonius=next` by:
/// - converting NLL typeck constraints to be localized
/// - encoding liveness constraints
pub(crate) fn create_localized_constraints<'tcx>(
regioncx: &mut RegionInferenceContext<'tcx>,
tcx: TyCtxt<'tcx>,
body: &Body<'tcx>,
localized_outlives_constraints: &mut LocalizedOutlivesConstraintSet,
) {
if !tcx.sess.opts.unstable_opts.polonius.is_next_enabled() {
return;
}

) -> LocalizedOutlivesConstraintSet {
let mut localized_outlives_constraints = LocalizedOutlivesConstraintSet::default();
convert_typeck_constraints(
body,
regioncx.liveness_constraints(),
regioncx.outlives_constraints(),
localized_outlives_constraints,
&mut localized_outlives_constraints,
);
create_liveness_constraints(
body,
regioncx.liveness_constraints(),
regioncx.universal_regions(),
localized_outlives_constraints,
&mut localized_outlives_constraints,
);

// FIXME: here, we can trace loan reachability in the constraint graph and record this as loan
// liveness for the next step in the chain, the NLL loan scope and active loans computations.

localized_outlives_constraints
}

/// Propagate loans throughout the subset graph at a given point (with some subtleties around the
Expand All @@ -90,8 +86,9 @@ fn convert_typeck_constraints<'tcx>(
for outlives_constraint in outlives_constraints {
match outlives_constraint.locations {
Locations::All(_) => {
// FIXME: for now, turn logical constraints holding at all points into physical
// edges at every point in the graph. We can encode this into *traversal* instead.
// For now, turn logical constraints holding at all points into physical edges at
// every point in the graph.
// FIXME: encode this into *traversal* instead.
for (block, bb) in body.basic_blocks.iter_enumerated() {
let statement_count = bb.statements.len();
for statement_index in 0..=statement_count {
Expand Down Expand Up @@ -168,9 +165,10 @@ fn propagate_loans_between_points(
localized_outlives_constraints: &mut LocalizedOutlivesConstraintSet,
) {
// Universal regions are semantically live at all points.
// FIXME: We always have universal regions but they're not always (or often) involved in the
// subset graph. So for now, we emit this edge, but we only need to emit edges for universal
// regions that existential regions can actually reach.
// Note: we always have universal regions but they're not always (or often) involved in the
// subset graph. For now, we emit all their edges unconditionally, but some of these subgraphs
// will be disconnected from the rest of the graph and thus, unnecessary.
// FIXME: only emit the edges of universal regions that existential regions can reach.
for region in universal_regions.universal_regions_iter() {
localized_outlives_constraints.push(LocalizedOutlivesConstraint {
source: region,
Expand Down

0 comments on commit aeb3d10

Please sign in to comment.