Rollup merge of #139191 - lcnr:interner-opaques, r=compiler-errors

small opaque type/borrowck cleanup

pulled out of #138785
This commit is contained in:
Stuart Cook 2025-04-02 13:10:40 +11:00 committed by GitHub
commit 2311b342bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 35 additions and 61 deletions

View file

@ -1,7 +1,7 @@
//! This file provides API for compiler consumers. //! This file provides API for compiler consumers.
use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::LocalDefId;
use rustc_index::{IndexSlice, IndexVec}; use rustc_index::IndexVec;
use rustc_middle::mir::{Body, Promoted}; use rustc_middle::mir::{Body, Promoted};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
@ -100,8 +100,5 @@ pub fn get_body_with_borrowck_facts(
def: LocalDefId, def: LocalDefId,
options: ConsumerOptions, options: ConsumerOptions,
) -> BodyWithBorrowckFacts<'_> { ) -> BodyWithBorrowckFacts<'_> {
let (input_body, promoted) = tcx.mir_promoted(def); *super::do_mir_borrowck(tcx, def, Some(options)).1.unwrap()
let input_body: &Body<'_> = &input_body.borrow();
let promoted: &IndexSlice<_, _> = &promoted.borrow();
*super::do_mir_borrowck(tcx, input_body, promoted, Some(options)).1.unwrap()
} }

View file

@ -103,11 +103,8 @@ pub fn provide(providers: &mut Providers) {
} }
fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> { fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
let (input_body, promoted) = tcx.mir_promoted(def); let (input_body, _) = tcx.mir_promoted(def);
debug!("run query mir_borrowck: {}", tcx.def_path_str(def));
let input_body: &Body<'_> = &input_body.borrow(); let input_body: &Body<'_> = &input_body.borrow();
if input_body.should_skip() || input_body.tainted_by_errors.is_some() { if input_body.should_skip() || input_body.tainted_by_errors.is_some() {
debug!("Skipping borrowck because of injected body or tainted body"); debug!("Skipping borrowck because of injected body or tainted body");
// Let's make up a borrowck result! Fun times! // Let's make up a borrowck result! Fun times!
@ -120,7 +117,7 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
return tcx.arena.alloc(result); return tcx.arena.alloc(result);
} }
let borrowck_result = do_mir_borrowck(tcx, input_body, &*promoted.borrow(), None).0; let borrowck_result = do_mir_borrowck(tcx, def, None).0;
debug!("mir_borrowck done"); debug!("mir_borrowck done");
tcx.arena.alloc(borrowck_result) tcx.arena.alloc(borrowck_result)
@ -131,15 +128,16 @@ 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(tcx, input_body, input_promoted), fields(id=?input_body.source.def_id()), level = "debug")] #[instrument(skip(tcx), level = "debug")]
fn do_mir_borrowck<'tcx>( fn do_mir_borrowck<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
input_body: &Body<'tcx>, def: LocalDefId,
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 infcx = BorrowckInferCtxt::new(tcx, def); let infcx = BorrowckInferCtxt::new(tcx, def);
let (input_body, promoted) = tcx.mir_promoted(def);
let input_body: &Body<'_> = &input_body.borrow();
let input_promoted: &IndexSlice<_, _> = &promoted.borrow();
if let Some(e) = input_body.tainted_by_errors { if let Some(e) = input_body.tainted_by_errors {
infcx.set_tainted_by_errors(e); infcx.set_tainted_by_errors(e);
} }
@ -499,7 +497,8 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
) )
}); });
self.inject_new_hidden_type_unchecked(key, hidden_ty); let prev = self.register_hidden_type_in_storage(key, hidden_ty);
assert_eq!(prev, None);
} }
} }
} }

View file

@ -198,13 +198,12 @@ impl<'tcx> InferCtxt<'tcx> {
/// it hasn't previously been defined. This does not emit any /// it hasn't previously been defined. This does not emit any
/// constraints and it's the responsibility of the caller to make /// constraints and it's the responsibility of the caller to make
/// sure that the item bounds of the opaque are checked. /// sure that the item bounds of the opaque are checked.
pub fn inject_new_hidden_type_unchecked( pub fn register_hidden_type_in_storage(
&self, &self,
opaque_type_key: OpaqueTypeKey<'tcx>, opaque_type_key: OpaqueTypeKey<'tcx>,
hidden_ty: OpaqueHiddenType<'tcx>, hidden_ty: OpaqueHiddenType<'tcx>,
) { ) -> Option<Ty<'tcx>> {
let prev = self.inner.borrow_mut().opaque_types().register(opaque_type_key, hidden_ty); self.inner.borrow_mut().opaque_types().register(opaque_type_key, hidden_ty)
assert_eq!(prev, None);
} }
/// Insert a hidden type into the opaque type storage, equating it /// Insert a hidden type into the opaque type storage, equating it

View file

