diff options
| author | Michael Goulet <michael@errs.io> | 2024-06-15 17:41:49 -0400 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2024-06-16 11:28:47 -0400 |
| commit | ff154c7122caa5a56c71e1dfa5e49e87e6f26428 (patch) | |
| tree | da386f01dff5c10861d12f277a7e8e0c22826a49 | |
| parent | 4c2d888a507cf28ecec80e630b61a2700b9d060d (diff) | |
| download | rust-ff154c7122caa5a56c71e1dfa5e49e87e6f26428.tar.gz rust-ff154c7122caa5a56c71e1dfa5e49e87e6f26428.zip | |
Uplift OpaqueTypeKey too, use it in response
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 40 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/opaque_types.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_type_ir/src/interner.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_type_ir/src/lib.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_type_ir/src/opaque_ty.rs | 51 | ||||
| -rw-r--r-- | compiler/rustc_type_ir/src/solve.rs | 2 |
7 files changed, 66 insertions, 51 deletions
diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 5a94a53e175..4e388de6fb8 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -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 diff --git a/compiler/rustc_middle/src/ty/opaque_types.rs b/compiler/rustc_middle/src/ty/opaque_types.rs index 52902aadd7c..08b2f9e8920 100644 --- a/compiler/rustc_middle/src/ty/opaque_types.rs +++ b/compiler/rustc_middle/src/ty/opaque_types.rs @@ -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. diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs index 2e020b03c5d..b5753d60f59 100644 --- a/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs +++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs @@ -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); } } } diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index 30beb4480e9..11c1f73fef3 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -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` diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 4775a0f8cbb..ac9b2808804 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -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::*; diff --git a/compiler/rustc_type_ir/src/opaque_ty.rs b/compiler/rustc_type_ir/src/opaque_ty.rs new file mode 100644 index 00000000000..60737066597 --- /dev/null +++ b/compiler/rustc_type_ir/src/opaque_ty.rs @@ -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 } + } +} diff --git a/compiler/rustc_type_ir/src/solve.rs b/compiler/rustc_type_ir/src/solve.rs index d4e43716c17..99d2fa74494 100644 --- a/compiler/rustc_type_ir/src/solve.rs +++ b/compiler/rustc_type_ir/src/solve.rs @@ -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>, } |
