1
Fork 0

change usages of explicit_item_bounds to bound_explicit_item_bounds

This commit is contained in:
Kyle Matsuda 2023-04-17 15:57:29 -06:00
parent a57fa08f48
commit 0892a7380b
13 changed files with 75 additions and 49 deletions

View file

@ -319,9 +319,12 @@ pub(super) fn check_opaque_for_inheriting_lifetimes(
selftys: vec![], selftys: vec![],
}; };
let prohibit_opaque = tcx let prohibit_opaque = tcx
.explicit_item_bounds(def_id) .bound_explicit_item_bounds(def_id.to_def_id())
.iter() .transpose_iter()
.try_for_each(|(predicate, _)| predicate.visit_with(&mut visitor)); .try_for_each(|bound| {
let predicate = bound.map_bound(|&(predicate, _)| predicate).subst_identity();
predicate.visit_with(&mut visitor)
});
if let Some(ty) = prohibit_opaque.break_value() { if let Some(ty) = prohibit_opaque.break_value() {
visitor.visit_item(&item); visitor.visit_item(&item);

View file

@ -360,7 +360,10 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
tcx, tcx,
param_env, param_env,
item_def_id, item_def_id,
tcx.explicit_item_bounds(item_def_id).to_vec(), tcx.bound_explicit_item_bounds(item_def_id.to_def_id())
.transpose_iter()
.map(|bound| bound.map_bound(|b| *b).subst_identity())
.collect::<Vec<_>>(),
&FxIndexSet::default(), &FxIndexSet::default(),
gat_def_id.def_id, gat_def_id.def_id,
gat_generics, gat_generics,
@ -1122,10 +1125,11 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
/// Assuming the defaults are used, check that all predicates (bounds on the /// Assuming the defaults are used, check that all predicates (bounds on the
/// assoc type and where clauses on the trait) hold. /// assoc type and where clauses on the trait) hold.
fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: ty::AssocItem, span: Span) { fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: ty::AssocItem, span: Span) {
let bounds = wfcx.tcx().explicit_item_bounds(item.def_id); let bounds = wfcx.tcx().bound_explicit_item_bounds(item.def_id);
debug!("check_associated_type_bounds: bounds={:?}", bounds); debug!("check_associated_type_bounds: bounds={:?}", bounds);
let wf_obligations = bounds.iter().flat_map(|&(bound, bound_span)| { let wf_obligations = bounds.transpose_iter().flat_map(|b| {
let (bound, bound_span) = b.map_bound(|b| *b).subst_identity();
let normalized_bound = wfcx.normalize(span, None, bound); let normalized_bound = wfcx.normalize(span, None, bound);
traits::wf::predicate_obligations( traits::wf::predicate_obligations(
wfcx.infcx, wfcx.infcx,

View file

@ -130,9 +130,10 @@ pub(super) fn item_bounds(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
def_id: DefId, def_id: DefId,
) -> ty::EarlyBinder<&'_ ty::List<ty::Predicate<'_>>> { ) -> ty::EarlyBinder<&'_ ty::List<ty::Predicate<'_>>> {
let bounds = tcx.mk_predicates_from_iter(util::elaborate( tcx.bound_explicit_item_bounds(def_id).map_bound(|bounds| {
tcx.mk_predicates_from_iter(util::elaborate(
tcx, tcx,
tcx.explicit_item_bounds(def_id).iter().map(|&(bound, _span)| bound), bounds.iter().map(|&(bound, _span)| bound),
)); ))
ty::EarlyBinder(bounds) })
} }

View file

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

View file

@ -74,7 +74,9 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
// For every projection predicate in the opaque type's explicit bounds, // For every projection predicate in the opaque type's explicit bounds,
// check that the type that we're assigning actually satisfies the bounds // check that the type that we're assigning actually satisfies the bounds
// of the associated type. // of the associated type.
for &(pred, pred_span) in cx.tcx.explicit_item_bounds(def_id) { for bound in cx.tcx.bound_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 // Liberate bound regions in the predicate since we
// don't actually care about lifetimes in this check. // don't actually care about lifetimes in this check.
let predicate = cx.tcx.liberate_late_bound_regions(def_id, pred.kind()); let predicate = cx.tcx.liberate_late_bound_regions(def_id, pred.kind());

View file

@ -254,7 +254,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
} }
ty::Adt(def, _) => is_def_must_use(cx, def.did(), span), ty::Adt(def, _) => is_def_must_use(cx, def.did(), span),
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => { ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
elaborate(cx.tcx, cx.tcx.explicit_item_bounds(def).iter().cloned()) elaborate(
cx.tcx,
cx.tcx
.bound_explicit_item_bounds(def)
.transpose_iter()
.map(|bound| bound.map_bound(|b| *b).subst_identity()),
)
// We only care about self bounds for the impl-trait // We only care about self bounds for the impl-trait
.filter_only_self() .filter_only_self()
.find_map(|(pred, _span)| { .find_map(|(pred, _span)| {

View file

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

View file

@ -1800,7 +1800,9 @@ fn check_must_not_suspend_ty<'tcx>(
// FIXME: support adding the attribute to TAITs // FIXME: support adding the attribute to TAITs
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => { ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
let mut has_emitted = false; let mut has_emitted = false;
for &(predicate, _) in tcx.explicit_item_bounds(def) { for bound in tcx.bound_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. // 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)) = if let ty::PredicateKind::Clause(ty::Clause::Trait(ref poly_trait_predicate)) =
predicate.kind().skip_binder() predicate.kind().skip_binder()

View file

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

View file

@ -297,8 +297,9 @@ fn bounds_reference_self(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span
tcx.associated_items(trait_def_id) tcx.associated_items(trait_def_id)
.in_definition_order() .in_definition_order()
.filter(|item| item.kind == ty::AssocKind::Type) .filter(|item| item.kind == ty::AssocKind::Type)
.flat_map(|item| tcx.explicit_item_bounds(item.def_id)) .flat_map(|item| tcx.bound_explicit_item_bounds(item.def_id).transpose_iter())
.filter_map(|pred_span| predicate_references_self(tcx, *pred_span)) .map(|bound| bound.map_bound(|b| *b).subst_identity())
.filter_map(|pred_span| predicate_references_self(tcx, pred_span))
.collect() .collect()
} }

View file

@ -421,9 +421,9 @@ fn clean_projection<'tcx>(
if cx.tcx.is_impl_trait_in_trait(ty.skip_binder().def_id) { if cx.tcx.is_impl_trait_in_trait(ty.skip_binder().def_id) {
let bounds = cx let bounds = cx
.tcx .tcx
.explicit_item_bounds(ty.skip_binder().def_id) .bound_explicit_item_bounds(ty.skip_binder().def_id)
.iter() .subst_iter_copied(cx.tcx, ty.skip_binder().substs)
.map(|(bound, _)| EarlyBinder(*bound).subst(cx.tcx, ty.skip_binder().substs)) .map(|(pred, _)| pred)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
return clean_middle_opaque_bounds(cx, bounds); return clean_middle_opaque_bounds(cx, bounds);
} }
@ -1315,10 +1315,13 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
} }
if let ty::TraitContainer = assoc_item.container { if let ty::TraitContainer = assoc_item.container {
let bounds = tcx.explicit_item_bounds(assoc_item.def_id); let bounds = tcx
.bound_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; let predicates = tcx.explicit_predicates_of(assoc_item.def_id).predicates;
let predicates = let predicates =
tcx.arena.alloc_from_iter(bounds.into_iter().chain(predicates).copied()); tcx.arena.alloc_from_iter(bounds.chain(predicates.iter().copied()));
let mut generics = clean_ty_generics( let mut generics = clean_ty_generics(
cx, cx,
tcx.generics_of(assoc_item.def_id), tcx.generics_of(assoc_item.def_id),
@ -1844,9 +1847,9 @@ pub(crate) fn clean_middle_ty<'tcx>(
// by looking up the bounds associated with the def_id. // by looking up the bounds associated with the def_id.
let bounds = cx let bounds = cx
.tcx .tcx
.explicit_item_bounds(def_id) .bound_explicit_item_bounds(def_id)
.iter() .subst_iter_copied(cx.tcx, substs)
.map(|(bound, _)| EarlyBinder(*bound).subst(cx.tcx, substs)) .map(|(bound, _)| bound)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
clean_middle_opaque_bounds(cx, bounds) clean_middle_opaque_bounds(cx, bounds)
} }

View file

@ -4,7 +4,7 @@ use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, FnDecl}; use rustc_hir::{Body, FnDecl};
use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, AliasTy, Clause, EarlyBinder, PredicateKind}; use rustc_middle::ty::{self, AliasTy, Clause, PredicateKind};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId; use rustc_span::def_id::LocalDefId;
use rustc_span::{sym, Span}; use rustc_span::{sym, Span};
@ -64,10 +64,9 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
} }
let ret_ty = return_ty(cx, cx.tcx.hir().local_def_id_to_hir_id(fn_def_id).expect_owner()); 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() { if let ty::Alias(ty::Opaque, AliasTy { def_id, substs, .. }) = *ret_ty.kind() {
let preds = cx.tcx.explicit_item_bounds(def_id); let preds = cx.tcx.bound_explicit_item_bounds(def_id);
let mut is_future = false; let mut is_future = false;
for &(p, _span) in preds { for (p, _span) in preds.subst_iter_copied(cx.tcx, substs) {
let p = EarlyBinder(p).subst(cx.tcx, substs);
if let Some(trait_pred) = p.to_opt_poly_trait_pred() { if let Some(trait_pred) = p.to_opt_poly_trait_pred() {
if Some(trait_pred.skip_binder().trait_ref.def_id) == cx.tcx.lang_items().future_trait() { if Some(trait_pred.skip_binder().trait_ref.def_id) == cx.tcx.lang_items().future_trait() {
is_future = true; is_future = true;

View file

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