fix: Resolve private fields in type inference

This commit is contained in:
Lukas Wirth 2022-02-23 16:45:58 +01:00
parent de0941301e
commit 41b6b372a7
2 changed files with 17 additions and 1 deletions

View file

@ -13,8 +13,8 @@
//! to certain types. To record this, we use the union-find implementation from //! to certain types. To record this, we use the union-find implementation from
//! the `ena` crate, which is extracted from rustc. //! the `ena` crate, which is extracted from rustc.
use std::ops::Index;
use std::sync::Arc; use std::sync::Arc;
use std::{collections::hash_map::Entry, ops::Index};
use chalk_ir::{cast::Cast, DebruijnIndex, Mutability, Safety, Scalar, TypeFlags}; use chalk_ir::{cast::Cast, DebruijnIndex, Mutability, Safety, Scalar, TypeFlags};
use hir_def::{ use hir_def::{
@ -459,6 +459,12 @@ impl<'a> InferenceContext<'a> {
self.result.field_resolutions.insert(expr, field); self.result.field_resolutions.insert(expr, field);
} }
fn write_field_resolution_if_empty(&mut self, expr: ExprId, field: FieldId) {
if let Entry::Vacant(entry) = self.result.field_resolutions.entry(expr) {
entry.insert(field);
}
}
fn write_variant_resolution(&mut self, id: ExprOrPatId, variant: VariantId) { fn write_variant_resolution(&mut self, id: ExprOrPatId, variant: VariantId) {
self.result.variant_resolutions.insert(id, variant); self.result.variant_resolutions.insert(id, variant);
} }

View file

@ -532,6 +532,11 @@ impl<'a> InferenceContext<'a> {
.substitute(Interner, &parameters), .substitute(Interner, &parameters),
) )
} else { } else {
// Write down the first field resolution even if it is not visible
// This aids IDE features for private fields like goto def and in
// case of autoderef finding an applicable field, this will be
// overwritten in a following cycle
self.write_field_resolution_if_empty(tgt_expr, field);
None None
} }
} }
@ -546,6 +551,11 @@ impl<'a> InferenceContext<'a> {
.substitute(Interner, &parameters), .substitute(Interner, &parameters),
) )
} else { } else {
// Write down the first field resolution even if it is not visible
// This aids IDE features for private fields like goto def and in
// case of autoderef finding an applicable field, this will be
// overwritten in a following cycle
self.write_field_resolution_if_empty(tgt_expr, field);
None None
} }
} }