Skip to content

Commit 8a34173

Browse files
committed
Auto merge of #129752 - compiler-errors:predicates-of-defaulted, r=<try>
Make supertrait and implied predicates queries defaulted r? `@ghost` only last commit is meaningful
2 parents 6cf068d + cb8ae18 commit 8a34173

File tree

21 files changed

+124
-145
lines changed

21 files changed

+124
-145
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.rs

-32
Original file line numberDiff line numberDiff line change
@@ -1070,34 +1070,6 @@ impl<'a> CrateMetadataRef<'a> {
10701070
)
10711071
}
10721072

1073-
fn get_explicit_item_bounds<'tcx>(
1074-
self,
1075-
index: DefIndex,
1076-
tcx: TyCtxt<'tcx>,
1077-
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
1078-
let lazy = self.root.tables.explicit_item_bounds.get(self, index);
1079-
let output = if lazy.is_default() {
1080-
&mut []
1081-
} else {
1082-
tcx.arena.alloc_from_iter(lazy.decode((self, tcx)))
1083-
};
1084-
ty::EarlyBinder::bind(&*output)
1085-
}
1086-
1087-
fn get_explicit_item_super_predicates<'tcx>(
1088-
self,
1089-
index: DefIndex,
1090-
tcx: TyCtxt<'tcx>,
1091-
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
1092-
let lazy = self.root.tables.explicit_item_super_predicates.get(self, index);
1093-
let output = if lazy.is_default() {
1094-
&mut []
1095-
} else {
1096-
tcx.arena.alloc_from_iter(lazy.decode((self, tcx)))
1097-
};
1098-
ty::EarlyBinder::bind(&*output)
1099-
}
1100-
11011073
fn get_variant(
11021074
self,
11031075
kind: DefKind,
@@ -1323,10 +1295,6 @@ impl<'a> CrateMetadataRef<'a> {
13231295
self.root.tables.optimized_mir.get(self, id).is_some()
13241296
}
13251297

1326-
fn cross_crate_inlinable(self, id: DefIndex) -> bool {
1327-
self.root.tables.cross_crate_inlinable.get(self, id)
1328-
}
1329-
13301298
fn get_fn_has_self_parameter(self, id: DefIndex, sess: &'a Session) -> bool {
13311299
self.root
13321300
.tables

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

+41-13
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ trait ProcessQueryValue<'tcx, T> {
3232
fn process_decoded(self, _tcx: TyCtxt<'tcx>, _err: impl Fn() -> !) -> T;
3333
}
3434

35-
impl<T> ProcessQueryValue<'_, Option<T>> for Option<T> {
35+
impl<T> ProcessQueryValue<'_, T> for T {
3636
#[inline(always)]
37-
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Option<T> {
37+
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> T {
3838
self
3939
}
4040
}
4141

42+
impl<'tcx, T> ProcessQueryValue<'tcx, ty::EarlyBinder<'tcx, T>> for T {
43+
#[inline(always)]
44+
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> ty::EarlyBinder<'tcx, T> {
45+
ty::EarlyBinder::bind(self)
46+
}
47+
}
48+
4249
impl<T> ProcessQueryValue<'_, T> for Option<T> {
4350
#[inline(always)]
4451
fn process_decoded(self, _tcx: TyCtxt<'_>, err: impl Fn() -> !) -> T {
@@ -69,6 +76,24 @@ impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>> ProcessQueryValue<'
6976
}
7077
}
7178

79+
impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
80+
ProcessQueryValue<'tcx, ty::EarlyBinder<'tcx, &'tcx [T]>>
81+
for Option<DecodeIterator<'a, 'tcx, T>>
82+
{
83+
#[inline(always)]
84+
fn process_decoded(
85+
self,
86+
tcx: TyCtxt<'tcx>,
87+
_err: impl Fn() -> !,
88+
) -> ty::EarlyBinder<'tcx, &'tcx [T]> {
89+
ty::EarlyBinder::bind(if let Some(iter) = self {
90+
tcx.arena.alloc_from_iter(iter)
91+
} else {
92+
&[]
93+
})
94+
}
95+
}
96+
7297
impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
7398
ProcessQueryValue<'tcx, Option<&'tcx [T]>> for Option<DecodeIterator<'a, 'tcx, T>>
7499
{
@@ -103,7 +128,12 @@ macro_rules! provide_one {
103128
provide_one! {
104129
$tcx, $def_id, $other, $cdata, $name => {
105130
let lazy = $cdata.root.tables.$name.get($cdata, $def_id.index);
106-
if lazy.is_default() { &[] } else { $tcx.arena.alloc_from_iter(lazy.decode(($cdata, $tcx))) }
131+
let value = if lazy.is_default() {
132+
&[] as &[_]
133+
} else {
134+
$tcx.arena.alloc_from_iter(lazy.decode(($cdata, $tcx)))
135+
};
136+
value.process_decoded($tcx, || panic!("{:?} does not have a {:?}", $def_id, stringify!($name)))
107137
}
108138
}
109139
};
@@ -212,15 +242,15 @@ impl IntoArgs for (CrateNum, SimplifiedType) {
212242
}
213243

214244
provide! { tcx, def_id, other, cdata,
215-
explicit_item_bounds => { cdata.get_explicit_item_bounds(def_id.index, tcx) }
216-
explicit_item_super_predicates => { cdata.get_explicit_item_super_predicates(def_id.index, tcx) }
245+
explicit_item_bounds => { table_defaulted_array }
246+
explicit_item_super_predicates => { table_defaulted_array }
217247
explicit_predicates_of => { table }
218248
generics_of => { table }
219249
inferred_outlives_of => { table_defaulted_array }
220-
explicit_super_predicates_of => { table }
221-
explicit_implied_predicates_of => { table }
250+
explicit_super_predicates_of => { table_defaulted_array }
251+
explicit_implied_predicates_of => { table_defaulted_array }
222252
type_of => { table }
223-
type_alias_is_lazy => { cdata.root.tables.type_alias_is_lazy.get(cdata, def_id.index) }
253+
type_alias_is_lazy => { table_direct }
224254
variances_of => { table }
225255
fn_sig => { table }
226256
codegen_fn_attrs => { table }
@@ -241,7 +271,7 @@ provide! { tcx, def_id, other, cdata,
241271
lookup_default_body_stability => { table }
242272
lookup_deprecation_entry => { table }
243273
params_in_repr => { table }
244-
unused_generic_params => { cdata.root.tables.unused_generic_params.get(cdata, def_id.index) }
274+
unused_generic_params => { table_direct }
245275
def_kind => { cdata.def_kind(def_id.index) }
246276
impl_parent => { table }
247277
defaultness => { table_direct }
@@ -287,9 +317,7 @@ provide! { tcx, def_id, other, cdata,
287317
.process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys")))
288318
}
289319

290-
associated_type_for_effects => {
291-
table
292-
}
320+
associated_type_for_effects => { table }
293321
associated_types_for_impl_traits_in_associated_fn => { table_defaulted_array }
294322

295323
visibility => { cdata.get_visibility(def_id.index) }
@@ -310,7 +338,7 @@ provide! { tcx, def_id, other, cdata,
310338
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
311339
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
312340
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
313-
cross_crate_inlinable => { cdata.cross_crate_inlinable(def_id.index) }
341+
cross_crate_inlinable => { table_direct }
314342

315343
dylib_dependency_formats => { cdata.get_dylib_dependency_formats(tcx) }
316344
is_private_dep => { cdata.private_dep }

0 commit comments

Comments
 (0)