Skip to content

Commit 9200452

Browse files
Stop using ty::GenericPredicates for non-predicates_of queries
1 parent ac77e88 commit 9200452

File tree

20 files changed

+101
-98
lines changed

20 files changed

+101
-98
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
420420
span: Span,
421421
def_id: LocalDefId,
422422
assoc_name: Ident,
423-
) -> ty::GenericPredicates<'tcx> {
423+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
424424
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name))
425425
}
426426

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+29-30
Original file line numberDiff line numberDiff line change
@@ -580,24 +580,24 @@ pub(super) fn explicit_predicates_of<'tcx>(
580580
/// Ensures that the super-predicates of the trait with a `DefId`
581581
/// of `trait_def_id` are lowered and stored. This also ensures that
582582
/// the transitive super-predicates are lowered.
583-
pub(super) fn explicit_super_predicates_of(
584-
tcx: TyCtxt<'_>,
583+
pub(super) fn explicit_super_predicates_of<'tcx>(
584+
tcx: TyCtxt<'tcx>,
585585
trait_def_id: LocalDefId,
586-
) -> ty::GenericPredicates<'_> {
586+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
587587
implied_predicates_with_filter(tcx, trait_def_id.to_def_id(), PredicateFilter::SelfOnly)
588588
}
589589

590-
pub(super) fn explicit_supertraits_containing_assoc_item(
591-
tcx: TyCtxt<'_>,
590+
pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
591+
tcx: TyCtxt<'tcx>,
592592
(trait_def_id, assoc_name): (DefId, Ident),
593-
) -> ty::GenericPredicates<'_> {
593+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
594594
implied_predicates_with_filter(tcx, trait_def_id, PredicateFilter::SelfThatDefines(assoc_name))
595595
}
596596

597-
pub(super) fn explicit_implied_predicates_of(
598-
tcx: TyCtxt<'_>,
597+
pub(super) fn explicit_implied_predicates_of<'tcx>(
598+
tcx: TyCtxt<'tcx>,
599599
trait_def_id: LocalDefId,
600-
) -> ty::GenericPredicates<'_> {
600+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
601601
implied_predicates_with_filter(
602602
tcx,
603603
trait_def_id.to_def_id(),
@@ -612,11 +612,11 @@ pub(super) fn explicit_implied_predicates_of(
612612
/// Ensures that the super-predicates of the trait with a `DefId`
613613
/// of `trait_def_id` are lowered and stored. This also ensures that
614614
/// the transitive super-predicates are lowered.
615-
pub(super) fn implied_predicates_with_filter(
616-
tcx: TyCtxt<'_>,
615+
pub(super) fn implied_predicates_with_filter<'tcx>(
616+
tcx: TyCtxt<'tcx>,
617617
trait_def_id: DefId,
618618
filter: PredicateFilter,
619-
) -> ty::GenericPredicates<'_> {
619+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
620620
let Some(trait_def_id) = trait_def_id.as_local() else {
621621
// if `assoc_name` is None, then the query should've been redirected to an
622622
// external provider
@@ -679,20 +679,16 @@ pub(super) fn implied_predicates_with_filter(
679679
_ => {}
680680
}
681681

682-
ty::GenericPredicates {
683-
parent: None,
684-
predicates: implied_bounds,
685-
effects_min_tys: ty::List::empty(),
686-
}
682+
ty::EarlyBinder::bind(implied_bounds)
687683
}
688684

689685
/// Returns the predicates defined on `item_def_id` of the form
690686
/// `X: Foo` where `X` is the type parameter `def_id`.
691687
#[instrument(level = "trace", skip(tcx))]
692-
pub(super) fn type_param_predicates(
693-
tcx: TyCtxt<'_>,
688+
pub(super) fn type_param_predicates<'tcx>(
689+
tcx: TyCtxt<'tcx>,
694690
(item_def_id, def_id, assoc_name): (LocalDefId, LocalDefId, Ident),
695-
) -> ty::GenericPredicates<'_> {
691+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
696692
use rustc_hir::*;
697693
use rustc_middle::ty::Ty;
698694

@@ -713,18 +709,20 @@ pub(super) fn type_param_predicates(
713709
tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
714710
};
715711

716-
let mut result = parent
717-
.map(|parent| {
718-
let icx = ItemCtxt::new(tcx, parent);
719-
icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_name)
720-
})
721-
.unwrap_or_default();
712+
let result = if let Some(parent) = parent {
713+
let icx = ItemCtxt::new(tcx, parent);
714+
icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_name)
715+
} else {
716+
ty::EarlyBinder::bind(&[] as &[_])
717+
};
722718
let mut extend = None;
723719

