diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 86b36c5650..13aaa408a2 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1972,9 +1972,9 @@ impl Type { pub fn type_parameters(&self) -> impl Iterator + '_ { self.ty .strip_references() - .substs() + .as_adt() .into_iter() - .flat_map(|substs| substs.iter(&Interner)) + .flat_map(|(_, substs)| substs.iter(&Interner)) .filter_map(|arg| arg.ty(&Interner).cloned()) .map(move |ty| self.derived(ty)) } @@ -2115,18 +2115,22 @@ impl Type { fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { let ty = type_.ty.strip_references(); match ty.kind(&Interner) { - TyKind::Adt(..) => { + TyKind::Adt(_, substs) => { cb(type_.derived(ty.clone())); + walk_substs(db, type_, &substs, cb); } - TyKind::AssociatedType(..) => { + TyKind::AssociatedType(_, substs) => { if let Some(_) = ty.associated_type_parent_trait(db) { cb(type_.derived(ty.clone())); } + walk_substs(db, type_, &substs, cb); } - TyKind::OpaqueType(..) => { + TyKind::OpaqueType(_, subst) => { if let Some(bounds) = ty.impl_trait_bounds(db) { walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); } + + walk_substs(db, type_, subst, cb); } TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { if let Some(bounds) = ty.impl_trait_bounds(db) { @@ -2156,11 +2160,17 @@ impl Type { walk_type(db, &type_.derived(ty.clone()), cb); } + TyKind::FnDef(_, substs) + | TyKind::Tuple(_, substs) + | TyKind::Closure(.., substs) => { + walk_substs(db, type_, &substs, cb); + } + TyKind::Function(hir_ty::FnPointer { substitution, .. }) => { + walk_substs(db, type_, &substitution.0, cb); + } + _ => {} } - if let Some(substs) = ty.substs() { - walk_substs(db, type_, &substs, cb); - } } walk_type(db, self, &mut cb); diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index ce6f3c0080..847d2537d2 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -20,7 +20,7 @@ use hir_def::{ use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_ty::{ diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, - InferenceResult, Interner, Substitution, TyLoweringContext, + InferenceResult, Interner, Substitution, TyExt, TyLoweringContext, }; use syntax::{ ast::{self, AstNode}, @@ -306,7 +306,7 @@ impl SourceAnalyzer { let infer = self.infer.as_ref()?; let expr_id = self.expr_id(db, &literal.clone().into())?; - let substs = infer.type_of_expr[expr_id].substs()?; + let substs = infer.type_of_expr[expr_id].as_adt()?.1; let (variant, missing_fields, _exhaustive) = record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; @@ -324,7 +324,7 @@ impl SourceAnalyzer { let infer = self.infer.as_ref()?; let pat_id = self.pat_id(&pattern.clone().into())?; - let substs = infer.type_of_pat[pat_id].substs()?; + let substs = infer.type_of_pat[pat_id].as_adt()?.1; let (variant, missing_fields, _exhaustive) = record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 9ab0fa212c..9841988c55 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 e4813c87c1..f88d5c5d34 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 ae3987752b..84645c4355 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 41ebf6137c..91116dcda3 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); }