Rollup merge of #82597 - noslaver:fix-82137, r=nagisa

Get TyCtxt from self instead of passing as argument in AutoTraitFinder

First contribution 🦀, let me know if anything is amiss.

Fix #82137.
This commit is contained in:
Yuki Okushi 2021-03-02 21:23:19 +09:00 committed by GitHub
commit 43bcfdb0d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 17 deletions

View File

@ -77,7 +77,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
ty: Ty<'tcx>, ty: Ty<'tcx>,
orig_env: ty::ParamEnv<'tcx>, orig_env: ty::ParamEnv<'tcx>,
trait_did: DefId, trait_did: DefId,
mut auto_trait_callback: impl FnMut(&InferCtxt<'_, 'tcx>, AutoTraitInfo<'tcx>) -> A, mut auto_trait_callback: impl FnMut(AutoTraitInfo<'tcx>) -> A,
) -> AutoTraitResult<A> { ) -> AutoTraitResult<A> {
let tcx = self.tcx; let tcx = self.tcx;
@ -211,7 +211,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
let info = AutoTraitInfo { full_user_env, region_data, vid_to_region }; let info = AutoTraitInfo { full_user_env, region_data, vid_to_region };
AutoTraitResult::PositiveImpl(auto_trait_callback(&infcx, info)) AutoTraitResult::PositiveImpl(auto_trait_callback(info))
}) })
} }
} }

View File

@ -46,7 +46,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
return None; return None;
} }
let result = f.find_auto_trait_generics(ty, param_env, trait_def_id, |infcx, info| { let result = f.find_auto_trait_generics(ty, param_env, trait_def_id, |info| {
let region_data = info.region_data; let region_data = info.region_data;
let names_map = tcx let names_map = tcx
@ -61,7 +61,6 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
.collect(); .collect();
let lifetime_predicates = Self::handle_lifetimes(&region_data, &names_map); let lifetime_predicates = Self::handle_lifetimes(&region_data, &names_map);
let new_generics = self.param_env_to_generics( let new_generics = self.param_env_to_generics(
infcx.tcx,
item_def_id, item_def_id,
info.full_user_env, info.full_user_env,
lifetime_predicates, lifetime_predicates,
@ -313,12 +312,9 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
lifetime_predicates lifetime_predicates
} }
fn extract_for_generics( fn extract_for_generics(&self, pred: ty::Predicate<'tcx>) -> FxHashSet<GenericParamDef> {
&self,
tcx: TyCtxt<'tcx>,
pred: ty::Predicate<'tcx>,
) -> FxHashSet<GenericParamDef> {
let bound_predicate = pred.kind(); let bound_predicate = pred.kind();
let tcx = self.cx.tcx;
let regions = match bound_predicate.skip_binder() { let regions = match bound_predicate.skip_binder() {
ty::PredicateKind::Trait(poly_trait_pred, _) => { ty::PredicateKind::Trait(poly_trait_pred, _) => {
tcx.collect_referenced_late_bound_regions(&bound_predicate.rebind(poly_trait_pred)) tcx.collect_referenced_late_bound_regions(&bound_predicate.rebind(poly_trait_pred))
@ -443,7 +439,6 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
// * We explicitly add a '?Sized' bound if we didn't find any 'Sized' predicates for a type // * We explicitly add a '?Sized' bound if we didn't find any 'Sized' predicates for a type
fn param_env_to_generics( fn param_env_to_generics(
&mut self, &mut self,
tcx: TyCtxt<'tcx>,
item_def_id: DefId, item_def_id: DefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
mut existing_predicates: Vec<WherePredicate>, mut existing_predicates: Vec<WherePredicate>,
@ -455,14 +450,15 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
item_def_id, param_env, existing_predicates item_def_id, param_env, existing_predicates
); );
let tcx = self.cx.tcx;
// The `Sized` trait must be handled specially, since we only display it when // The `Sized` trait must be handled specially, since we only display it when
// it is *not* required (i.e., '?Sized') // it is *not* required (i.e., '?Sized')
let sized_trait = self.cx.tcx.require_lang_item(LangItem::Sized, None); let sized_trait = tcx.require_lang_item(LangItem::Sized, None);
let mut replacer = RegionReplacer { vid_to_region: &vid_to_region, tcx }; let mut replacer = RegionReplacer { vid_to_region: &vid_to_region, tcx };
let orig_bounds: FxHashSet<_> = let orig_bounds: FxHashSet<_> = tcx.param_env(item_def_id).caller_bounds().iter().collect();
self.cx.tcx.param_env(item_def_id).caller_bounds().iter().collect();
let clean_where_predicates = param_env let clean_where_predicates = param_env
.caller_bounds() .caller_bounds()
.iter() .iter()
@ -512,7 +508,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
continue; continue;
} }
let mut for_generics = self.extract_for_generics(tcx, orig_p); let mut for_generics = self.extract_for_generics(orig_p);
assert!(bounds.len() == 1); assert!(bounds.len() == 1);
let mut b = bounds.pop().expect("bounds were empty"); let mut b = bounds.pop().expect("bounds were empty");
@ -541,7 +537,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
// that we don't end up with duplicate bounds (e.g., for<'b, 'b>) // that we don't end up with duplicate bounds (e.g., for<'b, 'b>)
for_generics.extend(p.generic_params.clone()); for_generics.extend(p.generic_params.clone());
p.generic_params = for_generics.into_iter().collect(); p.generic_params = for_generics.into_iter().collect();
self.is_fn_ty(tcx, &p.trait_) self.is_fn_ty(&p.trait_)
} }
_ => false, _ => false,
}; };
@ -576,7 +572,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
} => { } => {
let mut new_trait_path = trait_path.clone(); let mut new_trait_path = trait_path.clone();
if self.is_fn_ty(tcx, trait_) && left_name == sym::Output { if self.is_fn_ty(trait_) && left_name == sym::Output {
ty_to_fn ty_to_fn
.entry(*ty.clone()) .entry(*ty.clone())
.and_modify(|e| *e = (e.0.clone(), Some(rhs.clone()))) .and_modify(|e| *e = (e.0.clone(), Some(rhs.clone())))
@ -734,7 +730,8 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
vec.sort_by_cached_key(|x| format!("{:?}", x)) vec.sort_by_cached_key(|x| format!("{:?}", x))
} }
fn is_fn_ty(&self, tcx: TyCtxt<'_>, ty: &Type) -> bool { fn is_fn_ty(&self, ty: &Type) -> bool {
let tcx = self.cx.tcx;
match ty { match ty {
&Type::ResolvedPath { did, .. } => { &Type::ResolvedPath { did, .. } => {
did == tcx.require_lang_item(LangItem::Fn, None) did == tcx.require_lang_item(LangItem::Fn, None)