about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2022-02-23 16:45:58 +0100
committerLukas Wirth <lukastw97@gmail.com>2022-02-23 16:45:58 +0100
commit41b6b372a789d738ec38ef6850e924d0514266e6 (patch)
treed85ace0328b043a60dbf1bb3437b81636954f20a
parentde0941301efd7092666911a65b43394d9653fa83 (diff)
downloadrust-41b6b372a789d738ec38ef6850e924d0514266e6.tar.gz
rust-41b6b372a789d738ec38ef6850e924d0514266e6.zip
fix: Resolve private fields in type inference
-rw-r--r--crates/hir_ty/src/infer.rs8
-rw-r--r--crates/hir_ty/src/infer/expr.rs10
2 files changed, 17 insertions, 1 deletions
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs
index 173380654e0..0a50df493fc 100644
--- a/crates/hir_ty/src/infer.rs
+++ b/crates/hir_ty/src/infer.rs
@@ -13,8 +13,8 @@
 //! to certain types. To record this, we use the union-find implementation from
 //! the `ena` crate, which is extracted from rustc.
 
-use std::ops::Index;
 use std::sync::Arc;
+use std::{collections::hash_map::Entry, ops::Index};
 
 use chalk_ir::{cast::Cast, DebruijnIndex, Mutability, Safety, Scalar, TypeFlags};
 use hir_def::{
@@ -459,6 +459,12 @@ impl<'a> InferenceContext<'a> {
         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) {
         self.result.variant_resolutions.insert(id, variant);
     }
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index 13f64d68252..b08a9618d8c 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -532,6 +532,11 @@ impl<'a> InferenceContext<'a> {
                                         .substitute(Interner, &parameters),
                                 )
                             } 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
                             }
                         }
@@ -546,6 +551,11 @@ impl<'a> InferenceContext<'a> {
                                         .substitute(Interner, &parameters),
                                 )
                             } 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
                             }
                         }