Skip to content

Commit ff154c7

Browse files
Uplift OpaqueTypeKey too, use it in response
1 parent 4c2d888 commit ff154c7

File tree

7 files changed

+66
-51
lines changed

7 files changed

+66
-51
lines changed

compiler/rustc_middle/src/ty/mod.rs

+1-39
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub use self::context::{
9494
};
9595
pub use self::instance::{Instance, InstanceDef, ReifyReason, ShortInstance, UnusedGenericParams};
9696
pub use self::list::{List, ListWithCachedTypeInfo};
97+
pub use self::opaque_types::OpaqueTypeKey;
9798
pub use self::parameterized::ParameterizedOverTcx;
9899
pub use self::pattern::{Pattern, PatternKind};
99100
pub use self::predicate::{
@@ -758,45 +759,6 @@ impl<'a, 'tcx> IntoIterator for &'a InstantiatedPredicates<'tcx> {
758759
}
759760
}
760761

761-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
762-
#[derive(TypeFoldable, TypeVisitable)]
763-
pub struct OpaqueTypeKey<'tcx> {
764-
pub def_id: LocalDefId,
765-
pub args: GenericArgsRef<'tcx>,
766-
}
767-
768-
impl<'tcx> OpaqueTypeKey<'tcx> {
769-
pub fn iter_captured_args(
770-
self,
771-
tcx: TyCtxt<'tcx>,
772-
) -> impl Iterator<Item = (usize, GenericArg<'tcx>)> {
773-
std::iter::zip(self.args, tcx.variances_of(self.def_id)).enumerate().filter_map(
774-
|(i, (arg, v))| match (arg.unpack(), v) {
775-
(_, ty::Invariant) => Some((i, arg)),
776-
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => None,
777-
_ => bug!("unexpected opaque type arg variance"),
778-
},
779-
)
780-
}
781-
782-
pub fn fold_captured_lifetime_args(
783-
self,
784-
tcx: TyCtxt<'tcx>,
785-
mut f: impl FnMut(Region<'tcx>) -> Region<'tcx>,
786-
) -> Self {
787-
let Self { def_id, args } = self;
788-
let args = std::iter::zip(args, tcx.variances_of(def_id)).map(|(arg, v)| {
789-
match (arg.unpack(), v) {
790-
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg,
791-
(ty::GenericArgKind::Lifetime(lt), _) => f(lt).into(),
792-
_ => arg,
793-
}
794-
});
795-
let args = tcx.mk_args_from_iter(args);
796-
Self { def_id, args }
797-
}
798-
}
799-
800762
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
801763
pub struct OpaqueHiddenType<'tcx> {
802764
/// The span of this particular definition of the opaque type. So

compiler/rustc_middle/src/ty/opaque_types.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use rustc_span::def_id::DefId;
77
use rustc_span::Span;
88
use tracing::{debug, instrument, trace};
99

10+
pub type OpaqueTypeKey<'tcx> = rustc_type_ir::OpaqueTypeKey<TyCtxt<'tcx>>;
11+
1012
/// Converts generic params of a TypeFoldable from one
1113
/// item's generics to another. Usually from a function's generics
1214
/// list to the opaque type's own generics.

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::solve::{
1414
inspect, response_no_constraints_raw, CanonicalResponse, QueryResult, Response,
1515
};
1616
use rustc_data_structures::fx::FxHashSet;
17-
use rustc_hir::def_id::LocalDefId;
1817
use rustc_index::IndexVec;
1918
use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
2019
use rustc_infer::infer::canonical::{CanonicalExt, QueryRegionConstraints};
@@ -224,7 +223,6 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
224223
.filter(|(a, _)| {
225224
self.predefined_opaques_in_body.opaque_types.iter().all(|(pa, _)| pa != a)
226225
})
227-
.map(|(key, value)| (key.def_id, key.args, value))
228226
.collect(),
229227
normalization_nested_goals,
230228
}
@@ -393,14 +391,10 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
393391
}
394392
}
395393

396-
fn register_new_opaque_types(
397-
&mut self,
398-
opaque_types: &[(LocalDefId, ty::GenericArgsRef<'tcx>, Ty<'tcx>)],
399-
) {
400-
for &(def_id, args, ty) in opaque_types {
394+
fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)]) {
395+
for &(key, ty) in opaque_types {
401396
let hidden_ty = ty::OpaqueHiddenType { ty, span: DUMMY_SP };
402-
self.infcx
403-
.inject_new_hidden_type_unchecked(ty::OpaqueTypeKey { def_id, args }, hidden_ty);
397+
self.infcx.inject_new_hidden_type_unchecked(key, hidden_ty);
404398
}
405399
}
406400
}

