Skip to content

Commit 3829ece

Browse files
committed
Suppress the error for private fields with non_exhaustive attribute
1 parent 9725c4b commit 3829ece

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2331,7 +2331,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23312331
}
23322332
};
23332333
self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2334-
} else if adt_kind != AdtKind::Union && !remaining_fields.is_empty() {
2334+
} else if adt_kind != AdtKind::Union
2335+
&& !remaining_fields.is_empty()
2336+
//~ non_exhaustive already reported, which will only happen for extern modules
2337+
&& !variant.field_list_has_applicable_non_exhaustive()
2338+
{
23352339
debug!(?remaining_fields);
23362340
let private_fields: Vec<&ty::FieldDef> = variant
23372341
.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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ aux-build:non_exhaustive_with_private.rs
2+
3+
// Test that creating a non-exhaustive struct from another crate
4+
// with private fields produces both errors:
5+
// 1. E0639: cannot create non-exhaustive struct using struct expression
6+
// 2. Error about private fields
7+
8+
extern crate non_exhaustive_with_private;
9+
10+
use non_exhaustive_with_private::{Bar, Foo};
11+
12+
fn main() {
13+
let foo = Foo {
14+
//~^ ERROR cannot create non-exhaustive struct using struct expression
15+
my_field: 10,
16+
};
17+
18+
let bar = Bar {
19+
//~^ ERROR cannot create non-exhaustive struct using struct expression
20+
my_field: 10,
21+
};
22+
}
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:13: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:18: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)