]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/struct-order-of-eval-3.rs
856ed7c105e8a29add09aec52b547f8bd6886580
[rust.git] / src / test / run-pass / struct-order-of-eval-3.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 // Checks that functional-record-update order-of-eval is as expected
12 // even when no Drop-implementations are involved.
13
14 use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
15
16 struct W { wrapped: u32 }
17 struct S { f0: W, _f1: i32 }
18
19 pub fn main() {
20     const VAL: u32 = 0x89AB_CDEF;
21     let w = W { wrapped: VAL };
22     let s = S {
23         f0: { event(0x01); W { wrapped: w.wrapped + 1 } },
24         ..S {
25             f0: { event(0x02); w},
26             _f1: 23
27         }
28     };
29     assert_eq!(s.f0.wrapped, VAL + 1);
30     let actual = event_log();
31     let expect = 0x01_02;
32     assert!(expect == actual,
33             "expect: 0x{:x} actual: 0x{:x}", expect, actual);
34 }
35
36 static LOG: AtomicUsize = ATOMIC_USIZE_INIT;
37
38 fn event_log() -> usize {
39     LOG.load(Ordering::SeqCst)
40 }
41
42 fn event(tag: u8) {
43     let old_log = LOG.load(Ordering::SeqCst);
44     let new_log = (old_log << 8) + tag as usize;
45     LOG.store(new_log, Ordering::SeqCst);
46 }