Skip to content

Commit 520ebcb

Browse files
committed
middle: ClauseKind-specific ParamEnv accessors
Replace the `caller_bounds` function of `ParamEnv` with `ClauseKind`-specific accessors (i.e. `trait_clauses`, `region_outlives_clauses`) which will make it easier to later change the internals of `ParamEnv`.
1 parent 97c966b commit 520ebcb

File tree

38 files changed

+325
-254
lines changed

38 files changed

+325
-254
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
254254
let place = &self.move_data.move_paths[mpi].place;
255255
let ty = place.ty(self.body, self.infcx.tcx).ty;
256256

257-
if self.infcx.param_env.caller_bounds().iter().any(|c| {
258-
c.as_trait_clause().is_some_and(|pred| {
259-
pred.skip_binder().self_ty() == ty && self.infcx.tcx.is_fn_trait(pred.def_id())
260-
})
257+
if self.infcx.param_env.trait_clauses().any(|pred| {
258+
pred.skip_binder().self_ty() == ty && self.infcx.tcx.is_fn_trait(pred.def_id())
261259
}) {
262260
// Suppress the next suggestion since we don't want to put more bounds onto
263261
// something that already has `Fn`-like bounds (or is a closure), so we can't

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,13 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
226226
// Normalize the assumptions we use to borrowck the program.
227227
let mut constraints = vec![];
228228
let mut known_type_outlives_obligations = vec![];
229-
for bound in param_env.caller_bounds() {
230-
if let Some(outlives) = bound.as_type_outlives_clause() {
231-
self.normalize_and_push_type_outlives_obligation(
232-
outlives,
233-
span,
234-
&mut known_type_outlives_obligations,
235-
&mut constraints,
236-
);
237-
};
229+
for outlives in param_env.type_outlives_clauses() {
230+
self.normalize_and_push_type_outlives_obligation(
231+
outlives,
232+
span,
233+
&mut known_type_outlives_obligations,
234+
&mut constraints,
235+
);
238236
}
239237

240238
let unnormalized_input_output_tys = self

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ fn compare_method_predicate_entailment<'tcx>(
235235
let normalize_cause = traits::ObligationCause::misc(impl_m_span, impl_m_def_id);
236236
let param_env = ty::ParamEnv::new(tcx.mk_clauses(&hybrid_preds));
237237
let param_env = traits::normalize_param_env_or_error(tcx, param_env, normalize_cause);
238-
debug!(caller_bounds=?param_env.caller_bounds());
238+
debug!(?param_env);
239239

240240
let infcx = &tcx.infer_ctxt().build(TypingMode::non_body_analysis());
241241
let ocx = ObligationCtxt::new_with_diagnostics(infcx);
@@ -1990,7 +1990,7 @@ fn compare_type_predicate_entailment<'tcx>(
19901990

19911991
let param_env = ty::ParamEnv::new(tcx.mk_clauses(&hybrid_preds));
19921992
let param_env = traits::normalize_param_env_or_error(tcx, param_env, normalize_cause);
1993-
debug!(caller_bounds=?param_env.caller_bounds());
1993+
debug!(?param_env);
19941994

19951995
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
19961996
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
@@ -2229,7 +2229,7 @@ fn param_env_with_gat_bounds<'tcx>(
22292229
) -> ty::ParamEnv<'tcx> {
22302230
let param_env = tcx.param_env(impl_ty.def_id);
22312231
let container_id = impl_ty.container_id(tcx);
2232-
let mut predicates = param_env.caller_bounds().to_vec();
2232+
let mut predicates = param_env.all_clauses().collect::<Vec<_>>();
22332233

22342234
// for RPITITs, we should install predicates that allow us to project all
22352235
// of the RPITITs associated with the same body. This is because checking

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,8 @@ fn augment_param_env<'tcx>(
586586
return param_env;
587587
}
588588

589-
let bounds = tcx.mk_clauses_from_iter(
590-
param_env.caller_bounds().iter().chain(new_predicates.iter().cloned()),
591-
);
589+
let bounds =
590+
tcx.mk_clauses_from_iter(param_env.all_clauses().chain(new_predicates.iter().cloned()));
592591
// FIXME(compiler-errors): Perhaps there is a case where we need to normalize this
593592
// i.e. traits::normalize_param_env_or_error
594593
ty::ParamEnv::new(bounds)

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_hir::{self as hir, HirId, ItemLocalMap};
1515
use rustc_hir_analysis::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
1616
use rustc_infer::infer;
1717
use rustc_infer::traits::Obligation;
18-
use rustc_middle::ty::{self, Const, Ty, TyCtxt, TypeVisitableExt};
18+
use rustc_middle::ty::{self, Const, Ty, TyCtxt, TypeVisitableExt, Upcast};
1919
use rustc_session::Session;
2020
use rustc_span::{self, DUMMY_SP, ErrorGuaranteed, Ident, Span, sym};
2121
use rustc_trait_selection::error_reporting::TypeErrCtxt;
@@ -296,16 +296,15 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
296296
// HACK(eddyb) should get the original `Span`.
297297
let span = tcx.def_span(def_id);
298298

299-
ty::EarlyBinder::bind(tcx.arena.alloc_from_iter(
300-
self.param_env.caller_bounds().iter().filter_map(|predicate| {
301-
match predicate.kind().skip_binder() {
302-
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
303-
Some((predicate, span))
304-
}
305-
_ => None,
306-
}
307-
}),
308-
))
299+
ty::EarlyBinder::bind(tcx.arena.alloc_from_iter(self.param_env.trait_clauses().filter_map(
300+
|predicate| {
301+
predicate
302+
.self_ty()
303+
.skip_binder()
304+
.is_param(index)
305+
.then_some((predicate.upcast(tcx), span))
306+
},
307+
)))
309308
}
310309

