]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / rfc-2361-dbg-macro / dbg-macro-expected-behavior.rs
1 // run-pass
2 // ignore-cloudabi no processes
3 // ignore-emscripten no processes
4
5 // Tests ensuring that `dbg!(expr)` has the expected run-time behavior.
6 // as well as some compile time properties we expect.
7
8 #![feature(dbg_macro)]
9
10 #[derive(Copy, Clone, Debug)]
11 struct Unit;
12
13 #[derive(Copy, Clone, Debug, PartialEq)]
14 struct Point<T> {
15     x: T,
16     y: T,
17 }
18
19 #[derive(Debug, PartialEq)]
20 struct NoCopy(usize);
21
22 fn test() {
23     let a: Unit = dbg!(Unit);
24     let _: Unit = dbg!(a);
25     // We can move `a` because it's Copy.
26     drop(a);
27
28     // `Point<T>` will be faithfully formatted according to `{:#?}`.
29     let a = Point { x: 42, y: 24 };
30     let b: Point<u8> = dbg!(Point { x: 42, y: 24 }); // test stringify!(..)
31     let c: Point<u8> = dbg!(b);
32     // Identity conversion:
33     assert_eq!(a, b);
34     assert_eq!(a, c);
35     // We can move `b` because it's Copy.
36     drop(b);
37
38     // Test that we can borrow and that successive applications is still identity.
39     let a = NoCopy(1337);
40     let b: &NoCopy = dbg!(dbg!(&a));
41     assert_eq!(&a, b);
42
43     // Test involving lifetimes of temporaries:
44     fn f<'a>(x: &'a u8) -> &'a u8 { x }
45     let a: &u8 = dbg!(f(&42));
46     assert_eq!(a, &42);
47
48     // Test side effects:
49     let mut foo = 41;
50     assert_eq!(7331, dbg!({
51         foo += 1;
52         eprintln!("before");
53         7331
54     }));
55     assert_eq!(foo, 42);
56 }
57
58 fn validate_stderr(stderr: Vec<String>) {
59     assert_eq!(stderr, &[
60         ":23] Unit = Unit",
61
62         ":24] a = Unit",
63
64         ":30] Point{x: 42, y: 24,} = Point {",
65         "    x: 42,",
66         "    y: 24",
67         "}",
68
69         ":31] b = Point {",
70         "    x: 42,",
71         "    y: 24",
72         "}",
73
74         ":40] &a = NoCopy(",
75         "    1337",
76         ")",
77
78         ":40] dbg!(& a) = NoCopy(",
79         "    1337",
80         ")",
81         ":45] f(&42) = 42",
82
83         "before",
84         ":50] { foo += 1; eprintln!(\"before\"); 7331 } = 7331",
85     ]);
86 }
87
88 fn main() {
89     // The following is a hack to deal with compiletest's inability
90     // to check the output (to stdout) of run-pass tests.
91     use std::env;
92     use std::process::Command;
93
94     let mut args = env::args();
95     let prog = args.next().unwrap();
96     let child = args.next();
97     if let Some("child") = child.as_ref().map(|s| &**s) {
98         // Only run the test if we've been spawned as 'child'
99         test()
100     } else {
101         // This essentially spawns as 'child' to run the tests
102         // and then it collects output of stderr and checks the output
103         // against what we expect.
104         let out = Command::new(&prog).arg("child").output().unwrap();
105         assert!(out.status.success());
106         assert!(out.stdout.is_empty());
107
108         let stderr = String::from_utf8(out.stderr).unwrap();
109         let stderr = stderr.lines().map(|mut s| {
110             if s.starts_with("[") {
111                 // Strip `[` and file path:
112                 s = s.trim_start_matches("[");
113                 assert!(s.starts_with(file!()));
114                 s = s.trim_start_matches(file!());
115             }
116             s.to_owned()
117         }).collect();
118
119         validate_stderr(stderr);
120     }
121 }