Skip to content

Commit 0b713ae

Browse files
committed
typeck: extract ban_private_field_access
1 parent 60960a2 commit 0b713ae

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/librustc_typeck/check/expr.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use syntax::source_map::Span;
2424
use syntax::util::lev_distance::find_best_match_for_name;
2525
use rustc::hir;
2626
use rustc::hir::{ExprKind, QPath};
27+
use rustc::hir::def_id::DefId;
2728
use rustc::hir::def::{CtorKind, Res, DefKind};
2829
use rustc::hir::ptr::P;
2930
use rustc::infer;
@@ -1336,23 +1337,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13361337
autoderef.unambiguous_final_ty(self);
13371338

13381339
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);
13561341
field_ty
13571342
} else if field.name == kw::Invalid {
13581343
self.tcx().types.err
@@ -1446,6 +1431,37 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14461431
}
14471432
}
14481433

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+
14491465
fn no_such_field_err<T: Display>(&self, span: Span, field: T, expr_t: &ty::TyS<'_>)
14501466
-> DiagnosticBuilder<'_> {
14511467
type_error_struct!(self.tcx().sess, span, expr_t, E0609,

0 commit comments

Comments
 (0)