mirror of
https://github.com/rust-lang/rust.git
synced 2025-10-31 21:16:44 +00:00
When trying to construct a struct that has a public field of a private type, suggest using `..` if that field has a default value.
```
error[E0603]: struct `Priv1` is private
--> $DIR/non-exhaustive-ctor.rs:25:39
|
LL | let _ = S { field: (), field1: m::Priv1 {} };
| ------ ^^^^^ private struct
| |
| while setting this field
|
note: the struct `Priv1` is defined here
--> $DIR/non-exhaustive-ctor.rs:14:4
|
LL | struct Priv1 {}
| ^^^^^^^^^^^^
help: the field `field1` you're trying to set has a default value, you can use `..` to use it
|
LL | let _ = S { field: (), .. };
| ~~
```
30 lines
752 B
Rust
30 lines
752 B
Rust
//@ aux-build:struct_field_default.rs
|
|
#![feature(default_field_values)]
|
|
|
|
extern crate struct_field_default as xc;
|
|
|
|
use m::S;
|
|
|
|
mod m {
|
|
pub struct S {
|
|
pub field: () = (),
|
|
pub field1: Priv1 = Priv1 {},
|
|
pub field2: Priv2 = Priv2,
|
|
}
|
|
struct Priv1 {}
|
|
struct Priv2;
|
|
}
|
|
|
|
fn main() {
|
|
let _ = S { field: (), field1: m::Priv1 {} };
|
|
//~^ ERROR missing field `field2`
|
|
//~| ERROR struct `Priv1` is private
|
|
let _ = S { field: (), field1: m::Priv1 {}, field2: m::Priv2 };
|
|
//~^ ERROR struct `Priv1` is private
|
|
//~| ERROR unit struct `Priv2` is private
|
|
let _ = xc::B { a: xc::Priv };
|
|
//~^ ERROR unit struct `Priv` is private
|
|
let _ = xc::C { a: xc::Priv };
|
|
//~^ ERROR unit struct `Priv` is private
|
|
}
|