diff options
Diffstat (limited to 'crates/hir_ty/src/traits/chalk.rs')
-rw-r--r-- | crates/hir_ty/src/traits/chalk.rs | 75 |
1 files changed, 38 insertions, 37 deletions
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 011bef6f6..dff87ef70 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs | |||
@@ -3,7 +3,7 @@ use std::sync::Arc; | |||
3 | 3 | ||
4 | use log::debug; | 4 | use log::debug; |
5 | 5 | ||
6 | use chalk_ir::{fold::shift::Shift, CanonicalVarKinds, GenericArg}; | 6 | use chalk_ir::{fold::shift::Shift, CanonicalVarKinds}; |
7 | use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; | 7 | use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; |
8 | 8 | ||
9 | use base_db::{salsa::InternKey, CrateId}; | 9 | use base_db::{salsa::InternKey, CrateId}; |
@@ -22,7 +22,7 @@ use crate::{ | |||
22 | to_assoc_type_id, to_chalk_trait_id, | 22 | to_assoc_type_id, to_chalk_trait_id, |
23 | utils::generics, | 23 | utils::generics, |
24 | AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution, | 24 | AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution, |
25 | TraitRef, Ty, TyKind, WhereClause, | 25 | TraitRef, Ty, TyBuilder, TyKind, WhereClause, |
26 | }; | 26 | }; |
27 | use mapping::{ | 27 | use mapping::{ |
28 | convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, | 28 | convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, |
@@ -80,7 +80,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
80 | fn impls_for_trait( | 80 | fn impls_for_trait( |
81 | &self, | 81 | &self, |
82 | trait_id: TraitId, | 82 | trait_id: TraitId, |
83 | parameters: &[GenericArg<Interner>], | 83 | parameters: &[chalk_ir::GenericArg<Interner>], |
84 | binders: &CanonicalVarKinds<Interner>, | 84 | binders: &CanonicalVarKinds<Interner>, |
85 | ) -> Vec<ImplId> { | 85 | ) -> Vec<ImplId> { |
86 | debug!("impls_for_trait {:?}", trait_id); | 86 | debug!("impls_for_trait {:?}", trait_id); |
@@ -92,7 +92,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
92 | ty: &Ty, | 92 | ty: &Ty, |
93 | binders: &CanonicalVarKinds<Interner>, | 93 | binders: &CanonicalVarKinds<Interner>, |
94 | ) -> Option<chalk_ir::TyVariableKind> { | 94 | ) -> Option<chalk_ir::TyVariableKind> { |
95 | if let TyKind::BoundVar(bv) = ty.interned(&Interner) { | 95 | if let TyKind::BoundVar(bv) = ty.kind(&Interner) { |
96 | let binders = binders.as_slice(&Interner); | 96 | let binders = binders.as_slice(&Interner); |
97 | if bv.debruijn == DebruijnIndex::INNERMOST { | 97 | if bv.debruijn == DebruijnIndex::INNERMOST { |
98 | if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind { | 98 | if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind { |
@@ -184,16 +184,21 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
184 | .db | 184 | .db |
185 | .return_type_impl_traits(func) | 185 | .return_type_impl_traits(func) |
186 | .expect("impl trait id without impl traits"); | 186 | .expect("impl trait id without impl traits"); |
187 | let data = &datas.value.impl_traits[idx as usize]; | 187 | let (datas, binders) = (*datas).as_ref().into_value_and_skipped_binders(); |
188 | let data = &datas.impl_traits[idx as usize]; | ||
188 | let bound = OpaqueTyDatumBound { | 189 | let bound = OpaqueTyDatumBound { |
189 | bounds: make_binders( | 190 | bounds: make_binders( |
190 | data.bounds.value.iter().cloned().map(|b| b.to_chalk(self.db)).collect(), | 191 | data.bounds |
192 | .skip_binders() | ||
193 | .iter() | ||
194 | .cloned() | ||
195 | .map(|b| b.to_chalk(self.db)) | ||
196 | .collect(), | ||
191 | 1, | 197 | 1, |
192 | ), | 198 | ), |
193 | where_clauses: make_binders(vec![], 0), | 199 | where_clauses: make_binders(vec![], 0), |
194 | }; | 200 | }; |
195 | let num_vars = datas.num_binders; | 201 | chalk_ir::Binders::new(binders, bound) |
196 | make_binders(bound, num_vars) | ||
197 | } | 202 | } |
198 | crate::ImplTraitId::AsyncBlockTypeImplTrait(..) => { | 203 | crate::ImplTraitId::AsyncBlockTypeImplTrait(..) => { |
199 | if let Some((future_trait, future_output)) = self | 204 | if let Some((future_trait, future_output)) = self |
@@ -265,7 +270,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
265 | 270 | ||
266 | fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> { | 271 | fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> { |
267 | // FIXME: actually provide the hidden type; it is relevant for auto traits | 272 | // FIXME: actually provide the hidden type; it is relevant for auto traits |
268 | TyKind::Unknown.intern(&Interner).to_chalk(self.db) | 273 | TyKind::Error.intern(&Interner).to_chalk(self.db) |
269 | } | 274 | } |
270 | 275 | ||
271 | fn is_object_safe(&self, _trait_id: chalk_ir::TraitId<Interner>) -> bool { | 276 | fn is_object_safe(&self, _trait_id: chalk_ir::TraitId<Interner>) -> bool { |
@@ -300,7 +305,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
300 | _closure_id: chalk_ir::ClosureId<Interner>, | 305 | _closure_id: chalk_ir::ClosureId<Interner>, |
301 | _substs: &chalk_ir::Substitution<Interner>, | 306 | _substs: &chalk_ir::Substitution<Interner>, |
302 | ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { | 307 | ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { |
303 | let ty = Ty::unit().to_chalk(self.db); | 308 | let ty = TyBuilder::unit().to_chalk(self.db); |
304 | make_binders(ty, 0) | 309 | make_binders(ty, 0) |
305 | } | 310 | } |
306 | fn closure_fn_substitution( | 311 | fn closure_fn_substitution( |
@@ -308,7 +313,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
308 | _closure_id: chalk_ir::ClosureId<Interner>, | 313 | _closure_id: chalk_ir::ClosureId<Interner>, |
309 | _substs: &chalk_ir::Substitution<Interner>, | 314 | _substs: &chalk_ir::Substitution<Interner>, |
310 | ) -> chalk_ir::Substitution<Interner> { | 315 | ) -> chalk_ir::Substitution<Interner> { |
311 | Substitution::empty().to_chalk(self.db) | 316 | Substitution::empty(&Interner).to_chalk(self.db) |
312 | } | 317 | } |
313 | 318 | ||
314 | fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String { | 319 | fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String { |
@@ -387,7 +392,7 @@ pub(crate) fn associated_ty_data_query( | |||
387 | // Lower bounds -- we could/should maybe move this to a separate query in `lower` | 392 | // Lower bounds -- we could/should maybe move this to a separate query in `lower` |
388 | let type_alias_data = db.type_alias_data(type_alias); | 393 | let type_alias_data = db.type_alias_data(type_alias); |
389 | let generic_params = generics(db.upcast(), type_alias.into()); | 394 | let generic_params = generics(db.upcast(), type_alias.into()); |
390 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 395 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
391 | let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); | 396 | let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); |
392 | let ctx = crate::TyLoweringContext::new(db, &resolver) | 397 | let ctx = crate::TyLoweringContext::new(db, &resolver) |
393 | .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); | 398 | .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); |
@@ -421,7 +426,7 @@ pub(crate) fn trait_datum_query( | |||
421 | let trait_data = db.trait_data(trait_); | 426 | let trait_data = db.trait_data(trait_); |
422 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); | 427 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); |
423 | let generic_params = generics(db.upcast(), trait_.into()); | 428 | let generic_params = generics(db.upcast(), trait_.into()); |
424 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 429 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
425 | let flags = rust_ir::TraitFlags { | 430 | let flags = rust_ir::TraitFlags { |
426 | auto: trait_data.is_auto, | 431 | auto: trait_data.is_auto, |
427 | upstream: trait_.lookup(db.upcast()).container.krate() != krate, | 432 | upstream: trait_.lookup(db.upcast()).container.krate() != krate, |
@@ -439,7 +444,7 @@ pub(crate) fn trait_datum_query( | |||
439 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); | 444 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); |
440 | let trait_datum = TraitDatum { | 445 | let trait_datum = TraitDatum { |
441 | id: trait_id, | 446 | id: trait_id, |
442 | binders: make_binders(trait_datum_bound, bound_vars.len()), | 447 | binders: make_binders(trait_datum_bound, bound_vars.len(&Interner)), |
443 | flags, | 448 | flags, |
444 | associated_ty_ids, | 449 | associated_ty_ids, |
445 | well_known, | 450 | well_known, |
@@ -490,7 +495,7 @@ pub(crate) fn struct_datum_query( | |||
490 | let upstream = adt_id.module(db.upcast()).krate() != krate; | 495 | let upstream = adt_id.module(db.upcast()).krate() != krate; |
491 | let where_clauses = { | 496 | let where_clauses = { |
492 | let generic_params = generics(db.upcast(), adt_id.into()); | 497 | let generic_params = generics(db.upcast(), adt_id.into()); |
493 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 498 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
494 | convert_where_clauses(db, adt_id.into(), &bound_vars) | 499 | convert_where_clauses(db, adt_id.into(), &bound_vars) |
495 | }; | 500 | }; |
496 | let flags = rust_ir::AdtFlags { | 501 | let flags = rust_ir::AdtFlags { |
@@ -535,11 +540,12 @@ fn impl_def_datum( | |||
535 | .impl_trait(impl_id) | 540 | .impl_trait(impl_id) |
536 | // ImplIds for impls where the trait ref can't be resolved should never reach Chalk | 541 | // ImplIds for impls where the trait ref can't be resolved should never reach Chalk |
537 | .expect("invalid impl passed to Chalk") | 542 | .expect("invalid impl passed to Chalk") |
538 | .value; | 543 | .into_value_and_skipped_binders() |
544 | .0; | ||
539 | let impl_data = db.impl_data(impl_id); | 545 | let impl_data = db.impl_data(impl_id); |
540 | 546 | ||
541 | let generic_params = generics(db.upcast(), impl_id.into()); | 547 | let generic_params = generics(db.upcast(), impl_id.into()); |
542 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 548 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
543 | let trait_ = trait_ref.hir_trait_id(); | 549 | let trait_ = trait_ref.hir_trait_id(); |
544 | let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate { | 550 | let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate { |
545 | rust_ir::ImplType::Local | 551 | rust_ir::ImplType::Local |
@@ -577,7 +583,7 @@ fn impl_def_datum( | |||
577 | .collect(); | 583 | .collect(); |
578 | debug!("impl_datum: {:?}", impl_datum_bound); | 584 | debug!("impl_datum: {:?}", impl_datum_bound); |
579 | let impl_datum = ImplDatum { | 585 | let impl_datum = ImplDatum { |
580 | binders: make_binders(impl_datum_bound, bound_vars.len()), | 586 | binders: make_binders(impl_datum_bound, bound_vars.len(&Interner)), |
581 | impl_type, | 587 | impl_type, |
582 | polarity, | 588 | polarity, |
583 | associated_ty_value_ids, | 589 | associated_ty_value_ids, |
@@ -605,18 +611,22 @@ fn type_alias_associated_ty_value( | |||
605 | _ => panic!("assoc ty value should be in impl"), | 611 | _ => panic!("assoc ty value should be in impl"), |
606 | }; | 612 | }; |
607 | 613 | ||
608 | let trait_ref = db.impl_trait(impl_id).expect("assoc ty value should not exist").value; // we don't return any assoc ty values if the impl'd trait can't be resolved | 614 | let trait_ref = db |
615 | .impl_trait(impl_id) | ||
616 | .expect("assoc ty value should not exist") | ||
617 | .into_value_and_skipped_binders() | ||
618 | .0; // we don't return any assoc ty values if the impl'd trait can't be resolved | ||
609 | 619 | ||
610 | let assoc_ty = db | 620 | let assoc_ty = db |
611 | .trait_data(trait_ref.hir_trait_id()) | 621 | .trait_data(trait_ref.hir_trait_id()) |
612 | .associated_type_by_name(&type_alias_data.name) | 622 | .associated_type_by_name(&type_alias_data.name) |
613 | .expect("assoc ty value should not exist"); // validated when building the impl data as well | 623 | .expect("assoc ty value should not exist"); // validated when building the impl data as well |
614 | let ty = db.ty(type_alias.into()); | 624 | let (ty, binders) = db.ty(type_alias.into()).into_value_and_skipped_binders(); |
615 | let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; | 625 | let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) }; |
616 | let value = rust_ir::AssociatedTyValue { | 626 | let value = rust_ir::AssociatedTyValue { |
617 | impl_id: impl_id.to_chalk(db), | 627 | impl_id: impl_id.to_chalk(db), |
618 | associated_ty_id: to_assoc_type_id(assoc_ty), | 628 | associated_ty_id: to_assoc_type_id(assoc_ty), |
619 | value: make_binders(value_bound, ty.num_binders), | 629 | value: chalk_ir::Binders::new(binders, value_bound), |
620 | }; | 630 | }; |
621 | Arc::new(value) | 631 | Arc::new(value) |
622 | } | 632 | } |
@@ -628,20 +638,15 @@ pub(crate) fn fn_def_datum_query( | |||
628 | ) -> Arc<FnDefDatum> { | 638 | ) -> Arc<FnDefDatum> { |
629 | let callable_def: CallableDefId = from_chalk(db, fn_def_id); | 639 | let callable_def: CallableDefId = from_chalk(db, fn_def_id); |
630 | let generic_params = generics(db.upcast(), callable_def.into()); | 640 | let generic_params = generics(db.upcast(), callable_def.into()); |
631 | let sig = db.callable_item_signature(callable_def); | 641 | let (sig, binders) = db.callable_item_signature(callable_def).into_value_and_skipped_binders(); |
632 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 642 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
633 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); | 643 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); |
634 | let bound = rust_ir::FnDefDatumBound { | 644 | let bound = rust_ir::FnDefDatumBound { |
635 | // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway | 645 | // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway |
636 | inputs_and_output: make_binders( | 646 | inputs_and_output: make_binders( |
637 | rust_ir::FnDefInputsAndOutputDatum { | 647 | rust_ir::FnDefInputsAndOutputDatum { |
638 | argument_types: sig | 648 | argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(db)).collect(), |
639 | .value | 649 | return_type: sig.ret().clone().to_chalk(db), |
640 | .params() | ||
641 | .iter() | ||
642 | .map(|ty| ty.clone().to_chalk(db)) | ||
643 | .collect(), | ||
644 | return_type: sig.value.ret().clone().to_chalk(db), | ||
645 | } | 650 | } |
646 | .shifted_in(&Interner), | 651 | .shifted_in(&Interner), |
647 | 0, | 652 | 0, |
@@ -650,12 +655,8 @@ pub(crate) fn fn_def_datum_query( | |||
650 | }; | 655 | }; |
651 | let datum = FnDefDatum { | 656 | let datum = FnDefDatum { |
652 | id: fn_def_id, | 657 | id: fn_def_id, |
653 | sig: chalk_ir::FnSig { | 658 | sig: chalk_ir::FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: sig.is_varargs }, |
654 | abi: (), | 659 | binders: chalk_ir::Binders::new(binders, bound), |
655 | safety: chalk_ir::Safety::Safe, | ||
656 | variadic: sig.value.is_varargs, | ||
657 | }, | ||
658 | binders: make_binders(bound, sig.num_binders), | ||
659 | }; | 660 | }; |
660 | Arc::new(datum) | 661 | Arc::new(datum) |
661 | } | 662 | } |