From b15152c430237d6850ec709ac75aab269c4b7dee Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 20:14:21 +0200 Subject: Add TyBuilder --- crates/hir_ty/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) (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 4c3d904bf..a8ddb43f5 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -810,6 +810,12 @@ impl TypeWalk for CallableSig { } } +struct TyBuilder {} + +impl TyBuilder { + +} + impl Ty { pub fn unit() -> Self { TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner) -- cgit v1.2.3 From b0fe3d929f6f8764f371970b9f9ca9e7c415dafd Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 20:22:59 +0200 Subject: Add TyBuilder::unit() and TyExt::is_unit() --- 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 a8ddb43f5..b6173d87c 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -23,6 +23,7 @@ pub mod diagnostics; mod tests; #[cfg(test)] mod test_db; +mod chalk_ext; use std::{iter, mem, sync::Arc}; @@ -42,6 +43,7 @@ use crate::{ }; pub use autoderef::autoderef; +pub use chalk_ext::TyExt; pub use infer::{could_unify, InferenceResult, InferenceVar}; pub use lower::{ associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, @@ -813,14 +815,12 @@ impl TypeWalk for CallableSig { struct TyBuilder {} impl TyBuilder { - -} - -impl Ty { - pub fn unit() -> Self { + pub fn unit() -> Ty { TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner) } +} +impl Ty { pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty { TyKind::Adt(AdtId(adt), substs).intern(&Interner) } -- cgit v1.2.3 From e6f007d9a8e676c4af5731001b211ca7a52bce16 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 20:27:57 +0200 Subject: Move Ty::fn_ptr to TyBuilder --- crates/hir_ty/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 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 b6173d87c..fd44efa07 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -818,14 +818,8 @@ impl TyBuilder { pub fn unit() -> Ty { TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner) } -} - -impl Ty { - pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty { - TyKind::Adt(AdtId(adt), substs).intern(&Interner) - } - pub fn fn_ptr(sig: CallableSig) -> Self { + pub fn fn_ptr(sig: CallableSig) -> Ty { TyKind::Function(FnPointer { num_args: sig.params().len(), sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs }, @@ -833,6 +827,12 @@ impl Ty { }) .intern(&Interner) } +} + +impl Ty { + pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty { + TyKind::Adt(AdtId(adt), substs).intern(&Interner) + } pub fn builtin(builtin: BuiltinType) -> Self { match builtin { -- cgit v1.2.3 From 620769f32276bb7e8c580eae2c91ee535a06d9f8 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 21:29:49 +0200 Subject: Add TyBuilder::adt --- crates/hir_ty/src/lib.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 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 fd44efa07..afe5424d6 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -812,9 +812,59 @@ impl TypeWalk for CallableSig { } } -struct TyBuilder {} +pub struct TyBuilder { + data: D, + vec: SmallVec<[GenericArg; 2]>, + param_count: usize, +} + +impl TyBuilder { + fn new(data: D, param_count: usize) -> TyBuilder { + TyBuilder { data, param_count, vec: SmallVec::with_capacity(param_count) } + } + + fn build_internal(self) -> (D, Substitution) { + assert_eq!(self.vec.len(), self.param_count); + // FIXME: would be good to have a way to construct a chalk_ir::Substitution from the interned form + let subst = Substitution(self.vec); + (self.data, subst) + } + + pub fn push(mut self, arg: impl CastTo) -> Self { + self.vec.push(arg.cast(&Interner)); + self + } + + fn remaining(&self) -> usize { + self.param_count - self.vec.len() + } + + pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { + self.fill( + (starting_from..) + .map(|idx| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), + ) + } + + pub fn fill_with_unknown(self) -> Self { + self.fill(iter::repeat(TyKind::Unknown.intern(&Interner))) + } + + pub fn fill(mut self, filler: impl Iterator>) -> Self { + self.vec.extend(filler.take(self.remaining()).casted(&Interner)); + assert_eq!(self.remaining(), 0); + self + } + + pub fn use_parent_substs(mut self, parent_substs: &Substitution) -> Self { + assert!(self.vec.is_empty()); + assert!(parent_substs.len(&Interner) <= self.param_count); + self.vec.extend(parent_substs.iter(&Interner).cloned()); + self + } +} -impl TyBuilder { +impl TyBuilder<()> { pub fn unit() -> Ty { TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner) } @@ -829,11 +879,38 @@ impl TyBuilder { } } -impl Ty { - pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty { - TyKind::Adt(AdtId(adt), substs).intern(&Interner) +impl TyBuilder { + pub fn adt(db: &dyn HirDatabase, adt: hir_def::AdtId) -> TyBuilder { + let generics = generics(db.upcast(), adt.into()); + let param_count = generics.len(); + TyBuilder::new(adt, param_count) } + pub fn fill_with_defaults( + mut self, + db: &dyn HirDatabase, + mut fallback: impl FnMut() -> Ty, + ) -> Self { + let defaults = db.generic_defaults(self.data.into()); + for default_ty in defaults.iter().skip(self.vec.len()) { + if default_ty.skip_binders().is_unknown() { + self.vec.push(fallback().cast(&Interner)); + } else { + // each default can depend on the previous parameters + let subst_so_far = Substitution(self.vec.clone()); + self.vec.push(default_ty.clone().subst(&subst_so_far).cast(&Interner)); + } + } + self + } + + pub fn build(self) -> Ty { + let (adt, subst) = self.build_internal(); + TyKind::Adt(AdtId(adt), subst).intern(&Interner) + } +} + +impl Ty { pub fn builtin(builtin: BuiltinType) -> Self { match builtin { BuiltinType::Char => TyKind::Scalar(Scalar::Char).intern(&Interner), -- cgit v1.2.3 From 2ead65190ecaf1096a998d88d4aab8505ce88afa Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 21:32:22 +0200 Subject: Move Ty::builtin to TyBuilder --- crates/hir_ty/src/lib.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 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 afe5424d6..be6fe5016 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -877,6 +877,23 @@ impl TyBuilder<()> { }) .intern(&Interner) } + + pub fn builtin(builtin: BuiltinType) -> Ty { + match builtin { + BuiltinType::Char => TyKind::Scalar(Scalar::Char).intern(&Interner), + BuiltinType::Bool => TyKind::Scalar(Scalar::Bool).intern(&Interner), + BuiltinType::Str => TyKind::Str.intern(&Interner), + BuiltinType::Int(t) => { + TyKind::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))).intern(&Interner) + } + BuiltinType::Uint(t) => { + TyKind::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))).intern(&Interner) + } + BuiltinType::Float(t) => { + TyKind::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t))).intern(&Interner) + } + } + } } impl TyBuilder { @@ -911,23 +928,6 @@ impl TyBuilder { } impl Ty { - pub fn builtin(builtin: BuiltinType) -> Self { - match builtin { - BuiltinType::Char => TyKind::Scalar(Scalar::Char).intern(&Interner), - BuiltinType::Bool => TyKind::Scalar(Scalar::Bool).intern(&Interner), - BuiltinType::Str => TyKind::Str.intern(&Interner), - BuiltinType::Int(t) => { - TyKind::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))).intern(&Interner) - } - BuiltinType::Uint(t) => { - TyKind::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))).intern(&Interner) - } - BuiltinType::Float(t) => { - TyKind::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t))).intern(&Interner) - } - } - } - pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { match self.kind(&Interner) { TyKind::Ref(mutability, ty) => Some((ty, *mutability)), -- cgit v1.2.3 From cd227f581e0c20d6b9ee81a3982509a9f6b2f67f Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 21:50:52 +0200 Subject: Add and start using TraitRef and ProjectionTy builders --- crates/hir_ty/src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (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 be6fe5016..e586d73d8 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -927,6 +927,35 @@ impl TyBuilder { } } +impl TyBuilder { + pub fn trait_ref(db: &dyn HirDatabase, trait_id: TraitId) -> TyBuilder { + let generics = generics(db.upcast(), trait_id.into()); + let param_count = generics.len(); + TyBuilder::new(trait_id, param_count) + } + + pub fn build(self) -> TraitRef { + let (trait_id, substitution) = self.build_internal(); + TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution } + } +} + +impl TyBuilder { + pub fn assoc_type_projection( + db: &dyn HirDatabase, + type_alias: TypeAliasId, + ) -> TyBuilder { + let generics = generics(db.upcast(), type_alias.into()); + let param_count = generics.len(); + TyBuilder::new(type_alias, param_count) + } + + pub fn build(self) -> ProjectionTy { + let (type_alias, substitution) = self.build_internal(); + ProjectionTy { associated_ty_id: to_assoc_type_id(type_alias), substitution } + } +} + impl Ty { pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { match self.kind(&Interner) { -- cgit v1.2.3 From 5d2b488aeb17410aec3b89eac69038c51f332448 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 22:18:02 +0200 Subject: Replace remaining uses of Substitution::build_for_def --- crates/hir_ty/src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 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 e586d73d8..75bf8bcd9 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -28,7 +28,10 @@ mod chalk_ext; use std::{iter, mem, sync::Arc}; use base_db::salsa; -use chalk_ir::cast::{CastTo, Caster}; +use chalk_ir::{ + cast::{CastTo, Caster}, + interner::HasInterner, +}; use hir_def::{ builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, @@ -490,13 +493,6 @@ impl Substitution { ) } - pub fn build_for_def(db: &dyn HirDatabase, def: impl Into) -> SubstsBuilder { - let def = def.into(); - let params = generics(db.upcast(), def); - let param_count = params.len(); - Substitution::builder(param_count) - } - pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { Substitution::builder(generic_params.len()) } @@ -894,6 +890,18 @@ impl TyBuilder<()> { } } } + + pub fn subst_for_def(db: &dyn HirDatabase, def: impl Into) -> TyBuilder<()> { + let def = def.into(); + let params = generics(db.upcast(), def); + let param_count = params.len(); + TyBuilder::new((), param_count) + } + + pub fn build(self) -> Substitution { + let ((), subst) = self.build_internal(); + subst + } } impl TyBuilder { @@ -956,6 +964,28 @@ impl TyBuilder { } } +impl> TyBuilder> { + fn subst_binders(b: Binders) -> Self { + let param_count = b.num_binders; + TyBuilder::new(b, param_count) + } + + pub fn build(self) -> T { + let (b, subst) = self.build_internal(); + b.subst(&subst) + } +} + +impl TyBuilder> { + pub fn def_ty(db: &dyn HirDatabase, def: TyDefId) -> TyBuilder> { + TyBuilder::subst_binders(db.ty(def.into())) + } + + pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder> { + TyBuilder::subst_binders(db.impl_self_ty(def)) + } +} + impl Ty { pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { match self.kind(&Interner) { -- cgit v1.2.3 From eaa03ef4465765a16951faa54dd8ebc53095e2c8 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 22:28:10 +0200 Subject: Some more TyBuilder use --- crates/hir_ty/src/lib.rs | 4 ---- 1 file changed, 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 75bf8bcd9..b37566958 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -493,10 +493,6 @@ impl Substitution { ) } - pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { - Substitution::builder(generic_params.len()) - } - fn builder(param_count: usize) -> SubstsBuilder { SubstsBuilder { vec: Vec::with_capacity(param_count), param_count } } -- cgit v1.2.3 From 505ca65216e7d1ea87a235170106338272b36a10 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 22:47:29 +0200 Subject: Remove CallableSig::from_substs --- crates/hir_ty/src/lib.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 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 b37566958..27ebb7b7c 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -754,7 +754,7 @@ impl CallableSig { pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig { CallableSig { - // FIXME: what to do about lifetime params? + // FIXME: what to do about lifetime params? -> return PolyFnSig params_and_return: fn_ptr .substs .clone() @@ -767,16 +767,6 @@ impl CallableSig { } } - pub fn from_substs(substs: &Substitution) -> CallableSig { - CallableSig { - params_and_return: substs - .iter(&Interner) - .map(|arg| arg.assert_ty_ref(&Interner).clone()) - .collect(), - is_varargs: false, - } - } - pub fn params(&self) -> &[Ty] { &self.params_and_return[0..self.params_and_return.len() - 1] } -- cgit v1.2.3 From 584d1c9e5bc39402e2855d0ffa9394ae5a066060 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 4 Apr 2021 12:48:10 +0200 Subject: Replace last uses of SubstsBuilder by TyBuilder --- crates/hir_ty/src/lib.rs | 66 ++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 50 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 27ebb7b7c..f99b70f2b 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -492,10 +492,6 @@ impl Substitution { .map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), ) } - - fn builder(param_count: usize) -> SubstsBuilder { - SubstsBuilder { vec: Vec::with_capacity(param_count), param_count } - } } /// Return an index of a parameter in the generic type parameter list by it's id. @@ -503,52 +499,6 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option { generics(db.upcast(), id.parent).param_idx(id) } -#[derive(Debug, Clone)] -pub struct SubstsBuilder { - vec: Vec, - param_count: usize, -} - -impl SubstsBuilder { - pub fn build(self) -> Substitution { - assert_eq!(self.vec.len(), self.param_count); - Substitution::from_iter(&Interner, self.vec) - } - - pub fn push(mut self, ty: impl CastTo) -> Self { - self.vec.push(ty.cast(&Interner)); - self - } - - fn remaining(&self) -> usize { - self.param_count - self.vec.len() - } - - pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { - self.fill( - (starting_from..) - .map(|idx| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), - ) - } - - pub fn fill_with_unknown(self) -> Self { - self.fill(iter::repeat(TyKind::Unknown.intern(&Interner))) - } - - pub fn fill(mut self, filler: impl Iterator>) -> Self { - self.vec.extend(filler.take(self.remaining()).casted(&Interner)); - assert_eq!(self.remaining(), 0); - self - } - - pub fn use_parent_substs(mut self, parent_substs: &Substitution) -> Self { - assert!(self.vec.is_empty()); - assert!(parent_substs.len(&Interner) <= self.param_count); - self.vec.extend(parent_substs.iter(&Interner).cloned()); - self - } -} - #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub struct Binders { pub num_binders: usize, @@ -921,6 +871,18 @@ impl TyBuilder { } } +struct Tuple(usize); +impl TyBuilder { + pub fn tuple(size: usize) -> TyBuilder { + TyBuilder::new(Tuple(size), size) + } + + pub fn build(self) -> Ty { + let (Tuple(size), subst) = self.build_internal(); + TyKind::Tuple(size, subst).intern(&Interner) + } +} + impl TyBuilder { pub fn trait_ref(db: &dyn HirDatabase, trait_id: TraitId) -> TyBuilder { let generics = generics(db.upcast(), trait_id.into()); @@ -970,6 +932,10 @@ impl TyBuilder> { pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder> { TyBuilder::subst_binders(db.impl_self_ty(def)) } + + pub fn value_ty(db: &dyn HirDatabase, def: ValueTyDefId) -> TyBuilder> { + TyBuilder::subst_binders(db.value_ty(def)) + } } impl Ty { -- cgit v1.2.3 From 715c178f0b52117c4c689c39a0921012bfbb2386 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 4 Apr 2021 12:55:47 +0200 Subject: Move TyBuilder to its own module --- crates/hir_ty/src/lib.rs | 214 +++-------------------------------------------- 1 file changed, 10 insertions(+), 204 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 f99b70f2b..ebdcc4804 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -14,6 +14,8 @@ mod lower; pub(crate) mod infer; pub(crate) mod utils; mod chalk_cast; +mod chalk_ext; +mod builder; pub mod display; pub mod db; @@ -23,21 +25,18 @@ pub mod diagnostics; mod tests; #[cfg(test)] mod test_db; -mod chalk_ext; -use std::{iter, mem, sync::Arc}; +use std::{mem, sync::Arc}; + +use chalk_ir::cast::{CastTo, Caster}; +use itertools::Itertools; +use smallvec::SmallVec; use base_db::salsa; -use chalk_ir::{ - cast::{CastTo, Caster}, - interner::HasInterner, -}; use hir_def::{ - builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, - GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, + expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, + LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, }; -use itertools::Itertools; -use smallvec::SmallVec; use crate::{ db::HirDatabase, @@ -46,6 +45,7 @@ use crate::{ }; pub use autoderef::autoderef; +pub use builder::TyBuilder; pub use chalk_ext::TyExt; pub use infer::{could_unify, InferenceResult, InferenceVar}; pub use lower::{ @@ -744,200 +744,6 @@ impl TypeWalk for CallableSig { } } -pub struct TyBuilder { - data: D, - vec: SmallVec<[GenericArg; 2]>, - param_count: usize, -} - -impl TyBuilder { - fn new(data: D, param_count: usize) -> TyBuilder { - TyBuilder { data, param_count, vec: SmallVec::with_capacity(param_count) } - } - - fn build_internal(self) -> (D, Substitution) { - assert_eq!(self.vec.len(), self.param_count); - // FIXME: would be good to have a way to construct a chalk_ir::Substitution from the interned form - let subst = Substitution(self.vec); - (self.data, subst) - } - - pub fn push(mut self, arg: impl CastTo) -> Self { - self.vec.push(arg.cast(&Interner)); - self - } - - fn remaining(&self) -> usize { - self.param_count - self.vec.len() - } - - pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { - self.fill( - (starting_from..) - .map(|idx| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), - ) - } - - pub fn fill_with_unknown(self) -> Self { - self.fill(iter::repeat(TyKind::Unknown.intern(&Interner))) - } - - pub fn fill(mut self, filler: impl Iterator>) -> Self { - self.vec.extend(filler.take(self.remaining()).casted(&Interner)); - assert_eq!(self.remaining(), 0); - self - } - - pub fn use_parent_substs(mut self, parent_substs: &Substitution) -> Self { - assert!(self.vec.is_empty()); - assert!(parent_substs.len(&Interner) <= self.param_count); - self.vec.extend(parent_substs.iter(&Interner).cloned()); - self - } -} - -impl TyBuilder<()> { - pub fn unit() -> Ty { - TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner) - } - - pub fn fn_ptr(sig: CallableSig) -> Ty { - TyKind::Function(FnPointer { - num_args: sig.params().len(), - sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs }, - substs: Substitution::from_iter(&Interner, sig.params_and_return.iter().cloned()), - }) - .intern(&Interner) - } - - pub fn builtin(builtin: BuiltinType) -> Ty { - match builtin { - BuiltinType::Char => TyKind::Scalar(Scalar::Char).intern(&Interner), - BuiltinType::Bool => TyKind::Scalar(Scalar::Bool).intern(&Interner), - BuiltinType::Str => TyKind::Str.intern(&Interner), - BuiltinType::Int(t) => { - TyKind::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))).intern(&Interner) - } - BuiltinType::Uint(t) => { - TyKind::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))).intern(&Interner) - } - BuiltinType::Float(t) => { - TyKind::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t))).intern(&Interner) - } - } - } - - pub fn subst_for_def(db: &dyn HirDatabase, def: impl Into) -> TyBuilder<()> { - let def = def.into(); - let params = generics(db.upcast(), def); - let param_count = params.len(); - TyBuilder::new((), param_count) - } - - pub fn build(self) -> Substitution { - let ((), subst) = self.build_internal(); - subst - } -} - -impl TyBuilder { - pub fn adt(db: &dyn HirDatabase, adt: hir_def::AdtId) -> TyBuilder { - let generics = generics(db.upcast(), adt.into()); - let param_count = generics.len(); - TyBuilder::new(adt, param_count) - } - - pub fn fill_with_defaults( - mut self, - db: &dyn HirDatabase, - mut fallback: impl FnMut() -> Ty, - ) -> Self { - let defaults = db.generic_defaults(self.data.into()); - for default_ty in defaults.iter().skip(self.vec.len()) { - if default_ty.skip_binders().is_unknown() { - self.vec.push(fallback().cast(&Interner)); - } else { - // each default can depend on the previous parameters - let subst_so_far = Substitution(self.vec.clone()); - self.vec.push(default_ty.clone().subst(&subst_so_far).cast(&Interner)); - } - } - self - } - - pub fn build(self) -> Ty { - let (adt, subst) = self.build_internal(); - TyKind::Adt(AdtId(adt), subst).intern(&Interner) - } -} - -struct Tuple(usize); -impl TyBuilder { - pub fn tuple(size: usize) -> TyBuilder { - TyBuilder::new(Tuple(size), size) - } - - pub fn build(self) -> Ty { - let (Tuple(size), subst) = self.build_internal(); - TyKind::Tuple(size, subst).intern(&Interner) - } -} - -impl TyBuilder { - pub fn trait_ref(db: &dyn HirDatabase, trait_id: TraitId) -> TyBuilder { - let generics = generics(db.upcast(), trait_id.into()); - let param_count = generics.len(); - TyBuilder::new(trait_id, param_count) - } - - pub fn build(self) -> TraitRef { - let (trait_id, substitution) = self.build_internal(); - TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution } - } -} - -impl TyBuilder { - pub fn assoc_type_projection( - db: &dyn HirDatabase, - type_alias: TypeAliasId, - ) -> TyBuilder { - let generics = generics(db.upcast(), type_alias.into()); - let param_count = generics.len(); - TyBuilder::new(type_alias, param_count) - } - - pub fn build(self) -> ProjectionTy { - let (type_alias, substitution) = self.build_internal(); - ProjectionTy { associated_ty_id: to_assoc_type_id(type_alias), substitution } - } -} - -impl> TyBuilder> { - fn subst_binders(b: Binders) -> Self { - let param_count = b.num_binders; - TyBuilder::new(b, param_count) - } - - pub fn build(self) -> T { - let (b, subst) = self.build_internal(); - b.subst(&subst) - } -} - -impl TyBuilder> { - pub fn def_ty(db: &dyn HirDatabase, def: TyDefId) -> TyBuilder> { - TyBuilder::subst_binders(db.ty(def.into())) - } - - pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder> { - TyBuilder::subst_binders(db.impl_self_ty(def)) - } - - pub fn value_ty(db: &dyn HirDatabase, def: ValueTyDefId) -> TyBuilder> { - TyBuilder::subst_binders(db.value_ty(def)) - } -} - impl Ty { pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { match self.kind(&Interner) { -- cgit v1.2.3 From a4d7bdf1c884a9f3dd415a882fa56422adae89bf Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 4 Apr 2021 13:07:06 +0200 Subject: Replace Substitution::bound_vars and ::type_params_for_generics --- crates/hir_ty/src/lib.rs | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 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 ebdcc4804..6d6443ca3 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -41,7 +41,7 @@ use hir_def::{ use crate::{ db::HirDatabase, display::HirDisplay, - utils::{generics, make_mut_slice, Generics}, + utils::{generics, make_mut_slice}, }; pub use autoderef::autoderef; @@ -463,34 +463,10 @@ impl Substitution { Substitution(elements.into_iter().casted(interner).collect()) } - /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). - pub(crate) fn type_params_for_generics( - db: &dyn HirDatabase, - generic_params: &Generics, - ) -> Substitution { - Substitution::from_iter( - &Interner, - generic_params - .iter() - .map(|(id, _)| TyKind::Placeholder(to_placeholder_idx(db, id)).intern(&Interner)), - ) - } - /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). pub fn type_params(db: &dyn HirDatabase, def: impl Into) -> Substitution { let params = generics(db.upcast(), def.into()); - Substitution::type_params_for_generics(db, ¶ms) - } - - /// Return Substs that replace each parameter by a bound variable. - pub(crate) fn bound_vars(generic_params: &Generics, debruijn: DebruijnIndex) -> Substitution { - Substitution::from_iter( - &Interner, - generic_params - .iter() - .enumerate() - .map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), - ) + params.type_params_subst(db) } } -- cgit v1.2.3 From ebdfc932e74ff11c8c14c513614212b7c07bf400 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 4 Apr 2021 13:16:16 +0200 Subject: Replace Substitution::type_params --- crates/hir_ty/src/lib.rs | 8 +------- 1 file changed, 1 insertion(+), 7 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 6d6443ca3..a8c87eadf 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -462,12 +462,6 @@ impl Substitution { ) -> Self { Substitution(elements.into_iter().casted(interner).collect()) } - - /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). - pub fn type_params(db: &dyn HirDatabase, def: impl Into) -> Substitution { - let params = generics(db.upcast(), def.into()); - params.type_params_subst(db) - } } /// Return an index of a parameter in the generic type parameter list by it's id. @@ -944,7 +938,7 @@ impl Ty { let param_data = &generic_params.types[id.local_id]; match param_data.provenance { hir_def::generics::TypeParamProvenance::ArgumentImplTrait => { - let substs = Substitution::type_params(db, id.parent); + let substs = TyBuilder::type_params_subst(db, id.parent); let predicates = db .generic_predicates(id.parent) .into_iter() -- cgit v1.2.3