Skip to content

Commit 6973225

Browse files
authored
Unrolled build for #147585
Rollup merge of #147585 - chenyukang:yukang-fix-147513-suppress-private-fields, r=Kivooeo Suppress the error for private fields with non_exhaustive attribute Fixes #147513
2 parents 018d269 + 6a709e7 commit 6973225

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21912191
}
21922192
};
21932193
self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2194-
} else if adt_kind != AdtKind::Union && !remaining_fields.is_empty() {
2194+
} else if adt_kind != AdtKind::Union
2195+
&& !remaining_fields.is_empty()
2196+
//~ non_exhaustive already reported, which will only happen for extern modules
2197+
&& !variant.field_list_has_applicable_non_exhaustive()
2198+
{
21952199
debug!(?remaining_fields);
21962200
let private_fields: Vec<&ty::FieldDef> = variant
21972201
.fields
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Auxiliary crate for testing non-exhaustive struct with private fields
2+
3+
#[non_exhaustive]
4+
pub struct Foo {
5+
pub my_field: u32,
6+
private_field: i32,
7+
}
8+
9+
#[non_exhaustive]
10+
pub struct Bar {
11+
pub my_field: u32,
12+
pub missing_field: i32,
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ aux-build:non_exhaustive_with_private.rs
2+
3+
extern crate non_exhaustive_with_private;
4+
5+
use non_exhaustive_with_private::{Bar, Foo};
6+
7+
fn main() {
8+
let foo = Foo {
9+
//~^ ERROR cannot create non-exhaustive struct using struct expression
10+
my_field: 10,
11+
};
12+
13+
let bar = Bar {
14+
//~^ ERROR cannot create non-exhaustive struct using struct expression
15+
my_field: 10,
16+
};
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0639]: cannot create non-exhaustive struct using struct expression
2+
--> $DIR/non-exhaustive-with-private-fields-147513.rs:8:15
3+
|
4+
LL | let foo = Foo {
5+
| _______________^
6+
LL | |
7+
LL | | my_field: 10,
8+
LL | | };
9+
| |_____^
10+
11+
error[E0639]: cannot create non-exhaustive struct using struct expression
12+
--> $DIR/non-exhaustive-with-private-fields-147513.rs:13:15
13+
|
14+
LL | let bar = Bar {
15+
| _______________^
16+
LL | |
17+
LL | | my_field: 10,
18+
LL | | };
19+
| |_____^
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0639`.

0 commit comments

Comments
 (0)