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.
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.
11 #![feature(rustc_attrs)]
13 // ignore-pretty : (#23623) problems when ending with // comments
15 // This test is ensuring that parameters are indeed dropped after
16 // temporaries in a fn body.
18 use std
::cell
::RefCell
;
23 let log
= RefCell
::new(vec
![]);
24 d
::println("created empty log");
27 assert_eq
!(&log
.borrow()[..],
30 // +-- Make D(da_0, 0)
31 // | +-- Make D(de_1, 1)
34 // | | +-- Make D(de_2, 2)
35 // | | | +-- Make D(da_1, 3)
36 // | | | | +-- Make D(de_3, 4)
37 // | | | | | +-- Make D(de_4, 5)
38 3, // | | | +-- Drop D(da_1, 3)
40 4, // | | | +-- Drop D(de_3, 4)
42 // | | | | eval tail of foo
43 // | | | +-- Make D(de_5, 6)
44 // | | | | +-- Make D(de_6, 7)
45 6, // | | | +-- Drop D(de_5, 6)
47 5, // | | | | +-- Drop D(de_4, 5)
49 2, // | | +-- Drop D(de_2, 2)
51 1, // | +-- Drop D(de_1, 1)
53 0, // +-- Drop D(da_0, 0)
55 // | result D(de_6, 7)
56 7 // +-- Drop D(de_6, 7)
61 fn test
<'a
>(log
: d
::Log
<'a
>) {
62 let da
= D
::new("da", 0, log
);
63 let de
= D
::new("de", 1, log
);
64 d
::println("calling foo");
65 let result
= foo(da
, de
);
66 d
::println(&format
!("result {}", result
));
69 #[rustc_no_mir] // FIXME #29855 MIR doesn't handle all drops correctly.
70 fn foo
<'a
>(da0
: D
<'a
>, de1
: D
<'a
>) -> D
<'a
> {
71 d
::println("entered foo");
72 let de2
= de1
.incr(); // creates D(de_2, 2)
74 let _da1
= da0
.incr(); // creates D(da_1, 3)
75 de2
.incr().incr() // creates D(de_3, 4) and D(de_4, 5)
77 d
::println("eval tail of foo");
78 de4
.incr().incr() // creates D(de_5, 6) and D(de_6, 7)
81 // This module provides simultaneous printouts of the dynamic extents
82 // of all of the D values, in addition to logging the order that each
85 const PREF_INDENT
: u32 = 16;
88 #![allow(unused_parens)]
91 use std
::cell
::RefCell
;
93 static mut counter
: u32 = 0;
94 static mut trails
: u64 = 0;
96 pub type Log
<'a
> = &'a RefCell
<Vec
<u32>>;
98 pub fn current_width() -> u32 {
99 unsafe { max_width() - trails.leading_zeros() }
102 pub fn max_width() -> u32 {
104 (mem
::size_of_val(&trails
)*8) as u32
108 pub fn indent_println(my_trails
: u32, s
: &str) {
109 let mut indent
: String
= String
::new();
110 for i
in 0..my_trails
{
112 if trails
& (1 << i
) != 0 {
113 indent
= indent
+ "| ";
115 indent
= indent
+ " ";
119 println
!("{}{}", indent
, s
);
122 pub fn println(s
: &str) {
123 indent_println(super::PREF_INDENT
, s
);
126 fn first_avail() -> u32 {
129 if trails
& (1 << i
) == 0 {
134 panic
!("exhausted trails");
138 name
: &'
static str, i
: u32, uid
: u32, trail
: u32, log
: Log
<'a
>
141 impl<'a
> fmt
::Display
for D
<'a
> {
142 fn fmt(&self, w
: &mut fmt
::Formatter
) -> fmt
::Result
{
143 write
!(w
, "D({}_{}, {})", self.name
, self.i
, self.uid
)
148 pub fn new(name
: &'
static str, i
: u32, log
: Log
<'a
>) -> D
<'a
> {
150 let trail
= first_avail();
153 trails
|= (1 << trail
);
155 name
: name
, i
: i
, log
: log
, uid
: ctr
, trail
: trail
157 indent_println(trail
, &format
!("+-- Make {}", ret
));
161 pub fn incr(&self) -> D
<'a
> {
162 D
::new(self.name
, self.i
+ 1, self.log
)
166 impl<'a
> Drop
for D
<'a
> {
168 unsafe { trails &= !(1 << self.trail); }
;
169 self.log
.borrow_mut().push(self.uid
);
170 indent_println(self.trail
, &format
!("+-- Drop {}", self));
171 indent_println(::PREF_INDENT
, "");