1
Fork 0

create context for errors and diagnostics for last borrowck phase

This commit is contained in:
Rémy Rakic 2025-01-30 10:37:40 +00:00
parent 6054a33bf2
commit a8fc140848
4 changed files with 28 additions and 17 deletions

View file

@ -198,7 +198,7 @@ fn do_mir_borrowck<'tcx>(
polonius_output, polonius_output,
opt_closure_req, opt_closure_req,
nll_errors, nll_errors,
localized_outlives_constraints, polonius_diagnostics,
} = nll::compute_regions( } = nll::compute_regions(
&infcx, &infcx,
free_regions, free_regions,
@ -329,7 +329,7 @@ fn do_mir_borrowck<'tcx>(
body, body,
&regioncx, &regioncx,
&borrow_set, &borrow_set,
localized_outlives_constraints, polonius_diagnostics,
&opt_closure_req, &opt_closure_req,
); );

View file

@ -27,7 +27,7 @@ use tracing::{debug, instrument};
use crate::borrow_set::BorrowSet; use crate::borrow_set::BorrowSet;
use crate::consumers::ConsumerOptions; use crate::consumers::ConsumerOptions;
use crate::diagnostics::{BorrowckDiagnosticsBuffer, RegionErrors}; use crate::diagnostics::{BorrowckDiagnosticsBuffer, RegionErrors};
use crate::polonius::LocalizedOutlivesConstraintSet; use crate::polonius::PoloniusDiagnosticsContext;
use crate::polonius::legacy::{ use crate::polonius::legacy::{
PoloniusFacts, PoloniusFactsExt, PoloniusLocationTable, PoloniusOutput, PoloniusFacts, PoloniusFactsExt, PoloniusLocationTable, PoloniusOutput,
}; };
@ -46,8 +46,9 @@ pub(crate) struct NllOutput<'tcx> {
pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>, pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>,
pub nll_errors: RegionErrors<'tcx>, pub nll_errors: RegionErrors<'tcx>,
/// When using `-Zpolonius=next`: the localized typeck and liveness constraints. /// When using `-Zpolonius=next`: the data used to compute errors and diagnostics, e.g.
pub localized_outlives_constraints: Option<LocalizedOutlivesConstraintSet>, /// localized typeck and liveness constraints.
pub polonius_diagnostics: Option<PoloniusDiagnosticsContext>,
} }
/// Rewrites the regions in the MIR to use NLL variables, also scraping out the set of universal /// Rewrites the regions in the MIR to use NLL variables, also scraping out the set of universal
@ -144,7 +145,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
// If requested for `-Zpolonius=next`, convert NLL constraints to localized outlives constraints // If requested for `-Zpolonius=next`, convert NLL constraints to localized outlives constraints
// and use them to compute loan liveness. // and use them to compute loan liveness.
let localized_outlives_constraints = polonius_context.as_ref().map(|polonius_context| { let polonius_diagnostics = polonius_context.as_ref().map(|polonius_context| {
polonius_context.compute_loan_liveness(infcx.tcx, &mut regioncx, body, borrow_set) polonius_context.compute_loan_liveness(infcx.tcx, &mut regioncx, body, borrow_set)
}); });
@ -188,7 +189,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
polonius_output, polonius_output,
opt_closure_req: closure_region_requirements, opt_closure_req: closure_region_requirements,
nll_errors, nll_errors,
localized_outlives_constraints, polonius_diagnostics,
} }
} }

View file

