about summary refs log tree commit diff
path: root/tests/ui/structs
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2025-01-18 20:07:05 +0000
committerEsteban Küber <esteban@kuber.com.ar>2025-01-18 20:33:15 +0000
commitdeef3ebaec2ff0ff818161f3b9b86a42bed5fe36 (patch)
tree29becbbad6d9a19783b3a25dfbcc54e3b5318c80 /tests/ui/structs
parentbbcf26fc33afb4695014aff22fa77dcb7d9eb715 (diff)
downloadrust-deef3ebaec2ff0ff818161f3b9b86a42bed5fe36.tar.gz
rust-deef3ebaec2ff0ff818161f3b9b86a42bed5fe36.zip
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
```
Diffstat (limited to 'tests/ui/structs')
-rw-r--r--tests/ui/structs/default-field-values/visibility.rs23
-rw-r--r--tests/ui/structs/default-field-values/visibility.stderr62
2 files changed, 64 insertions, 21 deletions
diff --git a/tests/ui/structs/default-field-values/visibility.rs b/tests/ui/structs/default-field-values/visibility.rs
index e1f76a5fe24..ff1245551b0 100644
--- a/tests/ui/structs/default-field-values/visibility.rs
+++ b/tests/ui/structs/default-field-values/visibility.rs
@@ -1,5 +1,6 @@
 #![feature(default_field_values)]
 pub mod foo {
+    #[derive(Default)]
     pub struct Alpha {
         beta: u8 = 42,
         gamma: bool = true,
@@ -7,10 +8,22 @@ pub mod foo {
 }
 
 mod bar {
+    use crate::foo::Alpha;
     fn baz() {
-        let x = crate::foo::Alpha { .. };
-        //~^ ERROR field `beta` of struct `Alpha` is private
-        //~| ERROR field `gamma` of struct `Alpha` is private
+        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
     }
 }
 
@@ -20,10 +33,10 @@ pub mod baz {
     }
 }
 fn main() {
-    let a = baz::S {
+    let _a = baz::S {
         .. //~ ERROR field `x` of struct `S` is private
     };
-    let b = baz::S {
+    let _b = baz::S {
         x: 0, //~ ERROR field `x` of struct `S` is private
     };
 }
diff --git a/tests/ui/structs/default-field-values/visibility.stderr b/tests/ui/structs/default-field-values/visibility.stderr
index 666782933d3..38b96033252 100644
--- a/tests/ui/structs/default-field-values/visibility.stderr
+++ b/tests/ui/structs/default-field-values/visibility.stderr
@@ -1,31 +1,61 @@
 error[E0451]: field `x` of struct `S` is private
-  --> $DIR/visibility.rs:24:9
+  --> $DIR/visibility.rs:37:9
    |
-LL |     let a = baz::S {
-   |             ------ in this type
+LL |     let _a = baz::S {
+   |              ------ in this type
 LL |         ..
    |         ^^ field `x` is private
 
 error[E0451]: field `x` of struct `S` is private
-  --> $DIR/visibility.rs:27:9
+  --> $DIR/visibility.rs:40:9
    |
-LL |     let b = baz::S {
-   |             ------ in this type
+LL |     let _b = baz::S {
+   |              ------ in this type
 LL |         x: 0,
-   |         ^^^^ private field
+   |         ^ private field
 
-error[E0451]: field `beta` of struct `Alpha` is private
-  --> $DIR/visibility.rs:11:37
+error[E0451]: fields `beta` and `gamma` of struct `Alpha` are private
+  --> $DIR/visibility.rs:13:26
    |
-LL |         let x = crate::foo::Alpha { .. };
-   |                                     ^^ field `beta` is private
+LL |         let _x = Alpha { .. };
+   |                          ^^ fields `beta` and `gamma` are private
 
-error[E0451]: field `gamma` of struct `Alpha` is private
-  --> $DIR/visibility.rs:11:37
+error[E0451]: fields `beta` and `gamma` of struct `Alpha` are private
+  --> $DIR/visibility.rs:16:13
    |
-LL |         let x = crate::foo::Alpha { .. };
-   |                                     ^^ field `gamma` is private
+LL |         let _x = Alpha {
+   |                  ----- in this type
+LL |             beta: 0,
+   |             ^^^^ private field
+LL |             gamma: false,
+   |             ^^^^^ private field
 
-error: aborting due to 4 previous errors
+error[E0451]: fields `beta` and `gamma` of struct `Alpha` are private
+  --> $DIR/visibility.rs:20:13
+   |
+LL |         let _x = Alpha {
+   |                  ----- in this type
+LL |             beta: 0,
+   |             ^^^^^^^ private field
+LL |             ..
+   |             ^^ field `gamma` is private
+
+error[E0451]: fields `beta` and `gamma` of struct `Alpha` are private
+  --> $DIR/visibility.rs:23:26
+   |
+LL |         let _x = Alpha { beta: 0, .. };
+   |                          ^^^^^^^  ^^ field `gamma` is private
+   |                          |
+   |                          private field
+
+error[E0451]: fields `beta` and `gamma` of struct `Alpha` are private
+  --> $DIR/visibility.rs:25:26
+   |
+LL |         let _x = Alpha { beta: 0, ..Default::default() };
+   |                          ^^^^^^^    ^^^^^^^^^^^^^^^^^^ field `gamma` is private
+   |                          |
+   |                          private field
+
+error: aborting due to 7 previous errors
 
 For more information about this error, try `rustc --explain E0451`.