diff --git a/compiler/rustc_borrowck/src/consumers.rs b/compiler/rustc_borrowck/src/consumers.rs index 5a89f7c351cf6..45cdd2325647d 100644 --- a/compiler/rustc_borrowck/src/consumers.rs +++ b/compiler/rustc_borrowck/src/consumers.rs @@ -1,7 +1,7 @@ //! This file provides API for compiler consumers. use rustc_hir::def_id::LocalDefId; -use rustc_index::{IndexSlice, IndexVec}; +use rustc_index::IndexVec; use rustc_middle::mir::{Body, Promoted}; use rustc_middle::ty::TyCtxt; @@ -100,8 +100,5 @@ pub fn get_body_with_borrowck_facts( def: LocalDefId, options: ConsumerOptions, ) -> BodyWithBorrowckFacts<'_> { - let (input_body, promoted) = tcx.mir_promoted(def); - let input_body: &Body<'_> = &input_body.borrow(); - let promoted: &IndexSlice<_, _> = &promoted.borrow(); - *super::do_mir_borrowck(tcx, input_body, promoted, Some(options)).1.unwrap() + *super::do_mir_borrowck(tcx, def, Some(options)).1.unwrap() } diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 84b7b8c6a2de9..7f0ee28531cfa 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -103,11 +103,8 @@ pub fn provide(providers: &mut Providers) { } fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> { - let (input_body, promoted) = tcx.mir_promoted(def); - debug!("run query mir_borrowck: {}", tcx.def_path_str(def)); - + let (input_body, _) = tcx.mir_promoted(def); let input_body: &Body<'_> = &input_body.borrow(); - if input_body.should_skip() || input_body.tainted_by_errors.is_some() { debug!("Skipping borrowck because of injected body or tainted body"); // 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); } - 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"); 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 /// [`BorrowCheckResult`] only. Otherwise, return [`BodyWithBorrowckFacts`] according /// 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>( tcx: TyCtxt<'tcx>, - input_body: &Body<'tcx>, - input_promoted: &IndexSlice>, + def: LocalDefId, consumer_options: Option, ) -> (BorrowCheckResult<'tcx>, Option>>) { - let def = input_body.source.def_id().expect_local(); 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 { 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); } } } diff --git a/compiler/rustc_infer/src/infer/opaque_types/mod.rs b/compiler/rustc_infer/src/infer/opaque_types/mod.rs index 215b133372664..d9297fb419446 100644 --- a/compiler/rustc_infer/src/infer/opaque_types/mod.rs +++ b/compiler/rustc_infer/src/infer/opaque_types/mod.rs @@ -198,13 +198,12 @@ impl<'tcx> InferCtxt<'tcx> { /// it hasn't previously been defined. This does not emit any /// constraints and it's the responsibility of the caller to make /// 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, opaque_type_key: OpaqueTypeKey<'tcx>, hidden_ty: OpaqueHiddenType<'tcx>, - ) { - let prev = self.inner.borrow_mut().opaque_types().register(opaque_type_key, hidden_ty); - assert_eq!(prev, None); + ) -> Option> { + self.inner.borrow_mut().opaque_types().register(opaque_type_key, hidden_ty) } /// Insert a hidden type into the opaque type storage, equating it diff --git a/compiler/rustc_next_trait_solver/src/delegate.rs b/compiler/rustc_next_trait_solver/src/delegate.rs index 259b39e2b9eed..c4b6b18c45d6f 100644 --- a/compiler/rustc_next_trait_solver/src/delegate.rs +++ b/compiler/rustc_next_trait_solver/src/delegate.rs @@ -62,14 +62,12 @@ pub trait SolverDelegate: Deref + Sized { universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex, ) -> ::GenericArg; - // FIXME: Can we implement this in terms of `add` and `inject`? - fn insert_hidden_type( + fn register_hidden_type_in_storage( &self, opaque_type_key: ty::OpaqueTypeKey, - param_env: ::ParamEnv, hidden_ty: ::Ty, - goals: &mut Vec::Predicate>>, - ) -> Result<(), NoSolution>; + span: ::Span, + ) -> Option<::Ty>; fn add_item_bounds_for_hidden_type( &self, @@ -79,14 +77,6 @@ pub trait SolverDelegate: Deref + Sized { hidden_ty: ::Ty, goals: &mut Vec::Predicate>>, ); - - fn inject_new_hidden_type_unchecked( - &self, - key: ty::OpaqueTypeKey, - hidden_ty: ::Ty, - span: ::Span, - ); - fn reset_opaque_types(&self); fn fetch_eligible_assoc_item( diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs index ac6b521f665b6..4edc293ad807d 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs @@ -425,7 +425,8 @@ where fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey, I::Ty)]) { 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); } } } diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs index 7ef36d0e9ae37..148ba02252d94 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs @@ -387,7 +387,8 @@ where }; 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() { @@ -1070,16 +1071,12 @@ where 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, opaque_type_key: ty::OpaqueTypeKey, - param_env: I::ParamEnv, hidden_ty: I::Ty, - ) -> Result<(), NoSolution> { - let mut goals = Vec::new(); - self.delegate.insert_hidden_type(opaque_type_key, param_env, hidden_ty, &mut goals)?; - self.add_goals(GoalSource::Misc, goals); - Ok(()) + ) -> Option { + self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span) } pub(super) fn add_item_bounds_for_hidden_type( diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs index 817dffa127bc1..82dae51b3d038 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs @@ -86,8 +86,8 @@ where } // Otherwise, define a new opaque type - // FIXME: should we use `inject_hidden_type_unchecked` here? - self.insert_hidden_type(opaque_type_key, goal.param_env, expected)?; + let prev = self.register_hidden_type_in_storage(opaque_type_key, expected); + assert_eq!(prev, None); self.add_item_bounds_for_hidden_type( def_id.into(), opaque_ty.args, diff --git a/compiler/rustc_trait_selection/src/solve/delegate.rs b/compiler/rustc_trait_selection/src/solve/delegate.rs index 3d9a90eb74e7a..f2725411e13c3 100644 --- a/compiler/rustc_trait_selection/src/solve/delegate.rs +++ b/compiler/rustc_trait_selection/src/solve/delegate.rs @@ -149,16 +149,16 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate< self.0.instantiate_canonical_var(span, cv_info, universe_map) } - fn insert_hidden_type( + fn register_hidden_type_in_storage( &self, - opaque_type_key: ty::OpaqueTypeKey<'tcx>, - param_env: ty::ParamEnv<'tcx>, - hidden_ty: Ty<'tcx>, - goals: &mut Vec>>, - ) -> Result<(), NoSolution> { - self.0 - .insert_hidden_type(opaque_type_key, DUMMY_SP, param_env, hidden_ty, goals) - .map_err(|_| NoSolution) + opaque_type_key: rustc_type_ir::OpaqueTypeKey, + hidden_ty: ::Ty, + span: ::Span, + ) -> Option<::Ty> { + self.0.register_hidden_type_in_storage( + opaque_type_key, + ty::OpaqueHiddenType { span, ty: hidden_ty }, + ) } 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); } - 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) { let _ = self.take_opaque_types(); }