Skip to content

Commit 6232ba8

Browse files
Migrate variance to the new solver
1 parent 7c871c2 commit 6232ba8

File tree

9 files changed

+228
-424
lines changed

9 files changed

+228
-424
lines changed
Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,9 @@
11
//! The implementation of `RustIrDatabase` for Chalk, which provides information
22
//! about the code that Chalk needs.
3-
use hir_def::{CallableDefId, GenericDefId};
43
5-
use crate::{Interner, db::HirDatabase, mapping::from_chalk};
4+
use crate::Interner;
65

76
pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
87
pub(crate) type TraitId = chalk_ir::TraitId<Interner>;
98
pub(crate) type AdtId = chalk_ir::AdtId<Interner>;
109
pub(crate) type ImplId = chalk_ir::ImplId<Interner>;
11-
pub(crate) type Variances = chalk_ir::Variances<Interner>;
12-
13-
impl chalk_ir::UnificationDatabase<Interner> for &dyn HirDatabase {
14-
fn fn_def_variance(
15-
&self,
16-
fn_def_id: chalk_ir::FnDefId<Interner>,
17-
) -> chalk_ir::Variances<Interner> {
18-
HirDatabase::fn_def_variance(*self, from_chalk(*self, fn_def_id))
19-
}
20-
21-
fn adt_variance(&self, adt_id: chalk_ir::AdtId<Interner>) -> chalk_ir::Variances<Interner> {
22-
HirDatabase::adt_variance(*self, adt_id.0)
23-
}
24-
}
25-
26-
pub(crate) fn fn_def_variance_query(
27-
db: &dyn HirDatabase,
28-
callable_def: CallableDefId,
29-
) -> Variances {
30-
Variances::from_iter(
31-
Interner,
32-
db.variances_of(GenericDefId::from_callable(db, callable_def))
33-
.as_deref()
34-
.unwrap_or_default()
35-
.iter()
36-
.map(|v| match v {
37-
crate::variance::Variance::Covariant => chalk_ir::Variance::Covariant,
38-
crate::variance::Variance::Invariant => chalk_ir::Variance::Invariant,
39-
crate::variance::Variance::Contravariant => chalk_ir::Variance::Contravariant,
40-
crate::variance::Variance::Bivariant => chalk_ir::Variance::Invariant,
41-
}),
42-
)
43-
}
44-
45-
pub(crate) fn adt_variance_query(db: &dyn HirDatabase, adt_id: hir_def::AdtId) -> Variances {
46-
Variances::from_iter(
47-
Interner,
48-
db.variances_of(adt_id.into()).as_deref().unwrap_or_default().iter().map(|v| match v {
49-
crate::variance::Variance::Covariant => chalk_ir::Variance::Covariant,
50-
crate::variance::Variance::Invariant => chalk_ir::Variance::Invariant,
51-
crate::variance::Variance::Contravariant => chalk_ir::Variance::Contravariant,
52-
crate::variance::Variance::Bivariant => chalk_ir::Variance::Invariant,
53-
}),
54-
)
55-
}

src/tools/rust-analyzer/crates/hir-ty/src/chalk_ext.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use hir_def::{ItemContainerId, Lookup, TraitId};
44

55
use crate::{
6-
Binders, DynTy, Interner, ProjectionTy, Substitution, TraitRef, Ty, db::HirDatabase,
7-
from_assoc_type_id, from_chalk_trait_id, generics::generics, to_chalk_trait_id,
6+
Interner, ProjectionTy, Substitution, TraitRef, Ty, db::HirDatabase, from_assoc_type_id,
7+
from_chalk_trait_id, generics::generics, to_chalk_trait_id,
88
};
99

1010
pub(crate) trait ProjectionTyExt {
@@ -35,23 +35,6 @@ impl ProjectionTyExt for ProjectionTy {
3535
}
3636
}
3737

38-
pub(crate) trait DynTyExt {
39-
fn principal(&self) -> Option<Binders<Binders<&TraitRef>>>;
40-
}
41-
42-
impl DynTyExt for DynTy {
43-
fn principal(&self) -> Option<Binders<Binders<&TraitRef>>> {
44-
self.bounds.as_ref().filter_map(|bounds| {
45-
bounds.interned().first().and_then(|b| {
46-
b.as_ref().filter_map(|b| match b {
47-
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
48-
_ => None,
49-
})
50-
})
51-
})
52-
}
53-
}
54-
5538
pub(crate) trait TraitRefExt {
5639
fn hir_trait_id(&self) -> TraitId;
5740
}

