Skip to content

small opaque type/borrowck cleanup #139191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions compiler/rustc_borrowck/src/consumers.rs
Original file line number Diff line number Diff line change
@@ -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()
}
19 changes: 9 additions & 10 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Promoted, Body<'tcx>>,
def: LocalDefId,
consumer_options: Option<ConsumerOptions>,
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
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);
}
}
}
7 changes: 3 additions & 4 deletions compiler/rustc_infer/src/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Ty<'tcx>> {
self.inner.borrow_mut().opaque_types().register(opaque_type_key, hidden_ty)
}

/// Insert a hidden type into the opaque type storage, equating it
16 changes: 3 additions & 13 deletions compiler/rustc_next_trait_solver/src/delegate.rs
Original file line number Diff line number Diff line change
@@ -62,14 +62,12 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex,
) -> <Self::Interner as Interner>::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<Self::Interner>,
param_env: <Self::Interner as Interner>::ParamEnv,
hidden_ty: <Self::Interner as Interner>::Ty,
goals: &mut Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
) -> Result<(), NoSolution>;
span: <Self::Interner as Interner>::Span,
) -> Option<<Self::Interner as Interner>::Ty>;

fn add_item_bounds_for_hidden_type(
&self,
@@ -79,14 +77,6 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
hidden_ty: <Self::Interner as Interner>::Ty,
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 fetch_eligible_assoc_item(
Original file line number Diff line number Diff line change
@@ -425,7 +425,8 @@ where

fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<I>, 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);
}
}
}
13 changes: 5 additions & 8 deletions compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs
Original file line number Diff line number Diff line change
@@ -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<I>,
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<I::Ty> {
self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
}

pub(super) fn add_item_bounds_for_hidden_type(
Original file line number Diff line number Diff line change
@@ -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,
27 changes: 9 additions & 18 deletions compiler/rustc_trait_selection/src/solve/delegate.rs
Original file line number Diff line number Diff line change
@@ -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<Goal<'tcx, ty::Predicate<'tcx>>>,
) -> 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<Self::Interner>,
hidden_ty: <Self::Interner as ty::Interner>::Ty,
span: <Self::Interner as ty::Interner>::Span,
) -> Option<<Self::Interner as ty::Interner>::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();
}