1
Fork 0

Remove bound_{explicit,}_item_bounds

This commit is contained in:
Michael Goulet 2023-01-03 05:01:17 +00:00
parent e1533a26f7
commit 90df86f474
6 changed files with 40 additions and 64 deletions

View file

@ -673,17 +673,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let tcx = self.infcx.tcx; let tcx = self.infcx.tcx;
// Find out if the predicates show that the type is a Fn or FnMut // Find out if the predicates show that the type is a Fn or FnMut
let find_fn_kind_from_did = |predicates: ty::EarlyBinder< let find_fn_kind_from_did = |(pred, _): (ty::Predicate<'tcx>, _)| {
&[(ty::Predicate<'tcx>, Span)], if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = pred.kind().skip_binder()
>, && pred.self_ty() == ty
substs| { {
predicates.0.iter().find_map(|(pred, _)| {
let pred = if let Some(substs) = substs {
predicates.rebind(*pred).subst(tcx, substs).kind().skip_binder()
} else {
pred.kind().skip_binder()
};
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = pred && pred.self_ty() == ty {
if Some(pred.def_id()) == tcx.lang_items().fn_trait() { if Some(pred.def_id()) == tcx.lang_items().fn_trait() {
return Some(hir::Mutability::Not); return Some(hir::Mutability::Not);
} else if Some(pred.def_id()) == tcx.lang_items().fn_mut_trait() { } else if Some(pred.def_id()) == tcx.lang_items().fn_mut_trait() {
@ -691,22 +684,23 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
} }
} }
None None
})
}; };
// If the type is opaque/param/closure, and it is Fn or FnMut, let's suggest (mutably) // If the type is opaque/param/closure, and it is Fn or FnMut, let's suggest (mutably)
// borrowing the type, since `&mut F: FnMut` iff `F: FnMut` and similarly for `Fn`. // borrowing the type, since `&mut F: FnMut` iff `F: FnMut` and similarly for `Fn`.
// These types seem reasonably opaque enough that they could be substituted with their // These types seem reasonably opaque enough that they could be substituted with their
// borrowed variants in a function body when we see a move error. // borrowed variants in a function body when we see a move error.
let borrow_level = match ty.kind() { let borrow_level = match *ty.kind() {
ty::Param(_) => find_fn_kind_from_did( ty::Param(_) => tcx
tcx.bound_explicit_predicates_of(self.mir_def_id().to_def_id()) .explicit_predicates_of(self.mir_def_id().to_def_id())
.map_bound(|p| p.predicates), .predicates
None, .iter()
), .copied()
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => { .find_map(find_fn_kind_from_did),
find_fn_kind_from_did(tcx.bound_explicit_item_bounds(*def_id), Some(*substs)) ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => tcx
} .bound_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() { ty::Closure(_, substs) => match substs.as_closure().kind() {
ty::ClosureKind::Fn => Some(hir::Mutability::Not), ty::ClosureKind::Fn => Some(hir::Mutability::Not),
ty::ClosureKind::FnMut => Some(hir::Mutability::Mut), ty::ClosureKind::FnMut => Some(hir::Mutability::Mut),

View file

@ -1309,7 +1309,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
let infcx = wfcx.infcx; let infcx = wfcx.infcx;
let tcx = wfcx.tcx(); let tcx = wfcx.tcx();
let predicates = tcx.bound_predicates_of(def_id.to_def_id()); let predicates = tcx.predicates_of(def_id.to_def_id());
let generics = tcx.generics_of(def_id); let generics = tcx.generics_of(def_id);
let is_our_default = |def: &ty::GenericParamDef| match def.kind { let is_our_default = |def: &ty::GenericParamDef| match def.kind {
@ -1410,7 +1410,6 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
// Now we build the substituted predicates. // Now we build the substituted predicates.
let default_obligations = predicates let default_obligations = predicates
.0
.predicates .predicates
.iter() .iter()
.flat_map(|&(pred, sp)| { .flat_map(|&(pred, sp)| {
@ -1441,13 +1440,13 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
} }
let mut param_count = CountParams::default(); let mut param_count = CountParams::default();
let has_region = pred.visit_with(&mut param_count).is_break(); let has_region = pred.visit_with(&mut param_count).is_break();
let substituted_pred = predicates.rebind(pred).subst(tcx, substs); let substituted_pred = ty::EarlyBinder(pred).subst(tcx, substs);
// Don't check non-defaulted params, dependent defaults (including lifetimes) // Don't check non-defaulted params, dependent defaults (including lifetimes)
// or preds with multiple params. // or preds with multiple params.
if substituted_pred.has_non_region_param() || param_count.params.len() > 1 || has_region if substituted_pred.has_non_region_param() || param_count.params.len() > 1 || has_region
{ {
None None
} else if predicates.0.predicates.iter().any(|&(p, _)| p == substituted_pred) { } else if predicates.predicates.iter().any(|&(p, _)| p == substituted_pred) {
// Avoid duplication of predicates that contain no parameters, for example. // Avoid duplication of predicates that contain no parameters, for example.
None None
} else { } else {
@ -1473,7 +1472,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
traits::Obligation::new(tcx, cause, wfcx.param_env, pred) traits::Obligation::new(tcx, cause, wfcx.param_env, pred)
}); });
let predicates = predicates.0.instantiate_identity(tcx); let predicates = predicates.instantiate_identity(tcx);
let predicates = wfcx.normalize(span, None, predicates); let predicates = wfcx.normalize(span, None, predicates);

View file

@ -330,9 +330,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let Ok(trait_predicates) = self let Ok(trait_predicates) = self
.tcx .tcx
.bound_explicit_predicates_of(trait_item_def_id) .explicit_predicates_of(trait_item_def_id)
.map_bound(|p| p.predicates) .instantiate_own(self.tcx, trait_item_substs)
.subst_iter_copied(self.tcx, trait_item_substs)
.map(|(pred, _)| { .map(|(pred, _)| {
if pred.is_suggestable(self.tcx, false) { if pred.is_suggestable(self.tcx, false) {
Ok(pred.to_string()) Ok(pred.to_string())

View file

@ -666,20 +666,6 @@ impl<'tcx> TyCtxt<'tcx> {
ty::EarlyBinder(self.item_bounds(def_id)) ty::EarlyBinder(self.item_bounds(def_id))
} }
pub fn bound_predicates_of(
self,
def_id: DefId,
) -> ty::EarlyBinder<ty::generics::GenericPredicates<'tcx>> {
ty::EarlyBinder(self.predicates_of(def_id))
}
pub fn bound_explicit_predicates_of(
self,
def_id: DefId,
) -> ty::EarlyBinder<ty::generics::GenericPredicates<'tcx>> {
ty::EarlyBinder(self.explicit_predicates_of(def_id))
}
pub fn bound_impl_subject(self, def_id: DefId) -> ty::EarlyBinder<ty::ImplSubject<'tcx>> { pub fn bound_impl_subject(self, def_id: DefId) -> ty::EarlyBinder<ty::ImplSubject<'tcx>> {
ty::EarlyBinder(self.impl_subject(def_id)) ty::EarlyBinder(self.impl_subject(def_id))
} }

View file

@ -2558,12 +2558,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// obligation will normalize to `<$0 as Iterator>::Item = $1` and // obligation will normalize to `<$0 as Iterator>::Item = $1` and
// `$1: Copy`, so we must ensure the obligations are emitted in // `$1: Copy`, so we must ensure the obligations are emitted in
// that order. // that order.
let predicates = tcx.bound_predicates_of(def_id); let predicates = tcx.predicates_of(def_id);
debug!(?predicates); assert_eq!(predicates.parent, None);
assert_eq!(predicates.0.parent, None); let predicates = predicates.instantiate_own(tcx, substs);
let mut obligations = Vec::with_capacity(predicates.0.predicates.len()); let mut obligations = Vec::with_capacity(predicates.len());
for (predicate, span) in predicates.0.predicates { for (predicate, span) in predicates {
let span = *span;
let cause = cause.clone().derived_cause(parent_trait_pred, |derived| { let cause = cause.clone().derived_cause(parent_trait_pred, |derived| {
ImplDerivedObligation(Box::new(ImplDerivedObligationCause { ImplDerivedObligation(Box::new(ImplDerivedObligationCause {
derived, derived,
@ -2576,7 +2575,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
param_env, param_env,
cause.clone(), cause.clone(),
recursion_depth, recursion_depth,
predicates.rebind(*predicate).subst(tcx, substs), predicate,
&mut obligations, &mut obligations,
); );
obligations.push(Obligation { cause, recursion_depth, param_env, predicate }); obligations.push(Obligation { cause, recursion_depth, param_env, predicate });

View file

@ -7,7 +7,7 @@
//! `crate::chalk::lowering` (to lower rustc types into Chalk types). //! `crate::chalk::lowering` (to lower rustc types into Chalk types).
use rustc_middle::traits::ChalkRustInterner as RustInterner; use rustc_middle::traits::ChalkRustInterner as RustInterner;
use rustc_middle::ty::{self, AssocKind, EarlyBinder, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable}; use rustc_middle::ty::{self, AssocKind, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable};
use rustc_middle::ty::{InternalSubsts, SubstsRef}; use rustc_middle::ty::{InternalSubsts, SubstsRef};
use rustc_target::abi::{Integer, IntegerType}; use rustc_target::abi::{Integer, IntegerType};
@ -38,13 +38,12 @@ impl<'tcx> RustIrDatabase<'tcx> {
def_id: DefId, def_id: DefId,
bound_vars: SubstsRef<'tcx>, bound_vars: SubstsRef<'tcx>,
) -> Vec<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>> { ) -> Vec<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>> {
let predicates = self.interner.tcx.predicates_defined_on(def_id).predicates; self.interner
predicates .tcx
.iter() .predicates_defined_on(def_id)
.map(|(wc, _)| EarlyBinder(*wc).subst(self.interner.tcx, bound_vars)) .instantiate_own(self.interner.tcx, bound_vars)
.filter_map(|wc| LowerInto::< .filter_map(|(wc, _)| LowerInto::lower_into(wc, self.interner))
Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>> .collect()
>::lower_into(wc, self.interner)).collect()
} }
fn bounds_for<T>(&self, def_id: DefId, bound_vars: SubstsRef<'tcx>) -> Vec<T> fn bounds_for<T>(&self, def_id: DefId, bound_vars: SubstsRef<'tcx>) -> Vec<T>