311310
fn lower_assoc_shared(

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -909,24 +909,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
909909

910910
#[instrument(level = "debug", skip(self))]
911911
fn assemble_inherent_candidates_from_param(&mut self, param_ty: ty::ParamTy) {
912-
let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
913-
let bound_predicate = predicate.kind();
914-
match bound_predicate.skip_binder() {
915-
ty::ClauseKind::Trait(trait_predicate) => {
916-
match *trait_predicate.trait_ref.self_ty().kind() {
917-
ty::Param(p) if p == param_ty => {
918-
Some(bound_predicate.rebind(trait_predicate.trait_ref))
919-
}
920-
_ => None,
921-
}
922-
}
923-
ty::ClauseKind::RegionOutlives(_)
924-
| ty::ClauseKind::TypeOutlives(_)
925-
| ty::ClauseKind::Projection(_)
926-
| ty::ClauseKind::ConstArgHasType(_, _)
927-
| ty::ClauseKind::WellFormed(_)
928-
| ty::ClauseKind::ConstEvaluatable(_)
929-
| ty::ClauseKind::HostEffect(..) => None,
912+
let bounds = self.param_env.trait_clauses().filter_map(|trait_predicate| {
913+
match *trait_predicate.self_ty().skip_binder().kind() {
914+
ty::Param(p) if p == param_ty => Some(trait_predicate.map_bound(|p| p.trait_ref)),
915+
_ => None,
930916
}
931917
});
932918

compiler/rustc_infer/src/infer/outlives/for_liveness.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ where
6565
let outlives_bounds: Vec<_> = tcx
6666
.item_bounds(def_id)
6767
.iter_instantiated(tcx, args)
68-
.chain(param_env.caller_bounds())
69-
.filter_map(|clause| {
70-
let outlives = clause.as_type_outlives_clause()?;
68+
.filter_map(|clause| clause.as_type_outlives_clause())
69+
.chain(param_env.type_outlives_clauses())
70+
.filter_map(|outlives| {
7171
if let Some(outlives) = outlives.no_bound_vars()
7272
&& outlives.0 == ty
7373
{

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ pub mod obligations;
1717
pub mod test_type_match;
1818
pub(crate) mod verify;
1919

20-
#[instrument(level = "debug", skip(param_env), ret)]
20+
#[instrument(level = "debug", skip(param_env))]
2121
pub fn explicit_outlives_bounds<'tcx>(
2222
param_env: ty::ParamEnv<'tcx>,
2323
) -> impl Iterator<Item = OutlivesBound<'tcx>> {
2424
param_env
25-
.caller_bounds()
26-
.into_iter()
27-
.filter_map(ty::Clause::as_region_outlives_clause)
25+
.region_outlives_clauses()
2826
.filter_map(ty::Binder::no_bound_vars)
2927
.map(|ty::OutlivesPredicate(r_a, r_b)| OutlivesBound::RegionSubRegion(r_b, r_a))
3028
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2437,7 +2437,7 @@ rustc_queries! {
24372437
desc { "resolving instance `{}`", ty::Instance::new(key.value.0, key.value.1) }
24382438
}
24392439

2440-
query reveal_opaque_types_in_bounds(key: ty::Clauses<'tcx>) -> ty::Clauses<'tcx> {
2440+
query reveal_opaque_types_in_bounds(key: ty::ParamEnv<'tcx>) -> ty::ParamEnv<'tcx> {
24412441
desc { "revealing opaque types in `{:?}`", key }
24422442
}
24432443

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for CtfeProvenance {
209209

210210
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::ParamEnv<'tcx> {
211211
fn encode(&self, e: &mut E) {
212-
self.caller_bounds().encode(e);
212+
self.caller_bounds.encode(e);
213213
}
214214
}
215215

@@ -340,8 +340,8 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::SymbolName<'tcx> {
340340

341341
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::ParamEnv<'tcx> {
342342
fn decode(d: &mut D) -> Self {
343-
let caller_bounds = Decodable::decode(d);
344-
ty::ParamEnv::new(caller_bounds)
343+
let all_clauses = Decodable::decode(d);
344+
ty::ParamEnv::new(all_clauses)
345345
}
346346
}
347347

0 commit comments

Comments
 (0)