Inline and remove RegionConstraintCollector::into_infos_and_data.

It's a weird method, and used weirdly:
- It's on `RegionConstraintCollector` but operates on
  `RegionConstraintStorage`. So at both call sites we create a temporary
  `RegionConstraintCollector`, using `with_log`, to call it.
- It `take`s just two of the six fields in `RegionConstraintStorage`.
  At one of the two call sites we unnecessarily clone the entire
  `RegionConstraintStorage` just to take those two fields.

This commit just inlines and removes it. We no longer need to `take` the
two fields, we can just use them directly.
This commit is contained in:
Nicholas Nethercote 2024-10-03 10:44:48 +10:00
parent 0293827e09
commit 2b57a785a9
3 changed files with 18 additions and 32 deletions

View File

@ -18,7 +18,7 @@
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::undo_log::Rollback;
use rustc_data_structures::undo_log::{Rollback, UndoLogs};
use rustc_data_structures::unify as ut;
use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
use rustc_hir as hir;
@ -50,6 +50,7 @@
use tracing::{debug, instrument};
use type_variable::TypeVariableOrigin;
use crate::infer::region_constraints::UndoLog;
use crate::traits::{self, ObligationCause, ObligationInspector, PredicateObligation, TraitEngine};
pub mod at;
@ -1043,18 +1044,14 @@ pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin {
/// Clone the list of variable regions. This is used only during NLL processing
/// to put the set of region variables into the NLL region context.
pub fn get_region_var_origins(&self) -> VarInfos {
let mut inner = self.inner.borrow_mut();
let (var_infos, data) = inner
.region_constraint_storage
// We clone instead of taking because borrowck still wants to use
// the inference context after calling this for diagnostics
// and the new trait solver.
.clone()
.expect("regions already resolved")
.with_log(&mut inner.undo_log)
.into_infos_and_data();
assert!(data.is_empty());
var_infos
let inner = self.inner.borrow();
assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&inner.undo_log));
let storage = inner.region_constraint_storage.as_ref().expect("regions already resolved");
assert!(storage.data.is_empty());
// We clone instead of taking because borrowck still wants to use the
// inference context after calling this for diagnostics and the new
// trait solver.
storage.var_infos.clone()
}
#[instrument(level = "debug", skip(self), ret)]

View File

@ -1,11 +1,12 @@
//! Various code related to computing outlives relations.
use rustc_data_structures::undo_log::UndoLogs;
use rustc_middle::traits::query::{NoSolution, OutlivesBound};
use rustc_middle::ty;
use tracing::instrument;
use self::env::OutlivesEnvironment;
use super::region_constraints::RegionConstraintData;
use super::region_constraints::{RegionConstraintData, UndoLog};
use super::{InferCtxt, RegionResolutionError, SubregionOrigin};
use crate::infer::free_regions::RegionRelations;
use crate::infer::lexical_region_resolve;
@ -63,7 +64,7 @@ pub fn resolve_regions_with_normalize(
}
};
let (var_infos, data) = {
let storage = {
let mut inner = self.inner.borrow_mut();
let inner = &mut *inner;
assert!(
@ -71,18 +72,14 @@ pub fn resolve_regions_with_normalize(
"region_obligations not empty: {:#?}",
inner.region_obligations
);
inner
.region_constraint_storage
.take()
.expect("regions already resolved")
.with_log(&mut inner.undo_log)
.into_infos_and_data()
assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&inner.undo_log));
inner.region_constraint_storage.take().expect("regions already resolved")
};
let region_rels = &RegionRelations::new(self.tcx, outlives_env.free_region_map());
let (lexical_region_resolutions, errors) =
lexical_region_resolve::resolve(region_rels, var_infos, data);
lexical_region_resolve::resolve(region_rels, storage.var_infos, storage.data);
let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
assert!(old_value.is_none());

View File

@ -27,9 +27,9 @@
#[derive(Clone, Default)]
pub struct RegionConstraintStorage<'tcx> {
/// For each `RegionVid`, the corresponding `RegionVariableOrigin`.
var_infos: IndexVec<RegionVid, RegionVariableInfo>,
pub(super) var_infos: IndexVec<RegionVid, RegionVariableInfo>,
data: RegionConstraintData<'tcx>,
pub(super) data: RegionConstraintData<'tcx>,
/// For a given pair of regions (R1, R2), maps to a region R3 that
/// is designated as their LUB (edges R1 <= R3 and R2 <= R3
@ -354,14 +354,6 @@ pub fn region_constraint_data(&self) -> &RegionConstraintData<'tcx> {
&self.data
}
/// Once all the constraints have been gathered, extract out the final data.
///
/// Not legal during a snapshot.
pub fn into_infos_and_data(self) -> (VarInfos, RegionConstraintData<'tcx>) {
assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&self.undo_log));
(mem::take(&mut self.storage.var_infos), mem::take(&mut self.storage.data))
}
/// Takes (and clears) the current set of constraints. Note that
/// the set of variables remains intact, but all relationships
/// between them are reset. This is used during NLL checking to