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 00:30:50 +0000
committerEsteban Küber <esteban@kuber.com.ar>2025-01-18 00:30:50 +0000
commit3e99055c4099f0ccb3e5ce5768c32af57d48c79b (patch)
treea3ba8c2fdd4d3c7701cf3a5600108f9262fb422d /tests/ui/structs
parentbcd0683e5dce1945b5d940714742e7502883bb5c (diff)
downloadrust-3e99055c4099f0ccb3e5ce5768c32af57d48c79b.tar.gz
rust-3e99055c4099f0ccb3e5ce5768c32af57d48c79b.zip
Add test for construction of struct with private field with default value
```
error[E0451]: field `beta` of struct `Alpha` is private
  --> $DIR/visibility.rs:11:37
   |
LL |         let x = crate::foo::Alpha { .. };
   |                                     ^^ field `beta` is private
```
Diffstat (limited to 'tests/ui/structs')
-rw-r--r--tests/ui/structs/default-field-values/visibility.rs29
-rw-r--r--tests/ui/structs/default-field-values/visibility.stderr27
2 files changed, 56 insertions, 0 deletions
diff --git a/tests/ui/structs/default-field-values/visibility.rs b/tests/ui/structs/default-field-values/visibility.rs
new file mode 100644
index 00000000000..e1f76a5fe24
--- /dev/null
+++ b/tests/ui/structs/default-field-values/visibility.rs
@@ -0,0 +1,29 @@
+#![feature(default_field_values)]
+pub mod foo {
+    pub struct Alpha {
+        beta: u8 = 42,
+        gamma: bool = true,
+    }
+}
+
+mod bar {
+    fn baz() {
+        let x = crate::foo::Alpha { .. };
+        //~^ ERROR field `beta` of struct `Alpha` is private
+        //~| ERROR field `gamma` of struct `Alpha` is 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
+    };
+}
diff --git a/tests/ui/structs/default-field-values/visibility.stderr b/tests/ui/structs/default-field-values/visibility.stderr
new file mode 100644
index 00000000000..63721d79be5
--- /dev/null
+++ b/tests/ui/structs/default-field-values/visibility.stderr
@@ -0,0 +1,27 @@
+error[E0451]: field `x` of struct `S` is private
+  --> $DIR/visibility.rs:24:9
+   |
+LL |         ..
+   |         ^^ field `x` is private
+
+error[E0451]: field `x` of struct `S` is private
+  --> $DIR/visibility.rs:27:9
+   |
+LL |         x: 0,
+   |         ^^^^ private field
+
+error[E0451]: field `beta` of struct `Alpha` is private
+  --> $DIR/visibility.rs:11:37
+   |
+LL |         let x = crate::foo::Alpha { .. };
+   |                                     ^^ field `beta` is private
+
+error[E0451]: field `gamma` of struct `Alpha` is private
+  --> $DIR/visibility.rs:11:37
+   |
+LL |         let x = crate::foo::Alpha { .. };
+   |                                     ^^ field `gamma` is private
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0451`.