rust/tests/ui/structs/default-field-values/visibility.rs
Esteban Küber deef3ebaec Emit a single privacy error for multiple fields on the same struct expression
Collect all unreachable fields in a single struct literal struct and emit a single error, instead of one error per private field.

```
error[E0451]: fields `beta` and `gamma` of struct `Alpha` are private
  --> $DIR/visibility.rs:18:13
   |
LL |     let _x = Alpha {
   |              ----- in this type
LL |         beta: 0,
   |         ^^^^^^^ private field
LL |         ..
   |         ^^ field `gamma` is private
```
2025-01-18 20:33:15 +00:00

43 lines
1.1 KiB
Rust

#![feature(default_field_values)]
pub mod foo {
#[derive(Default)]
pub struct Alpha {
beta: u8 = 42,
gamma: bool = true,
}
}
mod bar {
use crate::foo::Alpha;
fn baz() {
let _x = Alpha { .. };
//~^ ERROR fields `beta` and `gamma` of struct `Alpha` are private
let _x = Alpha {
beta: 0, //~ ERROR fields `beta` and `gamma` of struct `Alpha` are private
gamma: false,
};
let _x = Alpha {
beta: 0, //~ ERROR fields `beta` and `gamma` of struct `Alpha` are private
..
};
let _x = Alpha { beta: 0, .. };
//~^ ERROR fields `beta` and `gamma` of struct `Alpha` are private
let _x = Alpha { beta: 0, ..Default::default() };
//~^ ERROR fields `beta` and `gamma` of struct `Alpha` are private
}
}
pub mod baz {
pub struct S {
x: i32 = 1,
}
}
fn main() {
let _a = baz::S {
.. //~ ERROR field `x` of struct `S` is private
};
let _b = baz::S {
x: 0, //~ ERROR field `x` of struct `S` is private
};
}