aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/traits/chalk.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/traits/chalk.rs')
-rw-r--r--crates/ra_hir_ty/src/traits/chalk.rs160
1 files changed, 123 insertions, 37 deletions
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs
index 943d5f125..1bc0f0713 100644
--- a/crates/ra_hir_ty/src/traits/chalk.rs
+++ b/crates/ra_hir_ty/src/traits/chalk.rs
@@ -3,7 +3,10 @@ use std::{fmt, sync::Arc};
3 3
4use log::debug; 4use log::debug;
5 5
6use chalk_ir::{cast::Cast, Goal, GoalData, Parameter, PlaceholderIndex, TypeName, UniverseIndex}; 6use chalk_ir::{
7 cast::Cast, fold::shift::Shift, Goal, GoalData, Parameter, PlaceholderIndex, TypeName,
8 UniverseIndex,
9};
7 10
8use hir_def::{AssocContainerId, AssocItemId, GenericDefId, HasModule, Lookup, TypeAliasId}; 11use hir_def::{AssocContainerId, AssocItemId, GenericDefId, HasModule, Lookup, TypeAliasId};
9use ra_db::{ 12use ra_db::{
@@ -59,53 +62,126 @@ impl chalk_ir::interner::Interner for Interner {
59 None 62 None
60 } 63 }
61 64
65 fn debug_ty(_ty: &chalk_ir::Ty<Self>, _fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
66 None
67 }
68
69 fn debug_lifetime(
70 _lifetime: &chalk_ir::Lifetime<Self>,
71 _fmt: &mut fmt::Formatter<'_>,
72 ) -> Option<fmt::Result> {
73 None
74 }
75
76 fn debug_parameter(
77 _parameter: &Parameter<Self>,
78 _fmt: &mut fmt::Formatter<'_>,
79 ) -> Option<fmt::Result> {
80 None
81 }
82
83 fn debug_goal(_goal: &Goal<Self>, _fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
84 None
85 }
86
87 fn debug_goals(
88 _goals: &chalk_ir::Goals<Self>,
89 _fmt: &mut fmt::Formatter<'_>,
90 ) -> Option<fmt::Result> {
91 None
92 }
93
94 fn debug_program_clause_implication(
95 _pci: &chalk_ir::ProgramClauseImplication<Self>,
96 _fmt: &mut fmt::Formatter<'_>,
97 ) -> Option<fmt::Result> {
98 None
99 }
100
101 fn debug_application_ty(
102 _application_ty: &chalk_ir::ApplicationTy<Self>,
103 _fmt: &mut fmt::Formatter<'_>,
104 ) -> Option<fmt::Result> {
105 None
106 }
107
108 fn debug_substitution(
109 _substitution: &chalk_ir::Substitution<Self>,
110 _fmt: &mut fmt::Formatter<'_>,
111 ) -> Option<fmt::Result> {
112 None
113 }
114
115 fn debug_separator_trait_ref(
116 _separator_trait_ref: &chalk_ir::SeparatorTraitRef<Self>,
117 _fmt: &mut fmt::Formatter<'_>,
118 ) -> Option<fmt::Result> {
119 None
120 }
121
62 fn intern_ty(&self, ty: chalk_ir::TyData<Self>) -> Box<chalk_ir::TyData<Self>> { 122 fn intern_ty(&self, ty: chalk_ir::TyData<Self>) -> Box<chalk_ir::TyData<Self>> {
63 Box::new(ty) 123 Box::new(ty)
64 } 124 }
65 125
66 fn ty_data(ty: &Box<chalk_ir::TyData<Self>>) -> &chalk_ir::TyData<Self> { 126 fn ty_data<'a>(&self, ty: &'a Box<chalk_ir::TyData<Self>>) -> &'a chalk_ir::TyData<Self> {
67 ty 127 ty
68 } 128 }
69 129
70 fn intern_lifetime(lifetime: chalk_ir::LifetimeData<Self>) -> chalk_ir::LifetimeData<Self> { 130 fn intern_lifetime(
131 &self,
132 lifetime: chalk_ir::LifetimeData<Self>,
133 ) -> chalk_ir::LifetimeData<Self> {
71 lifetime 134 lifetime
72 } 135 }
73 136
74 fn lifetime_data(lifetime: &chalk_ir::LifetimeData<Self>) -> &chalk_ir::LifetimeData<Self> { 137 fn lifetime_data<'a>(
138 &self,
139 lifetime: &'a chalk_ir::LifetimeData<Self>,
140 ) -> &'a chalk_ir::LifetimeData<Self> {
75 lifetime 141 lifetime
76 } 142 }
77 143
78 fn intern_parameter(parameter: chalk_ir::ParameterData<Self>) -> chalk_ir::ParameterData<Self> { 144 fn intern_parameter(
145 &self,
146 parameter: chalk_ir::ParameterData<Self>,
147 ) -> chalk_ir::ParameterData<Self> {
79 parameter 148 parameter
80 } 149 }
81 150
82 fn parameter_data(parameter: &chalk_ir::ParameterData<Self>) -> &chalk_ir::ParameterData<Self> { 151 fn parameter_data<'a>(
152 &self,
153 parameter: &'a chalk_ir::ParameterData<Self>,
154 ) -> &'a chalk_ir::ParameterData<Self> {
83 parameter 155 parameter
84 } 156 }
85 157
86 fn intern_goal(goal: GoalData<Self>) -> Arc<GoalData<Self>> { 158 fn intern_goal(&self, goal: GoalData<Self>) -> Arc<GoalData<Self>> {
87 Arc::new(goal) 159 Arc::new(goal)
88 } 160 }
89 161
90 fn intern_goals(data: impl IntoIterator<Item = Goal<Self>>) -> Self::InternedGoals { 162 fn intern_goals(&self, data: impl IntoIterator<Item = Goal<Self>>) -> Self::InternedGoals {
91 data.into_iter().collect() 163 data.into_iter().collect()
92 } 164 }
93 165
94 fn goal_data(goal: &Arc<GoalData<Self>>) -> &GoalData<Self> { 166 fn goal_data<'a>(&self, goal: &'a Arc<GoalData<Self>>) -> &'a GoalData<Self> {
95 goal 167 goal
96 } 168 }
97 169
98 fn goals_data(goals: &Vec<Goal<Interner>>) -> &[Goal<Interner>] { 170 fn goals_data<'a>(&self, goals: &'a Vec<Goal<Interner>>) -> &'a [Goal<Interner>] {
99 goals 171 goals
100 } 172 }
101 173
102 fn intern_substitution<E>( 174 fn intern_substitution<E>(
175 &self,
103 data: impl IntoIterator<Item = Result<Parameter<Self>, E>>, 176 data: impl IntoIterator<Item = Result<Parameter<Self>, E>>,
104 ) -> Result<Vec<Parameter<Self>>, E> { 177 ) -> Result<Vec<Parameter<Self>>, E> {
105 data.into_iter().collect() 178 data.into_iter().collect()
106 } 179 }
107 180
108 fn substitution_data(substitution: &Vec<Parameter<Self>>) -> &[Parameter<Self>] { 181 fn substitution_data<'a>(
182 &self,
183 substitution: &'a Vec<Parameter<Self>>,
184 ) -> &'a [Parameter<Self>] {
109 substitution 185 substitution
110 } 186 }
111} 187}
@@ -145,12 +221,14 @@ impl ToChalk for Ty {
145 Ty::Apply(apply_ty) => { 221 Ty::Apply(apply_ty) => {
146 let name = apply_ty.ctor.to_chalk(db); 222 let name = apply_ty.ctor.to_chalk(db);
147 let substitution = apply_ty.parameters.to_chalk(db); 223 let substitution = apply_ty.parameters.to_chalk(db);
148 chalk_ir::ApplicationTy { name, substitution }.cast().intern(&Interner) 224 chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner)
149 } 225 }
150 Ty::Projection(proj_ty) => { 226 Ty::Projection(proj_ty) => {
151 let associated_ty_id = proj_ty.associated_ty.to_chalk(db); 227 let associated_ty_id = proj_ty.associated_ty.to_chalk(db);
152 let substitution = proj_ty.parameters.to_chalk(db); 228 let substitution = proj_ty.parameters.to_chalk(db);
153 chalk_ir::AliasTy { associated_ty_id, substitution }.cast().intern(&Interner) 229 chalk_ir::AliasTy { associated_ty_id, substitution }
230 .cast(&Interner)
231 .intern(&Interner)
154 } 232 }
155 Ty::Placeholder(id) => { 233 Ty::Placeholder(id) => {
156 let interned_id = db.intern_type_param_id(id); 234 let interned_id = db.intern_type_param_id(id);
@@ -160,7 +238,7 @@ impl ToChalk for Ty {
160 } 238 }
161 .to_ty::<Interner>(&Interner) 239 .to_ty::<Interner>(&Interner)
162 } 240 }
163 Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx as usize).intern(&Interner), 241 Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner),
164 Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), 242 Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"),
165 Ty::Dyn(predicates) => { 243 Ty::Dyn(predicates) => {
166 let where_clauses = predicates 244 let where_clauses = predicates
@@ -173,14 +251,14 @@ impl ToChalk for Ty {
173 chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner) 251 chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner)
174 } 252 }
175 Ty::Opaque(_) | Ty::Unknown => { 253 Ty::Opaque(_) | Ty::Unknown => {
176 let substitution = chalk_ir::Substitution::empty(); 254 let substitution = chalk_ir::Substitution::empty(&Interner);
177 let name = TypeName::Error; 255 let name = TypeName::Error;
178 chalk_ir::ApplicationTy { name, substitution }.cast().intern(&Interner) 256 chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner)
179 } 257 }
180 } 258 }
181 } 259 }
182 fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { 260 fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
183 match chalk.data().clone() { 261 match chalk.data(&Interner).clone() {
184 chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name { 262 chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name {
185 TypeName::Error => Ty::Unknown, 263 TypeName::Error => Ty::Unknown,
186 _ => { 264 _ => {
@@ -202,7 +280,7 @@ impl ToChalk for Ty {
202 Ty::Projection(ProjectionTy { associated_ty, parameters }) 280 Ty::Projection(ProjectionTy { associated_ty, parameters })
203 } 281 }
204 chalk_ir::TyData::Function(_) => unimplemented!(), 282 chalk_ir::TyData::Function(_) => unimplemented!(),
205 chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx as u32), 283 chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx),
206 chalk_ir::TyData::InferenceVar(_iv) => Ty::Unknown, 284 chalk_ir::TyData::InferenceVar(_iv) => Ty::Unknown,
207 chalk_ir::TyData::Dyn(where_clauses) => { 285 chalk_ir::TyData::Dyn(where_clauses) => {
208 assert_eq!(where_clauses.bounds.binders.len(), 1); 286 assert_eq!(where_clauses.bounds.binders.len(), 1);
@@ -218,13 +296,13 @@ impl ToChalk for Substs {
218 type Chalk = chalk_ir::Substitution<Interner>; 296 type Chalk = chalk_ir::Substitution<Interner>;
219 297
220 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> { 298 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> {
221 chalk_ir::Substitution::from(self.iter().map(|ty| ty.clone().to_chalk(db))) 299 chalk_ir::Substitution::from(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db)))
222 } 300 }
223 301
224 fn from_chalk(db: &dyn HirDatabase, parameters: chalk_ir::Substitution<Interner>) -> Substs { 302 fn from_chalk(db: &dyn HirDatabase, parameters: chalk_ir::Substitution<Interner>) -> Substs {
225 let tys = parameters 303 let tys = parameters
226 .into_iter() 304 .iter(&Interner)
227 .map(|p| match p.ty() { 305 .map(|p| match p.ty(&Interner) {
228 Some(ty) => from_chalk(db, ty.clone()), 306 Some(ty) => from_chalk(db, ty.clone()),
229 None => unimplemented!(), 307 None => unimplemented!(),
230 }) 308 })
@@ -332,15 +410,15 @@ impl ToChalk for GenericPredicate {
332 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::QuantifiedWhereClause<Interner> { 410 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::QuantifiedWhereClause<Interner> {
333 match self { 411 match self {
334 GenericPredicate::Implemented(trait_ref) => { 412 GenericPredicate::Implemented(trait_ref) => {
335 make_binders(chalk_ir::WhereClause::Implemented(trait_ref.to_chalk(db)), 0) 413 let chalk_trait_ref = trait_ref.to_chalk(db);
414 let chalk_trait_ref = chalk_trait_ref.shifted_in(&Interner);
415 make_binders(chalk_ir::WhereClause::Implemented(chalk_trait_ref), 0)
416 }
417 GenericPredicate::Projection(projection_pred) => {
418 let ty = projection_pred.ty.to_chalk(db).shifted_in(&Interner);
419 let alias = projection_pred.projection_ty.to_chalk(db).shifted_in(&Interner);
420 make_binders(chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { alias, ty }), 0)
336 } 421 }
337 GenericPredicate::Projection(projection_pred) => make_binders(
338 chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq {
339 alias: projection_pred.projection_ty.to_chalk(db),
340 ty: projection_pred.ty.to_chalk(db),
341 }),
342 0,
343 ),
344 GenericPredicate::Error => panic!("tried passing GenericPredicate::Error to Chalk"), 422 GenericPredicate::Error => panic!("tried passing GenericPredicate::Error to Chalk"),
345 } 423 }
346 } 424 }
@@ -349,7 +427,12 @@ impl ToChalk for GenericPredicate {
349 db: &dyn HirDatabase, 427 db: &dyn HirDatabase,
350 where_clause: chalk_ir::QuantifiedWhereClause<Interner>, 428 where_clause: chalk_ir::QuantifiedWhereClause<Interner>,
351 ) -> GenericPredicate { 429 ) -> GenericPredicate {
352 match where_clause.value { 430 // we don't produce any where clauses with binders and can't currently deal with them
431 match where_clause
432 .value
433 .shifted_out(&Interner)
434 .expect("unexpected bound vars in where clause")
435 {
353 chalk_ir::WhereClause::Implemented(tr) => { 436 chalk_ir::WhereClause::Implemented(tr) => {
354 GenericPredicate::Implemented(from_chalk(db, tr)) 437 GenericPredicate::Implemented(from_chalk(db, tr))
355 } 438 }
@@ -400,8 +483,8 @@ impl ToChalk for Obligation {
400 483
401 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::DomainGoal<Interner> { 484 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::DomainGoal<Interner> {
402 match self { 485 match self {
403 Obligation::Trait(tr) => tr.to_chalk(db).cast(), 486 Obligation::Trait(tr) => tr.to_chalk(db).cast(&Interner),
404 Obligation::Projection(pr) => pr.to_chalk(db).cast(), 487 Obligation::Projection(pr) => pr.to_chalk(db).cast(&Interner),
405 } 488 }
406 } 489 }
407 490
@@ -438,8 +521,8 @@ impl ToChalk for Arc<super::TraitEnvironment> {
438 continue; 521 continue;
439 } 522 }
440 let program_clause: chalk_ir::ProgramClause<Interner> = 523 let program_clause: chalk_ir::ProgramClause<Interner> =
441 pred.clone().to_chalk(db).cast(); 524 pred.clone().to_chalk(db).cast(&Interner);
442 clauses.push(program_clause.into_from_env_clause()); 525 clauses.push(program_clause.into_from_env_clause(&Interner));
443 } 526 }
444 chalk_ir::Environment::new().add_clauses(clauses) 527 chalk_ir::Environment::new().add_clauses(clauses)
445 } 528 }
@@ -504,7 +587,8 @@ impl ToChalk for builtin::BuiltinImplAssocTyValueData {
504 type Chalk = AssociatedTyValue; 587 type Chalk = AssociatedTyValue;
505 588
506 fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { 589 fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue {
507 let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: self.value.to_chalk(db) }; 590 let ty = self.value.to_chalk(db);
591 let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty };
508 592
509 chalk_rust_ir::AssociatedTyValue { 593 chalk_rust_ir::AssociatedTyValue {
510 associated_ty_id: self.assoc_ty_id.to_chalk(db), 594 associated_ty_id: self.assoc_ty_id.to_chalk(db),
@@ -578,9 +662,9 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
578 .map(|impl_| impl_.to_chalk(self.db)) 662 .map(|impl_| impl_.to_chalk(self.db))
579 .collect(); 663 .collect();
580 664
581 let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref().clone()); 665 let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone());
582 let arg: Option<Ty> = 666 let arg: Option<Ty> =
583 parameters.get(1).map(|p| from_chalk(self.db, p.assert_ty_ref().clone())); 667 parameters.get(1).map(|p| from_chalk(self.db, p.assert_ty_ref(&Interner).clone()));
584 668
585 builtin::get_builtin_impls(self.db, self.krate, &ty, &arg, trait_, |i| { 669 builtin::get_builtin_impls(self.db, self.krate, &ty, &arg, trait_, |i| {
586 result.push(i.to_chalk(self.db)) 670 result.push(i.to_chalk(self.db))
@@ -663,11 +747,13 @@ pub(crate) fn trait_datum_query(
663 let associated_ty_ids = 747 let associated_ty_ids =
664 trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect(); 748 trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect();
665 let trait_datum_bound = chalk_rust_ir::TraitDatumBound { where_clauses }; 749 let trait_datum_bound = chalk_rust_ir::TraitDatumBound { where_clauses };
750 let well_known = None; // FIXME set this (depending on lang items)
666 let trait_datum = TraitDatum { 751 let trait_datum = TraitDatum {
667 id: trait_id, 752 id: trait_id,
668 binders: make_binders(trait_datum_bound, bound_vars.len()), 753 binders: make_binders(trait_datum_bound, bound_vars.len()),
669 flags, 754 flags,
670 associated_ty_ids, 755 associated_ty_ids,
756 well_known,
671 }; 757 };
672 Arc::new(trait_datum) 758 Arc::new(trait_datum)
673} 759}