diff options
| author | Santiago Pastorino <spastorino@gmail.com> | 2023-11-25 17:48:09 -0300 |
|---|---|---|
| committer | Santiago Pastorino <spastorino@gmail.com> | 2023-11-25 17:48:09 -0300 |
| commit | 440f46dc165052d56561d7cb4d0808790e141a8e (patch) | |
| tree | 9151278f1a351e5e3fd301e1929e37f6088d2e37 /compiler/rustc_hir_analysis/src | |
| parent | 4b26bb544e89206880176da12416b15566d1a667 (diff) | |
| download | rust-440f46dc165052d56561d7cb4d0808790e141a8e.tar.gz rust-440f46dc165052d56561d7cb4d0808790e141a8e.zip | |
Get rid of infer vars in inherent assoc types selection by using probe
Diffstat (limited to 'compiler/rustc_hir_analysis/src')
| -rw-r--r-- | compiler/rustc_hir_analysis/src/astconv/mod.rs | 81 |
1 files changed, 34 insertions, 47 deletions
diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 455eb6f039e..8a0814d06c1 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -26,12 +26,12 @@ use rustc_hir::def::{CtorOf, DefKind, Namespace, Res}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::{walk_generics, Visitor as _}; use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin}; -use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt}; -use rustc_infer::traits::{Obligation, ObligationCause}; +use rustc_infer::infer::{InferCtxt, TyCtxtInferExt}; +use rustc_infer::traits::ObligationCause; use rustc_middle::middle::stability::AllowUnstable; use rustc_middle::ty::GenericParamDefKind; use rustc_middle::ty::{ - self, Const, GenericArgKind, GenericArgsRef, IsSuggestable, ParamEnv, Predicate, Ty, TyCtxt, + self, Const, GenericArgKind, GenericArgsRef, IsSuggestable, ParamEnv, Ty, TyCtxt, TypeVisitableExt, }; use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS; @@ -40,7 +40,7 @@ use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::{sym, BytePos, Span, DUMMY_SP}; use rustc_target::spec::abi; use rustc_trait_selection::traits::wf::object_region_bounds; -use rustc_trait_selection::traits::{self, NormalizeExt, ObligationCtxt}; +use rustc_trait_selection::traits::{self, ObligationCtxt}; use std::fmt::Display; use std::slice; @@ -1606,7 +1606,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { // FIXME(inherent_associated_types): Acquiring the ParamEnv this early leads to cycle errors // when inside of an ADT (#108491) or where clause. let param_env = tcx.param_env(block.owner); - let cause = ObligationCause::misc(span, block.owner.def_id); let mut universes = if self_ty.has_escaping_bound_vars() { vec![None; self_ty.outer_exclusive_binder().as_usize()] @@ -1614,44 +1613,33 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { vec![] }; - crate::traits::project::with_replaced_escaping_bound_vars( - infcx, - &mut universes, - self_ty, - |self_ty| { - let tcx = self.tcx(); - let InferOk { value: self_ty, obligations } = - infcx.at(&cause, param_env).normalize(self_ty); - - let (impl_, (assoc_item, def_scope)) = self.select_inherent_assoc_type_candidates( - infcx, - name, - span, - self_ty, - cause, - param_env, - obligations, - candidates, - )?; - - self.check_assoc_ty(assoc_item, name, def_scope, block, span); - - // FIXME(fmease): Currently creating throwaway `parent_args` to please - // `create_args_for_associated_item`. Modify the latter instead (or sth. similar) to - // not require the parent args logic. - let parent_args = ty::GenericArgs::identity_for_item(tcx, impl_); - let args = - self.create_args_for_associated_item(span, assoc_item, segment, parent_args); - let args = tcx.mk_args_from_iter( - std::iter::once(ty::GenericArg::from(self_ty)) - .chain(args.into_iter().skip(parent_args.len())), - ); + let (impl_, (assoc_item, def_scope)) = + crate::traits::project::with_replaced_escaping_bound_vars( + infcx, + &mut universes, + self_ty, + |self_ty| { + self.select_inherent_assoc_type_candidates( + infcx, name, span, self_ty, param_env, candidates, + ) + }, + )?; + + self.check_assoc_ty(assoc_item, name, def_scope, block, span); + + // FIXME(fmease): Currently creating throwaway `parent_args` to please + // `create_args_for_associated_item`. Modify the latter instead (or sth. similar) to + // not require the parent args logic. + let parent_args = ty::GenericArgs::identity_for_item(tcx, impl_); + let args = self.create_args_for_associated_item(span, assoc_item, segment, parent_args); + let args = tcx.mk_args_from_iter( + std::iter::once(ty::GenericArg::from(self_ty)) + .chain(args.into_iter().skip(parent_args.len())), + ); - let ty = Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new(tcx, assoc_item, args)); + let ty = Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new(tcx, assoc_item, args)); - Ok(Some((ty, assoc_item))) - }, - ) + Ok(Some((ty, assoc_item))) } fn select_inherent_assoc_type_candidates( @@ -1660,9 +1648,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { name: Ident, span: Span, self_ty: Ty<'tcx>, - cause: ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, - obligations: Vec<Obligation<'tcx, Predicate<'tcx>>>, candidates: Vec<(DefId, (DefId, DefId))>, ) -> Result<(DefId, (DefId, DefId)), ErrorGuaranteed> { let tcx = self.tcx(); @@ -1674,11 +1660,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { .filter(|&(impl_, _)| { infcx.probe(|_| { let ocx = ObligationCtxt::new(infcx); - ocx.register_obligations(obligations.clone()); + let self_ty = ocx.normalize(&ObligationCause::dummy(), param_env, self_ty); let impl_args = infcx.fresh_args_for_item(span, impl_); let impl_ty = tcx.type_of(impl_).instantiate(tcx, impl_args); - let impl_ty = ocx.normalize(&cause, param_env, impl_ty); + let impl_ty = ocx.normalize(&ObligationCause::dummy(), param_env, impl_ty); // Check that the self types can be related. if ocx.eq(&ObligationCause::dummy(), param_env, impl_ty, self_ty).is_err() { @@ -1687,9 +1673,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { // Check whether the impl imposes obligations we have to worry about. let impl_bounds = tcx.predicates_of(impl_).instantiate(tcx, impl_args); - let impl_bounds = ocx.normalize(&cause, param_env, impl_bounds); + let impl_bounds = + ocx.normalize(&ObligationCause::dummy(), param_env, impl_bounds); let impl_obligations = traits::predicates_for_generics( - |_, _| cause.clone(), + |_, _| ObligationCause::dummy(), param_env, impl_bounds, ); |
