Skip to content

Commit eb248d8

Browse files
Merge #8402
8402: Remove Ty::substs{_mut} r=flodiebold a=flodiebold 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. Co-authored-by: Florian Diebold <flodiebold@gmail.com>
2 parents 062c8eb + 92dcc53 commit eb248d8

File tree

6 files changed

+45
-52
lines changed

6 files changed

+45
-52
lines changed

crates/hir/src/lib.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,9 +1972,9 @@ impl Type {
19721972
pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ {
19731973
self.ty
19741974
.strip_references()
1975-
.substs()
1975+
.as_adt()
19761976
.into_iter()
1977-
.flat_map(|substs| substs.iter(&Interner))
1977+
.flat_map(|(_, substs)| substs.iter(&Interner))
19781978
.filter_map(|arg| arg.ty(&Interner).cloned())
19791979
.map(move |ty| self.derived(ty))
19801980
}
@@ -2115,18 +2115,22 @@ impl Type {
21152115
fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
21162116
let ty = type_.ty.strip_references();
21172117
match ty.kind(&Interner) {
2118-
TyKind::Adt(..) => {
2118+
TyKind::Adt(_, substs) => {
21192119
cb(type_.derived(ty.clone()));
2120+
walk_substs(db, type_, &substs, cb);
21202121
}
2121-
TyKind::AssociatedType(..) => {
2122+
TyKind::AssociatedType(_, substs) => {
21222123
if let Some(_) = ty.associated_type_parent_trait(db) {
21232124
cb(type_.derived(ty.clone()));
21242125
}
2126+
walk_substs(db, type_, &substs, cb);
21252127
}
2126-
TyKind::OpaqueType(..) => {
2128+
TyKind::OpaqueType(_, subst) => {
21272129
if let Some(bounds) = ty.impl_trait_bounds(db) {
21282130
walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
21292131
}
2132+
2133+
walk_substs(db, type_, subst, cb);
21302134
}
21312135
TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
21322136
if let Some(bounds) = ty.impl_trait_bounds(db) {
@@ -2156,11 +2160,17 @@ impl Type {
21562160
walk_type(db, &type_.derived(ty.clone()), cb);
21572161
}
21582162

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+
21592172
_ => {}
21602173
}
2161-
if let Some(substs) = ty.substs() {
2162-
walk_substs(db, type_, &substs, cb);
2163-
}
21642174
}
21652175

21662176
walk_type(db, self, &mut cb);

crates/hir/src/source_analyzer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use hir_def::{
2020
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
2121
use hir_ty::{
2222
diagnostics::{record_literal_missing_fields, record_pattern_missing_fields},
23-
InferenceResult, Interner, Substitution, TyLoweringContext,
23+
InferenceResult, Interner, Substitution, TyExt, TyLoweringContext,
2424
};
2525
use syntax::{
2626
ast::{self, AstNode},
@@ -306,7 +306,7 @@ impl SourceAnalyzer {
306306
let infer = self.infer.as_ref()?;
307307

308308
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;
310310

311311
let (variant, missing_fields, _exhaustive) =
312312
record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?;
@@ -324,7 +324,7 @@ impl SourceAnalyzer {
324324
let infer = self.infer.as_ref()?;
325325

326326
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;
328328

329329
let (variant, missing_fields, _exhaustive) =
330330
record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?;

crates/hir_ty/src/infer/expr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,10 @@ impl<'a> InferenceContext<'a> {
412412

413413
self.unify(&ty, &expected.ty);
414414

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));
416419
let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default();
417420
let variant_data = def_id.map(|it| it.variant_data(self.db.upcast()));
418421
for field in fields.iter() {

crates/hir_ty/src/infer/pat.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ impl<'a> InferenceContext<'a> {
3333
}
3434
self.unify(&ty, expected);
3535

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));
3738

3839
let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
3940
let (pre, post) = match ellipsis {
@@ -74,7 +75,8 @@ impl<'a> InferenceContext<'a> {
7475

7576
self.unify(&ty, expected);
7677

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));
7880

7981
let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
8082
for subpat in subpats {

crates/hir_ty/src/lib.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -198,34 +198,6 @@ impl Ty {
198198
_ => false,
199199
}
200200
}
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-
}
229201
}
230202

231203
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]

crates/hir_ty/src/walk.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,15 @@ impl TypeWalk for Ty {
162162
TyKind::Function(fn_pointer) => {
163163
fn_pointer.substitution.0.walk(f);
164164
}
165-
_ => {
166-
if let Some(substs) = self.substs() {
167-
for t in substs.iter(&Interner) {
168-
t.walk(f);
169-
}
170-
}
165+
TyKind::Adt(_, substs)
166+
| TyKind::FnDef(_, substs)
167+
| TyKind::Tuple(_, substs)
168+
| TyKind::OpaqueType(_, substs)
169+
| TyKind::AssociatedType(_, substs)
170+
| TyKind::Closure(.., substs) => {
171+
substs.walk(f);
171172
}
173+
_ => {}
172174
}
173175
f(self);
174176
}
@@ -199,11 +201,15 @@ impl TypeWalk for Ty {
199201
TyKind::Function(fn_pointer) => {
200202
fn_pointer.substitution.0.walk_mut_binders(f, binders.shifted_in());
201203
}
202-
_ => {
203-
if let Some(substs) = self.substs_mut() {
204-
substs.walk_mut_binders(f, binders);
205-
}
204+
TyKind::Adt(_, substs)
205+
| TyKind::FnDef(_, substs)
206+
| TyKind::Tuple(_, substs)
207+
| TyKind::OpaqueType(_, substs)
208+
| TyKind::AssociatedType(_, substs)
209+
| TyKind::Closure(.., substs) => {
210+
substs.walk_mut_binders(f, binders);
206211
}
212+
_ => {}
207213
}
208214
f(self, binders);
209215
}

0 commit comments

Comments
 (0)