src/tools/rust-analyzer/crates/hir-ty/src/db.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use triomphe::Arc;
1717

1818
use crate::{
1919
Binders, ImplTraitId, ImplTraits, InferenceResult, TraitEnvironment, Ty, TyDefId, ValueTyDefId,
20-
chalk_db,
2120
consteval::ConstEvalError,
2221
dyn_compatibility::DynCompatibilityViolation,
2322
layout::{Layout, LayoutError},
@@ -308,19 +307,13 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
308307
#[salsa::interned]
309308
fn intern_coroutine(&self, id: InternedCoroutine) -> InternedCoroutineId;
310309

311-
#[salsa::invoke(chalk_db::fn_def_variance_query)]
312-
fn fn_def_variance(&self, fn_def_id: CallableDefId) -> chalk_db::Variances;
313-
314-
#[salsa::invoke(chalk_db::adt_variance_query)]
315-
fn adt_variance(&self, adt_id: AdtId) -> chalk_db::Variances;
316-
317310
#[salsa::invoke(crate::variance::variances_of)]
318311
#[salsa::cycle(
319312
// cycle_fn = crate::variance::variances_of_cycle_fn,
320313
// cycle_initial = crate::variance::variances_of_cycle_initial,
321314
cycle_result = crate::variance::variances_of_cycle_initial,
322315
)]
323-
fn variances_of(&self, def: GenericDefId) -> Option<Arc<[crate::variance::Variance]>>;
316+
fn variances_of(&self, def: GenericDefId) -> crate::next_solver::VariancesOf<'_>;
324317

325318
// next trait solver
326319

src/tools/rust-analyzer/crates/hir-ty/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ pub use utils::{
117117
TargetFeatureIsSafeInTarget, Unsafety, all_super_traits, direct_super_traits,
118118
is_fn_unsafe_to_call, target_feature_is_safe_in_target,
119119
};
120-
pub use variance::Variance;
121120

122121
use chalk_ir::{BoundVar, DebruijnIndex, Safety, Scalar};
123122

src/tools/rust-analyzer/crates/hir-ty/src/next_solver/def_id.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl From<DefWithBodyId> for SolverDefId {
155155
}
156156

