1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
// run-pass
#![allow(non_upper_case_globals)]
// Issue 23611: this test is ensuring that, for an instance `X` of the
// enum `E`, if you swap in a different variant during the execution
// of the `<E as Drop>::drop`, then the appropriate substructure will
// be torn down after the `<E as Drop>::drop` method returns.
use std::cell::RefCell;
use std::mem;
use self::d::D;
pub fn main() {
let log = RefCell::new(vec![]);
d::println("created empty log");
test(&log);
// println!("log: {:?}", &log.borrow()[..]);
assert_eq!(
&log.borrow()[..],
[
// created empty log
// +-- Make D(test_1, 10000000)
// | +-- Make D(g_b_5, 50000001)
// | | in g_B(b2b0) from E::drop, b=b4b0
// | +-- Drop D(g_b_5, 50000001)
50000001,
// |
// | +-- Make D(drop_6, 60000002)
// | | +-- Make D(g_b_5, 50000003)
// | | | in g_B(b2b0) from E::drop, b=b4b1
// | | +-- Drop D(g_b_5, 50000003)
50000003,
// | |
// | | +-- Make D(GaspB::drop_3, 30000004)
// | | | +-- Make D(g_b_5, 50000005)
// | | | | in g_B(b4b2) from GaspB::drop
// | | | +-- Drop D(g_b_5, 50000005)
50000005,
// | | |
// | | +-- Drop D(GaspB::drop_3, 30000004)
30000004,
// | |
// +-- Drop D(test_1, 10000000)
10000000,
// |
// +-- Make D(GaspA::drop_2, 20000006)
// | | +-- Make D(f_a_4, 40000007)
// | | | in f_A(a3a0) from GaspA::drop
// | | +-- Drop D(f_a_4, 40000007)
40000007,
// | |
// +-- Drop D(GaspA::drop_2, 20000006)
20000006,
// |
// +-- Drop D(drop_6, 60000002)
60000002
//
]);
// For reference purposes, the old (incorrect) behavior would produce the following
// output, which you can compare to the above:
//
// created empty log
// +-- Make D(test_1, 10000000)
// | +-- Make D(g_b_5, 50000001)
// | | in g_B(b2b0) from E::drop, b=b4b0
// | +-- Drop D(g_b_5, 50000001)
// |
// | +-- Make D(drop_6, 60000002)
// | | +-- Make D(g_b_5, 50000003)
// | | | in g_B(b2b0) from E::drop, b=b4b1
// | | +-- Drop D(g_b_5, 50000003)
// | |
// | | +-- Make D(GaspB::drop_3, 30000004)
// | | | +-- Make D(g_b_5, 50000005)
// | | | | in g_B(b4b2) from GaspB::drop
// | | | +-- Drop D(g_b_5, 50000005)
// | | |
// | | +-- Drop D(GaspB::drop_3, 30000004)
// | |
// +-- Drop D(test_1, 10000000)
// |
// +-- Make D(GaspB::drop_3, 30000006)
// | | +-- Make D(f_a_4, 40000007)
// | | | in f_A(a3a0) from GaspB::drop
// | | +-- Drop D(f_a_4, 40000007)
// | |
// +-- Drop D(GaspB::drop_3, 30000006)
// |
// +-- Drop D(drop_6, 60000002)
// Note that this calls f_A from GaspB::drop (and thus creates a D
// with a uid incorporating the origin of GaspB's drop that
// surrounds the f_A invocation), but the code as written only
// ever hands f_A off to instances of GaspA, and thus one should
// be able to prove the invariant that f_A is *only* invoked from
// from an instance of GaspA (either via the GaspA drop
// implementation or the E drop implementation). Yet the old (bad)
// behavior allowed a call to f_A to leak in while we are tearing
// down a value of type GaspB.
}
fn test<'a>(log: d::Log<'a>) {
let _e = E::B(GaspB(g_b, 0xB4B0, log, D::new("test", 1, log)), true);
}
struct GaspA<'a>(for <'b> fn(u32, &'b str, d::Log<'a>), u32, d::Log<'a>, d::D<'a>);
struct GaspB<'a>(for <'b> fn(u32, &'b str, d::Log<'a>), u32, d::Log<'a>, d::D<'a>);
impl<'a> Drop for GaspA<'a> {
fn drop(&mut self) {
let _d = d::D::new("GaspA::drop", 2, self.2);
(self.0)(self.1, "GaspA::drop", self.2);
}
}
impl<'a> Drop for GaspB<'a> {
fn drop(&mut self) {
let _d = d::D::new("GaspB::drop", 3, self.2);
(self.0)(self.1, "GaspB::drop", self.2);
}
}
enum E<'a> {
A(GaspA<'a>, bool), B(GaspB<'a>, bool),
}
fn f_a(x: u32, ctxt: &str, log: d::Log) {
let _d = d::D::new("f_a", 4, log);
d::println(&format!("in f_A({:x}) from {}", x, ctxt));
}
fn g_b(y: u32, ctxt: &str, log: d::Log) {
let _d = d::D::new("g_b", 5, log);
d::println(&format!("in g_B({:x}) from {}", y, ctxt));
}
impl<'a> Drop for E<'a> {
fn drop(&mut self) {
let (do_drop, log) = match *self {
E::A(GaspA(ref f, ref mut val_a, log, ref _d_a), ref mut do_drop) => {
f(0xA1A0, &format!("E::drop, a={:x}", val_a), log);
*val_a += 1;
// swap in do_drop := false to avoid infinite dtor regress
(mem::replace(do_drop, false), log)
}
E::B(GaspB(ref g, ref mut val_b, log, ref _d_b), ref mut do_drop) => {
g(0xB2B0, &format!("E::drop, b={:x}", val_b), log);
*val_b += 1;
// swap in do_drop := false to avoid infinite dtor regress
(mem::replace(do_drop, false), log)
}
};
#[allow(unused_must_use)]
if do_drop {
mem::replace(self, E::A(GaspA(f_a, 0xA3A0, log, D::new("drop", 6, log)), true));
}
}
}
// This module provides simultaneous printouts of the dynamic extents
// of all of the D values, in addition to logging the order that each
// is dropped.
//
// This code is similar to a support code module embedded within
// test/run-pass/issue-123338-ensure-param-drop-order.rs; however,
// that (slightly simpler) code only identifies objects in the log via
// (creation) time-stamps; this incorporates both timestamping and the
// point of origin within the source code into the unique ID (uid).
const PREF_INDENT: u32 = 20;
pub mod d {
#![allow(unused_parens)]
use std::fmt;
use std::mem;
use std::cell::RefCell;
static mut counter: u16 = 0;
static mut trails: u64 = 0;
pub type Log<'a> = &'a RefCell<Vec<u32>>;
pub fn current_width() -> u32 {
unsafe { max_width() - trails.leading_zeros() }
}
pub fn max_width() -> u32 {
unsafe {
(mem::size_of_val(&trails)*8) as u32
}
}
pub fn indent_println(my_trails: u32, s: &str) {
let mut indent: String = String::new();
for i in 0..my_trails {
unsafe {
if trails & (1 << i) != 0 {
indent = indent + "| ";
} else {
indent = indent + " ";
}
}
}
println!("{}{}", indent, s);
}
pub fn println(s: &str) {
indent_println(super::PREF_INDENT, s);
}
fn first_avail() -> u32 {
unsafe {
for i in 0..64 {
if trails & (1 << i) == 0 {
return i;
}
}
}
panic!("exhausted trails");
}
pub struct D<'a> {
name: &'static str, i: u8, uid: u32, trail: u32, log: Log<'a>
}
impl<'a> fmt::Display for D<'a> {
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
write!(w, "D({}_{}, {})", self.name, self.i, self.uid)
}
}
impl<'a> D<'a> {
pub fn new(name: &'static str, i: u8, log: Log<'a>) -> D<'a> {
unsafe {
let trail = first_avail();
let ctr = ((i as u32) * 10_000_000) + (counter as u32);
counter += 1;
trails |= (1 << trail);
let ret = D {
name: name, i: i, log: log, uid: ctr, trail: trail
};
indent_println(trail, &format!("+-- Make {}", ret));
ret
}
}
}
impl<'a> Drop for D<'a> {
fn drop(&mut self) {
unsafe { trails &= !(1 << self.trail); };
self.log.borrow_mut().push(self.uid);
indent_println(self.trail, &format!("+-- Drop {}", self));
indent_println(::PREF_INDENT, "");
}
}
}
|