BorrowckInferCtxt: infcx by value

This commit is contained in:
lcnr 2024-05-06 15:20:13 +00:00
parent 25e3949aa1
commit 5f044f3528
7 changed files with 31 additions and 40 deletions

View File

@ -2,7 +2,6 @@
use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::LocalDefId;
use rustc_index::{IndexSlice, IndexVec}; use rustc_index::{IndexSlice, IndexVec};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::mir::{Body, Promoted}; use rustc_middle::mir::{Body, Promoted};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use std::rc::Rc; use std::rc::Rc;
@ -105,8 +104,7 @@ pub fn get_body_with_borrowck_facts(
options: ConsumerOptions, options: ConsumerOptions,
) -> BodyWithBorrowckFacts<'_> { ) -> BodyWithBorrowckFacts<'_> {
let (input_body, promoted) = tcx.mir_promoted(def); let (input_body, promoted) = tcx.mir_promoted(def);
let infcx = tcx.infer_ctxt().with_opaque_type_inference(def).build();
let input_body: &Body<'_> = &input_body.borrow(); let input_body: &Body<'_> = &input_body.borrow();
let promoted: &IndexSlice<_, _> = &promoted.borrow(); let promoted: &IndexSlice<_, _> = &promoted.borrow();
*super::do_mir_borrowck(&infcx, input_body, promoted, Some(options)).1.unwrap() *super::do_mir_borrowck(tcx, input_body, promoted, Some(options)).1.unwrap()
} }

View File

