Rollup merge of #147585 - chenyukang:yukang-fix-147513-suppress-private-fields, r=Kivooeo

Suppress the error for private fields with non_exhaustive attribute

Fixes rust-lang/rust#147513
This commit is contained in:
Matthias Krüger 2025-12-09 06:17:21 +01:00 committed by GitHub
commit 3c19acc607
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 58 additions and 1 deletions

View file

@ -2191,7 +2191,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
};
self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
} else if adt_kind != AdtKind::Union && !remaining_fields.is_empty() {
} else if adt_kind != AdtKind::Union
&& !remaining_fields.is_empty()
//~ non_exhaustive already reported, which will only happen for extern modules
&& !variant.field_list_has_applicable_non_exhaustive()
{
debug!(?remaining_fields);
let private_fields: Vec<&ty::FieldDef> = variant
.fields

View file

@ -0,0 +1,13 @@
// Auxiliary crate for testing non-exhaustive struct with private fields
#[non_exhaustive]
pub struct Foo {
pub my_field: u32,
private_field: i32,
}
#[non_exhaustive]
pub struct Bar {
pub my_field: u32,
pub missing_field: i32,
}

View file

@ -0,0 +1,17 @@
//@ aux-build:non_exhaustive_with_private.rs
extern crate non_exhaustive_with_private;
use non_exhaustive_with_private::{Bar, Foo};
fn main() {
let foo = Foo {
//~^ ERROR cannot create non-exhaustive struct using struct expression
my_field: 10,
};
let bar = Bar {
//~^ ERROR cannot create non-exhaustive struct using struct expression
my_field: 10,
};
}

View file

@ -0,0 +1,23 @@
error[E0639]: cannot create non-exhaustive struct using struct expression
--> $DIR/non-exhaustive-with-private-fields-147513.rs:8:15
|
LL | let foo = Foo {
| _______________^
LL | |
LL | | my_field: 10,
LL | | };
| |_____^
error[E0639]: cannot create non-exhaustive struct using struct expression
--> $DIR/non-exhaustive-with-private-fields-147513.rs:13:15
|
LL | let bar = Bar {
| _______________^
LL | |
LL | | my_field: 10,
LL | | };
| |_____^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0639`.