Don't pass InferCtxt to WfPredicates

This commit is contained in:
Jack Huey 2022-07-10 15:52:19 -04:00
parent 2d15f1ca42
commit a479f23f37

View File

@ -60,12 +60,19 @@ pub fn obligations<'a, 'tcx>(
GenericArgKind::Lifetime(..) => return Some(Vec::new()), GenericArgKind::Lifetime(..) => return Some(Vec::new()),
}; };
let mut wf = let mut wf = WfPredicates {
WfPredicates { infcx, param_env, body_id, span, out: vec![], recursion_depth, item: None }; tcx: infcx.tcx,
param_env,
body_id,
span,
out: vec![],
recursion_depth,
item: None,
};
wf.compute(arg); wf.compute(arg);
debug!("wf::obligations({:?}, body_id={:?}) = {:?}", arg, body_id, wf.out); debug!("wf::obligations({:?}, body_id={:?}) = {:?}", arg, body_id, wf.out);
let result = wf.normalize(); let result = wf.normalize(infcx);
debug!("wf::obligations({:?}, body_id={:?}) ~~> {:?}", arg, body_id, result); debug!("wf::obligations({:?}, body_id={:?}) ~~> {:?}", arg, body_id, result);
Some(result) Some(result)
} }
@ -83,7 +90,7 @@ pub fn trait_obligations<'a, 'tcx>(
item: &'tcx hir::Item<'tcx>, item: &'tcx hir::Item<'tcx>,
) -> Vec<traits::PredicateObligation<'tcx>> { ) -> Vec<traits::PredicateObligation<'tcx>> {
let mut wf = WfPredicates { let mut wf = WfPredicates {
infcx, tcx: infcx.tcx,
param_env, param_env,
body_id, body_id,
span, span,
@ -93,7 +100,7 @@ pub fn trait_obligations<'a, 'tcx>(
}; };
wf.compute_trait_ref(trait_ref, Elaborate::All); wf.compute_trait_ref(trait_ref, Elaborate::All);
debug!(obligations = ?wf.out); debug!(obligations = ?wf.out);
wf.normalize() wf.normalize(infcx)
} }
pub fn predicate_obligations<'a, 'tcx>( pub fn predicate_obligations<'a, 'tcx>(
@ -104,7 +111,7 @@ pub fn predicate_obligations<'a, 'tcx>(
span: Span, span: Span,
) -> Vec<traits::PredicateObligation<'tcx>> { ) -> Vec<traits::PredicateObligation<'tcx>> {
let mut wf = WfPredicates { let mut wf = WfPredicates {
infcx, tcx: infcx.tcx,
param_env, param_env,
body_id, body_id,
span, span,
@ -159,11 +166,11 @@ pub fn predicate_obligations<'a, 'tcx>(
} }
} }
wf.normalize() wf.normalize(infcx)
} }
struct WfPredicates<'a, 'tcx> { struct WfPredicates<'tcx> {
infcx: &'a InferCtxt<'a, 'tcx>, tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: hir::HirId,
span: Span, span: Span,
@ -260,18 +267,17 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>(
} }
} }
impl<'a, 'tcx> WfPredicates<'a, 'tcx> { impl<'tcx> WfPredicates<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> { fn tcx(&self) -> TyCtxt<'tcx> {
self.infcx.tcx self.tcx
} }
fn cause(&self, code: traits::ObligationCauseCode<'tcx>) -> traits::ObligationCause<'tcx> { fn cause(&self, code: traits::ObligationCauseCode<'tcx>) -> traits::ObligationCause<'tcx> {
traits::ObligationCause::new(self.span, self.body_id, code) traits::ObligationCause::new(self.span, self.body_id, code)
} }
fn normalize(mut self) -> Vec<traits::PredicateObligation<'tcx>> { fn normalize(self, infcx: &InferCtxt<'_, 'tcx>) -> Vec<traits::PredicateObligation<'tcx>> {
let cause = self.cause(traits::WellFormed(None)); let cause = self.cause(traits::WellFormed(None));
let infcx = &mut self.infcx;
let param_env = self.param_env; let param_env = self.param_env;
let mut obligations = Vec::with_capacity(self.out.len()); let mut obligations = Vec::with_capacity(self.out.len());
for mut obligation in self.out { for mut obligation in self.out {
@ -296,7 +302,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
/// Pushes the obligations required for `trait_ref` to be WF into `self.out`. /// Pushes the obligations required for `trait_ref` to be WF into `self.out`.
fn compute_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>, elaborate: Elaborate) { fn compute_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>, elaborate: Elaborate) {
let tcx = self.infcx.tcx; let tcx = self.tcx;
let obligations = self.nominal_obligations(trait_ref.def_id, trait_ref.substs); let obligations = self.nominal_obligations(trait_ref.def_id, trait_ref.substs);
debug!("compute_trait_ref obligations {:?}", obligations); debug!("compute_trait_ref obligations {:?}", obligations);
@ -410,14 +416,14 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
if !subty.has_escaping_bound_vars() { if !subty.has_escaping_bound_vars() {
let cause = self.cause(cause); let cause = self.cause(cause);
let trait_ref = ty::TraitRef { let trait_ref = ty::TraitRef {
def_id: self.infcx.tcx.require_lang_item(LangItem::Sized, None), def_id: self.tcx.require_lang_item(LangItem::Sized, None),
substs: self.infcx.tcx.mk_substs_trait(subty, &[]), substs: self.tcx.mk_substs_trait(subty, &[]),
}; };
self.out.push(traits::Obligation::with_depth( self.out.push(traits::Obligation::with_depth(
cause, cause,
self.recursion_depth, self.recursion_depth,
self.param_env, self.param_env,
ty::Binder::dummy(trait_ref).without_const().to_predicate(self.infcx.tcx), ty::Binder::dummy(trait_ref).without_const().to_predicate(self.tcx),
)); ));
} }
} }
@ -617,7 +623,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
// of whatever returned this exact `impl Trait`. // of whatever returned this exact `impl Trait`.
// for named opaque `impl Trait` types we still need to check them // for named opaque `impl Trait` types we still need to check them
if ty::is_impl_trait_defn(self.infcx.tcx, did).is_none() { if ty::is_impl_trait_defn(self.tcx, did).is_none() {
let obligations = self.nominal_obligations(did, substs); let obligations = self.nominal_obligations(did, substs);
self.out.extend(obligations); self.out.extend(obligations);
} }
@ -683,15 +689,15 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
def_id: DefId, def_id: DefId,
substs: SubstsRef<'tcx>, substs: SubstsRef<'tcx>,
) -> Vec<traits::PredicateObligation<'tcx>> { ) -> Vec<traits::PredicateObligation<'tcx>> {
let predicates = self.infcx.tcx.predicates_of(def_id); let predicates = self.tcx.predicates_of(def_id);
let mut origins = vec![def_id; predicates.predicates.len()]; let mut origins = vec![def_id; predicates.predicates.len()];
let mut head = predicates; let mut head = predicates;
while let Some(parent) = head.parent { while let Some(parent) = head.parent {
head = self.infcx.tcx.predicates_of(parent); head = self.tcx.predicates_of(parent);
origins.extend(iter::repeat(parent).take(head.predicates.len())); origins.extend(iter::repeat(parent).take(head.predicates.len()));
} }
let predicates = predicates.instantiate(self.infcx.tcx, substs); let predicates = predicates.instantiate(self.tcx, substs);
debug_assert_eq!(predicates.predicates.len(), origins.len()); debug_assert_eq!(predicates.predicates.len(), origins.len());
iter::zip(iter::zip(predicates.predicates, predicates.spans), origins.into_iter().rev()) iter::zip(iter::zip(predicates.predicates, predicates.spans), origins.into_iter().rev())
@ -746,7 +752,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
// Note: in fact we only permit builtin traits, not `Bar<'d>`, I // Note: in fact we only permit builtin traits, not `Bar<'d>`, I
// am looking forward to the future here. // am looking forward to the future here.
if !data.has_escaping_bound_vars() && !region.has_escaping_bound_vars() { if !data.has_escaping_bound_vars() && !region.has_escaping_bound_vars() {
let implicit_bounds = object_region_bounds(self.infcx.tcx, data); let implicit_bounds = object_region_bounds(self.tcx, data);
let explicit_bound = region; let explicit_bound = region;
@ -759,7 +765,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
cause, cause,
self.recursion_depth, self.recursion_depth,
self.param_env, self.param_env,
outlives.to_predicate(self.infcx.tcx), outlives.to_predicate(self.tcx),
)); ));
} }
} }