11//! Type inference for expressions.
22
33use std:: {
4- collections:: hash_map:: Entry ,
54 iter:: { repeat, repeat_with} ,
65 mem,
76} ;
@@ -521,6 +520,7 @@ impl<'a> InferenceContext<'a> {
521520 let receiver_ty = self . infer_expr_inner ( * expr, & Expectation :: none ( ) ) ;
522521
523522 let mut autoderef = Autoderef :: new ( & mut self . table , receiver_ty) ;
523+ let mut private_field = None ;
524524 let ty = autoderef. by_ref ( ) . find_map ( |( derefed_ty, _) | {
525525 let ( field_id, parameters) = match derefed_ty. kind ( Interner ) {
526526 TyKind :: Tuple ( _, substs) => {
@@ -547,13 +547,8 @@ impl<'a> InferenceContext<'a> {
547547 let is_visible = self . db . field_visibilities ( field_id. parent ) [ field_id. local_id ]
548548 . is_visible_from ( self . db . upcast ( ) , self . resolver . module ( ) ) ;
549549 if !is_visible {
550- // Write down the first field resolution even if it is not visible
551- // This aids IDE features for private fields like goto def and in
552- // case of autoderef finding an applicable field, this will be
553- // overwritten in a following cycle
554- if let Entry :: Vacant ( entry) = self . result . field_resolutions . entry ( tgt_expr)
555- {
556- entry. insert ( field_id) ;
550+ if private_field. is_none ( ) {
551+ private_field = Some ( field_id) ;
557552 }
558553 return None ;
559554 }
@@ -572,7 +567,17 @@ impl<'a> InferenceContext<'a> {
572567 let ty = self . normalize_associated_types_in ( ty) ;
573568 ty
574569 }
575- _ => self . err_ty ( ) ,
570+ _ => {
571+ // Write down the first private field resolution if we found no field
572+ // This aids IDE features for private fields like goto def
573+ if let Some ( field) = private_field {
574+ self . result . field_resolutions . insert ( tgt_expr, field) ;
575+ self . result
576+ . diagnostics
577+ . push ( InferenceDiagnostic :: PrivateField { expr : tgt_expr, field } ) ;
578+ }
579+ self . err_ty ( )
580+ }
576581 } ;
577582 ty
578583 }
0 commit comments