tcx -> cx in rustc_type_ir

This commit is contained in:
Michael Goulet 2024-07-16 00:06:30 -04:00
parent a6510507e7
commit 8d90f442ca
14 changed files with 149 additions and 152 deletions

View File

@ -49,10 +49,10 @@ impl<I: Interner, U: Interner, T> Lift<U> for Binder<I, T>
{ {
type Lifted = Binder<U, T::Lifted>; type Lifted = Binder<U, T::Lifted>;
fn lift_to_interner(self, tcx: U) -> Option<Self::Lifted> { fn lift_to_interner(self, cx: U) -> Option<Self::Lifted> {
Some(Binder { Some(Binder {
value: self.value.lift_to_interner(tcx)?, value: self.value.lift_to_interner(cx)?,
bound_vars: self.bound_vars.lift_to_interner(tcx)?, bound_vars: self.bound_vars.lift_to_interner(cx)?,
}) })
} }
} }
@ -439,11 +439,11 @@ impl<I: Interner, Iter: IntoIterator> EarlyBinder<I, Iter>
where where
Iter::Item: TypeFoldable<I>, Iter::Item: TypeFoldable<I>,
{ {
pub fn iter_instantiated<A>(self, tcx: I, args: A) -> IterInstantiated<I, Iter, A> pub fn iter_instantiated<A>(self, cx: I, args: A) -> IterInstantiated<I, Iter, A>
where where
A: SliceLike<Item = I::GenericArg>, A: SliceLike<Item = I::GenericArg>,
{ {
IterInstantiated { it: self.value.into_iter(), tcx, args } IterInstantiated { it: self.value.into_iter(), cx, args }
} }
/// Similar to [`instantiate_identity`](EarlyBinder::instantiate_identity), /// Similar to [`instantiate_identity`](EarlyBinder::instantiate_identity),
@ -455,7 +455,7 @@ pub fn iter_identity(self) -> Iter::IntoIter {
pub struct IterInstantiated<I: Interner, Iter: IntoIterator, A> { pub struct IterInstantiated<I: Interner, Iter: IntoIterator, A> {
it: Iter::IntoIter, it: Iter::IntoIter,
tcx: I, cx: I,
args: A, args: A,
} }
@ -469,7 +469,7 @@ impl<I: Interner, Iter: IntoIterator, A> Iterator for IterInstantiated<I, Iter,
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
Some( Some(
EarlyBinder { value: self.it.next()?, _tcx: PhantomData } EarlyBinder { value: self.it.next()?, _tcx: PhantomData }
.instantiate(self.tcx, self.args), .instantiate(self.cx, self.args),
) )
} }
@ -487,7 +487,7 @@ impl<I: Interner, Iter: IntoIterator, A> DoubleEndedIterator for IterInstantiate
fn next_back(&mut self) -> Option<Self::Item> { fn next_back(&mut self) -> Option<Self::Item> {
Some( Some(
EarlyBinder { value: self.it.next_back()?, _tcx: PhantomData } EarlyBinder { value: self.it.next_back()?, _tcx: PhantomData }
.instantiate(self.tcx, self.args), .instantiate(self.cx, self.args),
) )
} }
} }
@ -507,10 +507,10 @@ impl<'s, I: Interner, Iter: IntoIterator> EarlyBinder<I, Iter>
{ {
pub fn iter_instantiated_copied( pub fn iter_instantiated_copied(
self, self,
tcx: I, cx: I,
args: &'s [I::GenericArg], args: &'s [I::GenericArg],
) -> IterInstantiatedCopied<'s, I, Iter> { ) -> IterInstantiatedCopied<'s, I, Iter> {
IterInstantiatedCopied { it: self.value.into_iter(), tcx, args } IterInstantiatedCopied { it: self.value.into_iter(), cx, args }
} }
/// Similar to [`instantiate_identity`](EarlyBinder::instantiate_identity), /// Similar to [`instantiate_identity`](EarlyBinder::instantiate_identity),
@ -522,7 +522,7 @@ pub fn iter_identity_copied(self) -> impl Iterator<Item = <Iter::Item as Deref>:
pub struct IterInstantiatedCopied<'a, I: Interner, Iter: IntoIterator> { pub struct IterInstantiatedCopied<'a, I: Interner, Iter: IntoIterator> {
it: Iter::IntoIter, it: Iter::IntoIter,
tcx: I, cx: I,
args: &'a [I::GenericArg], args: &'a [I::GenericArg],
} }
@ -535,7 +535,7 @@ impl<I: Interner, Iter: IntoIterator> Iterator for IterInstantiatedCopied<'_, I,
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.it.next().map(|value| { self.it.next().map(|value| {
EarlyBinder { value: *value, _tcx: PhantomData }.instantiate(self.tcx, self.args) EarlyBinder { value: *value, _tcx: PhantomData }.instantiate(self.cx, self.args)
}) })
} }
@ -552,7 +552,7 @@ impl<I: Interner, Iter: IntoIterator> DoubleEndedIterator for IterInstantiatedCo
{ {
fn next_back(&mut self) -> Option<Self::Item> { fn next_back(&mut self) -> Option<Self::Item> {
self.it.next_back().map(|value| { self.it.next_back().map(|value| {
EarlyBinder { value: *value, _tcx: PhantomData }.instantiate(self.tcx, self.args) EarlyBinder { value: *value, _tcx: PhantomData }.instantiate(self.cx, self.args)
}) })
} }
} }
@ -589,11 +589,11 @@ fn size_hint(&self) -> (usize, Option<usize>) {
} }
impl<I: Interner, T: TypeFoldable<I>> ty::EarlyBinder<I, T> { impl<I: Interner, T: TypeFoldable<I>> ty::EarlyBinder<I, T> {
pub fn instantiate<A>(self, tcx: I, args: A) -> T pub fn instantiate<A>(self, cx: I, args: A) -> T
where where
A: SliceLike<Item = I::GenericArg>, A: SliceLike<Item = I::GenericArg>,
{ {
let mut folder = ArgFolder { tcx, args: args.as_slice(), binders_passed: 0 }; let mut folder = ArgFolder { cx, args: args.as_slice(), binders_passed: 0 };
self.value.fold_with(&mut folder) self.value.fold_with(&mut folder)
} }
@ -619,7 +619,7 @@ pub fn no_bound_vars(self) -> Option<T> {
// The actual instantiation engine itself is a type folder. // The actual instantiation engine itself is a type folder.
struct ArgFolder<'a, I: Interner> { struct ArgFolder<'a, I: Interner> {
tcx: I, cx: I,
args: &'a [I::GenericArg], args: &'a [I::GenericArg],
/// Number of region binders we have passed through while doing the instantiation /// Number of region binders we have passed through while doing the instantiation
@ -629,7 +629,7 @@ struct ArgFolder<'a, I: Interner> {
impl<'a, I: Interner> TypeFolder<I> for ArgFolder<'a, I> { impl<'a, I: Interner> TypeFolder<I> for ArgFolder<'a, I> {
#[inline] #[inline]
fn cx(&self) -> I { fn cx(&self) -> I {
self.tcx self.cx
} }
fn fold_binder<T: TypeFoldable<I>>(&mut self, t: ty::Binder<I, T>) -> ty::Binder<I, T> { fn fold_binder<T: TypeFoldable<I>>(&mut self, t: ty::Binder<I, T>) -> ty::Binder<I, T> {
@ -858,6 +858,6 @@ fn shift_region_through_binders(&self, region: I::Region) -> I::Region {
if self.binders_passed == 0 || !region.has_escaping_bound_vars() { if self.binders_passed == 0 || !region.has_escaping_bound_vars() {
return region; return region;
} }
ty::fold::shift_region(self.tcx, region, self.binders_passed) ty::fold::shift_region(self.cx, region, self.binders_passed)
} }
} }

View File

@ -330,25 +330,25 @@ pub fn is_identity_modulo_regions(&self) -> bool {
// Given a list of canonical variables, construct a set of values which are // Given a list of canonical variables, construct a set of values which are
// the identity response. // the identity response.
pub fn make_identity(tcx: I, infos: I::CanonicalVars) -> CanonicalVarValues<I> { pub fn make_identity(cx: I, infos: I::CanonicalVars) -> CanonicalVarValues<I> {
CanonicalVarValues { CanonicalVarValues {
var_values: tcx.mk_args_from_iter(infos.iter().enumerate().map( var_values: cx.mk_args_from_iter(infos.iter().enumerate().map(
|(i, info)| -> I::GenericArg { |(i, info)| -> I::GenericArg {
match info.kind { match info.kind {
CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => { CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => {
Ty::new_anon_bound(tcx, ty::INNERMOST, ty::BoundVar::from_usize(i)) Ty::new_anon_bound(cx, ty::INNERMOST, ty::BoundVar::from_usize(i))
.into() .into()
} }
CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => { CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => {
Region::new_anon_bound(tcx, ty::INNERMOST, ty::BoundVar::from_usize(i)) Region::new_anon_bound(cx, ty::INNERMOST, ty::BoundVar::from_usize(i))
.into() .into()
} }
CanonicalVarKind::Effect => { CanonicalVarKind::Effect => {
Const::new_anon_bound(tcx, ty::INNERMOST, ty::BoundVar::from_usize(i)) Const::new_anon_bound(cx, ty::INNERMOST, ty::BoundVar::from_usize(i))
.into() .into()
} }
CanonicalVarKind::Const(_) | CanonicalVarKind::PlaceholderConst(_) => { CanonicalVarKind::Const(_) | CanonicalVarKind::PlaceholderConst(_) => {
Const::new_anon_bound(tcx, ty::INNERMOST, ty::BoundVar::from_usize(i)) Const::new_anon_bound(cx, ty::INNERMOST, ty::BoundVar::from_usize(i))
.into() .into()
} }
} }

View File

@ -10,38 +10,38 @@ pub enum EffectKind {
} }
impl EffectKind { impl EffectKind {
pub fn try_from_def_id<I: Interner>(tcx: I, def_id: I::DefId) -> Option<EffectKind> { pub fn try_from_def_id<I: Interner>(cx: I, def_id: I::DefId) -> Option<EffectKind> {
if tcx.is_lang_item(def_id, EffectsMaybe) { if cx.is_lang_item(def_id, EffectsMaybe) {
Some(EffectKind::Maybe) Some(EffectKind::Maybe)
} else if tcx.is_lang_item(def_id, EffectsRuntime) { } else if cx.is_lang_item(def_id, EffectsRuntime) {
Some(EffectKind::Runtime) Some(EffectKind::Runtime)
} else if tcx.is_lang_item(def_id, EffectsNoRuntime) { } else if cx.is_lang_item(def_id, EffectsNoRuntime) {
Some(EffectKind::NoRuntime) Some(EffectKind::NoRuntime)
} else { } else {
None None
} }
} }
pub fn to_def_id<I: Interner>(self, tcx: I) -> I::DefId { pub fn to_def_id<I: Interner>(self, cx: I) -> I::DefId {
let lang_item = match self { let lang_item = match self {
EffectKind::Maybe => EffectsMaybe, EffectKind::Maybe => EffectsMaybe,
EffectKind::NoRuntime => EffectsNoRuntime, EffectKind::NoRuntime => EffectsNoRuntime,
EffectKind::Runtime => EffectsRuntime, EffectKind::Runtime => EffectsRuntime,
}; };
tcx.require_lang_item(lang_item) cx.require_lang_item(lang_item)
} }
pub fn try_from_ty<I: Interner>(tcx: I, ty: I::Ty) -> Option<EffectKind> { pub fn try_from_ty<I: Interner>(cx: I, ty: I::Ty) -> Option<EffectKind> {
if let crate::Adt(def, _) = ty.kind() { if let crate::Adt(def, _) = ty.kind() {
Self::try_from_def_id(tcx, def.def_id()) Self::try_from_def_id(cx, def.def_id())
} else { } else {
None None
} }
} }
pub fn to_ty<I: Interner>(self, tcx: I) -> I::Ty { pub fn to_ty<I: Interner>(self, cx: I) -> I::Ty {
I::Ty::new_adt(tcx, tcx.adt_def(self.to_def_id(tcx)), Default::default()) I::Ty::new_adt(cx, cx.adt_def(self.to_def_id(cx)), Default::default())
} }
/// Returns an intersection between two effect kinds. If one effect kind /// Returns an intersection between two effect kinds. If one effect kind

View File

@ -258,17 +258,17 @@ pub fn supertrait_def_ids<I: Interner>(
} }
pub fn supertraits<I: Interner>( pub fn supertraits<I: Interner>(
tcx: I, cx: I,
trait_ref: ty::Binder<I, ty::TraitRef<I>>, trait_ref: ty::Binder<I, ty::TraitRef<I>>,
) -> FilterToTraits<I, Elaborator<I, I::Clause>> { ) -> FilterToTraits<I, Elaborator<I, I::Clause>> {
elaborate(tcx, [trait_ref.upcast(tcx)]).filter_only_self().filter_to_traits() elaborate(cx, [trait_ref.upcast(cx)]).filter_only_self().filter_to_traits()
} }
pub fn transitive_bounds<I: Interner>( pub fn transitive_bounds<I: Interner>(
tcx: I, cx: I,
trait_refs: impl Iterator<Item = ty::Binder<I, ty::TraitRef<I>>>, trait_refs: impl Iterator<Item = ty::Binder<I, ty::TraitRef<I>>>,
) -> FilterToTraits<I, Elaborator<I, I::Clause>> { ) -> FilterToTraits<I, Elaborator<I, I::Clause>> {
elaborate(tcx, trait_refs.map(|trait_ref| trait_ref.upcast(tcx))) elaborate(cx, trait_refs.map(|trait_ref| trait_ref.upcast(cx)))
.filter_only_self() .filter_only_self()
.filter_to_traits() .filter_to_traits()
} }

View File

@ -105,7 +105,7 @@ pub enum TreatParams {
/// ///
/// ¹ meaning that if the outermost layers are different, then the whole types are also different. /// ¹ meaning that if the outermost layers are different, then the whole types are also different.
pub fn simplify_type<I: Interner>( pub fn simplify_type<I: Interner>(
tcx: I, cx: I,
ty: I::Ty, ty: I::Ty,
treat_params: TreatParams, treat_params: TreatParams,
) -> Option<SimplifiedType<I::DefId>> { ) -> Option<SimplifiedType<I::DefId>> {
@ -119,10 +119,10 @@ pub fn simplify_type<I: Interner>(
ty::Str => Some(SimplifiedType::Str), ty::Str => Some(SimplifiedType::Str),
ty::Array(..) => Some(SimplifiedType::Array), ty::Array(..) => Some(SimplifiedType::Array),
ty::Slice(..) => Some(SimplifiedType::Slice), ty::Slice(..) => Some(SimplifiedType::Slice),
ty::Pat(ty, ..) => simplify_type(tcx, ty, treat_params), ty::Pat(ty, ..) => simplify_type(cx, ty, treat_params),
ty::RawPtr(_, mutbl) => Some(SimplifiedType::Ptr(mutbl)), ty::RawPtr(_, mutbl) => Some(SimplifiedType::Ptr(mutbl)),
ty::Dynamic(trait_info, ..) => match trait_info.principal_def_id() { ty::Dynamic(trait_info, ..) => match trait_info.principal_def_id() {
Some(principal_def_id) if !tcx.trait_is_auto(principal_def_id) => { Some(principal_def_id) if !cx.trait_is_auto(principal_def_id) => {
Some(SimplifiedType::Trait(principal_def_id)) Some(SimplifiedType::Trait(principal_def_id))
} }
_ => Some(SimplifiedType::MarkerTraitObject), _ => Some(SimplifiedType::MarkerTraitObject),

View File

@ -345,20 +345,20 @@ fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self,
// `rustc_middle/src/ty/generic_args.rs` for more details. // `rustc_middle/src/ty/generic_args.rs` for more details.
struct Shifter<I: Interner> { struct Shifter<I: Interner> {
tcx: I, cx: I,
current_index: ty::DebruijnIndex, current_index: ty::DebruijnIndex,
amount: u32, amount: u32,
} }
impl<I: Interner> Shifter<I> { impl<I: Interner> Shifter<I> {
pub fn new(tcx: I, amount: u32) -> Self { pub fn new(cx: I, amount: u32) -> Self {
Shifter { tcx, current_index: ty::INNERMOST, amount } Shifter { cx, current_index: ty::INNERMOST, amount }
} }
} }
impl<I: Interner> TypeFolder<I> for Shifter<I> { impl<I: Interner> TypeFolder<I> for Shifter<I> {
fn cx(&self) -> I { fn cx(&self) -> I {
self.tcx self.cx
} }
fn fold_binder<T: TypeFoldable<I>>(&mut self, t: ty::Binder<I, T>) -> ty::Binder<I, T> { fn fold_binder<T: TypeFoldable<I>>(&mut self, t: ty::Binder<I, T>) -> ty::Binder<I, T> {
@ -372,7 +372,7 @@ fn fold_region(&mut self, r: I::Region) -> I::Region {
match r.kind() { match r.kind() {
ty::ReBound(debruijn, br) if debruijn >= self.current_index => { ty::ReBound(debruijn, br) if debruijn >= self.current_index => {
let debruijn = debruijn.shifted_in(self.amount); let debruijn = debruijn.shifted_in(self.amount);
Region::new_bound(self.tcx, debruijn, br) Region::new_bound(self.cx, debruijn, br)
} }
_ => r, _ => r,
} }
@ -382,7 +382,7 @@ fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
match ty.kind() { match ty.kind() {
ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => { ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
let debruijn = debruijn.shifted_in(self.amount); let debruijn = debruijn.shifted_in(self.amount);
Ty::new_bound(self.tcx, debruijn, bound_ty) Ty::new_bound(self.cx, debruijn, bound_ty)
} }
_ if ty.has_vars_bound_at_or_above(self.current_index) => ty.super_fold_with(self), _ if ty.has_vars_bound_at_or_above(self.current_index) => ty.super_fold_with(self),
@ -394,7 +394,7 @@ fn fold_const(&mut self, ct: I::Const) -> I::Const {
match ct.kind() { match ct.kind() {
ty::ConstKind::Bound(debruijn, bound_ct) if debruijn >= self.current_index => { ty::ConstKind::Bound(debruijn, bound_ct) if debruijn >= self.current_index => {
let debruijn = debruijn.shifted_in(self.amount); let debruijn = debruijn.shifted_in(self.amount);
Const::new_bound(self.tcx, debruijn, bound_ct) Const::new_bound(self.cx, debruijn, bound_ct)
} }
_ => ct.super_fold_with(self), _ => ct.super_fold_with(self),
} }
@ -405,16 +405,16 @@ fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
} }
} }
pub fn shift_region<I: Interner>(tcx: I, region: I::Region, amount: u32) -> I::Region { pub fn shift_region<I: Interner>(cx: I, region: I::Region, amount: u32) -> I::Region {
match region.kind() { match region.kind() {
ty::ReBound(debruijn, br) if amount > 0 => { ty::ReBound(debruijn, br) if amount > 0 => {
Region::new_bound(tcx, debruijn.shifted_in(amount), br) Region::new_bound(cx, debruijn.shifted_in(amount), br)
} }
_ => region, _ => region,
} }
} }
pub fn shift_vars<I: Interner, T>(tcx: I, value: T, amount: u32) -> T pub fn shift_vars<I: Interner, T>(cx: I, value: T, amount: u32) -> T
where where
T: TypeFoldable<I>, T: TypeFoldable<I>,
{ {
@ -424,5 +424,5 @@ pub fn shift_vars<I: Interner, T>(tcx: I, value: T, amount: u32) -> T
return value; return value;
} }
value.fold_with(&mut Shifter::new(tcx, amount)) value.fold_with(&mut Shifter::new(cx, amount))
} }

View File

@ -475,7 +475,7 @@ fn as_projection_clause(self) -> Option<ty::Binder<I, ty::ProjectionPredicate<I>
/// poly-trait-ref to supertraits that must hold if that /// poly-trait-ref to supertraits that must hold if that
/// poly-trait-ref holds. This is slightly different from a normal /// poly-trait-ref holds. This is slightly different from a normal
/// instantiation in terms of what happens with bound regions. /// instantiation in terms of what happens with bound regions.
fn instantiate_supertrait(self, tcx: I, trait_ref: ty::Binder<I, ty::TraitRef<I>>) -> Self; fn instantiate_supertrait(self, cx: I, trait_ref: ty::Binder<I, ty::TraitRef<I>>) -> Self;
} }
/// Common capabilities of placeholder kinds /// Common capabilities of placeholder kinds

View File

@ -17,5 +17,5 @@
/// e.g., `()` or `u8`, was interned in a different context. /// e.g., `()` or `u8`, was interned in a different context.
pub trait Lift<I>: std::fmt::Debug { pub trait Lift<I>: std::fmt::Debug {
type Lifted: std::fmt::Debug; type Lifted: std::fmt::Debug;
fn lift_to_interner(self, tcx: I) -> Option<Self::Lifted>; fn lift_to_interner(self, cx: I) -> Option<Self::Lifted>;
} }

View File

@ -22,8 +22,8 @@ pub struct OpaqueTypeKey<I: Interner> {
} }
impl<I: Interner> OpaqueTypeKey<I> { impl<I: Interner> OpaqueTypeKey<I> {
pub fn iter_captured_args(self, tcx: I) -> impl Iterator<Item = (usize, I::GenericArg)> { pub fn iter_captured_args(self, cx: I) -> impl Iterator<Item = (usize, I::GenericArg)> {
let variances = tcx.variances_of(self.def_id.into()); let variances = cx.variances_of(self.def_id.into());
std::iter::zip(self.args.iter(), variances.iter()).enumerate().filter_map( std::iter::zip(self.args.iter(), variances.iter()).enumerate().filter_map(
|(i, (arg, v))| match (arg.kind(), v) { |(i, (arg, v))| match (arg.kind(), v) {
(_, ty::Invariant) => Some((i, arg)), (_, ty::Invariant) => Some((i, arg)),
@ -35,18 +35,18 @@ pub fn iter_captured_args(self, tcx: I) -> impl Iterator<Item = (usize, I::Gener
pub fn fold_captured_lifetime_args( pub fn fold_captured_lifetime_args(
self, self,
tcx: I, cx: I,
mut f: impl FnMut(I::Region) -> I::Region, mut f: impl FnMut(I::Region) -> I::Region,
) -> Self { ) -> Self {
let Self { def_id, args } = self; let Self { def_id, args } = self;
let variances = tcx.variances_of(def_id.into()); let variances = cx.variances_of(def_id.into());
let args = let args =
std::iter::zip(args.iter(), variances.iter()).map(|(arg, v)| match (arg.kind(), v) { std::iter::zip(args.iter(), variances.iter()).map(|(arg, v)| match (arg.kind(), v) {
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg, (ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg,
(ty::GenericArgKind::Lifetime(lt), _) => f(lt).into(), (ty::GenericArgKind::Lifetime(lt), _) => f(lt).into(),
_ => arg, _ => arg,
}); });
let args = tcx.mk_args_from_iter(args); let args = cx.mk_args_from_iter(args);
Self { def_id, args } Self { def_id, args }
} }
} }

View File

@ -54,15 +54,15 @@ pub enum Component<I: Interner> {
/// Push onto `out` all the things that must outlive `'a` for the condition /// Push onto `out` all the things that must outlive `'a` for the condition
/// `ty0: 'a` to hold. Note that `ty0` must be a **fully resolved type**. /// `ty0: 'a` to hold. Note that `ty0` must be a **fully resolved type**.
pub fn push_outlives_components<I: Interner>( pub fn push_outlives_components<I: Interner>(
tcx: I, cx: I,
ty: I::Ty, ty: I::Ty,
out: &mut SmallVec<[Component<I>; 4]>, out: &mut SmallVec<[Component<I>; 4]>,
) { ) {
ty.visit_with(&mut OutlivesCollector { tcx, out, visited: Default::default() }); ty.visit_with(&mut OutlivesCollector { cx, out, visited: Default::default() });
} }
struct OutlivesCollector<'a, I: Interner> { struct OutlivesCollector<'a, I: Interner> {
tcx: I, cx: I,
out: &'a mut SmallVec<[Component<I>; 4]>, out: &'a mut SmallVec<[Component<I>; 4]>,
visited: SsoHashSet<I::Ty>, visited: SsoHashSet<I::Ty>,
} }
@ -147,7 +147,7 @@ fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
// OutlivesProjectionComponents. Continue walking // OutlivesProjectionComponents. Continue walking
// through and constrain Pi. // through and constrain Pi.
let mut subcomponents = smallvec![]; let mut subcomponents = smallvec![];
compute_alias_components_recursive(self.tcx, ty, &mut subcomponents); compute_alias_components_recursive(self.cx, ty, &mut subcomponents);
self.out.push(Component::EscapingAlias(subcomponents.into_iter().collect())); self.out.push(Component::EscapingAlias(subcomponents.into_iter().collect()));
} }
} }
@ -206,7 +206,7 @@ fn visit_region(&mut self, lt: I::Region) -> Self::Result {
/// This should not be used to get the components of `parent` itself. /// This should not be used to get the components of `parent` itself.
/// Use [push_outlives_components] instead. /// Use [push_outlives_components] instead.
pub fn compute_alias_components_recursive<I: Interner>( pub fn compute_alias_components_recursive<I: Interner>(
tcx: I, cx: I,
alias_ty: I::Ty, alias_ty: I::Ty,
out: &mut SmallVec<[Component<I>; 4]>, out: &mut SmallVec<[Component<I>; 4]>,
) { ) {
@ -215,9 +215,9 @@ pub fn compute_alias_components_recursive<I: Interner>(
}; };
let opt_variances = let opt_variances =
if kind == ty::Opaque { Some(tcx.variances_of(alias_ty.def_id)) } else { None }; if kind == ty::Opaque { Some(cx.variances_of(alias_ty.def_id)) } else { None };
let mut visitor = OutlivesCollector { tcx, out, visited: Default::default() }; let mut visitor = OutlivesCollector { cx, out, visited: Default::default() };
for (index, child) in alias_ty.args.iter().enumerate() { for (index, child) in alias_ty.args.iter().enumerate() {
if opt_variances.and_then(|variances| variances.get(index)) == Some(ty::Bivariant) { if opt_variances.and_then(|variances| variances.get(index)) == Some(ty::Bivariant) {

View File

@ -34,8 +34,8 @@ impl<I: Interner, U: Interner, A> Lift<U> for OutlivesPredicate<I, A>
{ {
type Lifted = OutlivesPredicate<U, A::Lifted>; type Lifted = OutlivesPredicate<U, A::Lifted>;
fn lift_to_interner(self, tcx: U) -> Option<Self::Lifted> { fn lift_to_interner(self, cx: U) -> Option<Self::Lifted> {
Some(OutlivesPredicate(self.0.lift_to_interner(tcx)?, self.1.lift_to_interner(tcx)?)) Some(OutlivesPredicate(self.0.lift_to_interner(cx)?, self.1.lift_to_interner(cx)?))
} }
} }
@ -267,25 +267,23 @@ impl<I: Interner> ty::Binder<I, ExistentialPredicate<I>> {
/// Given an existential predicate like `?Self: PartialEq<u32>` (e.g., derived from `dyn PartialEq<u32>`), /// Given an existential predicate like `?Self: PartialEq<u32>` (e.g., derived from `dyn PartialEq<u32>`),
/// and a concrete type `self_ty`, returns a full predicate where the existentially quantified variable `?Self` /// and a concrete type `self_ty`, returns a full predicate where the existentially quantified variable `?Self`
/// has been replaced with `self_ty` (e.g., `self_ty: PartialEq<u32>`, in our example). /// has been replaced with `self_ty` (e.g., `self_ty: PartialEq<u32>`, in our example).
pub fn with_self_ty(&self, tcx: I, self_ty: I::Ty) -> I::Clause { pub fn with_self_ty(&self, cx: I, self_ty: I::Ty) -> I::Clause {
match self.skip_binder() { match self.skip_binder() {
ExistentialPredicate::Trait(tr) => { ExistentialPredicate::Trait(tr) => self.rebind(tr).with_self_ty(cx, self_ty).upcast(cx),
self.rebind(tr).with_self_ty(tcx, self_ty).upcast(tcx)
}
ExistentialPredicate::Projection(p) => { ExistentialPredicate::Projection(p) => {
self.rebind(p.with_self_ty(tcx, self_ty)).upcast(tcx) self.rebind(p.with_self_ty(cx, self_ty)).upcast(cx)
} }
ExistentialPredicate::AutoTrait(did) => { ExistentialPredicate::AutoTrait(did) => {
let generics = tcx.generics_of(did); let generics = cx.generics_of(did);
let trait_ref = if generics.count() == 1 { let trait_ref = if generics.count() == 1 {
ty::TraitRef::new(tcx, did, [self_ty]) ty::TraitRef::new(cx, did, [self_ty])
} else { } else {
// If this is an ill-formed auto trait, then synthesize // If this is an ill-formed auto trait, then synthesize
// new error args for the missing generics. // new error args for the missing generics.
let err_args = GenericArgs::extend_with_error(tcx, did, &[self_ty.into()]); let err_args = GenericArgs::extend_with_error(cx, did, &[self_ty.into()]);
ty::TraitRef::new_from_args(tcx, did, err_args) ty::TraitRef::new_from_args(cx, did, err_args)
}; };
self.rebind(trait_ref).upcast(tcx) self.rebind(trait_ref).upcast(cx)
} }
} }
} }
@ -345,8 +343,8 @@ pub fn def_id(&self) -> I::DefId {
/// we convert the principal trait-ref into a normal trait-ref, /// we convert the principal trait-ref into a normal trait-ref,
/// you must give *some* self type. A common choice is `mk_err()` /// you must give *some* self type. A common choice is `mk_err()`
/// or some placeholder type. /// or some placeholder type.
pub fn with_self_ty(&self, tcx: I, self_ty: I::Ty) -> ty::Binder<I, TraitRef<I>> { pub fn with_self_ty(&self, cx: I, self_ty: I::Ty) -> ty::Binder<I, TraitRef<I>> {
self.map_bound(|trait_ref| trait_ref.with_self_ty(tcx, self_ty)) self.map_bound(|trait_ref| trait_ref.with_self_ty(cx, self_ty))
} }
} }
@ -406,8 +404,8 @@ pub fn erase_self_ty(interner: I, projection_predicate: ProjectionPredicate<I>)
} }
impl<I: Interner> ty::Binder<I, ExistentialProjection<I>> { impl<I: Interner> ty::Binder<I, ExistentialProjection<I>> {
pub fn with_self_ty(&self, tcx: I, self_ty: I::Ty) -> ty::Binder<I, ProjectionPredicate<I>> { pub fn with_self_ty(&self, cx: I, self_ty: I::Ty) -> ty::Binder<I, ProjectionPredicate<I>> {
self.map_bound(|p| p.with_self_ty(tcx, self_ty)) self.map_bound(|p| p.with_self_ty(cx, self_ty))
} }
pub fn item_def_id(&self) -> I::DefId { pub fn item_def_id(&self) -> I::DefId {
@ -669,21 +667,21 @@ pub fn def_id(self) -> I::DefId {
impl<I: Interner> ty::Binder<I, ProjectionPredicate<I>> { impl<I: Interner> ty::Binder<I, ProjectionPredicate<I>> {
/// Returns the `DefId` of the trait of the associated item being projected. /// Returns the `DefId` of the trait of the associated item being projected.
#[inline] #[inline]
pub fn trait_def_id(&self, tcx: I) -> I::DefId { pub fn trait_def_id(&self, cx: I) -> I::DefId {
self.skip_binder().projection_term.trait_def_id(tcx) self.skip_binder().projection_term.trait_def_id(cx)
} }
/// Get the trait ref required for this projection to be well formed. /// Get the trait ref required for this projection to be well formed.
/// Note that for generic associated types the predicates of the associated /// Note that for generic associated types the predicates of the associated
/// type also need to be checked. /// type also need to be checked.
#[inline] #[inline]
pub fn required_poly_trait_ref(&self, tcx: I) -> ty::Binder<I, TraitRef<I>> { pub fn required_poly_trait_ref(&self, cx: I) -> ty::Binder<I, TraitRef<I>> {
// Note: unlike with `TraitRef::to_poly_trait_ref()`, // Note: unlike with `TraitRef::to_poly_trait_ref()`,
// `self.0.trait_ref` is permitted to have escaping regions. // `self.0.trait_ref` is permitted to have escaping regions.
// This is because here `self` has a `Binder` and so does our // This is because here `self` has a `Binder` and so does our
// return value, so we are preserving the number of binding // return value, so we are preserving the number of binding
// levels. // levels.
self.map_bound(|predicate| predicate.projection_term.trait_ref(tcx)) self.map_bound(|predicate| predicate.projection_term.trait_ref(cx))
} }
pub fn term(&self) -> ty::Binder<I, I::Term> { pub fn term(&self) -> ty::Binder<I, I::Term> {

View File

@ -56,7 +56,7 @@ pub fn xform(self, other: VarianceDiagInfo<I>) -> VarianceDiagInfo<I> {
} }
pub trait TypeRelation<I: Interner>: Sized { pub trait TypeRelation<I: Interner>: Sized {
fn tcx(&self) -> I; fn cx(&self) -> I;
/// Returns a static string we can use for printouts. /// Returns a static string we can use for printouts.
fn tag(&self) -> &'static str; fn tag(&self) -> &'static str;
@ -80,8 +80,8 @@ fn relate_item_args(
item_def_id, a_arg, b_arg item_def_id, a_arg, b_arg
); );
let tcx = self.tcx(); let cx = self.cx();
let opt_variances = tcx.variances_of(item_def_id); let opt_variances = cx.variances_of(item_def_id);
relate_args_with_variances(self, item_def_id, opt_variances, a_arg, b_arg, true) relate_args_with_variances(self, item_def_id, opt_variances, a_arg, b_arg, true)
} }
@ -128,7 +128,7 @@ pub fn relate_args_invariantly<I: Interner, R: TypeRelation<I>>(
a_arg: I::GenericArgs, a_arg: I::GenericArgs,
b_arg: I::GenericArgs, b_arg: I::GenericArgs,
) -> RelateResult<I, I::GenericArgs> { ) -> RelateResult<I, I::GenericArgs> {
relation.tcx().mk_args_from_iter(iter::zip(a_arg.iter(), b_arg.iter()).map(|(a, b)| { relation.cx().mk_args_from_iter(iter::zip(a_arg.iter(), b_arg.iter()).map(|(a, b)| {
relation.relate_with_variance(ty::Invariant, VarianceDiagInfo::default(), a, b) relation.relate_with_variance(ty::Invariant, VarianceDiagInfo::default(), a, b)
})) }))
} }
@ -141,14 +141,13 @@ pub fn relate_args_with_variances<I: Interner, R: TypeRelation<I>>(
b_arg: I::GenericArgs, b_arg: I::GenericArgs,
fetch_ty_for_diag: bool, fetch_ty_for_diag: bool,
) -> RelateResult<I, I::GenericArgs> { ) -> RelateResult<I, I::GenericArgs> {
let tcx = relation.tcx(); let cx = relation.cx();
let mut cached_ty = None; let mut cached_ty = None;
let params = iter::zip(a_arg.iter(), b_arg.iter()).enumerate().map(|(i, (a, b))| { let params = iter::zip(a_arg.iter(), b_arg.iter()).enumerate().map(|(i, (a, b))| {
let variance = variances.get(i).unwrap(); let variance = variances.get(i).unwrap();
let variance_info = if variance == ty::Invariant && fetch_ty_for_diag { let variance_info = if variance == ty::Invariant && fetch_ty_for_diag {
let ty = let ty = *cached_ty.get_or_insert_with(|| cx.type_of(ty_def_id).instantiate(cx, a_arg));
*cached_ty.get_or_insert_with(|| tcx.type_of(ty_def_id).instantiate(tcx, a_arg));
VarianceDiagInfo::Invariant { ty, param_index: i.try_into().unwrap() } VarianceDiagInfo::Invariant { ty, param_index: i.try_into().unwrap() }
} else { } else {
VarianceDiagInfo::default() VarianceDiagInfo::default()
@ -156,7 +155,7 @@ pub fn relate_args_with_variances<I: Interner, R: TypeRelation<I>>(
relation.relate_with_variance(variance, variance_info, a, b) relation.relate_with_variance(variance, variance_info, a, b)
}); });
tcx.mk_args_from_iter(params) cx.mk_args_from_iter(params)
} }
impl<I: Interner> Relate<I> for ty::FnSig<I> { impl<I: Interner> Relate<I> for ty::FnSig<I> {
@ -165,7 +164,7 @@ fn relate<R: TypeRelation<I>>(
a: ty::FnSig<I>, a: ty::FnSig<I>,
b: ty::FnSig<I>, b: ty::FnSig<I>,
) -> RelateResult<I, ty::FnSig<I>> { ) -> RelateResult<I, ty::FnSig<I>> {
let tcx = relation.tcx(); let cx = relation.cx();
if a.c_variadic != b.c_variadic { if a.c_variadic != b.c_variadic {
return Err(TypeError::VariadicMismatch({ return Err(TypeError::VariadicMismatch({
@ -210,7 +209,7 @@ fn relate<R: TypeRelation<I>>(
r => r, r => r,
}); });
Ok(ty::FnSig { Ok(ty::FnSig {
inputs_and_output: tcx.mk_type_list_from_iter(inputs_and_output)?, inputs_and_output: cx.mk_type_list_from_iter(inputs_and_output)?,
c_variadic: a.c_variadic, c_variadic: a.c_variadic,
safety, safety,
abi, abi,
@ -245,11 +244,11 @@ fn relate<R: TypeRelation<I>>(
ExpectedFound::new(true, a, b) ExpectedFound::new(true, a, b)
})) }))
} else { } else {
let args = match a.kind(relation.tcx()) { let args = match a.kind(relation.cx()) {
ty::Opaque => relate_args_with_variances( ty::Opaque => relate_args_with_variances(
relation, relation,
a.def_id, a.def_id,
relation.tcx().variances_of(a.def_id), relation.cx().variances_of(a.def_id),
a.args, a.args,
b.args, b.args,
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
@ -258,7 +257,7 @@ fn relate<R: TypeRelation<I>>(
relate_args_invariantly(relation, a.args, b.args)? relate_args_invariantly(relation, a.args, b.args)?
} }
}; };
Ok(ty::AliasTy::new_from_args(relation.tcx(), a.def_id, args)) Ok(ty::AliasTy::new_from_args(relation.cx(), a.def_id, args))
} }
} }
} }
@ -276,11 +275,11 @@ fn relate<R: TypeRelation<I>>(
ExpectedFound::new(true, a, b) ExpectedFound::new(true, a, b)
})) }))
} else { } else {
let args = match a.kind(relation.tcx()) { let args = match a.kind(relation.cx()) {
ty::AliasTermKind::OpaqueTy => relate_args_with_variances( ty::AliasTermKind::OpaqueTy => relate_args_with_variances(
relation, relation,
a.def_id, a.def_id,
relation.tcx().variances_of(a.def_id), relation.cx().variances_of(a.def_id),
a.args, a.args,
b.args, b.args,
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
@ -293,7 +292,7 @@ fn relate<R: TypeRelation<I>>(
relate_args_invariantly(relation, a.args, b.args)? relate_args_invariantly(relation, a.args, b.args)?
} }
}; };
Ok(ty::AliasTerm::new_from_args(relation.tcx(), a.def_id, args)) Ok(ty::AliasTerm::new_from_args(relation.cx(), a.def_id, args))
} }
} }
} }
@ -343,7 +342,7 @@ fn relate<R: TypeRelation<I>>(
})) }))
} else { } else {
let args = relate_args_invariantly(relation, a.args, b.args)?; let args = relate_args_invariantly(relation, a.args, b.args)?;
Ok(ty::TraitRef::new_from_args(relation.tcx(), a.def_id, args)) Ok(ty::TraitRef::new_from_args(relation.cx(), a.def_id, args))
} }
} }
} }
@ -377,7 +376,7 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
a: I::Ty, a: I::Ty,
b: I::Ty, b: I::Ty,
) -> RelateResult<I, I::Ty> { ) -> RelateResult<I, I::Ty> {
let tcx = relation.tcx(); let cx = relation.cx();
match (a.kind(), b.kind()) { match (a.kind(), b.kind()) {
(ty::Infer(_), _) | (_, ty::Infer(_)) => { (ty::Infer(_), _) | (_, ty::Infer(_)) => {
// The caller should handle these cases! // The caller should handle these cases!
@ -388,7 +387,7 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
panic!("bound types encountered in structurally_relate_tys") panic!("bound types encountered in structurally_relate_tys")
} }
(ty::Error(guar), _) | (_, ty::Error(guar)) => Ok(Ty::new_error(tcx, guar)), (ty::Error(guar), _) | (_, ty::Error(guar)) => Ok(Ty::new_error(cx, guar)),
(ty::Never, _) (ty::Never, _)
| (ty::Char, _) | (ty::Char, _)
@ -412,16 +411,16 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
(ty::Adt(a_def, a_args), ty::Adt(b_def, b_args)) if a_def == b_def => { (ty::Adt(a_def, a_args), ty::Adt(b_def, b_args)) if a_def == b_def => {
let args = relation.relate_item_args(a_def.def_id(), a_args, b_args)?; let args = relation.relate_item_args(a_def.def_id(), a_args, b_args)?;
Ok(Ty::new_adt(tcx, a_def, args)) Ok(Ty::new_adt(cx, a_def, args))
} }
(ty::Foreign(a_id), ty::Foreign(b_id)) if a_id == b_id => Ok(Ty::new_foreign(tcx, a_id)), (ty::Foreign(a_id), ty::Foreign(b_id)) if a_id == b_id => Ok(Ty::new_foreign(cx, a_id)),
(ty::Dynamic(a_obj, a_region, a_repr), ty::Dynamic(b_obj, b_region, b_repr)) (ty::Dynamic(a_obj, a_region, a_repr), ty::Dynamic(b_obj, b_region, b_repr))
if a_repr == b_repr => if a_repr == b_repr =>
{ {
Ok(Ty::new_dynamic( Ok(Ty::new_dynamic(
tcx, cx,
relation.relate(a_obj, b_obj)?, relation.relate(a_obj, b_obj)?,
relation.relate(a_region, b_region)?, relation.relate(a_region, b_region)?,
a_repr, a_repr,
@ -433,7 +432,7 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
// the (anonymous) type of the same coroutine expression. So // the (anonymous) type of the same coroutine expression. So
// all of their regions should be equated. // all of their regions should be equated.
let args = relate_args_invariantly(relation, a_args, b_args)?; let args = relate_args_invariantly(relation, a_args, b_args)?;
Ok(Ty::new_coroutine(tcx, a_id, args)) Ok(Ty::new_coroutine(cx, a_id, args))
} }
(ty::CoroutineWitness(a_id, a_args), ty::CoroutineWitness(b_id, b_args)) (ty::CoroutineWitness(a_id, a_args), ty::CoroutineWitness(b_id, b_args))
@ -443,7 +442,7 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
// the (anonymous) type of the same coroutine expression. So // the (anonymous) type of the same coroutine expression. So
// all of their regions should be equated. // all of their regions should be equated.
let args = relate_args_invariantly(relation, a_args, b_args)?; let args = relate_args_invariantly(relation, a_args, b_args)?;
Ok(Ty::new_coroutine_witness(tcx, a_id, args)) Ok(Ty::new_coroutine_witness(cx, a_id, args))
} }
(ty::Closure(a_id, a_args), ty::Closure(b_id, b_args)) if a_id == b_id => { (ty::Closure(a_id, a_args), ty::Closure(b_id, b_args)) if a_id == b_id => {
@ -451,14 +450,14 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
// the (anonymous) type of the same closure expression. So // the (anonymous) type of the same closure expression. So
// all of their regions should be equated. // all of their regions should be equated.
let args = relate_args_invariantly(relation, a_args, b_args)?; let args = relate_args_invariantly(relation, a_args, b_args)?;
Ok(Ty::new_closure(tcx, a_id, args)) Ok(Ty::new_closure(cx, a_id, args))
} }
(ty::CoroutineClosure(a_id, a_args), ty::CoroutineClosure(b_id, b_args)) (ty::CoroutineClosure(a_id, a_args), ty::CoroutineClosure(b_id, b_args))
if a_id == b_id => if a_id == b_id =>
{ {
let args = relate_args_invariantly(relation, a_args, b_args)?; let args = relate_args_invariantly(relation, a_args, b_args)?;
Ok(Ty::new_coroutine_closure(tcx, a_id, args)) Ok(Ty::new_coroutine_closure(cx, a_id, args))
} }
(ty::RawPtr(a_ty, a_mutbl), ty::RawPtr(b_ty, b_mutbl)) => { (ty::RawPtr(a_ty, a_mutbl), ty::RawPtr(b_ty, b_mutbl)) => {
@ -475,7 +474,7 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
let ty = relation.relate_with_variance(variance, info, a_ty, b_ty)?; let ty = relation.relate_with_variance(variance, info, a_ty, b_ty)?;
Ok(Ty::new_ptr(tcx, ty, a_mutbl)) Ok(Ty::new_ptr(cx, ty, a_mutbl))
} }
(ty::Ref(a_r, a_ty, a_mutbl), ty::Ref(b_r, b_ty, b_mutbl)) => { (ty::Ref(a_r, a_ty, a_mutbl), ty::Ref(b_r, b_ty, b_mutbl)) => {
@ -493,18 +492,18 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
let r = relation.relate(a_r, b_r)?; let r = relation.relate(a_r, b_r)?;
let ty = relation.relate_with_variance(variance, info, a_ty, b_ty)?; let ty = relation.relate_with_variance(variance, info, a_ty, b_ty)?;
Ok(Ty::new_ref(tcx, r, ty, a_mutbl)) Ok(Ty::new_ref(cx, r, ty, a_mutbl))
} }
(ty::Array(a_t, sz_a), ty::Array(b_t, sz_b)) => { (ty::Array(a_t, sz_a), ty::Array(b_t, sz_b)) => {
let t = relation.relate(a_t, b_t)?; let t = relation.relate(a_t, b_t)?;
match relation.relate(sz_a, sz_b) { match relation.relate(sz_a, sz_b) {
Ok(sz) => Ok(Ty::new_array_with_const_len(tcx, t, sz)), Ok(sz) => Ok(Ty::new_array_with_const_len(cx, t, sz)),
Err(err) => { Err(err) => {
// Check whether the lengths are both concrete/known values, // Check whether the lengths are both concrete/known values,
// but are unequal, for better diagnostics. // but are unequal, for better diagnostics.
let sz_a = sz_a.try_to_target_usize(tcx); let sz_a = sz_a.try_to_target_usize(cx);
let sz_b = sz_b.try_to_target_usize(tcx); let sz_b = sz_b.try_to_target_usize(cx);
match (sz_a, sz_b) { match (sz_a, sz_b) {
(Some(sz_a_val), Some(sz_b_val)) if sz_a_val != sz_b_val => Err( (Some(sz_a_val), Some(sz_b_val)) if sz_a_val != sz_b_val => Err(
@ -518,13 +517,13 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
(ty::Slice(a_t), ty::Slice(b_t)) => { (ty::Slice(a_t), ty::Slice(b_t)) => {
let t = relation.relate(a_t, b_t)?; let t = relation.relate(a_t, b_t)?;
Ok(Ty::new_slice(tcx, t)) Ok(Ty::new_slice(cx, t))
} }
(ty::Tuple(as_), ty::Tuple(bs)) => { (ty::Tuple(as_), ty::Tuple(bs)) => {
if as_.len() == bs.len() { if as_.len() == bs.len() {
Ok(Ty::new_tup_from_iter( Ok(Ty::new_tup_from_iter(
tcx, cx,
iter::zip(as_.iter(), bs.iter()).map(|(a, b)| relation.relate(a, b)), iter::zip(as_.iter(), bs.iter()).map(|(a, b)| relation.relate(a, b)),
)?) )?)
} else if !(as_.is_empty() || bs.is_empty()) { } else if !(as_.is_empty() || bs.is_empty()) {
@ -536,25 +535,25 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
(ty::FnDef(a_def_id, a_args), ty::FnDef(b_def_id, b_args)) if a_def_id == b_def_id => { (ty::FnDef(a_def_id, a_args), ty::FnDef(b_def_id, b_args)) if a_def_id == b_def_id => {
let args = relation.relate_item_args(a_def_id, a_args, b_args)?; let args = relation.relate_item_args(a_def_id, a_args, b_args)?;
Ok(Ty::new_fn_def(tcx, a_def_id, args)) Ok(Ty::new_fn_def(cx, a_def_id, args))
} }
(ty::FnPtr(a_fty), ty::FnPtr(b_fty)) => { (ty::FnPtr(a_fty), ty::FnPtr(b_fty)) => {
let fty = relation.relate(a_fty, b_fty)?; let fty = relation.relate(a_fty, b_fty)?;
Ok(Ty::new_fn_ptr(tcx, fty)) Ok(Ty::new_fn_ptr(cx, fty))
} }
// Alias tend to mostly already be handled downstream due to normalization. // Alias tend to mostly already be handled downstream due to normalization.
(ty::Alias(a_kind, a_data), ty::Alias(b_kind, b_data)) => { (ty::Alias(a_kind, a_data), ty::Alias(b_kind, b_data)) => {
let alias_ty = relation.relate(a_data, b_data)?; let alias_ty = relation.relate(a_data, b_data)?;
assert_eq!(a_kind, b_kind); assert_eq!(a_kind, b_kind);
Ok(Ty::new_alias(tcx, a_kind, alias_ty)) Ok(Ty::new_alias(cx, a_kind, alias_ty))
} }
(ty::Pat(a_ty, a_pat), ty::Pat(b_ty, b_pat)) => { (ty::Pat(a_ty, a_pat), ty::Pat(b_ty, b_pat)) => {
let ty = relation.relate(a_ty, b_ty)?; let ty = relation.relate(a_ty, b_ty)?;
let pat = relation.relate(a_pat, b_pat)?; let pat = relation.relate(a_pat, b_pat)?;
Ok(Ty::new_pat(tcx, ty, pat)) Ok(Ty::new_pat(cx, ty, pat))
} }
_ => Err(TypeError::Sorts(ExpectedFound::new(true, a, b))), _ => Err(TypeError::Sorts(ExpectedFound::new(true, a, b))),
@ -573,11 +572,11 @@ pub fn structurally_relate_consts<I: Interner, R: TypeRelation<I>>(
mut b: I::Const, mut b: I::Const,
) -> RelateResult<I, I::Const> { ) -> RelateResult<I, I::Const> {
debug!("{}.structurally_relate_consts(a = {:?}, b = {:?})", relation.tag(), a, b); debug!("{}.structurally_relate_consts(a = {:?}, b = {:?})", relation.tag(), a, b);
let tcx = relation.tcx(); let cx = relation.cx();
if tcx.features().generic_const_exprs() { if cx.features().generic_const_exprs() {
a = tcx.expand_abstract_consts(a); a = cx.expand_abstract_consts(a);
b = tcx.expand_abstract_consts(b); b = cx.expand_abstract_consts(b);
} }
debug!("{}.structurally_relate_consts(normed_a = {:?}, normed_b = {:?})", relation.tag(), a, b); debug!("{}.structurally_relate_consts(normed_a = {:?}, normed_b = {:?})", relation.tag(), a, b);
@ -607,8 +606,8 @@ pub fn structurally_relate_consts<I: Interner, R: TypeRelation<I>>(
// be stabilized. // be stabilized.
(ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu)) if au.def == bu.def => { (ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu)) if au.def == bu.def => {
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
let a_ty = tcx.type_of(au.def).instantiate(tcx, au.args); let a_ty = cx.type_of(au.def).instantiate(cx, au.args);
let b_ty = tcx.type_of(bu.def).instantiate(tcx, bu.args); let b_ty = cx.type_of(bu.def).instantiate(cx, bu.args);
assert_eq!(a_ty, b_ty); assert_eq!(a_ty, b_ty);
} }
@ -618,11 +617,11 @@ pub fn structurally_relate_consts<I: Interner, R: TypeRelation<I>>(
au.args, au.args,
bu.args, bu.args,
)?; )?;
return Ok(Const::new_unevaluated(tcx, ty::UnevaluatedConst { def: au.def, args })); return Ok(Const::new_unevaluated(cx, ty::UnevaluatedConst { def: au.def, args }));
} }
(ty::ConstKind::Expr(ae), ty::ConstKind::Expr(be)) => { (ty::ConstKind::Expr(ae), ty::ConstKind::Expr(be)) => {
let expr = relation.relate(ae, be)?; let expr = relation.relate(ae, be)?;
return Ok(Const::new_expr(tcx, expr)); return Ok(Const::new_expr(cx, expr));
} }
_ => false, _ => false,
}; };

View File

@ -106,13 +106,13 @@ pub struct Goal<I: Interner, P> {
} }
impl<I: Interner, P> Goal<I, P> { impl<I: Interner, P> Goal<I, P> {
pub fn new(tcx: I, param_env: I::ParamEnv, predicate: impl Upcast<I, P>) -> Goal<I, P> { pub fn new(cx: I, param_env: I::ParamEnv, predicate: impl Upcast<I, P>) -> Goal<I, P> {
Goal { param_env, predicate: predicate.upcast(tcx) } Goal { param_env, predicate: predicate.upcast(cx) }
} }
/// Updates the goal to one with a different `predicate` but the same `param_env`. /// Updates the goal to one with a different `predicate` but the same `param_env`.
pub fn with<Q>(self, tcx: I, predicate: impl Upcast<I, Q>) -> Goal<I, Q> { pub fn with<Q>(self, cx: I, predicate: impl Upcast<I, Q>) -> Goal<I, Q> {
Goal { param_env: self.param_env, predicate: predicate.upcast(tcx) } Goal { param_env: self.param_env, predicate: predicate.upcast(cx) }
} }
} }

View File

@ -136,9 +136,9 @@ pub struct ClosureArgsParts<I: Interner> {
impl<I: Interner> ClosureArgs<I> { impl<I: Interner> ClosureArgs<I> {
/// Construct `ClosureArgs` from `ClosureArgsParts`, containing `Args` /// Construct `ClosureArgs` from `ClosureArgsParts`, containing `Args`
/// for the closure parent, alongside additional closure-specific components. /// for the closure parent, alongside additional closure-specific components.
pub fn new(tcx: I, parts: ClosureArgsParts<I>) -> ClosureArgs<I> { pub fn new(cx: I, parts: ClosureArgsParts<I>) -> ClosureArgs<I> {
ClosureArgs { ClosureArgs {
args: tcx.mk_args_from_iter(parts.parent_args.iter().chain([ args: cx.mk_args_from_iter(parts.parent_args.iter().chain([
parts.closure_kind_ty.into(), parts.closure_kind_ty.into(),
parts.closure_sig_as_fn_ptr_ty.into(), parts.closure_sig_as_fn_ptr_ty.into(),
parts.tupled_upvars_ty.into(), parts.tupled_upvars_ty.into(),
@ -258,9 +258,9 @@ pub struct CoroutineClosureArgsParts<I: Interner> {
} }
impl<I: Interner> CoroutineClosureArgs<I> { impl<I: Interner> CoroutineClosureArgs<I> {
pub fn new(tcx: I, parts: CoroutineClosureArgsParts<I>) -> CoroutineClosureArgs<I> { pub fn new(cx: I, parts: CoroutineClosureArgsParts<I>) -> CoroutineClosureArgs<I> {
CoroutineClosureArgs { CoroutineClosureArgs {
args: tcx.mk_args_from_iter(parts.parent_args.iter().chain([ args: cx.mk_args_from_iter(parts.parent_args.iter().chain([
parts.closure_kind_ty.into(), parts.closure_kind_ty.into(),
parts.signature_parts_ty.into(), parts.signature_parts_ty.into(),
parts.tupled_upvars_ty.into(), parts.tupled_upvars_ty.into(),
@ -409,14 +409,14 @@ impl<I: Interner> CoroutineClosureSignature<I> {
/// When the kind and upvars are known, use the other helper functions. /// When the kind and upvars are known, use the other helper functions.
pub fn to_coroutine( pub fn to_coroutine(
self, self,
tcx: I, cx: I,
parent_args: I::GenericArgsSlice, parent_args: I::GenericArgsSlice,
coroutine_kind_ty: I::Ty, coroutine_kind_ty: I::Ty,
coroutine_def_id: I::DefId, coroutine_def_id: I::DefId,
tupled_upvars_ty: I::Ty, tupled_upvars_ty: I::Ty,
) -> I::Ty { ) -> I::Ty {
let coroutine_args = ty::CoroutineArgs::new( let coroutine_args = ty::CoroutineArgs::new(
tcx, cx,
ty::CoroutineArgsParts { ty::CoroutineArgsParts {
parent_args, parent_args,
kind_ty: coroutine_kind_ty, kind_ty: coroutine_kind_ty,
@ -428,7 +428,7 @@ pub fn to_coroutine(
}, },
); );
Ty::new_coroutine(tcx, coroutine_def_id, coroutine_args.args) Ty::new_coroutine(cx, coroutine_def_id, coroutine_args.args)
} }
/// Given known upvars and a [`ClosureKind`](ty::ClosureKind), compute the coroutine /// Given known upvars and a [`ClosureKind`](ty::ClosureKind), compute the coroutine
@ -438,7 +438,7 @@ pub fn to_coroutine(
/// that the `ClosureKind` is actually supported by the coroutine-closure. /// that the `ClosureKind` is actually supported by the coroutine-closure.
pub fn to_coroutine_given_kind_and_upvars( pub fn to_coroutine_given_kind_and_upvars(
self, self,
tcx: I, cx: I,
parent_args: I::GenericArgsSlice, parent_args: I::GenericArgsSlice,
coroutine_def_id: I::DefId, coroutine_def_id: I::DefId,
goal_kind: ty::ClosureKind, goal_kind: ty::ClosureKind,
@ -447,7 +447,7 @@ pub fn to_coroutine_given_kind_and_upvars(
coroutine_captures_by_ref_ty: I::Ty, coroutine_captures_by_ref_ty: I::Ty,
) -> I::Ty { ) -> I::Ty {
let tupled_upvars_ty = Self::tupled_upvars_by_closure_kind( let tupled_upvars_ty = Self::tupled_upvars_by_closure_kind(
tcx, cx,
goal_kind, goal_kind,
self.tupled_inputs_ty, self.tupled_inputs_ty,
closure_tupled_upvars_ty, closure_tupled_upvars_ty,
@ -456,9 +456,9 @@ pub fn to_coroutine_given_kind_and_upvars(
); );
self.to_coroutine( self.to_coroutine(
tcx, cx,
parent_args, parent_args,
Ty::from_coroutine_closure_kind(tcx, goal_kind), Ty::from_coroutine_closure_kind(cx, goal_kind),
coroutine_def_id, coroutine_def_id,
tupled_upvars_ty, tupled_upvars_ty,
) )
@ -474,7 +474,7 @@ pub fn to_coroutine_given_kind_and_upvars(
/// lifetimes are related to the lifetime of the borrow on the closure made for /// lifetimes are related to the lifetime of the borrow on the closure made for
/// the call. This allows borrowck to enforce the self-borrows correctly. /// the call. This allows borrowck to enforce the self-borrows correctly.
pub fn tupled_upvars_by_closure_kind( pub fn tupled_upvars_by_closure_kind(
tcx: I, cx: I,
kind: ty::ClosureKind, kind: ty::ClosureKind,
tupled_inputs_ty: I::Ty, tupled_inputs_ty: I::Ty,
closure_tupled_upvars_ty: I::Ty, closure_tupled_upvars_ty: I::Ty,
@ -488,12 +488,12 @@ pub fn tupled_upvars_by_closure_kind(
}; };
let coroutine_captures_by_ref_ty = let coroutine_captures_by_ref_ty =
sig.output().skip_binder().fold_with(&mut FoldEscapingRegions { sig.output().skip_binder().fold_with(&mut FoldEscapingRegions {
interner: tcx, interner: cx,
region: env_region, region: env_region,
debruijn: ty::INNERMOST, debruijn: ty::INNERMOST,
}); });
Ty::new_tup_from_iter( Ty::new_tup_from_iter(
tcx, cx,
tupled_inputs_ty tupled_inputs_ty
.tuple_fields() .tuple_fields()
.iter() .iter()
@ -501,7 +501,7 @@ pub fn tupled_upvars_by_closure_kind(
) )
} }
ty::ClosureKind::FnOnce => Ty::new_tup_from_iter( ty::ClosureKind::FnOnce => Ty::new_tup_from_iter(
tcx, cx,
tupled_inputs_ty tupled_inputs_ty
.tuple_fields() .tuple_fields()
.iter() .iter()
@ -615,9 +615,9 @@ pub struct CoroutineArgsParts<I: Interner> {
impl<I: Interner> CoroutineArgs<I> { impl<I: Interner> CoroutineArgs<I> {
/// Construct `CoroutineArgs` from `CoroutineArgsParts`, containing `Args` /// Construct `CoroutineArgs` from `CoroutineArgsParts`, containing `Args`
/// for the coroutine parent, alongside additional coroutine-specific components. /// for the coroutine parent, alongside additional coroutine-specific components.
pub fn new(tcx: I, parts: CoroutineArgsParts<I>) -> CoroutineArgs<I> { pub fn new(cx: I, parts: CoroutineArgsParts<I>) -> CoroutineArgs<I> {
CoroutineArgs { CoroutineArgs {
args: tcx.mk_args_from_iter(parts.parent_args.iter().chain([ args: cx.mk_args_from_iter(parts.parent_args.iter().chain([
parts.kind_ty.into(), parts.kind_ty.into(),
parts.resume_ty.into(), parts.resume_ty.into(),
parts.yield_ty.into(), parts.yield_ty.into(),