724720
let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
725721

726722
let hir_node = tcx.hir_node(item_hir_id);
727-
let Some(hir_generics) = hir_node.generics() else { return result };
723+
let Some(hir_generics) = hir_node.generics() else {
724+
return result;
725+
};
728726
if let Node::Item(item) = hir_node
729727
&& let ItemKind::Trait(..) = item.kind
730728
// Implied `Self: Trait` and supertrait bounds.
@@ -748,9 +746,10 @@ pub(super) fn type_param_predicates(
748746
_ => false,
749747
}),
750748
);
751-
result.predicates =
752-
tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(extra_predicates));
753-
result
749+
750+
ty::EarlyBinder::bind(
751+
tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates)),
752+
)
754753
}
755754

756755
impl<'tcx> ItemCtxt<'tcx> {

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
17611761
break Some((bound_vars.into_iter().collect(), assoc_item));
17621762
}
17631763
let predicates = tcx.explicit_supertraits_containing_assoc_item((def_id, assoc_name));
1764-
let obligations = predicates.predicates.iter().filter_map(|&(pred, _)| {
1764+
let obligations = predicates.iter_identity_copied().filter_map(|(pred, _)| {
17651765
let bound_predicate = pred.kind();
17661766
match bound_predicate.skip_binder() {
17671767
ty::ClauseKind::Trait(data) => {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub trait HirTyLowerer<'tcx> {
136136
span: Span,
137137
def_id: LocalDefId,
138138
assoc_name: Ident,
139-
) -> ty::GenericPredicates<'tcx>;
139+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]>;
140140

141141
/// Lower an associated type to a projection.
142142
///
@@ -831,13 +831,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
831831
debug!(?ty_param_def_id, ?assoc_name, ?span);
832832
let tcx = self.tcx();
833833

834-
let predicates = &self.probe_ty_param_bounds(span, ty_param_def_id, assoc_name).predicates;
834+
let predicates = &self.probe_ty_param_bounds(span, ty_param_def_id, assoc_name);
835835
debug!("predicates={:#?}", predicates);
836836

837837
self.probe_single_bound_for_assoc_item(
838838
|| {
839839
let trait_refs = predicates
840-
.iter()
840+
.iter_identity_copied()
841841
.filter_map(|(p, _)| Some(p.as_trait_clause()?.map_bound(|t| t.trait_ref)));
842842
traits::transitive_bounds_that_define_assoc_item(tcx, trait_refs, assoc_name)
843843
},

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -263,27 +263,24 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
263263
_: Span,
264264
def_id: LocalDefId,
265265
_: Ident,
266-
) -> ty::GenericPredicates<'tcx> {
266+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
267267
let tcx = self.tcx;
268268
let item_def_id = tcx.hir().ty_param_owner(def_id);
269269
let generics = tcx.generics_of(item_def_id);
270270
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
271271
// HACK(eddyb) should get the original `Span`.
272272
let span = tcx.def_span(def_id);
273-
ty::GenericPredicates {
274-
parent: None,
275-
predicates: tcx.arena.alloc_from_iter(
276-
self.param_env.caller_bounds().iter().filter_map(|predicate| {
277-
match predicate.kind().skip_binder() {
278-
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
279-
Some((predicate, span))
280-
}
281-
_ => None,
273+
274+
ty::EarlyBinder::bind(tcx.arena.alloc_from_iter(
275+
self.param_env.caller_bounds().iter().filter_map(|predicate| {
276+
match predicate.kind().skip_binder() {
277+
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
278+
Some((predicate, span))
282279
}
283-
}),
284-
),
285-
effects_min_tys: ty::List::empty(),
286-
}
280+
_ => None,
281+
}
282+
}),
283+
))
287284
}
288285

289286
fn lower_assoc_ty(

compiler/rustc_infer/src/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub fn transitive_bounds_that_define_assoc_item<'tcx>(
123123

124124
stack.extend(
125125
tcx.explicit_supertraits_containing_assoc_item((trait_ref.def_id(), assoc_name))
126-
.instantiate_own_identity()
126+
.iter_identity_copied()
127127
.map(|(clause, _)| clause.instantiate_supertrait(tcx, trait_ref))
128128
.filter_map(|clause| clause.as_trait_clause())
129129
// FIXME: Negative supertraits are elaborated here lol

compiler/rustc_lint/src/multiple_supertrait_upcastable.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable {
4545
let direct_super_traits_iter = cx
4646
.tcx
4747
.explicit_super_predicates_of(def_id)
48-
.predicates
49-
.into_iter()
48+
.iter_identity_copied()
5049
.filter_map(|(pred, _)| pred.as_trait_clause());
5150
if direct_super_traits_iter.count() > 1 {
5251
cx.emit_span_lint(

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+18
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>> ProcessQueryValue<'
6969
}
7070
}
7171

72+
impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
73+
ProcessQueryValue<'tcx, ty::EarlyBinder<'tcx, &'tcx [T]>>
74+
for Option<DecodeIterator<'a, 'tcx, T>>
75+
{
76+
#[inline(always)]
77+
fn process_decoded(
78+
self,
79+
tcx: TyCtxt<'tcx>,
80+
_err: impl Fn() -> !,
81+
) -> ty::EarlyBinder<'tcx, &'tcx [T]> {
82+
ty::EarlyBinder::bind(if let Some(iter) = self {
83+
tcx.arena.alloc_from_iter(iter)
84+
} else {
85+
&[]
86+
})
87+
}
88+
}
89+
7290
impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
7391
ProcessQueryValue<'tcx, Option<&'tcx [T]>> for Option<DecodeIterator<'a, 'tcx, T>>
7492
{

compiler/rustc_metadata/src/rmeta/encoder.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1443,17 +1443,21 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14431443
}
14441444
if let DefKind::Trait = def_kind {
14451445
record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
1446-
record!(self.tables.explicit_super_predicates_of[def_id] <- self.tcx.explicit_super_predicates_of(def_id));
1447-
record!(self.tables.explicit_implied_predicates_of[def_id] <- self.tcx.explicit_implied_predicates_of(def_id));
1446+
record_array!(self.tables.explicit_super_predicates_of[def_id] <-
1447+
self.tcx.explicit_super_predicates_of(def_id).skip_binder());
1448+
record_array!(self.tables.explicit_implied_predicates_of[def_id] <-
1449+
self.tcx.explicit_implied_predicates_of(def_id).skip_binder());
14481450

14491451
let module_children = self.tcx.module_children_local(local_id);
14501452
record_array!(self.tables.module_children_non_reexports[def_id] <-
14511453
module_children.iter().map(|child| child.res.def_id().index));
14521454
}
14531455
if let DefKind::TraitAlias = def_kind {
14541456
record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
1455-
record!(self.tables.explicit_super_predicates_of[def_id] <- self.tcx.explicit_super_predicates_of(def_id));
1456-
record!(self.tables.explicit_implied_predicates_of[def_id] <- self.tcx.explicit_implied_predicates_of(def_id));
1457+
record_array!(self.tables.explicit_super_predicates_of[def_id] <-
1458+
self.tcx.explicit_super_predicates_of(def_id).skip_binder());
1459+
record_array!(self.tables.explicit_implied_predicates_of[def_id] <-
1460+
self.tcx.explicit_implied_predicates_of(def_id).skip_binder());
14571461
}
14581462
if let DefKind::Trait | DefKind::Impl { .. } = def_kind {
14591463
let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);

compiler/rustc_metadata/src/rmeta/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,10 @@ define_tables! {
419419
lookup_deprecation_entry: Table<DefIndex, LazyValue<attr::Deprecation>>,
420420
explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
421421
generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
422-
explicit_super_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
422+
explicit_super_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
423423
// As an optimization, we only store this for trait aliases,
424424
// since it's identical to explicit_super_predicates_of for traits.
425-
explicit_implied_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
425+
explicit_implied_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
426426
type_of: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, Ty<'static>>>>,
427427
variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
428428
fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::PolyFnSig<'static>>>>,

compiler/rustc_middle/src/query/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ rustc_queries! {
651651
/// is a subset of the full list of predicates. We store these in a separate map
652652
/// because we must evaluate them even during type conversion, often before the full
653653
/// predicates are available (note that super-predicates must not be cyclic).
654-
query explicit_super_predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
654+
query explicit_super_predicates_of(key: DefId) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
655655
desc { |tcx| "computing the super predicates of `{}`", tcx.def_path_str(key) }
656656
cache_on_disk_if { key.is_local() }
657657
separate_provide_extern
@@ -662,7 +662,7 @@ rustc_queries! {
662662
/// of the trait. For regular traits, this includes all super-predicates and their
663663
/// associated type bounds. For trait aliases, currently, this includes all of the
664664
/// predicates of the trait alias.
665-
query explicit_implied_predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
665+
query explicit_implied_predicates_of(key: DefId) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
666666
desc { |tcx| "computing the implied predicates of `{}`", tcx.def_path_str(key) }
667667
cache_on_disk_if { key.is_local() }
668668
separate_provide_extern
@@ -671,7 +671,9 @@ rustc_queries! {
671671
/// The Ident is the name of an associated type.The query returns only the subset
672672
/// of supertraits that define the given associated type. This is used to avoid
673673
/// cycles in resolving type-dependent associated item paths like `T::Item`.
674-
query explicit_supertraits_containing_assoc_item(key: (DefId, rustc_span::symbol::Ident)) -> ty::GenericPredicates<'tcx> {
674+
query explicit_supertraits_containing_assoc_item(
675+
key: (DefId, rustc_span::symbol::Ident)
676+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
675677
desc { |tcx| "computing the super traits of `{}` with associated type name `{}`",
676678
tcx.def_path_str(key.0),
677679
key.1
@@ -680,7 +682,9 @@ rustc_queries! {
680682

681683
/// To avoid cycles within the predicates of a single item we compute
682684
/// per-type-parameter predicates for resolving `T::AssocTy`.
683-
query type_param_predicates(key: (LocalDefId, LocalDefId, rustc_span::symbol::Ident)) -> ty::GenericPredicates<'tcx> {
685+
query type_param_predicates(
686+
key: (LocalDefId, LocalDefId, rustc_span::symbol::Ident)
687+
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
684688
desc { |tcx| "computing the bounds for type parameter `{}`", tcx.hir().ty_param_name(key.1) }
685689
}
686690

compiler/rustc_middle/src/ty/context.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -349,16 +349,14 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
349349
self,
350350
def_id: DefId,
351351
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
352-
ty::EarlyBinder::bind(self.explicit_super_predicates_of(def_id).instantiate_identity(self))
352+
self.explicit_super_predicates_of(def_id).map_bound(|preds| preds.into_iter().copied())
353353
}
354354

355355
fn explicit_implied_predicates_of(
356356
self,
357357
def_id: DefId,
358358
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
359-
ty::EarlyBinder::bind(
360-
self.explicit_implied_predicates_of(def_id).instantiate_identity(self),
361-
)
359+
self.explicit_implied_predicates_of(def_id).map_bound(|preds| preds.into_iter().copied())
362360
}
363361

364362
fn has_target_features(self, def_id: DefId) -> bool {

compiler/rustc_trait_selection/src/traits/object_safety.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,11 @@ fn predicates_reference_self(
185185
) -> SmallVec<[Span; 1]> {
186186
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(tcx, trait_def_id));
187187
let predicates = if supertraits_only {
188-
tcx.explicit_super_predicates_of(trait_def_id)
188+
tcx.explicit_super_predicates_of(trait_def_id).skip_binder()
189189
} else {
190-
tcx.predicates_of(trait_def_id)
190+
tcx.predicates_of(trait_def_id).predicates
191191
};
192192
predicates
193-
.predicates
194193
.iter()
195194
.map(|&(predicate, sp)| (predicate.instantiate_supertrait(tcx, trait_ref), sp))
196195
.filter_map(|(clause, sp)| {
@@ -266,9 +265,8 @@ fn super_predicates_have_non_lifetime_binders(
266265
return SmallVec::new();
267266
}
268267
tcx.explicit_super_predicates_of(trait_def_id)
269-
.predicates
270-
.iter()
271-
.filter_map(|(pred, span)| pred.has_non_region_bound_vars().then_some(*span))
268+
.iter_identity_copied()
269+
.filter_map(|(pred, span)| pred.has_non_region_bound_vars().then_some(span))
272270
.collect()
273271
}
274272

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -600,21 +600,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
600600

601601
// Check supertraits hold. This is so that their associated type bounds
602602
// will be checked in the code below.
603-
for super_trait in tcx
603+
for (supertrait, _) in tcx
604604
.explicit_super_predicates_of(trait_predicate.def_id())
605-
.instantiate(tcx, trait_predicate.trait_ref.args)
606-
.predicates
607-
.into_iter()
605+
.iter_instantiated_copied(tcx, trait_predicate.trait_ref.args)
608606
{
609-
let normalized_super_trait = normalize_with_depth_to(
607+
let normalized_supertrait = normalize_with_depth_to(
610608
self,
611609
obligation.param_env,
612610
obligation.cause.clone(),
613611
obligation.recursion_depth + 1,
614-
super_trait,
612+
supertrait,
615613
&mut nested,
616614
);
617-
nested.push(obligation.with(tcx, normalized_super_trait));
615+
nested.push(obligation.with(tcx, normalized_supertrait));
618616
}
619617

620618
let assoc_types: Vec<_> = tcx

0 commit comments

Comments
 (0)