From 69714d36e6617800f3edea174f5d6f76c985ad4c Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 17:13:50 +0200 Subject: Hide Binders internals more --- crates/hir_ty/src/lib.rs | 44 +++++++++++--------------------------------- 1 file changed, 11 insertions(+), 33 deletions(-) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index adfdcaa37..61b5cf269 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -118,49 +118,27 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option { } impl Binders { - pub fn new(num_binders: usize, value: T) -> Self { - Self { num_binders, value } - } - pub fn wrap_empty(value: T) -> Self where T: TypeWalk, { - Self { num_binders: 0, value: value.shift_bound_vars(DebruijnIndex::ONE) } - } - - pub fn as_ref(&self) -> Binders<&T> { - Binders { num_binders: self.num_binders, value: &self.value } - } - - pub fn map(self, f: impl FnOnce(T) -> U) -> Binders { - Binders { num_binders: self.num_binders, value: f(self.value) } - } - - pub fn filter_map(self, f: impl FnOnce(T) -> Option) -> Option> { - Some(Binders { num_binders: self.num_binders, value: f(self.value)? }) - } - - pub fn skip_binders(&self) -> &T { - &self.value - } - - pub fn into_value_and_skipped_binders(self) -> (T, usize) { - (self.value, self.num_binders) + Binders::empty(&Interner, value.shift_bound_vars(DebruijnIndex::ONE)) } } impl Binders<&T> { pub fn cloned(&self) -> Binders { - Binders { num_binders: self.num_binders, value: self.value.clone() } + let (value, binders) = self.into_value_and_skipped_binders(); + Binders::new(binders, value.clone()) } } impl Binders { /// Substitutes all variables. pub fn subst(self, subst: &Substitution) -> T { - assert_eq!(subst.len(&Interner), self.num_binders); - self.value.subst_bound_vars(subst) + let (value, binders) = self.into_value_and_skipped_binders(); + assert_eq!(subst.len(&Interner), binders); + value.subst_bound_vars(subst) } } @@ -334,12 +312,12 @@ impl Ty { /// If this is a `dyn Trait` type, this returns the `Trait` part. fn dyn_trait_ref(&self) -> Option<&TraitRef> { match self.kind(&Interner) { - TyKind::Dyn(dyn_ty) => { - dyn_ty.bounds.value.interned().get(0).and_then(|b| match b.skip_binders() { + TyKind::Dyn(dyn_ty) => dyn_ty.bounds.skip_binders().interned().get(0).and_then(|b| { + match b.skip_binders() { WhereClause::Implemented(trait_ref) => Some(trait_ref), _ => None, - }) - } + } + }), _ => None, } } @@ -459,7 +437,7 @@ impl Ty { ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(), }; - predicates.map(|it| it.value) + predicates.map(|it| it.into_value_and_skipped_binders().0) } TyKind::Placeholder(idx) => { let id = from_placeholder_idx(db, *idx); -- cgit v1.2.3 From ad20f00844cec9c794e34869be163673ebbed182 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 17:45:18 +0200 Subject: Use VariableKinds in Binders --- crates/hir_ty/src/lib.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 61b5cf269..86973c7ed 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -66,6 +66,8 @@ pub type ClosureId = chalk_ir::ClosureId; pub type OpaqueTyId = chalk_ir::OpaqueTyId; pub type PlaceholderIndex = chalk_ir::PlaceholderIndex; +pub type VariableKind = chalk_ir::VariableKind; +pub type VariableKinds = chalk_ir::VariableKinds; pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds; pub type ChalkTraitId = chalk_ir::TraitId; @@ -126,22 +128,26 @@ impl Binders { } } -impl Binders<&T> { - pub fn cloned(&self) -> Binders { - let (value, binders) = self.into_value_and_skipped_binders(); - Binders::new(binders, value.clone()) - } -} - impl Binders { /// Substitutes all variables. pub fn subst(self, subst: &Substitution) -> T { let (value, binders) = self.into_value_and_skipped_binders(); - assert_eq!(subst.len(&Interner), binders); + assert_eq!(subst.len(&Interner), binders.len(&Interner)); value.subst_bound_vars(subst) } } +pub fn make_only_type_binders(num_vars: usize, value: T) -> Binders { + Binders::new( + VariableKinds::from_iter( + &Interner, + std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General)) + .take(num_vars), + ), + value, + ) +} + impl TraitRef { pub fn self_type_parameter(&self, interner: &Interner) -> &Ty { &self.substitution.at(interner, 0).assert_ty_ref(interner) @@ -407,8 +413,8 @@ impl Ty { // This is only used by type walking. // Parameters will be walked outside, and projection predicate is not used. // So just provide the Future trait. - let impl_bound = Binders::new( - 0, + let impl_bound = Binders::empty( + &Interner, WhereClause::Implemented(TraitRef { trait_id: to_chalk_trait_id(future_trait), substitution: Substitution::empty(&Interner), -- cgit v1.2.3 From 05eba0db3dd76f016aabdd49af6211e70a1812ed Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 18:39:53 +0200 Subject: Binders::subst -> substitute --- crates/hir_ty/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 86973c7ed..e15840c9a 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -130,7 +130,7 @@ impl Binders { impl Binders { /// Substitutes all variables. - pub fn subst(self, subst: &Substitution) -> T { + pub fn substitute(self, subst: &Substitution) -> T { let (value, binders) = self.into_value_and_skipped_binders(); assert_eq!(subst.len(&Interner), binders.len(&Interner)); value.subst_bound_vars(subst) @@ -362,7 +362,7 @@ impl Ty { TyKind::FnDef(def, parameters) => { let callable_def = db.lookup_intern_callable_def((*def).into()); let sig = db.callable_item_signature(callable_def); - Some(sig.subst(¶meters)) + Some(sig.substitute(¶meters)) } TyKind::Closure(.., substs) => { let sig_param = substs.at(&Interner, 0).assert_ty_ref(&Interner); @@ -436,7 +436,7 @@ impl Ty { let data = (*it) .as_ref() .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); - data.subst(&opaque_ty.substitution) + data.substitute(&opaque_ty.substitution) }) } // It always has an parameter for Future::Output type. @@ -455,7 +455,7 @@ impl Ty { let predicates = db .generic_predicates(id.parent) .into_iter() - .map(|pred| pred.clone().subst(&substs)) + .map(|pred| pred.clone().substitute(&substs)) .filter(|wc| match &wc.skip_binders() { WhereClause::Implemented(tr) => { tr.self_type_parameter(&Interner) == self -- cgit v1.2.3 From 30a339e038bfd94d8c91f79287be9b7db4f0cb4e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 18:49:26 +0200 Subject: Add Interner parameter to Binders::substitute --- crates/hir_ty/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index e15840c9a..827317fce 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -130,9 +130,9 @@ impl Binders { impl Binders { /// Substitutes all variables. - pub fn substitute(self, subst: &Substitution) -> T { + pub fn substitute(self, interner: &Interner, subst: &Substitution) -> T { let (value, binders) = self.into_value_and_skipped_binders(); - assert_eq!(subst.len(&Interner), binders.len(&Interner)); + assert_eq!(subst.len(interner), binders.len(interner)); value.subst_bound_vars(subst) } } @@ -362,7 +362,7 @@ impl Ty { TyKind::FnDef(def, parameters) => { let callable_def = db.lookup_intern_callable_def((*def).into()); let sig = db.callable_item_signature(callable_def); - Some(sig.substitute(¶meters)) + Some(sig.substitute(&Interner, ¶meters)) } TyKind::Closure(.., substs) => { let sig_param = substs.at(&Interner, 0).assert_ty_ref(&Interner); @@ -436,7 +436,7 @@ impl Ty { let data = (*it) .as_ref() .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); - data.substitute(&opaque_ty.substitution) + data.substitute(&Interner, &opaque_ty.substitution) }) } // It always has an parameter for Future::Output type. @@ -455,7 +455,7 @@ impl Ty { let predicates = db .generic_predicates(id.parent) .into_iter() - .map(|pred| pred.clone().substitute(&substs)) + .map(|pred| pred.clone().substitute(&Interner, &substs)) .filter(|wc| match &wc.skip_binders() { WhereClause::Implemented(tr) => { tr.self_type_parameter(&Interner) == self -- cgit v1.2.3 From a316d583600e11ee1fcc8027a838efafe435f03c Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 19:15:13 +0200 Subject: Rename shift_bound_vars{_out} to align with Chalk --- crates/hir_ty/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 827317fce..daf379ef8 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -124,7 +124,7 @@ impl Binders { where T: TypeWalk, { - Binders::empty(&Interner, value.shift_bound_vars(DebruijnIndex::ONE)) + Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE)) } } @@ -209,7 +209,8 @@ impl CallableSig { params_and_return: fn_ptr .substs .clone() - .shift_bound_vars_out(DebruijnIndex::ONE) + .shifted_out_to(DebruijnIndex::ONE) + .expect("unexpected lifetime vars in fn ptr") .interned() .iter() .map(|arg| arg.assert_ty_ref(&Interner).clone()) -- cgit v1.2.3