]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/functional-struct-update-respects-privacy.rs
rustdoc: pretty-print Unevaluated expressions in types.
[rust.git] / src / test / compile-fail / functional-struct-update-respects-privacy.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // RFC 736 (and Issue 21407): functional struct update should respect privacy.
12
13 #![feature(const_fn)]
14
15 // The `foo` module attempts to maintains an invariant that each `S`
16 // has a unique `u64` id.
17 use self::foo::S;
18 mod foo {
19     use std::cell::{UnsafeCell};
20
21     static mut count : UnsafeCell<u64> = UnsafeCell::new(1);
22
23     pub struct S { pub a: u8, pub b: String, secret_uid: u64 }
24
25     pub fn make_secrets(a: u8, b: String) -> S {
26         let val = unsafe { let p = count.get(); let val = *p; *p = val + 1; val };
27         println!("creating {}, uid {}", b, val);
28         S { a: a, b: b, secret_uid: val }
29     }
30
31     impl Drop for S {
32         fn drop(&mut self) {
33             println!("dropping {}, uid {}", self.b, self.secret_uid);
34         }
35     }
36 }
37
38 fn main() {
39     let s_1 = foo::make_secrets(3, format!("ess one"));
40     let s_2 = foo::S { b: format!("ess two"), ..s_1 }; // FRU ...
41     //~^ ERROR field `secret_uid` of struct `foo::S` is private
42     println!("main forged an S named: {}", s_2.b);
43     // at end of scope, ... both s_1 *and* s_2 get dropped.  Boom!
44 }