compiler/rustc_type_ir/src/interner.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub trait Interner:
2929
+ IrPrint<ty::FnSig<Self>>
3030
{
3131
type DefId: Copy + Debug + Hash + Eq + TypeFoldable<Self>;
32-
type LocalDefId: Copy + Debug + Hash + Eq + TypeFoldable<Self>;
32+
type LocalDefId: Copy + Debug + Hash + Eq + Into<Self::DefId> + TypeFoldable<Self>;
3333
type AdtDef: AdtDef<Self>;
3434

3535
type GenericArgs: GenericArgs<Self>;
@@ -104,7 +104,11 @@ pub trait Interner:
104104
type GenericsOf: GenericsOf<Self>;
105105
fn generics_of(self, def_id: Self::DefId) -> Self::GenericsOf;
106106

107-
type VariancesOf: Copy + Debug + Deref<Target = [ty::Variance]>;
107+
type VariancesOf: Copy
108+
+ Debug
109+
+ Deref<Target = [ty::Variance]>
110+
// FIXME: This is terrible!
111+
+ IntoIterator<Item: Deref<Target = ty::Variance>>;
108112
fn variances_of(self, def_id: Self::DefId) -> Self::VariancesOf;
109113

110114
// FIXME: Remove after uplifting `EarlyBinder`

compiler/rustc_type_ir/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ mod flags;
4747
mod generic_arg;
4848
mod infcx;
4949
mod interner;
50+
mod opaque_ty;
5051
mod predicate;
5152
mod predicate_kind;
5253
mod region_kind;
@@ -63,6 +64,7 @@ pub use flags::*;
6364
pub use generic_arg::*;
6465
pub use infcx::InferCtxtLike;
6566
pub use interner::*;
67+
pub use opaque_ty::*;
6668
pub use predicate::*;
6769
pub use predicate_kind::*;
6870
pub use region_kind::*;
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use rustc_macros::{HashStable_NoContext, TyDecodable, TyEncodable};
2+
use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
3+
4+
use crate::inherent::*;
5+
use crate::{self as ty, Interner};
6+
7+
#[derive(derivative::Derivative)]
8+
#[derivative(
9+
Clone(bound = ""),
10+
Hash(bound = ""),
11+
PartialEq(bound = ""),
12+
Eq(bound = ""),
13+
Debug(bound = ""),
14+
Copy(bound = "")
15+
)]
16+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
17+
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
18+
pub struct OpaqueTypeKey<I: Interner> {
19+
pub def_id: I::LocalDefId,
20+
pub args: I::GenericArgs,
21+
}
22+
23+
impl<I: Interner> OpaqueTypeKey<I> {
24+
pub fn iter_captured_args(self, tcx: I) -> impl Iterator<Item = (usize, I::GenericArg)> {
25+
let variances = tcx.variances_of(self.def_id.into());
26+
std::iter::zip(self.args, variances.into_iter()).enumerate().filter_map(|(i, (arg, v))| {
27+
match (arg.kind(), *v) {
28+
(_, ty::Invariant) => Some((i, arg)),
29+
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => None,
30+
_ => panic!("unexpected opaque type arg variance"),
31+
}
32+
})
33+
}
34+
35+
pub fn fold_captured_lifetime_args(
36+
self,
37+
tcx: I,
38+
mut f: impl FnMut(I::Region) -> I::Region,
39+
) -> Self {
40+
let Self { def_id, args } = self;
41+
let variances = tcx.variances_of(def_id.into());
42+
let args =
43+
std::iter::zip(args, variances.into_iter()).map(|(arg, v)| match (arg.kind(), *v) {
44+
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg,
45+
(ty::GenericArgKind::Lifetime(lt), _) => f(lt).into(),
46+
_ => arg,
47+
});
48+
let args = tcx.mk_args_from_iter(args);
49+
Self { def_id, args }
50+
}
51+
}

compiler/rustc_type_ir/src/solve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ pub struct Response<I: Interner> {
268268
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
269269
pub struct ExternalConstraintsData<I: Interner> {
270270
pub region_constraints: Vec<ty::OutlivesPredicate<I, I::GenericArg>>,
271-
pub opaque_types: Vec<(I::LocalDefId, I::GenericArgs, I::Ty)>,
271+
pub opaque_types: Vec<(ty::OpaqueTypeKey<I>, I::Ty)>,
272272
pub normalization_nested_goals: NestedNormalizationGoals<I>,
273273
}
274274

0 commit comments

Comments
 (0)