@ -62,14 +62,12 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex, universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex,
) -> <Self::Interner as Interner>::GenericArg; ) -> <Self::Interner as Interner>::GenericArg;
// FIXME: Can we implement this in terms of `add` and `inject`? fn register_hidden_type_in_storage(
fn insert_hidden_type(
&self, &self,
opaque_type_key: ty::OpaqueTypeKey<Self::Interner>, opaque_type_key: ty::OpaqueTypeKey<Self::Interner>,
param_env: <Self::Interner as Interner>::ParamEnv,
hidden_ty: <Self::Interner as Interner>::Ty, hidden_ty: <Self::Interner as Interner>::Ty,
goals: &mut Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, span: <Self::Interner as Interner>::Span,
) -> Result<(), NoSolution>; ) -> Option<<Self::Interner as Interner>::Ty>;
fn add_item_bounds_for_hidden_type( fn add_item_bounds_for_hidden_type(
&self, &self,
@ -79,14 +77,6 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
hidden_ty: <Self::Interner as Interner>::Ty, hidden_ty: <Self::Interner as Interner>::Ty,
goals: &mut Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, goals: &mut Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
); );
fn inject_new_hidden_type_unchecked(
&self,
key: ty::OpaqueTypeKey<Self::Interner>,
hidden_ty: <Self::Interner as Interner>::Ty,
span: <Self::Interner as Interner>::Span,
);
fn reset_opaque_types(&self); fn reset_opaque_types(&self);
fn fetch_eligible_assoc_item( fn fetch_eligible_assoc_item(

View file

@ -425,7 +425,8 @@ where
fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<I>, I::Ty)]) { fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<I>, I::Ty)]) {
for &(key, ty) in opaque_types { for &(key, ty) in opaque_types {
self.delegate.inject_new_hidden_type_unchecked(key, ty, self.origin_span); let prev = self.delegate.register_hidden_type_in_storage(key, ty, self.origin_span);
assert_eq!(prev, None);
} }
} }
} }

View file

@ -387,7 +387,8 @@ where
}; };
for &(key, ty) in &input.predefined_opaques_in_body.opaque_types { for &(key, ty) in &input.predefined_opaques_in_body.opaque_types {
ecx.delegate.inject_new_hidden_type_unchecked(key, ty, ecx.origin_span); let prev = ecx.delegate.register_hidden_type_in_storage(key, ty, ecx.origin_span);
assert_eq!(prev, None);
} }
if !ecx.nested_goals.is_empty() { if !ecx.nested_goals.is_empty() {
@ -1070,16 +1071,12 @@ where
self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id) self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
} }
pub(super) fn insert_hidden_type( pub(super) fn register_hidden_type_in_storage(
&mut self, &mut self,
opaque_type_key: ty::OpaqueTypeKey<I>, opaque_type_key: ty::OpaqueTypeKey<I>,
param_env: I::ParamEnv,
hidden_ty: I::Ty, hidden_ty: I::Ty,
) -> Result<(), NoSolution> { ) -> Option<I::Ty> {
let mut goals = Vec::new(); self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
self.delegate.insert_hidden_type(opaque_type_key, param_env, hidden_ty, &mut goals)?;
self.add_goals(GoalSource::Misc, goals);
Ok(())
} }
pub(super) fn add_item_bounds_for_hidden_type( pub(super) fn add_item_bounds_for_hidden_type(

View file

@ -86,8 +86,8 @@ where
} }
// Otherwise, define a new opaque type // Otherwise, define a new opaque type
// FIXME: should we use `inject_hidden_type_unchecked` here? let prev = self.register_hidden_type_in_storage(opaque_type_key, expected);
self.insert_hidden_type(opaque_type_key, goal.param_env, expected)?; assert_eq!(prev, None);
self.add_item_bounds_for_hidden_type( self.add_item_bounds_for_hidden_type(
def_id.into(), def_id.into(),
opaque_ty.args, opaque_ty.args,

View file

@ -149,16 +149,16 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
self.0.instantiate_canonical_var(span, cv_info, universe_map) self.0.instantiate_canonical_var(span, cv_info, universe_map)
} }
fn insert_hidden_type( fn register_hidden_type_in_storage(
&self, &self,
opaque_type_key: ty::OpaqueTypeKey<'tcx>, opaque_type_key: rustc_type_ir::OpaqueTypeKey<Self::Interner>,
param_env: ty::ParamEnv<'tcx>, hidden_ty: <Self::Interner as ty::Interner>::Ty,
hidden_ty: Ty<'tcx>, span: <Self::Interner as ty::Interner>::Span,
goals: &mut Vec<Goal<'tcx, ty::Predicate<'tcx>>>, ) -> Option<<Self::Interner as ty::Interner>::Ty> {
) -> Result<(), NoSolution> { self.0.register_hidden_type_in_storage(
self.0 opaque_type_key,
.insert_hidden_type(opaque_type_key, DUMMY_SP, param_env, hidden_ty, goals) ty::OpaqueHiddenType { span, ty: hidden_ty },
.map_err(|_| NoSolution) )
} }
fn add_item_bounds_for_hidden_type( fn add_item_bounds_for_hidden_type(
@ -172,15 +172,6 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
self.0.add_item_bounds_for_hidden_type(def_id, args, param_env, hidden_ty, goals); self.0.add_item_bounds_for_hidden_type(def_id, args, param_env, hidden_ty, goals);
} }
fn inject_new_hidden_type_unchecked(
&self,
key: ty::OpaqueTypeKey<'tcx>,
hidden_ty: Ty<'tcx>,
span: Span,
) {
self.0.inject_new_hidden_type_unchecked(key, ty::OpaqueHiddenType { ty: hidden_ty, span })
}
fn reset_opaque_types(&self) { fn reset_opaque_types(&self) {
let _ = self.take_opaque_types(); let _ = self.take_opaque_types();
} }