Skip to content

Commit 2311b34

Browse files
authored
Rollup merge of #139191 - lcnr:interner-opaques, r=compiler-errors
small opaque type/borrowck cleanup pulled out of #138785
2 parents 1692ebd + cb275d4 commit 2311b34

File tree

8 files changed

+35
-61
lines changed

8 files changed

+35
-61
lines changed

compiler/rustc_borrowck/src/consumers.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This file provides API for compiler consumers.
22
33
use rustc_hir::def_id::LocalDefId;
4-
use rustc_index::{IndexSlice, IndexVec};
4+
use rustc_index::IndexVec;
55
use rustc_middle::mir::{Body, Promoted};
66
use rustc_middle::ty::TyCtxt;
77

@@ -100,8 +100,5 @@ pub fn get_body_with_borrowck_facts(
100100
def: LocalDefId,
101101
options: ConsumerOptions,
102102
) -> BodyWithBorrowckFacts<'_> {
103-
let (input_body, promoted) = tcx.mir_promoted(def);
104-
let input_body: &Body<'_> = &input_body.borrow();
105-
let promoted: &IndexSlice<_, _> = &promoted.borrow();
106-
*super::do_mir_borrowck(tcx, input_body, promoted, Some(options)).1.unwrap()
103+
*super::do_mir_borrowck(tcx, def, Some(options)).1.unwrap()
107104
}

compiler/rustc_borrowck/src/lib.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,8 @@ pub fn provide(providers: &mut Providers) {
103103
}
104104

105105
fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
106-
let (input_body, promoted) = tcx.mir_promoted(def);
107-
debug!("run query mir_borrowck: {}", tcx.def_path_str(def));
108-
106+
let (input_body, _) = tcx.mir_promoted(def);
109107
let input_body: &Body<'_> = &input_body.borrow();
110-
111108
if input_body.should_skip() || input_body.tainted_by_errors.is_some() {
112109
debug!("Skipping borrowck because of injected body or tainted body");
113110
// Let's make up a borrowck result! Fun times!
@@ -120,7 +117,7 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
120117
return tcx.arena.alloc(result);
121118
}
122119

123-
let borrowck_result = do_mir_borrowck(tcx, input_body, &*promoted.borrow(), None).0;
120+
let borrowck_result = do_mir_borrowck(tcx, def, None).0;
124121
debug!("mir_borrowck done");
125122

126123
tcx.arena.alloc(borrowck_result)
@@ -131,15 +128,16 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
131128
/// Use `consumer_options: None` for the default behavior of returning
132129
/// [`BorrowCheckResult`] only. Otherwise, return [`BodyWithBorrowckFacts`] according
133130
/// to the given [`ConsumerOptions`].
134-
#[instrument(skip(tcx, input_body, input_promoted), fields(id=?input_body.source.def_id()), level = "debug")]
131+
#[instrument(skip(tcx), level = "debug")]
135132
fn do_mir_borrowck<'tcx>(
136133
tcx: TyCtxt<'tcx>,
137-
input_body: &Body<'tcx>,
138-
input_promoted: &IndexSlice<Promoted, Body<'tcx>>,
134+
def: LocalDefId,
139135
consumer_options: Option<ConsumerOptions>,
140136
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
141-
let def = input_body.source.def_id().expect_local();
142137
let infcx = BorrowckInferCtxt::new(tcx, def);
138+
let (input_body, promoted) = tcx.mir_promoted(def);
139+
let input_body: &Body<'_> = &input_body.borrow();
140+
let input_promoted: &IndexSlice<_, _> = &promoted.borrow();
143141
if let Some(e) = input_body.tainted_by_errors {
144142
infcx.set_tainted_by_errors(e);
145143
}
@@ -499,7 +497,8 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
499497
)
500498
});
501499

502-
self.inject_new_hidden_type_unchecked(key, hidden_ty);
500+
let prev = self.register_hidden_type_in_storage(key, hidden_ty);
501+
assert_eq!(prev, None);
503502
}
504503
}
505504
}

compiler/rustc_infer/src/infer/opaque_types/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,12 @@ impl<'tcx> InferCtxt<'tcx> {
198198
/// it hasn't previously been defined. This does not emit any
199199
/// constraints and it's the responsibility of the caller to make
200200
/// sure that the item bounds of the opaque are checked.
201-
pub fn inject_new_hidden_type_unchecked(
201+
pub fn register_hidden_type_in_storage(
202202
&self,
203203
opaque_type_key: OpaqueTypeKey<'tcx>,
204204
hidden_ty: OpaqueHiddenType<'tcx>,
205-
) {
206-
let prev = self.inner.borrow_mut().opaque_types().register(opaque_type_key, hidden_ty);
207-
assert_eq!(prev, None);
205+
) -> Option<Ty<'tcx>> {
206+
self.inner.borrow_mut().opaque_types().register(opaque_type_key, hidden_ty)
208207
}
209208

210209
/// Insert a hidden type into the opaque type storage, equating it

compiler/rustc_next_trait_solver/src/delegate.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
6262
universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex,
6363
) -> <Self::Interner as Interner>::GenericArg;
6464

