rustc_type_ir/
opaque_ty.rs1use derive_where::derive_where;
2#[cfg(feature = "nightly")]
3use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
4use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
5
6use crate::inherent::*;
7use crate::{self as ty, Interner};
8
9#[derive_where(Clone, Copy, Hash, PartialEq, Debug; I: Interner)]
10#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
11#[cfg_attr(
12 feature = "nightly",
13 derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
14)]
15pub struct OpaqueTypeKey<I: Interner> {
16 pub def_id: I::LocalDefId,
17 pub args: I::GenericArgs,
18}
19
20impl<I: Interner> Eq for OpaqueTypeKey<I> {}
21
22impl<I: Interner> OpaqueTypeKey<I> {
23 pub fn iter_captured_args(self, cx: I) -> impl Iterator<Item = (usize, I::GenericArg)> {
24 let variances = cx.variances_of(self.def_id.into());
25 std::iter::zip(self.args.iter(), variances.iter()).enumerate().filter_map(
26 |(i, (arg, v))| match (arg.kind(), v) {
27 (_, ty::Invariant) => Some((i, arg)),
28 (ty::GenericArgKind::Lifetime(_), ty::Bivariant) => None,
29 _ => panic!("unexpected opaque type arg variance"),
30 },
31 )
32 }
33
34 pub fn fold_captured_lifetime_args(
35 self,
36 cx: I,
37 mut f: impl FnMut(I::Region) -> I::Region,
38 ) -> Self {
39 let Self { def_id, args } = self;
40 let variances = cx.variances_of(def_id.into());
41 let args =
42 std::iter::zip(args.iter(), variances.iter()).map(|(arg, v)| match (arg.kind(), v) {
43 (ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg,
44 (ty::GenericArgKind::Lifetime(lt), _) => f(lt).into(),
45 _ => arg,
46 });
47 let args = cx.mk_args_from_iter(args);
48 Self { def_id, args }
49 }
50}