aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r--crates/hir_ty/src/lib.rs27
1 files changed, 20 insertions, 7 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index 503910dde..850385280 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -31,6 +31,7 @@ use hir_def::{
31 GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, 31 GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId,
32}; 32};
33use itertools::Itertools; 33use itertools::Itertools;
34use smallvec::SmallVec;
34 35
35use crate::{ 36use crate::{
36 db::HirDatabase, 37 db::HirDatabase,
@@ -272,7 +273,7 @@ impl Ty {
272 273
273/// A list of substitutions for generic parameters. 274/// A list of substitutions for generic parameters.
274#[derive(Clone, PartialEq, Eq, Debug, Hash)] 275#[derive(Clone, PartialEq, Eq, Debug, Hash)]
275pub struct Substs(Arc<[Ty]>); 276pub struct Substs(SmallVec<[Ty; 2]>);
276 277
277impl TypeWalk for Substs { 278impl TypeWalk for Substs {
278 fn walk(&self, f: &mut impl FnMut(&Ty)) { 279 fn walk(&self, f: &mut impl FnMut(&Ty)) {
@@ -286,19 +287,27 @@ impl TypeWalk for Substs {
286 f: &mut impl FnMut(&mut Ty, DebruijnIndex), 287 f: &mut impl FnMut(&mut Ty, DebruijnIndex),
287 binders: DebruijnIndex, 288 binders: DebruijnIndex,
288 ) { 289 ) {
289 for t in make_mut_slice(&mut self.0) { 290 for t in &mut self.0 {
290 t.walk_mut_binders(f, binders); 291 t.walk_mut_binders(f, binders);
291 } 292 }
292 } 293 }
293} 294}
294 295
295impl Substs { 296impl Substs {
297 pub fn interned(&self, _: &Interner) -> &[Ty] {
298 &self.0
299 }
300
296 pub fn empty() -> Substs { 301 pub fn empty() -> Substs {
297 Substs(Arc::new([])) 302 Substs(SmallVec::new())
298 } 303 }
299 304
300 pub fn single(ty: Ty) -> Substs { 305 pub fn single(ty: Ty) -> Substs {
301 Substs(Arc::new([ty])) 306 Substs({
307 let mut v = SmallVec::new();
308 v.push(ty);
309 v
310 })
302 } 311 }
303 312
304 pub fn prefix(&self, n: usize) -> Substs { 313 pub fn prefix(&self, n: usize) -> Substs {
@@ -316,6 +325,10 @@ impl Substs {
316 &self.0[0] 325 &self.0[0]
317 } 326 }
318 327
328 pub fn from_iter(_interner: &Interner, elements: impl IntoIterator<Item = Ty>) -> Self {
329 Substs(elements.into_iter().collect())
330 }
331
319 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). 332 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
320 pub(crate) fn type_params_for_generics( 333 pub(crate) fn type_params_for_generics(
321 db: &dyn HirDatabase, 334 db: &dyn HirDatabase,
@@ -600,13 +613,13 @@ impl CallableSig {
600 613
601 pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig { 614 pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
602 CallableSig { 615 CallableSig {
603 params_and_return: Arc::clone(&fn_ptr.substs.0), 616 params_and_return: fn_ptr.substs.interned(&Interner).iter().cloned().collect(),
604 is_varargs: fn_ptr.sig.variadic, 617 is_varargs: fn_ptr.sig.variadic,
605 } 618 }
606 } 619 }
607 620
608 pub fn from_substs(substs: &Substs) -> CallableSig { 621 pub fn from_substs(substs: &Substs) -> CallableSig {
609 CallableSig { params_and_return: Arc::clone(&substs.0), is_varargs: false } 622 CallableSig { params_and_return: substs.iter().cloned().collect(), is_varargs: false }
610 } 623 }
611 624
612 pub fn params(&self) -> &[Ty] { 625 pub fn params(&self) -> &[Ty] {
@@ -649,7 +662,7 @@ impl Ty {
649 TyKind::Function(FnPointer { 662 TyKind::Function(FnPointer {
650 num_args: sig.params().len(), 663 num_args: sig.params().len(),
651 sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs }, 664 sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs },
652 substs: Substs(sig.params_and_return), 665 substs: Substs::from_iter(&Interner, sig.params_and_return.iter().cloned()),
653 }) 666 })
654 .intern(&Interner) 667 .intern(&Interner)
655 } 668 }