65-
// FIXME: Can we implement this in terms of `add` and `inject`?
66-
fn insert_hidden_type(
65+
fn register_hidden_type_in_storage(
6766
&self,
6867
opaque_type_key: ty::OpaqueTypeKey<Self::Interner>,
69-
param_env: <Self::Interner as Interner>::ParamEnv,
7068
hidden_ty: <Self::Interner as Interner>::Ty,
71-
goals: &mut Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
72-
) -> Result<(), NoSolution>;
69+
span: <Self::Interner as Interner>::Span,
70+
) -> Option<<Self::Interner as Interner>::Ty>;
7371

7472
fn add_item_bounds_for_hidden_type(
7573
&self,
@@ -79,14 +77,6 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
7977
hidden_ty: <Self::Interner as Interner>::Ty,
8078
goals: &mut Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
8179
);
82-
83-
fn inject_new_hidden_type_unchecked(
84-
&self,
85-
key: ty::OpaqueTypeKey<Self::Interner>,
86-
hidden_ty: <Self::Interner as Interner>::Ty,
87-
span: <Self::Interner as Interner>::Span,
88-
);
89-
9080
fn reset_opaque_types(&self);
9181

9282
fn fetch_eligible_assoc_item(

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ where
425425

426426
fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<I>, I::Ty)]) {
427427
for &(key, ty) in opaque_types {
428-
self.delegate.inject_new_hidden_type_unchecked(key, ty, self.origin_span);
428+
let prev = self.delegate.register_hidden_type_in_storage(key, ty, self.origin_span);
429+
assert_eq!(prev, None);
429430
}
430431
}
431432
}

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ where
387387
};
388388

389389
for &(key, ty) in &input.predefined_opaques_in_body.opaque_types {
390-
ecx.delegate.inject_new_hidden_type_unchecked(key, ty, ecx.origin_span);
390+
let prev = ecx.delegate.register_hidden_type_in_storage(key, ty, ecx.origin_span);
391+
assert_eq!(prev, None);
391392
}
392393

393394
if !ecx.nested_goals.is_empty() {
@@ -1070,16 +1071,12 @@ where
10701071
self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
10711072
}
10721073

1073-
pub(super) fn insert_hidden_type(
1074+
pub(super) fn register_hidden_type_in_storage(
10741075
&mut self,
10751076
opaque_type_key: ty::OpaqueTypeKey<I>,
1076-
param_env: I::ParamEnv,
10771077
hidden_ty: I::Ty,
1078-
) -> Result<(), NoSolution> {
1079-
let mut goals = Vec::new();
1080-
self.delegate.insert_hidden_type(opaque_type_key, param_env, hidden_ty, &mut goals)?;
1081-
self.add_goals(GoalSource::Misc, goals);
1082-
Ok(())
1078+
) -> Option<I::Ty> {
1079+
self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
10831080
}
10841081

10851082
pub(super) fn add_item_bounds_for_hidden_type(

compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ where
8686
}
8787

8888
// Otherwise, define a new opaque type
89-
// FIXME: should we use `inject_hidden_type_unchecked` here?
90-
self.insert_hidden_type(opaque_type_key, goal.param_env, expected)?;
89+
let prev = self.register_hidden_type_in_storage(opaque_type_key, expected);
90+
assert_eq!(prev, None);
9191
self.add_item_bounds_for_hidden_type(
9292
def_id.into(),
9393
opaque_ty.args,

compiler/rustc_trait_selection/src/solve/delegate.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
149149
self.0.instantiate_canonical_var(span, cv_info, universe_map)
150150
}
151151

152-
fn insert_hidden_type(
152+
fn register_hidden_type_in_storage(
153153
&self,
154-
opaque_type_key: ty::OpaqueTypeKey<'tcx>,
155-
param_env: ty::ParamEnv<'tcx>,
156-
hidden_ty: Ty<'tcx>,
157-
goals: &mut Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
158-
) -> Result<(), NoSolution> {
159-
self.0
160-
.insert_hidden_type(opaque_type_key, DUMMY_SP, param_env, hidden_ty, goals)
161-
.map_err(|_| NoSolution)
154+
opaque_type_key: rustc_type_ir::OpaqueTypeKey<Self::Interner>,
155+
hidden_ty: <Self::Interner as ty::Interner>::Ty,
156+
span: <Self::Interner as ty::Interner>::Span,
157+
) -> Option<<Self::Interner as ty::Interner>::Ty> {
158+
self.0.register_hidden_type_in_storage(
159+
opaque_type_key,
160+
ty::OpaqueHiddenType { span, ty: hidden_ty },
161+
)
162162
}
163163

164164
fn add_item_bounds_for_hidden_type(
@@ -172,15 +172,6 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
172172
self.0.add_item_bounds_for_hidden_type(def_id, args, param_env, hidden_ty, goals);
173173
}
174174

175-
fn inject_new_hidden_type_unchecked(
176-
&self,
177-
key: ty::OpaqueTypeKey<'tcx>,
178-
hidden_ty: Ty<'tcx>,
179-
span: Span,
180-
) {
181-
self.0.inject_new_hidden_type_unchecked(key, ty::OpaqueHiddenType { ty: hidden_ty, span })
182-
}
183-
184175
fn reset_opaque_types(&self) {
185176
let _ = self.take_opaque_types();
186177
}

0 commit comments

Comments
 (0)