Skip to content

Commit

Permalink
Get rid of infer vars in inherent assoc types selection by using probe
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Nov 25, 2023
1 parent 4b26bb5 commit 440f46d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 48 deletions.
81 changes: 34 additions & 47 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1606,52 +1606,40 @@ 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()]
} else {
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(
Expand All @@ -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();
Expand All @@ -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() {
Expand All @@ -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,
);
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,18 @@ pub fn compute_inherent_assoc_ty_args<'a, 'b, 'tcx>(

// Infer the generic parameters of the impl by unifying the
// impl type with the self type of the projection.
let self_ty = alias_ty.self_ty();
let mut self_ty = alias_ty.self_ty();
if !selcx.infcx.next_trait_solver() {
self_ty = normalize_with_depth_to(
selcx,
param_env,
cause.clone(),
depth + 1,
self_ty,
obligations,
);
}

match selcx.infcx.at(&cause, param_env).eq(DefineOpaqueTypes::No, impl_ty, self_ty) {
Ok(mut ok) => obligations.append(&mut ok.obligations),
Err(_) => {
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/associated-inherent-types/assoc-inherent-late-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// check-pass

#![feature(inherent_associated_types)]
#![allow(incomplete_features)]

struct Foo<T>(T);

impl<'a> Foo<fn(&'a ())> {
type Assoc = &'a ();
}

fn bar(_: for<'a> fn(Foo<fn(Foo<fn(&'static ())>::Assoc)>::Assoc)) {}

fn main() {}

0 comments on commit 440f46d

Please sign in to comment.