Skip to content

Commit 836e0cb

Browse files
committed
chore: Remove dead field from InferenceContext
1 parent 1494a27 commit 836e0cb

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/infer.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -460,19 +460,17 @@ pub struct InferenceResult {
460460
/// Whenever a tuple field expression access a tuple field, we allocate a tuple id in
461461
/// [`InferenceContext`] and store the tuples substitution there. This map is the reverse of
462462
/// that which allows us to resolve a [`TupleFieldId`]s type.
463-
pub tuple_field_access_types: FxHashMap<TupleId, Substitution>,
463+
tuple_field_access_types: FxHashMap<TupleId, Substitution>,
464464
/// During inference this field is empty and [`InferenceContext::diagnostics`] is filled instead.
465-
pub diagnostics: Vec<InferenceDiagnostic>,
466-
pub type_of_expr: ArenaMap<ExprId, Ty>,
465+
diagnostics: Vec<InferenceDiagnostic>,
466+
pub(crate) type_of_expr: ArenaMap<ExprId, Ty>,
467467
/// For each pattern record the type it resolves to.
468468
///
469469
/// **Note**: When a pattern type is resolved it may still contain
470470
/// unresolved or missing subpatterns or subpatterns of mismatched types.
471-
pub type_of_pat: ArenaMap<PatId, Ty>,
472-
pub type_of_binding: ArenaMap<BindingId, Ty>,
473-
pub type_of_rpit: ArenaMap<ImplTraitIdx, Ty>,
474-
/// Type of the result of `.into_iter()` on the for. `ExprId` is the one of the whole for loop.
475-
pub type_of_for_iterator: FxHashMap<ExprId, Ty>,
471+
pub(crate) type_of_pat: ArenaMap<PatId, Ty>,
472+
pub(crate) type_of_binding: ArenaMap<BindingId, Ty>,
473+
pub(crate) type_of_rpit: ArenaMap<ImplTraitIdx, Ty>,
476474
type_mismatches: FxHashMap<ExprOrPatId, TypeMismatch>,
477475
/// Whether there are any type-mismatching errors in the result.
478476
// FIXME: This isn't as useful as initially thought due to us falling back placeholders to
@@ -483,7 +481,7 @@ pub struct InferenceResult {
483481
// FIXME: Move this into `InferenceContext`
484482
standard_types: InternedStandardTypes,
485483
/// Stores the types which were implicitly dereferenced in pattern binding modes.
486-
pub pat_adjustments: FxHashMap<PatId, Vec<Ty>>,
484+
pub(crate) pat_adjustments: FxHashMap<PatId, Vec<Ty>>,
487485
/// Stores the binding mode (`ref` in `let ref x = 2`) of bindings.
488486
///
489487
/// This one is tied to the `PatId` instead of `BindingId`, because in some rare cases, a binding in an
@@ -497,12 +495,12 @@ pub struct InferenceResult {
497495
/// }
498496
/// ```
499497
/// the first `rest` has implicit `ref` binding mode, but the second `rest` binding mode is `move`.
500-
pub binding_modes: ArenaMap<PatId, BindingMode>,
501-
pub expr_adjustments: FxHashMap<ExprId, Box<[Adjustment]>>,
498+
pub(crate) binding_modes: ArenaMap<PatId, BindingMode>,
499+
pub(crate) expr_adjustments: FxHashMap<ExprId, Box<[Adjustment]>>,
502500
pub(crate) closure_info: FxHashMap<ClosureId, (Vec<CapturedItem>, FnTrait)>,
503501
// FIXME: remove this field
504502
pub mutated_bindings_in_closure: FxHashSet<BindingId>,
505-
pub coercion_casts: FxHashSet<ExprId>,
503+
pub(crate) coercion_casts: FxHashSet<ExprId>,
506504
}
507505

508506
impl InferenceResult {
@@ -566,6 +564,26 @@ impl InferenceResult {
566564
pub fn is_erroneous(&self) -> bool {
567565
self.has_errors && self.type_of_expr.iter().count() == 0
568566
}
567+
568+
pub fn diagnostics(&self) -> &[InferenceDiagnostic] {
569+
&self.diagnostics
570+
}
571+
572+
pub fn tuple_field_access_type(&self, id: TupleId) -> &Substitution {
573+
&self.tuple_field_access_types[&id]
574+
}
575+
576+
pub fn pat_adjustment(&self, id: PatId) -> Option<&[Ty]> {
577+
self.pat_adjustments.get(&id).map(|it| &**it)
578+
}
579+
580+
pub fn expr_adjustment(&self, id: ExprId) -> Option<&[Adjustment]> {
581+
self.expr_adjustments.get(&id).map(|it| &**it)
582+
}
583+
584+
pub fn binding_mode(&self, id: PatId) -> Option<BindingMode> {
585+
self.binding_modes.get(id).copied()
586+
}
569587
}
570588

571589
impl Index<ExprId> for InferenceResult {
@@ -772,7 +790,6 @@ impl<'db> InferenceContext<'db> {
772790
type_of_pat,
773791
type_of_binding,
774792
type_of_rpit,
775-
type_of_for_iterator,
776793
type_mismatches,
777794
has_errors,
778795
standard_types: _,
@@ -832,11 +849,6 @@ impl<'db> InferenceContext<'db> {
832849
*has_errors = *has_errors || ty.contains_unknown();
833850
}
834851
type_of_rpit.shrink_to_fit();
835-
for ty in type_of_for_iterator.values_mut() {
836-
*ty = table.resolve_completely(ty.clone());
837-
*has_errors = *has_errors || ty.contains_unknown();
838-
}
839-
type_of_for_iterator.shrink_to_fit();
840852

841853
*has_errors |= !type_mismatches.is_empty();
842854

src/tools/rust-analyzer/crates/hir/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,9 @@ impl TupleField {
12601260
}
12611261

12621262
pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> Type<'db> {
1263-
let ty = db.infer(self.owner).tuple_field_access_types[&self.tuple]
1263+
let ty = db
1264+
.infer(self.owner)
1265+
.tuple_field_access_type(self.tuple)
12641266
.as_slice(Interner)
12651267
.get(self.index as usize)
12661268
.and_then(|arg| arg.ty(Interner))
@@ -1927,7 +1929,7 @@ impl DefWithBody {
19271929
expr_store_diagnostics(db, acc, &source_map);
19281930

19291931
let infer = db.infer(self.into());
1930-
for d in &infer.diagnostics {
1932+
for d in infer.diagnostics() {
19311933
acc.extend(AnyDiagnostic::inference_diagnostic(
19321934
db,
19331935
self.into(),

src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<'db> SourceAnalyzer<'db> {
254254
// expressions nor patterns).
255255
let expr_id = self.expr_id(expr.clone())?.as_expr()?;
256256
let infer = self.infer()?;
257-
infer.expr_adjustments.get(&expr_id).map(|v| &**v)
257+
infer.expr_adjustment(expr_id)
258258
}
259259

260260
pub(crate) fn type_of_type(
@@ -286,7 +286,7 @@ impl<'db> SourceAnalyzer<'db> {
286286
let infer = self.infer()?;
287287
let coerced = expr_id
288288
.as_expr()
289-
.and_then(|expr_id| infer.expr_adjustments.get(&expr_id))
289+
.and_then(|expr_id| infer.expr_adjustment(expr_id))
290290
.and_then(|adjusts| adjusts.last().map(|adjust| adjust.target.clone()));
291291
let ty = infer[expr_id].clone();
292292
let mk_ty = |ty| Type::new_with_resolver(db, &self.resolver, ty);
@@ -302,12 +302,11 @@ impl<'db> SourceAnalyzer<'db> {
302302
let infer = self.infer()?;
303303
let coerced = match expr_or_pat_id {
304304
ExprOrPatId::ExprId(idx) => infer
305-
.expr_adjustments
306-
.get(&idx)
305+
.expr_adjustment(idx)
307306
.and_then(|adjusts| adjusts.last().cloned())
308307
.map(|adjust| adjust.target),
309308
ExprOrPatId::PatId(idx) => {
310-
infer.pat_adjustments.get(&idx).and_then(|adjusts| adjusts.last().cloned())
309+
infer.pat_adjustment(idx).and_then(|adjusts| adjusts.last().cloned())
311310
}
312311
};
313312

@@ -345,7 +344,7 @@ impl<'db> SourceAnalyzer<'db> {
345344
) -> Option<BindingMode> {
346345
let id = self.pat_id(&pat.clone().into())?;
347346
let infer = self.infer()?;
348-
infer.binding_modes.get(id.as_pat()?).map(|bm| match bm {
347+
infer.binding_mode(id.as_pat()?).map(|bm| match bm {
349348
hir_ty::BindingMode::Move => BindingMode::Move,
350349
hir_ty::BindingMode::Ref(hir_ty::Mutability::Mut) => BindingMode::Ref(Mutability::Mut),
351350
hir_ty::BindingMode::Ref(hir_ty::Mutability::Not) => {
@@ -362,8 +361,7 @@ impl<'db> SourceAnalyzer<'db> {
362361
let infer = self.infer()?;
363362
Some(
364363
infer
365-
.pat_adjustments
366-
.get(&pat_id.as_pat()?)?
364+
.pat_adjustment(pat_id.as_pat()?)?
367365
.iter()
368366
.map(|ty| Type::new_with_resolver(db, &self.resolver, ty.clone()))
369367
.collect(),
@@ -736,7 +734,7 @@ impl<'db> SourceAnalyzer<'db> {
736734
let variant = self.infer()?.variant_resolution_for_pat(pat_id.as_pat()?)?;
737735
let variant_data = variant.fields(db);
738736
let field = FieldId { parent: variant, local_id: variant_data.field(&field_name)? };
739-
let (adt, subst) = self.infer()?.type_of_pat.get(pat_id.as_pat()?)?.as_adt()?;
737+
let (adt, subst) = self.infer()?[pat_id.as_pat()?].as_adt()?;
740738
let field_ty =
741739
db.field_types(variant).get(field.local_id)?.clone().substitute(Interner, subst);
742740
Some((
@@ -1250,7 +1248,7 @@ impl<'db> SourceAnalyzer<'db> {
12501248
let infer = self.infer()?;
12511249

12521250
let pat_id = self.pat_id(&pattern.clone().into())?.as_pat()?;
1253-
let substs = infer.type_of_pat[pat_id].as_adt()?.1;
1251+
let substs = infer[pat_id].as_adt()?.1;
12541252

12551253
let (variant, missing_fields, _exhaustive) =
12561254
record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?;
@@ -1786,8 +1784,8 @@ pub(crate) fn name_hygiene(db: &dyn HirDatabase, name: InFile<&SyntaxNode>) -> H
17861784
}
17871785

17881786
fn type_of_expr_including_adjust(infer: &InferenceResult, id: ExprId) -> Option<&Ty> {
1789-
match infer.expr_adjustments.get(&id).and_then(|adjustments| adjustments.last()) {
1787+
match infer.expr_adjustment(id).and_then(|adjustments| adjustments.last()) {
17901788
Some(adjustment) => Some(&adjustment.target),
1791-
None => infer.type_of_expr.get(id),
1789+
None => Some(&infer[id]),
17921790
}
17931791
}

0 commit comments

Comments
 (0)