Skip to content

Commit 41b6b37

Browse files
committed
fix: Resolve private fields in type inference
1 parent de09413 commit 41b6b37

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

crates/hir_ty/src/infer.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
//! to certain types. To record this, we use the union-find implementation from
1414
//! the `ena` crate, which is extracted from rustc.
1515
16-
use std::ops::Index;
1716
use std::sync::Arc;
17+
use std::{collections::hash_map::Entry, ops::Index};
1818

1919
use chalk_ir::{cast::Cast, DebruijnIndex, Mutability, Safety, Scalar, TypeFlags};
2020
use hir_def::{
@@ -459,6 +459,12 @@ impl<'a> InferenceContext<'a> {
459459
self.result.field_resolutions.insert(expr, field);
460460
}
461461

462+
fn write_field_resolution_if_empty(&mut self, expr: ExprId, field: FieldId) {
463+
if let Entry::Vacant(entry) = self.result.field_resolutions.entry(expr) {
464+
entry.insert(field);
465+
}
466+
}
467+
462468
fn write_variant_resolution(&mut self, id: ExprOrPatId, variant: VariantId) {
463469
self.result.variant_resolutions.insert(id, variant);
464470
}

crates/hir_ty/src/infer/expr.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,11 @@ impl<'a> InferenceContext<'a> {
532532
.substitute(Interner, &parameters),
533533
)
534534
} else {
535+
// Write down the first field resolution even if it is not visible
536+
// This aids IDE features for private fields like goto def and in
537+
// case of autoderef finding an applicable field, this will be
538+
// overwritten in a following cycle
539+
self.write_field_resolution_if_empty(tgt_expr, field);
535540
None
536541
}
537542
}
@@ -546,6 +551,11 @@ impl<'a> InferenceContext<'a> {
546551
.substitute(Interner, &parameters),
547552
)
548553
} else {
554+
// Write down the first field resolution even if it is not visible
555+
// This aids IDE features for private fields like goto def and in
556+
// case of autoderef finding an applicable field, this will be
557+
// overwritten in a following cycle
558+
self.write_field_resolution_if_empty(tgt_expr, field);
549559
None
550560
}
551561
}

0 commit comments

Comments
 (0)