Skip to content

Commit bc17e62

Browse files
Rollup merge of rust-lang#106600 - compiler-errors:no-private-field-ty-err, r=estebank
Suppress type errors that come from private fields Fixes rust-lang#57320 There was some discussion here (rust-lang#57320 (comment)), but I honestly think the second error is worth suppressing regardless. I would be open to feedback though -- perhaps we can suppress the `.len()` suggestion if there's type error (since we have access to [`Expectation`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_typeck/enum.Expectation.html), we can determine that). r? `@estebank`
2 parents 236a93d + 59aa421 commit bc17e62

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2217,7 +2217,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22172217
self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
22182218
return field_ty;
22192219
}
2220-
private_candidate = Some((adjustments, base_def.did(), field_ty));
2220+
private_candidate = Some((adjustments, base_def.did()));
22212221
}
22222222
}
22232223
ty::Tuple(tys) => {
@@ -2240,12 +2240,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22402240
}
22412241
self.structurally_resolved_type(autoderef.span(), autoderef.final_ty(false));
22422242

2243-
if let Some((adjustments, did, field_ty)) = private_candidate {
2243+
if let Some((adjustments, did)) = private_candidate {
22442244
// (#90483) apply adjustments to avoid ExprUseVisitor from
22452245
// creating erroneous projection.
22462246
self.apply_adjustments(base, adjustments);
22472247
self.ban_private_field_access(expr, base_ty, field, did);
2248-
return field_ty;
2248+
return self.tcx().ty_error();
22492249
}
22502250

22512251
if field.name == kw::Empty {

src/test/ui/issues/issue-25386.rs

-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ macro_rules! check_ptr_exist {
2424
fn main() {
2525
let item = stuff::Item::new();
2626
println!("{}", check_ptr_exist!(item, name));
27-
//~^ ERROR field `name` of struct `CObj` is private
2827
}

src/test/ui/issues/issue-25386.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ LL | println!("{}", check_ptr_exist!(item, name));
99
|
1010
= note: this error originates in the macro `check_ptr_exist` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error[E0616]: field `name` of struct `CObj` is private
13-
--> $DIR/issue-25386.rs:26:43
14-
|
15-
LL | println!("{}", check_ptr_exist!(item, name));
16-
| ^^^^ private field
17-
18-
error: aborting due to 2 previous errors
12+
error: aborting due to previous error
1913

2014
For more information about this error, try `rustc --explain E0616`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fn main() {
2+
let x = foo::Foo::default();
3+
if x.len {
4+
//~^ ERROR field `len` of struct `Foo` is private
5+
println!("foo");
6+
}
7+
}
8+
9+
mod foo {
10+
#[derive(Default)]
11+
pub struct Foo {
12+
len: String,
13+
}
14+
15+
impl Foo {
16+
pub fn len(&self) -> usize {
17+
42
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0616]: field `len` of struct `Foo` is private
2+
--> $DIR/private-field-ty-err.rs:3:10
3+
|
4+
LL | if x.len {
5+
| ^^^ private field
6+
|
7+
help: a method `len` also exists, call it with parentheses
8+
|
9+
LL | if x.len() {
10+
| ++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0616`.

0 commit comments

Comments
 (0)