From 92dcc53f94455e8933d76a8ba20642ceb069362d Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 17:26:01 +0200 Subject: Remove Ty::substs{_mut} Almost all uses actually only care about ADT substs, so it's better to be explicit. The methods were a bad abstraction anyway since they already didn't include the inner types of e.g. `TyKind::Ref` anymore. --- crates/hir_ty/src/infer/expr.rs | 5 ++++- crates/hir_ty/src/infer/pat.rs | 6 ++++-- crates/hir_ty/src/lib.rs | 28 ---------------------------- crates/hir_ty/src/walk.rs | 26 ++++++++++++++++---------- 4 files changed, 24 insertions(+), 41 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 9ab0fa212..9841988c5 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -412,7 +412,10 @@ impl<'a> InferenceContext<'a> { self.unify(&ty, &expected.ty); - let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); + let substs = ty + .as_adt() + .map(|(_, s)| s.clone()) + .unwrap_or_else(|| Substitution::empty(&Interner)); let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); let variant_data = def_id.map(|it| it.variant_data(self.db.upcast())); for field in fields.iter() { diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index e4813c87c..f88d5c5d3 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -33,7 +33,8 @@ impl<'a> InferenceContext<'a> { } self.unify(&ty, expected); - let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); + let substs = + ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner)); let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); let (pre, post) = match ellipsis { @@ -74,7 +75,8 @@ impl<'a> InferenceContext<'a> { self.unify(&ty, expected); - let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); + let substs = + ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner)); let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); for subpat in subpats { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index ae3987752..84645c435 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -198,34 +198,6 @@ impl Ty { _ => false, } } - - /// Returns the type parameters of this type if it has some (i.e. is an ADT - /// or function); so if `self` is `Option`, this returns the `u32`. - pub fn substs(&self) -> Option<&Substitution> { - match self.kind(&Interner) { - TyKind::Adt(_, substs) - | TyKind::FnDef(_, substs) - | TyKind::Tuple(_, substs) - | TyKind::OpaqueType(_, substs) - | TyKind::AssociatedType(_, substs) - | TyKind::Closure(.., substs) => Some(substs), - TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&substs.0), - _ => None, - } - } - - fn substs_mut(&mut self) -> Option<&mut Substitution> { - match self.interned_mut() { - TyKind::Adt(_, substs) - | TyKind::FnDef(_, substs) - | TyKind::Tuple(_, substs) - | TyKind::OpaqueType(_, substs) - | TyKind::AssociatedType(_, substs) - | TyKind::Closure(.., substs) => Some(substs), - TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&mut substs.0), - _ => None, - } - } } #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] diff --git a/crates/hir_ty/src/walk.rs b/crates/hir_ty/src/walk.rs index 41ebf6137..91116dcda 100644 --- a/crates/hir_ty/src/walk.rs +++ b/crates/hir_ty/src/walk.rs @@ -162,13 +162,15 @@ impl TypeWalk for Ty { TyKind::Function(fn_pointer) => { fn_pointer.substitution.0.walk(f); } - _ => { - if let Some(substs) = self.substs() { - for t in substs.iter(&Interner) { - t.walk(f); - } - } + TyKind::Adt(_, substs) + | TyKind::FnDef(_, substs) + | TyKind::Tuple(_, substs) + | TyKind::OpaqueType(_, substs) + | TyKind::AssociatedType(_, substs) + | TyKind::Closure(.., substs) => { + substs.walk(f); } + _ => {} } f(self); } @@ -199,11 +201,15 @@ impl TypeWalk for Ty { TyKind::Function(fn_pointer) => { fn_pointer.substitution.0.walk_mut_binders(f, binders.shifted_in()); } - _ => { - if let Some(substs) = self.substs_mut() { - substs.walk_mut_binders(f, binders); - } + TyKind::Adt(_, substs) + | TyKind::FnDef(_, substs) + | TyKind::Tuple(_, substs) + | TyKind::OpaqueType(_, substs) + | TyKind::AssociatedType(_, substs) + | TyKind::Closure(.., substs) => { + substs.walk_mut_binders(f, binders); } + _ => {} } f(self, binders); } -- cgit v1.2.3