157157
impl TryFrom<SolverDefId> for GenericDefId {
158-
type Error = SolverDefId;
158+
type Error = ();
159159

160160
fn try_from(value: SolverDefId) -> Result<Self, Self::Error> {
161161
Ok(match value {
@@ -170,7 +170,7 @@ impl TryFrom<SolverDefId> for GenericDefId {
170170
| SolverDefId::InternedCoroutineId(_)
171171
| SolverDefId::InternedOpaqueTyId(_)
172172
| SolverDefId::EnumVariantId(_)
173-
| SolverDefId::Ctor(_) => return Err(value),
173+
| SolverDefId::Ctor(_) => return Err(()),
174174
})
175175
}
176176
}

src/tools/rust-analyzer/crates/hir-ty/src/next_solver/interner.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ macro_rules! _interned_vec_nolifetime_salsa {
8383
($name:ident, $ty:ty) => {
8484
interned_vec_nolifetime_salsa!($name, $ty, nofold);
8585

86-
impl<'db> rustc_type_ir::TypeFoldable<DbInterner<'db>> for $name {
86+
impl<'db> rustc_type_ir::TypeFoldable<DbInterner<'db>> for $name<'db> {
8787
fn try_fold_with<F: rustc_type_ir::FallibleTypeFolder<DbInterner<'db>>>(
8888
self,
8989
folder: &mut F,
@@ -104,7 +104,7 @@ macro_rules! _interned_vec_nolifetime_salsa {
104104
}
105105
}
106106

107-
impl<'db> rustc_type_ir::TypeVisitable<DbInterner<'db>> for $name {
107+
impl<'db> rustc_type_ir::TypeVisitable<DbInterner<'db>> for $name<'db> {
108108
fn visit_with<V: rustc_type_ir::TypeVisitor<DbInterner<'db>>>(
109109
&self,
110110
visitor: &mut V,
@@ -117,14 +117,14 @@ macro_rules! _interned_vec_nolifetime_salsa {
117117
}
118118
};
119119
($name:ident, $ty:ty, nofold) => {
120-
#[salsa::interned(no_lifetime, constructor = new_, debug)]
120+
#[salsa::interned(constructor = new_, debug)]
121121
pub struct $name {
122122
#[returns(ref)]
123123
inner_: smallvec::SmallVec<[$ty; 2]>,
124124
}
125125

126-
impl $name {
127-
pub fn new_from_iter<'db>(
126+
impl<'db> $name<'db> {
127+
pub fn new_from_iter(
128128
interner: DbInterner<'db>,
129129
data: impl IntoIterator<Item = $ty>,
130130
) -> Self {
@@ -140,7 +140,7 @@ macro_rules! _interned_vec_nolifetime_salsa {
140140
}
141141
}
142142

143-
impl rustc_type_ir::inherent::SliceLike for $name {
143+
impl<'db> rustc_type_ir::inherent::SliceLike for $name<'db> {
144144
type Item = $ty;
145145

146146
type IntoIter = <smallvec::SmallVec<[$ty; 2]> as IntoIterator>::IntoIter;
@@ -154,7 +154,7 @@ macro_rules! _interned_vec_nolifetime_salsa {
154154
}
155155
}
156156

157-
impl IntoIterator for $name {
157+
impl<'db> IntoIterator for $name<'db> {
158158
type Item = $ty;
159159
type IntoIter = <Self as rustc_type_ir::inherent::SliceLike>::IntoIter;
160160

@@ -163,7 +163,7 @@ macro_rules! _interned_vec_nolifetime_salsa {
163163
}
164164
}
165165

166-
impl Default for $name {
166+
impl<'db> Default for $name<'db> {
167167
fn default() -> Self {
168168
$name::new_from_iter(DbInterner::conjure(), [])
169169
}
@@ -887,7 +887,7 @@ macro_rules! as_lang_item {
887887
impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
888888
type DefId = SolverDefId;
889889
type LocalDefId = SolverDefId;
890-
type LocalDefIds = SolverDefIds;
890+
type LocalDefIds = SolverDefIds<'db>;
891891
type TraitId = TraitIdWrapper;
892892
type ForeignId = TypeAliasIdWrapper;
893893
type FunctionId = CallableIdWrapper;
@@ -904,7 +904,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
904904

905905
type Term = Term<'db>;
906906

907-
type BoundVarKinds = BoundVarKinds;
907+
type BoundVarKinds = BoundVarKinds<'db>;
908908
type BoundVarKind = BoundVarKind;
909909

910910
type PredefinedOpaques = PredefinedOpaques<'db>;
@@ -977,7 +977,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
977977

978978
type GenericsOf = Generics;
979979

980-
type VariancesOf = VariancesOf;
980+
type VariancesOf = VariancesOf<'db>;
981981

982982
type AdtDef = AdtDef;
983983

@@ -1045,10 +1045,9 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10451045

10461046
fn variances_of(self, def_id: Self::DefId) -> Self::VariancesOf {
10471047
let generic_def = match def_id {
1048-
SolverDefId::FunctionId(def_id) => def_id.into(),
1049-
SolverDefId::AdtId(def_id) => def_id.into(),
1050-
SolverDefId::Ctor(Ctor::Struct(def_id)) => def_id.into(),
1051-
SolverDefId::Ctor(Ctor::Enum(def_id)) => def_id.loc(self.db).parent.into(),
1048+
SolverDefId::Ctor(Ctor::Enum(def_id)) | SolverDefId::EnumVariantId(def_id) => {
1049+
def_id.loc(self.db).parent.into()
1050+
}
10521051
SolverDefId::InternedOpaqueTyId(_def_id) => {
10531052
// FIXME(next-solver): track variances
10541053
//
@@ -1059,17 +1058,20 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10591058
(0..self.generics_of(def_id).count()).map(|_| Variance::Invariant),
10601059
);
10611060
}
1062-
_ => return VariancesOf::new_from_iter(self, []),
1061+
SolverDefId::Ctor(Ctor::Struct(def_id)) => def_id.into(),
1062+
SolverDefId::AdtId(def_id) => def_id.into(),
1063+
SolverDefId::FunctionId(def_id) => def_id.into(),
1064+
SolverDefId::ConstId(_)
1065+
| SolverDefId::StaticId(_)
1066+
| SolverDefId::TraitId(_)
1067+
| SolverDefId::TypeAliasId(_)
1068+
| SolverDefId::ImplId(_)
1069+
| SolverDefId::InternedClosureId(_)
1070+
| SolverDefId::InternedCoroutineId(_) => {
1071+
return VariancesOf::new_from_iter(self, []);
1072+
}
10631073
};
1064-
VariancesOf::new_from_iter(
1065-
self,
1066-
self.db()
1067-
.variances_of(generic_def)
1068-
.as_deref()
1069-
.unwrap_or_default()
1070-
.iter()
1071-
.map(|v| v.to_nextsolver(self)),
1072-
)
1074+
self.db.variances_of(generic_def)
10731075
}
10741076

10751077
fn type_of(self, def_id: Self::DefId) -> EarlyBinder<Self, Self::Ty> {

src/tools/rust-analyzer/crates/hir-ty/src/next_solver/mapping.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -605,16 +605,16 @@ impl<'db, T: NextSolverToChalk<'db, U>, U: HasInterner<Interner = Interner>>
605605
}
606606
}
607607

608-
impl<'db> ChalkToNextSolver<'db, BoundVarKinds> for chalk_ir::VariableKinds<Interner> {
609-
fn to_nextsolver(&self, interner: DbInterner<'db>) -> BoundVarKinds {
608+
impl<'db> ChalkToNextSolver<'db, BoundVarKinds<'db>> for chalk_ir::VariableKinds<Interner> {
609+
fn to_nextsolver(&self, interner: DbInterner<'db>) -> BoundVarKinds<'db> {
610610
BoundVarKinds::new_from_iter(
611611
interner,
612612
self.iter(Interner).map(|v| v.to_nextsolver(interner)),
613613
)
614614
}
615615
}
616616

617-
impl<'db> NextSolverToChalk<'db, chalk_ir::VariableKinds<Interner>> for BoundVarKinds {
617+
impl<'db> NextSolverToChalk<'db, chalk_ir::VariableKinds<Interner>> for BoundVarKinds<'db> {
618618
fn to_chalk(self, interner: DbInterner<'db>) -> chalk_ir::VariableKinds<Interner> {
619619
chalk_ir::VariableKinds::from_iter(Interner, self.iter().map(|v| v.to_chalk(interner)))
620620
}
@@ -763,36 +763,6 @@ impl<'db> ChalkToNextSolver<'db, rustc_ast_ir::Mutability> for chalk_ir::Mutabil
763763
}
764764
}
765765

766-
impl<'db> ChalkToNextSolver<'db, rustc_type_ir::Variance> for crate::Variance {
767-
fn to_nextsolver(&self, interner: DbInterner<'db>) -> rustc_type_ir::Variance {
768-
match self {
769-
crate::Variance::Covariant => rustc_type_ir::Variance::Covariant,
770-
crate::Variance::Invariant => rustc_type_ir::Variance::Invariant,
771-
crate::Variance::Contravariant => rustc_type_ir::Variance::Contravariant,
772-
crate::Variance::Bivariant => rustc_type_ir::Variance::Bivariant,
773-
}
774-
}
775-
}
776-
777-
impl<'db> ChalkToNextSolver<'db, rustc_type_ir::Variance> for chalk_ir::Variance {
778-
fn to_nextsolver(&self, interner: DbInterner<'db>) -> rustc_type_ir::Variance {
779-
match self {
780-
chalk_ir::Variance::Covariant => rustc_type_ir::Variance::Covariant,
781-
chalk_ir::Variance::Invariant => rustc_type_ir::Variance::Invariant,
782-
chalk_ir::Variance::Contravariant => rustc_type_ir::Variance::Contravariant,
783-
}
784-
}
785-
}
786-
787-
impl<'db> ChalkToNextSolver<'db, VariancesOf> for chalk_ir::Variances<Interner> {
788-
fn to_nextsolver(&self, interner: DbInterner<'db>) -> VariancesOf {
789-
VariancesOf::new_from_iter(
790-
interner,
791-
self.as_slice(Interner).iter().map(|v| v.to_nextsolver(interner)),
792-
)
793-
}
794-
}
795-
796766
impl<'db> ChalkToNextSolver<'db, Goal<DbInterner<'db>, Predicate<'db>>>
797767
for chalk_ir::InEnvironment<chalk_ir::Goal<Interner>>
798768
{

0 commit comments

Comments
 (0)