add EarlyBinder to output of explicit_item_bounds; replace bound_explicit_item_bounds usages; remove bound_explicit_item_bounds query

This commit is contained in:
Kyle Matsuda 2023-04-17 16:43:46 -06:00
parent 0892a7380b
commit f3b279fcc5
26 changed files with 49 additions and 56 deletions

View file

@ -706,7 +706,7 @@ fn suggest_borrow_fn_like(
.copied()
.find_map(find_fn_kind_from_did),
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => tcx
.bound_explicit_item_bounds(def_id)
.explicit_item_bounds(def_id)
.subst_iter_copied(tcx, substs)
.find_map(find_fn_kind_from_did),
ty::Closure(_, substs) => match substs.as_closure().kind() {

View file

@ -318,10 +318,8 @@ fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
tcx,
selftys: vec![],
};
let prohibit_opaque = tcx
.bound_explicit_item_bounds(def_id.to_def_id())
.transpose_iter()
.try_for_each(|bound| {
let prohibit_opaque =
tcx.explicit_item_bounds(def_id).transpose_iter().try_for_each(|bound| {
let predicate = bound.map_bound(|&(predicate, _)| predicate).subst_identity();
predicate.visit_with(&mut visitor)
});

View file

@ -839,7 +839,7 @@ fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
});
self.types.insert(proj.def_id, (infer_ty, proj.substs));
// Recurse into bounds
for (pred, pred_span) in self.interner().bound_explicit_item_bounds(proj.def_id).subst_iter_copied(self.interner(), proj.substs) {
for (pred, pred_span) in self.interner().explicit_item_bounds(proj.def_id).subst_iter_copied(self.interner(), proj.substs) {
let pred = pred.fold_with(self);
let pred = self.ocx.normalize(
&ObligationCause::misc(self.span, self.body_id),
@ -2023,7 +2023,7 @@ pub(super) fn check_type_bounds<'tcx>(
};
let obligations: Vec<_> = tcx
.bound_explicit_item_bounds(trait_ty.def_id)
.explicit_item_bounds(trait_ty.def_id)
.subst_iter_copied(tcx, rebased_substs)
.map(|(concrete_ty_bound, span)| {
debug!("check_type_bounds: concrete_ty_bound = {:?}", concrete_ty_bound);

View file

@ -360,7 +360,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
tcx,
param_env,
item_def_id,
tcx.bound_explicit_item_bounds(item_def_id.to_def_id())
tcx.explicit_item_bounds(item_def_id)
.transpose_iter()
.map(|bound| bound.map_bound(|b| *b).subst_identity())
.collect::<Vec<_>>(),
@ -1125,7 +1125,7 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
/// Assuming the defaults are used, check that all predicates (bounds on the
/// assoc type and where clauses on the trait) hold.
fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: ty::AssocItem, span: Span) {
let bounds = wfcx.tcx().bound_explicit_item_bounds(item.def_id);
let bounds = wfcx.tcx().explicit_item_bounds(item.def_id);
debug!("check_associated_type_bounds: bounds={:?}", bounds);
let wf_obligations = bounds.transpose_iter().flat_map(|b| {
@ -1592,7 +1592,7 @@ fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<!> {
}
});
for (bound, bound_span) in tcx
.bound_explicit_item_bounds(opaque_ty.def_id)
.explicit_item_bounds(opaque_ty.def_id)
.subst_iter_copied(tcx, opaque_ty.substs)
{
let bound = self.wfcx.normalize(bound_span, None, bound);

View file

@ -79,14 +79,14 @@ fn opaque_type_bounds<'tcx>(
pub(super) fn explicit_item_bounds(
tcx: TyCtxt<'_>,
def_id: LocalDefId,
) -> &'_ [(ty::Predicate<'_>, Span)] {
) -> ty::EarlyBinder<&'_ [(ty::Predicate<'_>, Span)]> {
match tcx.opt_rpitit_info(def_id.to_def_id()) {
// RPITIT's bounds are the same as opaque type bounds, but with
// a projection self type.
Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
let item = tcx.hir().get_by_def_id(opaque_def_id.expect_local()).expect_item();
let opaque_ty = item.expect_opaque_ty();
return opaque_type_bounds(
return ty::EarlyBinder(opaque_type_bounds(
tcx,
opaque_def_id.expect_local(),
opaque_ty.bounds,
@ -95,7 +95,7 @@ pub(super) fn explicit_item_bounds(
ty::InternalSubsts::identity_for_item(tcx, def_id),
),
item.span,
);
));
}
// These should have been fed!
Some(ty::ImplTraitInTraitData::Impl { .. }) => unreachable!(),
@ -103,7 +103,7 @@ pub(super) fn explicit_item_bounds(
}
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
match tcx.hir().get(hir_id) {
let bounds = match tcx.hir().get(hir_id) {
hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Type(bounds, _),
span,
@ -123,14 +123,15 @@ pub(super) fn explicit_item_bounds(
opaque_type_bounds(tcx, def_id, bounds, item_ty, *span)
}
_ => bug!("item_bounds called on {:?}", def_id),
}
};
ty::EarlyBinder(bounds)
}
pub(super) fn item_bounds(
tcx: TyCtxt<'_>,
def_id: DefId,
) -> ty::EarlyBinder<&'_ ty::List<ty::Predicate<'_>>> {
tcx.bound_explicit_item_bounds(def_id).map_bound(|bounds| {
tcx.explicit_item_bounds(def_id).map_bound(|bounds| {
tcx.mk_predicates_from_iter(util::elaborate(
tcx,
bounds.iter().map(|&(bound, _span)| bound),

View file

@ -153,7 +153,7 @@ fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
let mut collector =
OpaqueTypeLifetimeCollector { tcx, root_def_id: item_def_id.to_def_id(), variances };
let id_substs = ty::InternalSubsts::identity_for_item(tcx, item_def_id);
for pred in tcx.bound_explicit_item_bounds(item_def_id.to_def_id()).transpose_iter() {
for pred in tcx.explicit_item_bounds(item_def_id).transpose_iter() {
let pred = pred.map_bound(|(pred, _)| *pred).subst(tcx, id_substs);
debug!(?pred);

View file

@ -530,7 +530,7 @@ pub(crate) fn opt_suggest_box_span(
for ty in [first_ty, second_ty] {
for (pred, _) in self
.tcx
.bound_explicit_item_bounds(rpit_def_id)
.explicit_item_bounds(rpit_def_id)
.subst_iter_copied(self.tcx, substs)
{
let pred = pred.kind().rebind(match pred.kind().skip_binder() {

View file

@ -172,7 +172,7 @@ fn deduce_closure_signature(
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => self
.deduce_closure_signature_from_predicates(
expected_ty,
self.tcx.bound_explicit_item_bounds(def_id).subst_iter_copied(self.tcx, substs),
self.tcx.explicit_item_bounds(def_id).subst_iter_copied(self.tcx, substs),
),
ty::Dynamic(ref object_type, ..) => {
let sig = object_type.projection_bounds().find_map(|pb| {
@ -713,13 +713,13 @@ fn deduce_future_output_from_obligations(
}
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => self
.tcx
.bound_explicit_item_bounds(def_id)
.explicit_item_bounds(def_id)
.subst_iter_copied(self.tcx, substs)
.find_map(|(p, s)| get_future_output(p, s))?,
ty::Error(_) => return None,
ty::Alias(ty::Projection, proj) if self.tcx.is_impl_trait_in_trait(proj.def_id) => self
.tcx
.bound_explicit_item_bounds(proj.def_id)
.explicit_item_bounds(proj.def_id)
.subst_iter_copied(self.tcx, proj.substs)
.find_map(|(p, s)| get_future_output(p, s))?,
_ => span_bug!(

View file

@ -571,8 +571,8 @@ fn check_must_not_suspend_ty<'tcx>(
// FIXME: support adding the attribute to TAITs
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
let mut has_emitted = false;
for bound in fcx.tcx.bound_explicit_item_bounds(def).transpose_iter() {
let predicate = bound.map_bound(|&(predicate, _)| predicate).subst_identity();
for bound in fcx.tcx.explicit_item_bounds(def).transpose_iter() {
let predicate = bound.map_bound(|&(pred, _)| pred).subst_identity();
// We only look at the `DefId`, so it is safe to skip the binder here.
if let ty::PredicateKind::Clause(ty::Clause::Trait(ref poly_trait_predicate)) =
predicate.kind().skip_binder()

View file

@ -402,7 +402,7 @@ pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
let future_trait = self.tcx.require_lang_item(LangItem::Future, None);
let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
self.tcx.bound_explicit_item_bounds(def_id).subst_iter_copied(self.tcx, substs).find_map(
self.tcx.explicit_item_bounds(def_id).subst_iter_copied(self.tcx, substs).find_map(
|(predicate, _)| {
predicate
.kind()

View file

@ -540,7 +540,7 @@ fn register_hidden_type(
.obligations;
}
let item_bounds = tcx.bound_explicit_item_bounds(def_id.to_def_id());
let item_bounds = tcx.explicit_item_bounds(def_id);
for (predicate, _) in item_bounds.subst_iter_copied(tcx, substs) {
let predicate = predicate.fold_with(&mut BottomUpFolder {

View file

@ -74,7 +74,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
// For every projection predicate in the opaque type's explicit bounds,
// check that the type that we're assigning actually satisfies the bounds
// of the associated type.
for bound in cx.tcx.bound_explicit_item_bounds(def_id).transpose_iter() {
for bound in cx.tcx.explicit_item_bounds(def_id).transpose_iter() {
let (pred, pred_span) = bound.map_bound(|b| *b).subst_identity();
// Liberate bound regions in the predicate since we
@ -114,7 +114,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
// with `impl Send: OtherTrait`.
for (assoc_pred, assoc_pred_span) in cx
.tcx
.bound_explicit_item_bounds(proj.projection_ty.def_id)
.explicit_item_bounds(proj.projection_ty.def_id)
.subst_iter_copied(cx.tcx, &proj.projection_ty.substs)
{
let assoc_pred = assoc_pred.fold_with(proj_replacer);

View file

@ -257,7 +257,7 @@ fn is_ty_must_use<'tcx>(
elaborate(
cx.tcx,
cx.tcx
.bound_explicit_item_bounds(def)
.explicit_item_bounds(def)
.transpose_iter()
.map(|bound| bound.map_bound(|b| *b).subst_identity()),
)

View file

@ -203,7 +203,11 @@ fn into_args(self) -> (DefId, SimplifiedType) {
}
provide! { tcx, def_id, other, cdata,
explicit_item_bounds => { table_defaulted_array }
explicit_item_bounds => {
let lazy = cdata.root.tables.explicit_item_bounds.get(cdata, def_id.index);
let output = if lazy.is_default() { &mut [] } else { tcx.arena.alloc_from_iter(lazy.decode((cdata, tcx))) };
ty::EarlyBinder(&*output)
}
explicit_predicates_of => { table }
generics_of => { table }
inferred_outlives_of => { table_defaulted_array }

View file

@ -1425,7 +1425,7 @@ fn encode_info_for_mod(&mut self, local_def_id: LocalDefId) {
fn encode_explicit_item_bounds(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id);
let bounds = self.tcx.explicit_item_bounds(def_id);
let bounds = self.tcx.explicit_item_bounds(def_id).skip_binder();
record_defaulted_array!(self.tables.explicit_item_bounds[def_id] <- bounds);
}

View file

@ -274,7 +274,7 @@
/// `key` is the `DefId` of the associated type or opaque type.
///
/// Bounds from the parent (e.g. with nested impl trait) are not included.
query explicit_item_bounds(key: DefId) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
query explicit_item_bounds(key: DefId) -> ty::EarlyBinder<&'tcx [(ty::Predicate<'tcx>, Span)]> {
desc { |tcx| "finding item bounds for `{}`", tcx.def_path_str(key) }
cache_on_disk_if { key.is_local() }
separate_provide_extern

View file

@ -1611,7 +1611,7 @@ pub fn ty_is_opaque_future(self, ty: Ty<'_>) -> bool {
let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = ty.kind() else { return false };
let future_trait = self.require_lang_item(LangItem::Future, None);
self.bound_explicit_item_bounds(*def_id).skip_binder().iter().any(|(predicate, _)| {
self.explicit_item_bounds(def_id).skip_binder().iter().any(|(predicate, _)| {
let ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() else {
return false;
};

View file

@ -911,7 +911,7 @@ fn pretty_print_opaque_impl_type(
// Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
// by looking up the projections associated with the def_id.
let bounds = tcx.bound_explicit_item_bounds(def_id);
let bounds = tcx.explicit_item_bounds(def_id);
let mut traits = FxIndexMap::default();
let mut fn_traits = FxIndexMap::default();

View file

@ -694,13 +694,6 @@ pub fn try_expand_impl_trait_type(
if visitor.found_recursion { Err(expanded_type) } else { Ok(expanded_type) }
}
pub fn bound_explicit_item_bounds(
self,
def_id: DefId,
) -> ty::EarlyBinder<&'tcx [(ty::Predicate<'tcx>, rustc_span::Span)]> {
ty::EarlyBinder(self.explicit_item_bounds(def_id))
}
/// Returns names of captured upvars for closures and generators.
///
/// Here are some examples:

View file

@ -1800,7 +1800,7 @@ fn check_must_not_suspend_ty<'tcx>(
// FIXME: support adding the attribute to TAITs
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
let mut has_emitted = false;
for bound in tcx.bound_explicit_item_bounds(def).transpose_iter() {
for bound in tcx.explicit_item_bounds(def).transpose_iter() {
let predicate = bound.map_bound(|&(pred, _)| pred).subst_identity();
// We only look at the `DefId`, so it is safe to skip the binder here.

View file

@ -269,7 +269,7 @@ fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<V::BreakTy> {
// and are visited by shallow visitors.
self.visit_predicates(ty::GenericPredicates {
parent: None,
predicates: tcx.bound_explicit_item_bounds(def_id).skip_binder(),
predicates: tcx.explicit_item_bounds(def_id).skip_binder(),
})?;
}
}
@ -1784,10 +1784,7 @@ fn predicates(&mut self) -> &mut Self {
fn bounds(&mut self) -> &mut Self {
self.visit_predicates(ty::GenericPredicates {
parent: None,
predicates: self
.tcx
.bound_explicit_item_bounds(self.item_def_id.to_def_id())
.skip_binder(),
predicates: self.tcx.explicit_item_bounds(self.item_def_id).skip_binder(),
});
self
}

View file

@ -297,7 +297,7 @@ fn predicates_reference_self(
tcx.associated_items(trait_def_id)
.in_definition_order()
.filter(|item| item.kind == ty::AssocKind::Type)
.flat_map(|item| tcx.bound_explicit_item_bounds(item.def_id).transpose_iter())
.flat_map(|item| tcx.explicit_item_bounds(item.def_id).transpose_iter())
.map(|bound| bound.map_bound(|b| *b).subst_identity())
.filter_map(|pred_span| predicate_references_self(tcx, pred_span))
.collect()

View file

@ -50,7 +50,7 @@ fn bounds_for<T>(&self, def_id: DefId, bound_vars: SubstsRef<'tcx>) -> Vec<T>
where
ty::Predicate<'tcx>: LowerInto<'tcx, std::option::Option<T>>,
{
let bounds = self.interner.tcx.bound_explicit_item_bounds(def_id);
let bounds = self.interner.tcx.explicit_item_bounds(def_id);
bounds
.0
.iter()
@ -506,7 +506,7 @@ fn opaque_ty_data(
let identity_substs = InternalSubsts::identity_for_item(self.interner.tcx, opaque_ty_id.0);
let explicit_item_bounds = self.interner.tcx.bound_explicit_item_bounds(opaque_ty_id.0);
let explicit_item_bounds = self.interner.tcx.explicit_item_bounds(opaque_ty_id.0);
let bounds =
explicit_item_bounds
.0

View file

@ -421,7 +421,7 @@ fn clean_projection<'tcx>(
if cx.tcx.is_impl_trait_in_trait(ty.skip_binder().def_id) {
let bounds = cx
.tcx
.bound_explicit_item_bounds(ty.skip_binder().def_id)
.explicit_item_bounds(ty.skip_binder().def_id)
.subst_iter_copied(cx.tcx, ty.skip_binder().substs)
.map(|(pred, _)| pred)
.collect::<Vec<_>>();
@ -1316,7 +1316,7 @@ fn param_eq_arg(param: &GenericParamDef, arg: &GenericArg) -> bool {
if let ty::TraitContainer = assoc_item.container {
let bounds = tcx
.bound_explicit_item_bounds(assoc_item.def_id)
.explicit_item_bounds(assoc_item.def_id)
.transpose_iter()
.map(|bound| bound.map_bound(|b| *b).subst_identity());
let predicates = tcx.explicit_predicates_of(assoc_item.def_id).predicates;
@ -1847,7 +1847,7 @@ pub(crate) fn clean_middle_ty<'tcx>(
// by looking up the bounds associated with the def_id.
let bounds = cx
.tcx
.bound_explicit_item_bounds(def_id)
.explicit_item_bounds(def_id)
.subst_iter_copied(cx.tcx, substs)
.map(|(bound, _)| bound)
.collect::<Vec<_>>();

View file

@ -64,7 +64,7 @@ fn check_fn(
}
let ret_ty = return_ty(cx, cx.tcx.hir().local_def_id_to_hir_id(fn_def_id).expect_owner());
if let ty::Alias(ty::Opaque, AliasTy { def_id, substs, .. }) = *ret_ty.kind() {
let preds = cx.tcx.bound_explicit_item_bounds(def_id);
let preds = cx.tcx.explicit_item_bounds(def_id);
let mut is_future = false;
for (p, _span) in preds.subst_iter_copied(cx.tcx, substs) {
if let Some(trait_pred) = p.to_opt_poly_trait_pred() {

View file

@ -90,7 +90,7 @@ fn contains_ty_adt_constructor_opaque_inner<'tcx>(
return false;
}
for bound in cx.tcx.bound_explicit_item_bounds(def_id).transpose_iter() {
for bound in cx.tcx.explicit_item_bounds(def_id).transpose_iter() {
let (predicate, _span) = bound.map_bound(|b| *b).subst_identity();
match predicate.kind().skip_binder() {
// For `impl Trait<U>`, it will register a predicate of `T: Trait<U>`, so we go through
@ -268,7 +268,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
},
ty::Tuple(substs) => substs.iter().any(|ty| is_must_use_ty(cx, ty)),
ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => {
for (predicate, _) in cx.tcx.bound_explicit_item_bounds(*def_id).skip_binder() {
for (predicate, _) in cx.tcx.explicit_item_bounds(def_id).skip_binder() {
if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() {
if cx.tcx.has_attr(trait_predicate.trait_ref.def_id, sym::must_use) {
return true;
@ -744,7 +744,7 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: AliasTy<'tcx>) -> Option
for (pred, _) in cx
.tcx
.bound_explicit_item_bounds(ty.def_id)
.explicit_item_bounds(ty.def_id)
.subst_iter_copied(cx.tcx, ty.substs)
{
match pred.kind().skip_binder() {