]> git.proxmox.com Git - rustc.git/blob - src/test/ui/functional-struct-update/functional-struct-update-respects-privacy.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / src / test / ui / functional-struct-update / functional-struct-update-respects-privacy.rs
1 // RFC 736 (and Issue 21407): functional struct update should respect privacy.
2
3 // The `foo` module attempts to maintains an invariant that each `S`
4 // has a unique `u64` id.
5 use self::foo::S;
6 mod foo {
7 use std::cell::{UnsafeCell};
8
9 static mut COUNT : UnsafeCell<u64> = UnsafeCell::new(1);
10
11 pub struct S { pub a: u8, pub b: String, secret_uid: u64 }
12
13 pub fn make_secrets(a: u8, b: String) -> S {
14 let val = unsafe { let p = COUNT.get(); let val = *p; *p = val + 1; val };
15 println!("creating {}, uid {}", b, val);
16 S { a: a, b: b, secret_uid: val }
17 }
18
19 impl Drop for S {
20 fn drop(&mut self) {
21 println!("dropping {}, uid {}", self.b, self.secret_uid);
22 }
23 }
24 }
25
26 fn main() {
27 let s_1 = foo::make_secrets(3, format!("ess one"));
28 let s_2 = foo::S { b: format!("ess two"), ..s_1 }; // FRU ...
29 //~^ ERROR field `secret_uid` of struct `S` is private
30 println!("main forged an S named: {}", s_2.b);
31 // at end of scope, ... both s_1 *and* s_2 get dropped. Boom!
32 }