diff options
author | Florian Diebold <[email protected]> | 2021-04-07 16:26:01 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2021-04-07 16:56:53 +0100 |
commit | 92dcc53f94455e8933d76a8ba20642ceb069362d (patch) | |
tree | a9f009f2a47f4292b35e0470cb0b1ade9e86de14 | |
parent | 8e900cb4a1c5a4faef801518272d56a5683dd488 (diff) |
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.
-rw-r--r-- | crates/hir/src/lib.rs | 26 | ||||
-rw-r--r-- | crates/hir/src/source_analyzer.rs | 6 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 5 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/pat.rs | 6 | ||||
-rw-r--r-- | crates/hir_ty/src/lib.rs | 28 | ||||
-rw-r--r-- | crates/hir_ty/src/walk.rs | 26 |
6 files changed, 45 insertions, 52 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 86b36c565..13aaa408a 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -1972,9 +1972,9 @@ impl Type { | |||
1972 | pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { | 1972 | pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { |
1973 | self.ty | 1973 | self.ty |
1974 | .strip_references() | 1974 | .strip_references() |
1975 | .substs() | 1975 | .as_adt() |
1976 | .into_iter() | 1976 | .into_iter() |
1977 | .flat_map(|substs| substs.iter(&Interner)) | 1977 | .flat_map(|(_, substs)| substs.iter(&Interner)) |
1978 | .filter_map(|arg| arg.ty(&Interner).cloned()) | 1978 | .filter_map(|arg| arg.ty(&Interner).cloned()) |
1979 | .map(move |ty| self.derived(ty)) | 1979 | .map(move |ty| self.derived(ty)) |
1980 | } | 1980 | } |
@@ -2115,18 +2115,22 @@ impl Type { | |||
2115 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { | 2115 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { |
2116 | let ty = type_.ty.strip_references(); | 2116 | let ty = type_.ty.strip_references(); |
2117 | match ty.kind(&Interner) { | 2117 | match ty.kind(&Interner) { |
2118 | TyKind::Adt(..) => { | 2118 | TyKind::Adt(_, substs) => { |
2119 | cb(type_.derived(ty.clone())); | 2119 | cb(type_.derived(ty.clone())); |
2120 | walk_substs(db, type_, &substs, cb); | ||
2120 | } | 2121 | } |
2121 | TyKind::AssociatedType(..) => { | 2122 | TyKind::AssociatedType(_, substs) => { |
2122 | if let Some(_) = ty.associated_type_parent_trait(db) { | 2123 | if let Some(_) = ty.associated_type_parent_trait(db) { |
2123 | cb(type_.derived(ty.clone())); | 2124 | cb(type_.derived(ty.clone())); |
2124 | } | 2125 | } |
2126 | walk_substs(db, type_, &substs, cb); | ||
2125 | } | 2127 | } |
2126 | TyKind::OpaqueType(..) => { | 2128 | TyKind::OpaqueType(_, subst) => { |
2127 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 2129 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
2128 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 2130 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
2129 | } | 2131 | } |
2132 | |||
2133 | walk_substs(db, type_, subst, cb); | ||
2130 | } | 2134 | } |
2131 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { | 2135 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { |
2132 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 2136 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
@@ -2156,11 +2160,17 @@ impl Type { | |||
2156 | walk_type(db, &type_.derived(ty.clone()), cb); | 2160 | walk_type(db, &type_.derived(ty.clone()), cb); |
2157 | } | 2161 | } |
2158 | 2162 | ||
2163 | TyKind::FnDef(_, substs) | ||
2164 | | TyKind::Tuple(_, substs) | ||
2165 | | TyKind::Closure(.., substs) => { | ||
2166 | walk_substs(db, type_, &substs, cb); | ||
2167 | } | ||
2168 | TyKind::Function(hir_ty::FnPointer { substitution, .. }) => { | ||
2169 | walk_substs(db, type_, &substitution.0, cb); | ||
2170 | } | ||
2171 | |||
2159 | _ => {} | 2172 | _ => {} |
2160 | } | 2173 | } |
2161 | if let Some(substs) = ty.substs() { | ||
2162 | walk_substs(db, type_, &substs, cb); | ||
2163 | } | ||
2164 | } | 2174 | } |
2165 | 2175 | ||
2166 | walk_type(db, self, &mut cb); | 2176 | walk_type(db, self, &mut cb); |
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index ce6f3c008..847d2537d 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs | |||
@@ -20,7 +20,7 @@ use hir_def::{ | |||
20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; | 20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; |
21 | use hir_ty::{ | 21 | use hir_ty::{ |
22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, | 22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, |
23 | InferenceResult, Interner, Substitution, TyLoweringContext, | 23 | InferenceResult, Interner, Substitution, TyExt, TyLoweringContext, |
24 | }; | 24 | }; |
25 | use syntax::{ | 25 | use syntax::{ |
26 | ast::{self, AstNode}, | 26 | ast::{self, AstNode}, |
@@ -306,7 +306,7 @@ impl SourceAnalyzer { | |||
306 | let infer = self.infer.as_ref()?; | 306 | let infer = self.infer.as_ref()?; |
307 | 307 | ||
308 | let expr_id = self.expr_id(db, &literal.clone().into())?; | 308 | let expr_id = self.expr_id(db, &literal.clone().into())?; |
309 | let substs = infer.type_of_expr[expr_id].substs()?; | 309 | let substs = infer.type_of_expr[expr_id].as_adt()?.1; |
310 | 310 | ||
311 | let (variant, missing_fields, _exhaustive) = | 311 | let (variant, missing_fields, _exhaustive) = |
312 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; | 312 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; |
@@ -324,7 +324,7 @@ impl SourceAnalyzer { | |||
324 | let infer = self.infer.as_ref()?; | 324 | let infer = self.infer.as_ref()?; |
325 | 325 | ||
326 | let pat_id = self.pat_id(&pattern.clone().into())?; | 326 | let pat_id = self.pat_id(&pattern.clone().into())?; |
327 | let substs = infer.type_of_pat[pat_id].substs()?; | 327 | let substs = infer.type_of_pat[pat_id].as_adt()?.1; |
328 | 328 | ||
329 | let (variant, missing_fields, _exhaustive) = | 329 | let (variant, missing_fields, _exhaustive) = |
330 | record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; | 330 | 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 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> { | |||
412 | 412 | ||
413 | self.unify(&ty, &expected.ty); | 413 | self.unify(&ty, &expected.ty); |
414 | 414 | ||
415 | let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); | 415 | let substs = ty |
416 | .as_adt() | ||
417 | .map(|(_, s)| s.clone()) | ||
418 | .unwrap_or_else(|| Substitution::empty(&Interner)); | ||
416 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); | 419 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); |
417 | let variant_data = def_id.map(|it| it.variant_data(self.db.upcast())); | 420 | let variant_data = def_id.map(|it| it.variant_data(self.db.upcast())); |
418 | for field in fields.iter() { | 421 | 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> { | |||
33 | } | 33 | } |
34 | self.unify(&ty, expected); | 34 | self.unify(&ty, expected); |
35 | 35 | ||
36 | let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); | 36 | let substs = |
37 | ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner)); | ||
37 | 38 | ||
38 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); | 39 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); |
39 | let (pre, post) = match ellipsis { | 40 | let (pre, post) = match ellipsis { |
@@ -74,7 +75,8 @@ impl<'a> InferenceContext<'a> { | |||
74 | 75 | ||
75 | self.unify(&ty, expected); | 76 | self.unify(&ty, expected); |
76 | 77 | ||
77 | let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); | 78 | let substs = |
79 | ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner)); | ||
78 | 80 | ||
79 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); | 81 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); |
80 | for subpat in subpats { | 82 | 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 { | |||
198 | _ => false, | 198 | _ => false, |
199 | } | 199 | } |
200 | } | 200 | } |
201 | |||
202 | /// Returns the type parameters of this type if it has some (i.e. is an ADT | ||
203 | /// or function); so if `self` is `Option<u32>`, this returns the `u32`. | ||
204 | pub fn substs(&self) -> Option<&Substitution> { | ||
205 | match self.kind(&Interner) { | ||
206 | TyKind::Adt(_, substs) | ||
207 | | TyKind::FnDef(_, substs) | ||
208 | | TyKind::Tuple(_, substs) | ||
209 | | TyKind::OpaqueType(_, substs) | ||
210 | | TyKind::AssociatedType(_, substs) | ||
211 | | TyKind::Closure(.., substs) => Some(substs), | ||
212 | TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&substs.0), | ||
213 | _ => None, | ||
214 | } | ||
215 | } | ||
216 | |||
217 | fn substs_mut(&mut self) -> Option<&mut Substitution> { | ||
218 | match self.interned_mut() { | ||
219 | TyKind::Adt(_, substs) | ||
220 | | TyKind::FnDef(_, substs) | ||
221 | | TyKind::Tuple(_, substs) | ||
222 | | TyKind::OpaqueType(_, substs) | ||
223 | | TyKind::AssociatedType(_, substs) | ||
224 | | TyKind::Closure(.., substs) => Some(substs), | ||
225 | TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&mut substs.0), | ||
226 | _ => None, | ||
227 | } | ||
228 | } | ||
229 | } | 201 | } |
230 | 202 | ||
231 | #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] | 203 | #[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 { | |||
162 | TyKind::Function(fn_pointer) => { | 162 | TyKind::Function(fn_pointer) => { |
163 | fn_pointer.substitution.0.walk(f); | 163 | fn_pointer.substitution.0.walk(f); |
164 | } | 164 | } |
165 | _ => { | 165 | TyKind::Adt(_, substs) |
166 | if let Some(substs) = self.substs() { | 166 | | TyKind::FnDef(_, substs) |
167 | for t in substs.iter(&Interner) { | 167 | | TyKind::Tuple(_, substs) |
168 | t.walk(f); | 168 | | TyKind::OpaqueType(_, substs) |
169 | } | 169 | | TyKind::AssociatedType(_, substs) |
170 | } | 170 | | TyKind::Closure(.., substs) => { |
171 | substs.walk(f); | ||
171 | } | 172 | } |
173 | _ => {} | ||
172 | } | 174 | } |
173 | f(self); | 175 | f(self); |
174 | } | 176 | } |
@@ -199,11 +201,15 @@ impl TypeWalk for Ty { | |||
199 | TyKind::Function(fn_pointer) => { | 201 | TyKind::Function(fn_pointer) => { |
200 | fn_pointer.substitution.0.walk_mut_binders(f, binders.shifted_in()); | 202 | fn_pointer.substitution.0.walk_mut_binders(f, binders.shifted_in()); |
201 | } | 203 | } |
202 | _ => { | 204 | TyKind::Adt(_, substs) |
203 | if let Some(substs) = self.substs_mut() { | 205 | | TyKind::FnDef(_, substs) |
204 | substs.walk_mut_binders(f, binders); | 206 | | TyKind::Tuple(_, substs) |
205 | } | 207 | | TyKind::OpaqueType(_, substs) |
208 | | TyKind::AssociatedType(_, substs) | ||
209 | | TyKind::Closure(.., substs) => { | ||
210 | substs.walk_mut_binders(f, binders); | ||
206 | } | 211 | } |
212 | _ => {} | ||
207 | } | 213 | } |
208 | f(self, binders); | 214 | f(self, binders); |
209 | } | 215 | } |