2 #![allow(non_upper_case_globals)]
4 // This test is ensuring that parameters are indeed dropped after
5 // temporaries in a fn body.
7 use std::cell::RefCell;
12 let log = RefCell::new(vec![]);
13 d::println("created empty log");
16 assert_eq!(&log.borrow()[..],
19 // +-- Make D(da_0, 0)
20 // | +-- Make D(de_1, 1)
23 // | | +-- Make D(de_2, 2)
24 // | | | +-- Make D(da_1, 3)
25 // | | | | +-- Make D(de_3, 4)
26 // | | | | | +-- Make D(de_4, 5)
27 3, // | | | +-- Drop D(da_1, 3)
29 4, // | | | +-- Drop D(de_3, 4)
31 // | | | | eval tail of foo
32 // | | | +-- Make D(de_5, 6)
33 // | | | | +-- Make D(de_6, 7)
34 5, // | | | | | +-- Drop D(de_4, 5)
36 2, // | | +-- Drop D(de_2, 2)
38 6, // | | +-- Drop D(de_5, 6)
40 1, // | +-- Drop D(de_1, 1)
42 0, // +-- Drop D(da_0, 0)
44 // | result D(de_6, 7)
45 7 // +-- Drop D(de_6, 7)
50 fn test<'a>(log: d::Log<'a>) {
51 let da = D::new("da", 0, log);
52 let de = D::new("de", 1, log);
53 d::println("calling foo");
54 let result = foo(da, de);
55 d::println(&format!("result {}", result));
58 fn foo<'a>(da0: D<'a>, de1: D<'a>) -> D<'a> {
59 d::println("entered foo");
60 let de2 = de1.incr(); // creates D(de_2, 2)
62 let _da1 = da0.incr(); // creates D(da_1, 3)
63 de2.incr().incr() // creates D(de_3, 4) and D(de_4, 5)
65 d::println("eval tail of foo");
66 de4.incr().incr() // creates D(de_5, 6) and D(de_6, 7)
69 // This module provides simultaneous printouts of the dynamic extents
70 // of all of the D values, in addition to logging the order that each
73 const PREF_INDENT: u32 = 16;
76 #![allow(unused_parens)]
79 use std::cell::RefCell;
81 static mut counter: u32 = 0;
82 static mut trails: u64 = 0;
84 pub type Log<'a> = &'a RefCell<Vec<u32>>;
86 pub fn current_width() -> u32 {
87 unsafe { max_width() - trails.leading_zeros() }
90 pub fn max_width() -> u32 {
92 (mem::size_of_val(&trails)*8) as u32
96 pub fn indent_println(my_trails: u32, s: &str) {
97 let mut indent: String = String::new();
98 for i in 0..my_trails {
100 if trails & (1 << i) != 0 {
101 indent = indent + "| ";
103 indent = indent + " ";
107 println!("{}{}", indent, s);
110 pub fn println(s: &str) {
111 indent_println(super::PREF_INDENT, s);
114 fn first_avail() -> u32 {
117 if trails & (1 << i) == 0 {
122 panic!("exhausted trails");
126 name: &'static str, i: u32, uid: u32, trail: u32, log: Log<'a>
129 impl<'a> fmt::Display for D<'a> {
130 fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
131 write!(w, "D({}_{}, {})", self.name, self.i, self.uid)
136 pub fn new(name: &'static str, i: u32, log: Log<'a>) -> D<'a> {
138 let trail = first_avail();
141 trails |= (1 << trail);
143 name: name, i: i, log: log, uid: ctr, trail: trail
145 indent_println(trail, &format!("+-- Make {}", ret));
149 pub fn incr(&self) -> D<'a> {
150 D::new(self.name, self.i + 1, self.log)
154 impl<'a> Drop for D<'a> {
156 unsafe { trails &= !(1 << self.trail); };
157 self.log.borrow_mut().push(self.uid);
158 indent_println(self.trail, &format!("+-- Drop {}", self));
159 indent_println(::PREF_INDENT, "");