@@ -24,6 +24,7 @@ use syntax::source_map::Span;
24
24
use syntax:: util:: lev_distance:: find_best_match_for_name;
25
25
use rustc:: hir;
26
26
use rustc:: hir:: { ExprKind , QPath } ;
27
+ use rustc:: hir:: def_id:: DefId ;
27
28
use rustc:: hir:: def:: { CtorKind , Res , DefKind } ;
28
29
use rustc:: hir:: ptr:: P ;
29
30
use rustc:: infer;
@@ -1336,23 +1337,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1336
1337
autoderef. unambiguous_final_ty ( self ) ;
1337
1338
1338
1339
if let Some ( ( did, field_ty) ) = private_candidate {
1339
- let struct_path = self . tcx ( ) . def_path_str ( did) ;
1340
- let mut err = struct_span_err ! ( self . tcx( ) . sess, expr. span, E0616 ,
1341
- "field `{}` of struct `{}` is private" ,
1342
- field, struct_path) ;
1343
- // Also check if an accessible method exists, which is often what is meant.
1344
- if self . method_exists ( field, expr_t, expr. hir_id , false )
1345
- && !self . expr_in_place ( expr. hir_id )
1346
- {
1347
- self . suggest_method_call (
1348
- & mut err,
1349
- & format ! ( "a method `{}` also exists, call it with parentheses" , field) ,
1350
- field,
1351
- expr_t,
1352
- expr. hir_id ,
1353
- ) ;
1354
- }
1355
- err. emit ( ) ;
1340
+ self . ban_private_field_access ( expr, expr_t, field, did) ;
1356
1341
field_ty
1357
1342
} else if field. name == kw:: Invalid {
1358
1343
self . tcx ( ) . types . err
@@ -1446,6 +1431,37 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1446
1431
}
1447
1432
}
1448
1433
1434
+ fn ban_private_field_access (
1435
+ & self ,
1436
+ expr : & ' tcx hir:: Expr ,
1437
+ expr_t : Ty < ' tcx > ,
1438
+ field : ast:: Ident ,
1439
+ def_id : DefId ,
1440
+ ) {
1441
+ let struct_path = self . tcx ( ) . def_path_str ( def_id) ;
1442
+ let mut err = struct_span_err ! (
1443
+ self . tcx( ) . sess,
1444
+ expr. span,
1445
+ E0616 ,
1446
+ "field `{}` of struct `{}` is private" ,
1447
+ field,
1448
+ struct_path
1449
+ ) ;
1450
+ // Also check if an accessible method exists, which is often what is meant.
1451
+ if self . method_exists ( field, expr_t, expr. hir_id , false )
1452
+ && !self . expr_in_place ( expr. hir_id )
1453
+ {
1454
+ self . suggest_method_call (
1455
+ & mut err,
1456
+ & format ! ( "a method `{}` also exists, call it with parentheses" , field) ,
1457
+ field,
1458
+ expr_t,
1459
+ expr. hir_id ,
1460
+ ) ;
1461
+ }
1462
+ err. emit ( ) ;
1463
+ }
1464
+
1449
1465
fn no_such_field_err < T : Display > ( & self , span : Span , field : T , expr_t : & ty:: TyS < ' _ > )
1450
1466
-> DiagnosticBuilder < ' _ > {
1451
1467
type_error_struct ! ( self . tcx( ) . sess, span, expr_t, E0609 ,
0 commit comments