Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress type errors that come from private fields #106600

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2217,7 +2217,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
return field_ty;
}
private_candidate = Some((adjustments, base_def.did(), field_ty));
private_candidate = Some((adjustments, base_def.did()));
}
}
ty::Tuple(tys) => {
Expand All @@ -2240,12 +2240,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
self.structurally_resolved_type(autoderef.span(), autoderef.final_ty(false));

if let Some((adjustments, did, field_ty)) = private_candidate {
if let Some((adjustments, did)) = private_candidate {
// (#90483) apply adjustments to avoid ExprUseVisitor from
// creating erroneous projection.
self.apply_adjustments(base, adjustments);
self.ban_private_field_access(expr, base_ty, field, did);
return field_ty;
return self.tcx().ty_error();
}

if field.name == kw::Empty {
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/issues/issue-25386.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ macro_rules! check_ptr_exist {
fn main() {
let item = stuff::Item::new();
println!("{}", check_ptr_exist!(item, name));
//~^ ERROR field `name` of struct `CObj` is private
}
8 changes: 1 addition & 7 deletions src/test/ui/issues/issue-25386.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ LL | println!("{}", check_ptr_exist!(item, name));
|
= note: this error originates in the macro `check_ptr_exist` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0616]: field `name` of struct `CObj` is private
--> $DIR/issue-25386.rs:26:43
|
LL | println!("{}", check_ptr_exist!(item, name));
| ^^^^ private field

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0616`.
20 changes: 20 additions & 0 deletions src/test/ui/privacy/private-field-ty-err.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
fn main() {
let x = foo::Foo::default();
if x.len {
//~^ ERROR field `len` of struct `Foo` is private
println!("foo");
}
}

mod foo {
#[derive(Default)]
pub struct Foo {
len: String,
}

impl Foo {
pub fn len(&self) -> usize {
42
}
}
}
14 changes: 14 additions & 0 deletions src/test/ui/privacy/private-field-ty-err.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0616]: field `len` of struct `Foo` is private
--> $DIR/private-field-ty-err.rs:3:10
|
LL | if x.len {
| ^^^ private field
|
help: a method `len` also exists, call it with parentheses
|
LL | if x.len() {
| ++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0616`.