Uplift OpaqueTypeKey too, use it in response

This commit is contained in:
Michael Goulet 2024-06-15 17:41:49 -04:00
parent 4c2d888a50
commit ff154c7122
7 changed files with 66 additions and 51 deletions

View File

@ -94,6 +94,7 @@ pub use self::context::{
};
pub use self::instance::{Instance, InstanceDef, ReifyReason, ShortInstance, UnusedGenericParams};
pub use self::list::{List, ListWithCachedTypeInfo};
pub use self::opaque_types::OpaqueTypeKey;
pub use self::parameterized::ParameterizedOverTcx;
pub use self::pattern::{Pattern, PatternKind};
pub use self::predicate::{
@ -758,45 +759,6 @@ impl<'a, 'tcx> IntoIterator for &'a InstantiatedPredicates<'tcx> {
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
#[derive(TypeFoldable, TypeVisitable)]
pub struct OpaqueTypeKey<'tcx> {
pub def_id: LocalDefId,
pub args: GenericArgsRef<'tcx>,
}
impl<'tcx> OpaqueTypeKey<'tcx> {
pub fn iter_captured_args(
self,
tcx: TyCtxt<'tcx>,
) -> impl Iterator<Item = (usize, GenericArg<'tcx>)> {
std::iter::zip(self.args, tcx.variances_of(self.def_id)).enumerate().filter_map(
|(i, (arg, v))| match (arg.unpack(), v) {
(_, ty::Invariant) => Some((i, arg)),
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => None,
_ => bug!("unexpected opaque type arg variance"),
},
)
}
pub fn fold_captured_lifetime_args(
self,
tcx: TyCtxt<'tcx>,
mut f: impl FnMut(Region<'tcx>) -> Region<'tcx>,
) -> Self {
let Self { def_id, args } = self;
let args = std::iter::zip(args, tcx.variances_of(def_id)).map(|(arg, v)| {
match (arg.unpack(), v) {
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg,
(ty::GenericArgKind::Lifetime(lt), _) => f(lt).into(),
_ => arg,
}
});
let args = tcx.mk_args_from_iter(args);
Self { def_id, args }
}
}
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
pub struct OpaqueHiddenType<'tcx> {
/// The span of this particular definition of the opaque type. So

View File

@ -7,6 +7,8 @@ use rustc_span::def_id::DefId;
use rustc_span::Span;
use tracing::{debug, instrument, trace};
pub type OpaqueTypeKey<'tcx> = rustc_type_ir::OpaqueTypeKey<TyCtxt<'tcx>>;
/// Converts generic params of a TypeFoldable from one
/// item's generics to another. Usually from a function's generics
/// list to the opaque type's own generics.

View File

@ -14,7 +14,6 @@ use crate::solve::{
inspect, response_no_constraints_raw, CanonicalResponse, QueryResult, Response,
};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::LocalDefId;
use rustc_index::IndexVec;
use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
use rustc_infer::infer::canonical::{CanonicalExt, QueryRegionConstraints};
@ -224,7 +223,6 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
.filter(|(a, _)| {
self.predefined_opaques_in_body.opaque_types.iter().all(|(pa, _)| pa != a)
})
.map(|(key, value)| (key.def_id, key.args, value))
.collect(),
normalization_nested_goals,
}
@ -393,14 +391,10 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
}
}
fn register_new_opaque_types(
&mut self,
opaque_types: &[(LocalDefId, ty::GenericArgsRef<'tcx>, Ty<'tcx>)],
) {
for &(def_id, args, ty) in opaque_types {
fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)]) {
for &(key, ty) in opaque_types {
let hidden_ty = ty::OpaqueHiddenType { ty, span: DUMMY_SP };
self.infcx
.inject_new_hidden_type_unchecked(ty::OpaqueTypeKey { def_id, args }, hidden_ty);
self.infcx.inject_new_hidden_type_unchecked(key, hidden_ty);
}
}
}

View File

@ -29,7 +29,7 @@ pub trait Interner:
+ IrPrint<ty::FnSig<Self>>
{
type DefId: Copy + Debug + Hash + Eq + TypeFoldable<Self>;
type LocalDefId: Copy + Debug + Hash + Eq + TypeFoldable<Self>;
type LocalDefId: Copy + Debug + Hash + Eq + Into<Self::DefId> + TypeFoldable<Self>;
type AdtDef: AdtDef<Self>;
type GenericArgs: GenericArgs<Self>;
@ -104,7 +104,11 @@ pub trait Interner:
type GenericsOf: GenericsOf<Self>;
fn generics_of(self, def_id: Self::DefId) -> Self::GenericsOf;
type VariancesOf: Copy + Debug + Deref<Target = [ty::Variance]>;
type VariancesOf: Copy
+ Debug
+ Deref<Target = [ty::Variance]>
// FIXME: This is terrible!
+ IntoIterator<Item: Deref<Target = ty::Variance>>;
fn variances_of(self, def_id: Self::DefId) -> Self::VariancesOf;
// FIXME: Remove after uplifting `EarlyBinder`

View File

@ -47,6 +47,7 @@ mod flags;
mod generic_arg;
mod infcx;
mod interner;
mod opaque_ty;
mod predicate;
mod predicate_kind;
mod region_kind;
@ -63,6 +64,7 @@ pub use flags::*;
pub use generic_arg::*;
pub use infcx::InferCtxtLike;
pub use interner::*;
pub use opaque_ty::*;
pub use predicate::*;
pub use predicate_kind::*;
pub use region_kind::*;

View File

@ -0,0 +1,51 @@
use rustc_macros::{HashStable_NoContext, TyDecodable, TyEncodable};
use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
use crate::inherent::*;
use crate::{self as ty, Interner};
#[derive(derivative::Derivative)]
#[derivative(
Clone(bound = ""),
Hash(bound = ""),
PartialEq(bound = ""),
Eq(bound = ""),
Debug(bound = ""),
Copy(bound = "")
)]
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
pub struct OpaqueTypeKey<I: Interner> {
pub def_id: I::LocalDefId,
pub args: I::GenericArgs,
}
impl<I: Interner> OpaqueTypeKey<I> {
pub fn iter_captured_args(self, tcx: I) -> impl Iterator<Item = (usize, I::GenericArg)> {
let variances = tcx.variances_of(self.def_id.into());
std::iter::zip(self.args, variances.into_iter()).enumerate().filter_map(|(i, (arg, v))| {
match (arg.kind(), *v) {
(_, ty::Invariant) => Some((i, arg)),
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => None,
_ => panic!("unexpected opaque type arg variance"),
}
})
}
pub fn fold_captured_lifetime_args(
self,
tcx: I,
mut f: impl FnMut(I::Region) -> I::Region,
) -> Self {
let Self { def_id, args } = self;
let variances = tcx.variances_of(def_id.into());
let args =
std::iter::zip(args, variances.into_iter()).map(|(arg, v)| match (arg.kind(), *v) {
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg,
(ty::GenericArgKind::Lifetime(lt), _) => f(lt).into(),
_ => arg,
});
let args = tcx.mk_args_from_iter(args);
Self { def_id, args }
}
}

View File

@ -268,7 +268,7 @@ pub struct Response<I: Interner> {
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
pub struct ExternalConstraintsData<I: Interner> {
pub region_constraints: Vec<ty::OutlivesPredicate<I, I::GenericArg>>,
pub opaque_types: Vec<(I::LocalDefId, I::GenericArgs, I::Ty)>,
pub opaque_types: Vec<(ty::OpaqueTypeKey<I>, I::Ty)>,
pub normalization_nested_goals: NestedNormalizationGoals<I>,
}