@ -12,7 +12,9 @@ use rustc_session::config::MirIncludeSpans;
use crate::borrow_set::BorrowSet; use crate::borrow_set::BorrowSet;
use crate::constraints::OutlivesConstraint; use crate::constraints::OutlivesConstraint;
use crate::polonius::{LocalizedOutlivesConstraint, LocalizedOutlivesConstraintSet}; use crate::polonius::{
LocalizedOutlivesConstraint, LocalizedOutlivesConstraintSet, PoloniusDiagnosticsContext,
};
use crate::region_infer::values::LivenessValues; use crate::region_infer::values::LivenessValues;
use crate::type_check::Locations; use crate::type_check::Locations;
use crate::{BorrowckInferCtxt, RegionInferenceContext}; use crate::{BorrowckInferCtxt, RegionInferenceContext};
@ -23,7 +25,7 @@ pub(crate) fn dump_polonius_mir<'tcx>(
body: &Body<'tcx>, body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>, regioncx: &RegionInferenceContext<'tcx>,
borrow_set: &BorrowSet<'tcx>, borrow_set: &BorrowSet<'tcx>,
localized_outlives_constraints: Option<LocalizedOutlivesConstraintSet>, polonius_diagnostics: Option<PoloniusDiagnosticsContext>,
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>, closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
) { ) {
let tcx = infcx.tcx; let tcx = infcx.tcx;
@ -35,8 +37,8 @@ pub(crate) fn dump_polonius_mir<'tcx>(
return; return;
} }
let localized_outlives_constraints = localized_outlives_constraints let polonius_diagnostics_context =
.expect("missing localized constraints with `-Zpolonius=next`"); polonius_diagnostics.expect("missing diagnostics context with `-Zpolonius=next`");
let _: io::Result<()> = try { let _: io::Result<()> = try {
let mut file = create_dump_file(tcx, "html", false, "polonius", &0, body)?; let mut file = create_dump_file(tcx, "html", false, "polonius", &0, body)?;
@ -45,7 +47,7 @@ pub(crate) fn dump_polonius_mir<'tcx>(
body, body,
regioncx, regioncx,
borrow_set, borrow_set,
localized_outlives_constraints, polonius_diagnostics_context.localized_outlives_constraints,
closure_region_requirements, closure_region_requirements,
&mut file, &mut file,
)?; )?;

View file

@ -39,9 +39,9 @@
//! 2) once that is done, variance data is transferred, and the NLL region liveness is converted to //! 2) once that is done, variance data is transferred, and the NLL region liveness is converted to
//! the polonius shape. That's the main [PoloniusContext]. //! the polonius shape. That's the main [PoloniusContext].
//! 3) during region inference, that data and the NLL outlives constraints are used to create the //! 3) during region inference, that data and the NLL outlives constraints are used to create the
//! localized outlives constraints, as described above. //! localized outlives constraints, as described above. That's the [PoloniusDiagnosticsContext].
//! 4) transfer these constraints back to the main borrowck procedure: it handles computing errors //! 4) transfer this back to the main borrowck procedure: it handles computing errors and
//! and diagnostics, debugging and MIR dumping concerns. //! diagnostics, debugging and MIR dumping concerns.
mod constraints; mod constraints;
mod dump; mod dump;
@ -89,6 +89,12 @@ pub(crate) struct PoloniusContext {
live_region_variances: BTreeMap<RegionVid, ConstraintDirection>, live_region_variances: BTreeMap<RegionVid, ConstraintDirection>,
} }
/// This struct holds the data needed by the borrowck error computation and diagnostics. Its data is
/// computed from the [PoloniusContext] when computing NLL regions.
pub(crate) struct PoloniusDiagnosticsContext {
localized_outlives_constraints: LocalizedOutlivesConstraintSet,
}
/// The direction a constraint can flow into. Used to create liveness constraints according to /// The direction a constraint can flow into. Used to create liveness constraints according to
/// variance. /// variance.
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
@ -135,13 +141,15 @@ impl PoloniusContext {
/// ///
/// Then, this graph is traversed, and combined with kills, reachability is recorded as loan /// Then, this graph is traversed, and combined with kills, reachability is recorded as loan
/// liveness, to be used by the loan scope and active loans computations. /// liveness, to be used by the loan scope and active loans computations.
///
/// The constraint data will be used to compute errors and diagnostics.
pub(crate) fn compute_loan_liveness<'tcx>( pub(crate) fn compute_loan_liveness<'tcx>(
&self, &self,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
regioncx: &mut RegionInferenceContext<'tcx>, regioncx: &mut RegionInferenceContext<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
borrow_set: &BorrowSet<'tcx>, borrow_set: &BorrowSet<'tcx>,
) -> LocalizedOutlivesConstraintSet { ) -> PoloniusDiagnosticsContext {
let mut localized_outlives_constraints = LocalizedOutlivesConstraintSet::default(); let mut localized_outlives_constraints = LocalizedOutlivesConstraintSet::default();
convert_typeck_constraints( convert_typeck_constraints(
tcx, tcx,
@ -173,6 +181,6 @@ impl PoloniusContext {
); );
regioncx.record_live_loans(live_loans); regioncx.record_live_loans(live_loans);
localized_outlives_constraints PoloniusDiagnosticsContext { localized_outlives_constraints }
} }
} }