clean up translate_outlives_facts

- remove dependency on `TypeChecker`
- move to legacy fact generation module
- group facts emitted during typeck together
This commit is contained in:
Rémy Rakic 2024-12-11 13:33:58 +00:00
parent 9d8f58adb2
commit afbe101f0e
3 changed files with 48 additions and 37 deletions

View File

@ -3,14 +3,19 @@
//! Will be removed in the future, once the in-tree `-Zpolonius=next` implementation reaches feature
//! parity.
use std::iter;
use either::Either;
use rustc_middle::mir::{Body, Local, LocalKind, Location, START_BLOCK};
use rustc_middle::ty::{GenericArg, TyCtxt};
use rustc_mir_dataflow::move_paths::{InitKind, InitLocation, MoveData};
use tracing::debug;
use crate::borrow_set::BorrowSet;
use crate::constraints::OutlivesConstraint;
use crate::facts::{AllFacts, PoloniusRegionVid};
use crate::location::LocationTable;
use crate::type_check::MirTypeckRegionConstraints;
use crate::type_check::free_region_relations::UniversalRegionRelations;
use crate::universal_regions::UniversalRegions;
@ -205,3 +210,31 @@ pub(crate) fn emit_drop_facts<'tcx>(
});
}
}
/// Emit facts about the outlives constraints: the `subset` base relation, i.e. not a transitive
/// closure.
pub(crate) fn emit_outlives_facts<'tcx>(
tcx: TyCtxt<'tcx>,
constraints: &MirTypeckRegionConstraints<'tcx>,
location_table: &LocationTable,
all_facts: &mut Option<AllFacts>,
) {
if let Some(facts) = all_facts {
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
facts.subset_base.extend(constraints.outlives_constraints.outlives().iter().flat_map(
|constraint: &OutlivesConstraint<'_>| {
if let Some(from_location) = constraint.locations.from_location() {
Either::Left(iter::once((
constraint.sup.into(),
constraint.sub.into(),
location_table.mid_index(from_location),
)))
} else {
Either::Right(location_table.all_points().map(move |location| {
(constraint.sup.into(), constraint.sub.into(), location)
}))
}
},
));
}
}

View File

@ -13,7 +13,6 @@ use tracing::debug;
use super::TypeChecker;
use crate::constraints::OutlivesConstraintSet;
use crate::polonius;
use crate::region_infer::values::LivenessValues;
use crate::universal_regions::UniversalRegions;
@ -45,15 +44,6 @@ pub(super) fn generate<'a, 'tcx>(
let (relevant_live_locals, boring_locals) =
compute_relevant_live_locals(typeck.tcx(), &free_regions, body);
polonius::legacy::emit_access_facts(
typeck.tcx(),
body,
move_data,
typeck.universal_regions,
typeck.location_table,
typeck.all_facts,
);
trace::trace(
typeck,
body,

View File

@ -3,7 +3,6 @@
use std::rc::Rc;
use std::{fmt, iter, mem};
use either::Either;
use rustc_abi::{FIRST_VARIANT, FieldIdx};
use rustc_data_structures::frozen::Frozen;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
@ -60,7 +59,7 @@ use crate::renumber::RegionCtxt;
use crate::session_diagnostics::{MoveUnsized, SimdIntrinsicArgConst};
use crate::type_check::free_region_relations::{CreateResult, UniversalRegionRelations};
use crate::universal_regions::{DefiningTy, UniversalRegions};
use crate::{BorrowckInferCtxt, path_utils};
use crate::{BorrowckInferCtxt, path_utils, polonius};
macro_rules! span_mirbug {
($context:expr, $elem:expr, $($message:tt)*) => ({
@ -182,7 +181,20 @@ pub(crate) fn type_check<'a, 'tcx>(
liveness::generate(&mut checker, body, &elements, flow_inits, move_data);
translate_outlives_facts(&mut checker);
polonius::legacy::emit_access_facts(
infcx.tcx,
body,
move_data,
&universal_region_relations.universal_regions,
location_table,
checker.all_facts,
);
polonius::legacy::emit_outlives_facts(
infcx.tcx,
checker.constraints,
location_table,
checker.all_facts,
);
let opaque_type_values = infcx.take_opaque_types();
let opaque_type_values = opaque_type_values
@ -234,30 +246,6 @@ pub(crate) fn type_check<'a, 'tcx>(
MirTypeckResults { constraints, universal_region_relations, opaque_type_values }
}
fn translate_outlives_facts(typeck: &mut TypeChecker<'_, '_>) {
if let Some(facts) = typeck.all_facts {
let _prof_timer = typeck.infcx.tcx.prof.generic_activity("polonius_fact_generation");
let location_table = typeck.location_table;
facts.subset_base.extend(
typeck.constraints.outlives_constraints.outlives().iter().flat_map(
|constraint: &OutlivesConstraint<'_>| {
if let Some(from_location) = constraint.locations.from_location() {
Either::Left(iter::once((
constraint.sup.into(),
constraint.sub.into(),
location_table.mid_index(from_location),
)))
} else {
Either::Right(location_table.all_points().map(move |location| {
(constraint.sup.into(), constraint.sub.into(), location)
}))
}
},
),
);
}
}
#[track_caller]
fn mirbug(tcx: TyCtxt<'_>, span: Span, msg: String) {
// We sometimes see MIR failures (notably predicate failures) due to