Skip to content

Commit 71efe90

Browse files
authored
Rollup merge of #93039 - terrarier2111:fix-field-help, r=nagisa
Don't suggest inaccessible fields Fixes: #92999
2 parents 55d5513 + ecd06e1 commit 71efe90

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

compiler/rustc_typeck/src/check/expr.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_hir as hir;
3131
use rustc_hir::def::{CtorKind, DefKind, Res};
3232
use rustc_hir::def_id::DefId;
3333
use rustc_hir::intravisit::Visitor;
34-
use rustc_hir::{ExprKind, QPath};
34+
use rustc_hir::{ExprKind, HirId, QPath};
3535
use rustc_infer::infer;
3636
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
3737
use 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
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile-flags: --crate-type lib
2+
pub struct S {
3+
pub val: string::MyString,
4+
}
5+
6+
pub fn test(s: S) {
7+
dbg!(s.cap) //~ ERROR: no field `cap` on type `S` [E0609]
8+
}
9+
10+
pub(crate) mod string {
11+
12+
pub struct MyString {
13+
buf: MyVec,
14+
}
15+
16+
struct MyVec {
17+
cap: usize,
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0609]: no field `cap` on type `S`
2+
--> $DIR/private-field.rs:7:12
3+
|
4+
LL | dbg!(s.cap)
5+
| ^^^ unknown field
6+
|
7+
= note: available fields are: `val`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0609`.

0 commit comments

Comments
 (0)