@ -23,9 +23,8 @@ use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::LocalDefId;
use rustc_index::bit_set::{BitSet, ChunkedBitSet}; use rustc_index::bit_set::{BitSet, ChunkedBitSet};
use rustc_index::{IndexSlice, IndexVec}; use rustc_index::{IndexSlice, IndexVec};
use rustc_infer::infer::{ use rustc_infer::infer::TyCtxtInferExt;
InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt, use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin};
};
use rustc_middle::mir::tcx::PlaceTy; use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::mir::*; use rustc_middle::mir::*;
use rustc_middle::query::Providers; use rustc_middle::query::Providers;
@ -123,9 +122,8 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
return tcx.arena.alloc(result); return tcx.arena.alloc(result);
} }
let infcx = tcx.infer_ctxt().with_opaque_type_inference(def).build();
let promoted: &IndexSlice<_, _> = &promoted.borrow(); let promoted: &IndexSlice<_, _> = &promoted.borrow();
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, None).0; let opt_closure_req = do_mir_borrowck(tcx, input_body, promoted, None).0;
debug!("mir_borrowck done"); debug!("mir_borrowck done");
tcx.arena.alloc(opt_closure_req) tcx.arena.alloc(opt_closure_req)
@ -136,18 +134,15 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
/// Use `consumer_options: None` for the default behavior of returning /// Use `consumer_options: None` for the default behavior of returning
/// [`BorrowCheckResult`] only. Otherwise, return [`BodyWithBorrowckFacts`] according /// [`BorrowCheckResult`] only. Otherwise, return [`BodyWithBorrowckFacts`] according
/// to the given [`ConsumerOptions`]. /// to the given [`ConsumerOptions`].
#[instrument(skip(infcx, input_body, input_promoted), fields(id=?input_body.source.def_id()), level = "debug")] #[instrument(skip(tcx, input_body, input_promoted), fields(id=?input_body.source.def_id()), level = "debug")]
fn do_mir_borrowck<'tcx>( fn do_mir_borrowck<'tcx>(
infcx: &InferCtxt<'tcx>, tcx: TyCtxt<'tcx>,
input_body: &Body<'tcx>, input_body: &Body<'tcx>,
input_promoted: &IndexSlice<Promoted, Body<'tcx>>, input_promoted: &IndexSlice<Promoted, Body<'tcx>>,
consumer_options: Option<ConsumerOptions>, consumer_options: Option<ConsumerOptions>,
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) { ) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
let def = input_body.source.def_id().expect_local(); let def = input_body.source.def_id().expect_local();
debug!(?def); let infcx = BorrowckInferCtxt::new(tcx, def);
let tcx = infcx.tcx;
let infcx = BorrowckInferCtxt::new(infcx);
let param_env = tcx.param_env(def); let param_env = tcx.param_env(def);
let mut local_names = IndexVec::from_elem(None, &input_body.local_decls); let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
@ -440,13 +435,14 @@ fn do_mir_borrowck<'tcx>(
(result, body_with_facts) (result, body_with_facts)
} }
pub struct BorrowckInferCtxt<'cx, 'tcx> { pub struct BorrowckInferCtxt<'tcx> {
pub(crate) infcx: &'cx InferCtxt<'tcx>, pub(crate) infcx: InferCtxt<'tcx>,
pub(crate) reg_var_to_origin: RefCell<FxIndexMap<ty::RegionVid, RegionCtxt>>, pub(crate) reg_var_to_origin: RefCell<FxIndexMap<ty::RegionVid, RegionCtxt>>,
} }
impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> { impl<'tcx> BorrowckInferCtxt<'tcx> {
pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self { pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
let infcx = tcx.infer_ctxt().with_opaque_type_inference(def_id).build();
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) } BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) }
} }
@ -494,16 +490,16 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
} }
} }
impl<'cx, 'tcx> Deref for BorrowckInferCtxt<'cx, 'tcx> { impl<'tcx> Deref for BorrowckInferCtxt<'tcx> {
type Target = InferCtxt<'tcx>; type Target = InferCtxt<'tcx>;
fn deref(&self) -> &'cx Self::Target { fn deref(&self) -> &Self::Target {
self.infcx &self.infcx
} }
} }
struct MirBorrowckCtxt<'cx, 'tcx> { struct MirBorrowckCtxt<'cx, 'tcx> {
infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>, infcx: &'cx BorrowckInferCtxt<'tcx>,
param_env: ParamEnv<'tcx>, param_env: ParamEnv<'tcx>,
body: &'cx Body<'tcx>, body: &'cx Body<'tcx>,
move_data: &'cx MoveData<'tcx>, move_data: &'cx MoveData<'tcx>,

View File

@ -51,7 +51,7 @@ pub(crate) struct NllOutput<'tcx> {
/// `compute_regions`. /// `compute_regions`.
#[instrument(skip(infcx, param_env, body, promoted), level = "debug")] #[instrument(skip(infcx, param_env, body, promoted), level = "debug")]
pub(crate) fn replace_regions_in_mir<'tcx>( pub(crate) fn replace_regions_in_mir<'tcx>(
infcx: &BorrowckInferCtxt<'_, 'tcx>, infcx: &BorrowckInferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body: &mut Body<'tcx>, body: &mut Body<'tcx>,
promoted: &mut IndexSlice<Promoted, Body<'tcx>>, promoted: &mut IndexSlice<Promoted, Body<'tcx>>,
@ -75,7 +75,7 @@ pub(crate) fn replace_regions_in_mir<'tcx>(
/// ///
/// This may result in errors being reported. /// This may result in errors being reported.
pub(crate) fn compute_regions<'cx, 'tcx>( pub(crate) fn compute_regions<'cx, 'tcx>(
infcx: &BorrowckInferCtxt<'_, 'tcx>, infcx: &BorrowckInferCtxt<'tcx>,
universal_regions: UniversalRegions<'tcx>, universal_regions: UniversalRegions<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
promoted: &IndexSlice<Promoted, Body<'tcx>>, promoted: &IndexSlice<Promoted, Body<'tcx>>,
@ -202,7 +202,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
} }
pub(super) fn dump_mir_results<'tcx>( pub(super) fn dump_mir_results<'tcx>(
infcx: &BorrowckInferCtxt<'_, 'tcx>, infcx: &BorrowckInferCtxt<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>, regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>, closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
@ -254,7 +254,7 @@ pub(super) fn dump_mir_results<'tcx>(
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
pub(super) fn dump_annotation<'tcx>( pub(super) fn dump_annotation<'tcx>(
infcx: &BorrowckInferCtxt<'_, 'tcx>, infcx: &BorrowckInferCtxt<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>, regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>, closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,

View File

@ -250,10 +250,7 @@ pub enum ExtraConstraintInfo {
} }
#[instrument(skip(infcx, sccs), level = "debug")] #[instrument(skip(infcx, sccs), level = "debug")]
fn sccs_info<'cx, 'tcx>( fn sccs_info<'tcx>(infcx: &BorrowckInferCtxt<'tcx>, sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>) {
infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>,
sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>,
) {
use crate::renumber::RegionCtxt; use crate::renumber::RegionCtxt;
let var_to_origin = infcx.reg_var_to_origin.borrow(); let var_to_origin = infcx.reg_var_to_origin.borrow();
@ -322,8 +319,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// ///
/// The `outlives_constraints` and `type_tests` are an initial set /// The `outlives_constraints` and `type_tests` are an initial set
/// of constraints produced by the MIR type check. /// of constraints produced by the MIR type check.
pub(crate) fn new<'cx>( pub(crate) fn new(
_infcx: &BorrowckInferCtxt<'cx, 'tcx>, _infcx: &BorrowckInferCtxt<'tcx>,
var_infos: VarInfos, var_infos: VarInfos,
universal_regions: Rc<UniversalRegions<'tcx>>, universal_regions: Rc<UniversalRegions<'tcx>>,
placeholder_indices: Rc<PlaceholderIndices>, placeholder_indices: Rc<PlaceholderIndices>,

View File

@ -11,7 +11,7 @@ use rustc_span::Symbol;
/// inference variables, returning the number of variables created. /// inference variables, returning the number of variables created.
#[instrument(skip(infcx, body, promoted), level = "debug")] #[instrument(skip(infcx, body, promoted), level = "debug")]
pub fn renumber_mir<'tcx>( pub fn renumber_mir<'tcx>(
infcx: &BorrowckInferCtxt<'_, 'tcx>, infcx: &BorrowckInferCtxt<'tcx>,
body: &mut Body<'tcx>, body: &mut Body<'tcx>,
promoted: &mut IndexSlice<Promoted, Body<'tcx>>, promoted: &mut IndexSlice<Promoted, Body<'tcx>>,
) { ) {
@ -57,7 +57,7 @@ impl RegionCtxt {
} }
struct RegionRenumberer<'a, 'tcx> { struct RegionRenumberer<'a, 'tcx> {
infcx: &'a BorrowckInferCtxt<'a, 'tcx>, infcx: &'a BorrowckInferCtxt<'tcx>,
} }
impl<'a, 'tcx> RegionRenumberer<'a, 'tcx> { impl<'a, 'tcx> RegionRenumberer<'a, 'tcx> {

View File

@ -122,7 +122,7 @@ mod relate_tys;
/// - `move_data` -- move-data constructed when performing the maybe-init dataflow analysis /// - `move_data` -- move-data constructed when performing the maybe-init dataflow analysis
/// - `elements` -- MIR region map /// - `elements` -- MIR region map
pub(crate) fn type_check<'mir, 'tcx>( pub(crate) fn type_check<'mir, 'tcx>(
infcx: &BorrowckInferCtxt<'_, 'tcx>, infcx: &BorrowckInferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
promoted: &IndexSlice<Promoted, Body<'tcx>>, promoted: &IndexSlice<Promoted, Body<'tcx>>,
@ -865,7 +865,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
/// way, it accrues region constraints -- these can later be used by /// way, it accrues region constraints -- these can later be used by
/// NLL region checking. /// NLL region checking.
struct TypeChecker<'a, 'tcx> { struct TypeChecker<'a, 'tcx> {
infcx: &'a BorrowckInferCtxt<'a, 'tcx>, infcx: &'a BorrowckInferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
last_span: Span, last_span: Span,
body: &'a Body<'tcx>, body: &'a Body<'tcx>,
@ -1020,7 +1020,7 @@ impl Locations {
impl<'a, 'tcx> TypeChecker<'a, 'tcx> { impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
fn new( fn new(
infcx: &'a BorrowckInferCtxt<'a, 'tcx>, infcx: &'a BorrowckInferCtxt<'tcx>,
body: &'a Body<'tcx>, body: &'a Body<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
region_bound_pairs: &'a RegionBoundPairs<'tcx>, region_bound_pairs: &'a RegionBoundPairs<'tcx>,

View File

@ -240,7 +240,7 @@ impl<'tcx> UniversalRegions<'tcx> {
/// signature. This will also compute the relationships that are /// signature. This will also compute the relationships that are
/// known between those regions. /// known between those regions.
pub fn new( pub fn new(
infcx: &BorrowckInferCtxt<'_, 'tcx>, infcx: &BorrowckInferCtxt<'tcx>,
mir_def: LocalDefId, mir_def: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
) -> Self { ) -> Self {
@ -411,7 +411,7 @@ impl<'tcx> UniversalRegions<'tcx> {
} }
struct UniversalRegionsBuilder<'cx, 'tcx> { struct UniversalRegionsBuilder<'cx, 'tcx> {
infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>, infcx: &'cx BorrowckInferCtxt<'tcx>,
mir_def: LocalDefId, mir_def: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
} }
@ -796,7 +796,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
} }
#[extension(trait InferCtxtExt<'tcx>)] #[extension(trait InferCtxtExt<'tcx>)]
impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> { impl<'tcx> BorrowckInferCtxt<'tcx> {
#[instrument(skip(self), level = "debug")] #[instrument(skip(self), level = "debug")]
fn replace_free_regions_with_nll_infer_vars<T>( fn replace_free_regions_with_nll_infer_vars<T>(
&self, &self,