@@ -31,7 +31,7 @@ use rustc_hir as hir;
3131use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
3232use rustc_hir:: def_id:: DefId ;
3333use rustc_hir:: intravisit:: Visitor ;
34- use rustc_hir:: { ExprKind , QPath } ;
34+ use rustc_hir:: { ExprKind , HirId , QPath } ;
3535use rustc_infer:: infer;
3636use rustc_infer:: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
3737use rustc_infer:: infer:: InferOk ;
@@ -1948,7 +1948,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19481948 "ban_nonexisting_field: field={:?}, base={:?}, expr={:?}, expr_ty={:?}" ,
19491949 field, base, expr, expr_t
19501950 ) ;
1951- let mut err = self . no_such_field_err ( field, expr_t) ;
1951+ let mut err = self . no_such_field_err ( field, expr_t, base . hir_id ) ;
19521952
19531953 match * expr_t. peel_refs ( ) . kind ( ) {
19541954 ty:: Array ( _, len) => {
@@ -2186,6 +2186,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21862186 & self ,
21872187 field : Ident ,
21882188 expr_t : & ' tcx ty:: TyS < ' tcx > ,
2189+ id : HirId ,
21892190 ) -> DiagnosticBuilder < ' _ > {
21902191 let span = field. span ;
21912192 debug ! ( "no_such_field_err(span: {:?}, field: {:?}, expr_t: {:?})" , span, field, expr_t) ;
@@ -2203,9 +2204,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22032204 // try to add a suggestion in case the field is a nested field of a field of the Adt
22042205 if let Some ( ( fields, substs) ) = self . get_field_candidates ( span, & expr_t) {
22052206 for candidate_field in fields. iter ( ) {
2206- if let Some ( field_path) =
2207- self . check_for_nested_field ( span, field, candidate_field, substs, vec ! [ ] )
2208- {
2207+ if let Some ( field_path) = self . check_for_nested_field (
2208+ span,
2209+ field,
2210+ candidate_field,
2211+ substs,
2212+ vec ! [ ] ,
2213+ self . tcx . parent_module ( id) . to_def_id ( ) ,
2214+ ) {
22092215 let field_path_str = field_path
22102216 . iter ( )
22112217 . map ( |id| id. name . to_ident_string ( ) )
@@ -2257,6 +2263,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22572263 candidate_field : & ty:: FieldDef ,
22582264 subst : SubstsRef < ' tcx > ,
22592265 mut field_path : Vec < Ident > ,
2266+ id : DefId ,
22602267 ) -> Option < Vec < Ident > > {
22612268 debug ! (
22622269 "check_for_nested_field(span: {:?}, candidate_field: {:?}, field_path: {:?}" ,
@@ -2276,17 +2283,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22762283 let field_ty = candidate_field. ty ( self . tcx , subst) ;
22772284 if let Some ( ( nested_fields, subst) ) = self . get_field_candidates ( span, & field_ty) {
22782285 for field in nested_fields. iter ( ) {
2279- let ident = field. ident ( self . tcx ) . normalize_to_macros_2_0 ( ) ;
2280- if ident == target_field {
2281- return Some ( field_path) ;
2282- } else {
2286+ let accessible = field. vis . is_accessible_from ( id, self . tcx ) ;
2287+ if accessible {
2288+ let ident = field. ident ( self . tcx ) . normalize_to_macros_2_0 ( ) ;
2289+ if ident == target_field {
2290+ return Some ( field_path) ;
2291+ }
22832292 let field_path = field_path. clone ( ) ;
22842293 if let Some ( path) = self . check_for_nested_field (
22852294 span,
22862295 target_field,
22872296 field,
22882297 subst,
22892298 field_path,
2299+ id,
22902300 ) {
22912301 return Some ( path) ;
22922302 }
0 commit comments