Skip to content

Commit 3d03e3c

Browse files
committed
Test cases for Issue 23338.
1 parent 9695011 commit 3d03e3c

File tree

3 files changed

+244
-0
lines changed

3 files changed

+244
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// This is just checking that we still reject code where temp values
12+
// are borrowing values for longer than they will be around.
13+
//
14+
// Compare to run-pass/issue-23338-params-outlive-temps-of-body.rs
15+
16+
use std::cell::RefCell;
17+
18+
fn foo(x: RefCell<String>) -> String {
19+
let y = x;
20+
y.borrow().clone() //~ ERROR `y` does not live long enough
21+
}
22+
23+
fn foo2(x: RefCell<String>) -> String {
24+
let ret = {
25+
let y = x;
26+
y.borrow().clone() //~ ERROR `y` does not live long enough
27+
};
28+
ret
29+
}
30+
31+
fn main() {
32+
let r = RefCell::new(format!("data"));
33+
assert_eq!(foo(r), "data");
34+
let r = RefCell::new(format!("data"));
35+
assert_eq!(foo2(r), "data");
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
// This test is ensuring that parameters are indeed dropped after
12+
// temporaries in a fn body.
13+
14+
use std::cell::RefCell;
15+
16+
use self::d::D;
17+
18+
pub fn main() {
19+
let log = RefCell::new(vec![]);
20+
d::println(&format!("created empty log"));
21+
test(&log);
22+
23+
assert_eq!(&log.borrow()[..],
24+
[
25+
// created empty log
26+
// +-- Make D(da_0, 0)
27+
// | +-- Make D(de_1, 1)
28+
// | | calling foo
29+
// | | entered foo
30+
// | | +-- Make D(de_2, 2)
31+
// | | | +-- Make D(da_1, 3)
32+
// | | | | +-- Make D(de_3, 4)
33+
// | | | | | +-- Make D(de_4, 5)
34+
3, // | | | +-- Drop D(da_1, 3)
35+
// | | | | |
36+
4, // | | | +-- Drop D(de_3, 4)
37+
// | | | |
38+
// | | | | eval tail of foo
39+
// | | | +-- Make D(de_5, 6)
40+
// | | | | +-- Make D(de_6, 7)
41+
6, // | | | +-- Drop D(de_5, 6)
42+
// | | | | |
43+
5, // | | | | +-- Drop D(de_4, 5)
44+
// | | | |
45+
2, // | | +-- Drop D(de_2, 2)
46+
// | | |
47+
1, // | +-- Drop D(de_1, 1)
48+
// | |
49+
0, // +-- Drop D(da_0, 0)
50+
// |
51+
// | result D(de_6, 7)
52+
7 // +-- Drop D(de_6, 7)
53+
54+
]);
55+
}
56+
57+
fn test<'a>(log: d::Log<'a>) {
58+
let da = D::new("da", 0, log);
59+
let de = D::new("de", 1, log);
60+
d::println(&format!("calling foo"));
61+
let result = foo(da, de);
62+
d::println(&format!("result {}", result));
63+
}
64+
65+
fn foo<'a>(da0: D<'a>, de1: D<'a>) -> D<'a> {
66+
d::println(&format!("entered foo"));
67+
let de2 = de1.incr(); // creates D(de_2, 2)
68+
let de4 = {
69+
let _da1 = da0.incr(); // creates D(da_1, 3)
70+
de2.incr().incr() // creates D(de_3, 4) and D(de_4, 5)
71+
};
72+
d::println(&format!("eval tail of foo"));
73+
de4.incr().incr() // creates D(de_5, 6) and D(de_6, 7)
74+
}
75+
76+
// This module provides simultaneous printouts of the dynamic extents
77+
// of all of the D values, in addition to logging the order that each
78+
// is dropped.
79+
80+
const PREF_INDENT: u32 = 16;
81+
82+
pub mod d {
83+
#![allow(unused_parens)]
84+
use std::fmt;
85+
use std::mem;
86+
use std::cell::RefCell;
87+
88+
static mut counter: u32 = 0;
89+
static mut trails: u64 = 0;
90+
91+
pub type Log<'a> = &'a RefCell<Vec<u32>>;
92+
93+
pub fn current_width() -> u32 {
94+
unsafe { max_width() - trails.leading_zeros() }
95+
}
96+
97+
pub fn max_width() -> u32 {
98+
unsafe {
99+
(mem::size_of_val(&trails)*8) as u32
100+
}
101+
}
102+
103+
pub fn indent_println(my_trails: u32, s: &str) {
104+
let mut indent: String = String::new();
105+
for i in 0..my_trails {
106+
unsafe {
107+
if trails & (1 << i) != 0 {
108+
indent = indent + "| ";
109+
} else {
110+
indent = indent + " ";
111+
}
112+
}
113+
}
114+
println!("{}{}", indent, s);
115+
}
116+
117+
pub fn println(s: &str) {
118+
indent_println(super::PREF_INDENT, s);
119+
}
120+
121+
fn first_avail() -> u32 {
122+
unsafe {
123+
for i in 0..64 {
124+
if trails & (1 << i) == 0 {
125+
return i;
126+
}
127+
}
128+
}
129+
panic!("exhausted trails");
130+
}
131+
132+
pub struct D<'a> {
133+
name: &'static str, i: u32, uid: u32, trail: u32, log: Log<'a>
134+
}
135+
136+
impl<'a> fmt::Display for D<'a> {
137+
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
138+
write!(w, "D({}_{}, {})", self.name, self.i, self.uid)
139+
}
140+
}
141+
142+
impl<'a> D<'a> {
143+
pub fn new(name: &'static str, i: u32, log: Log<'a>) -> D<'a> {
144+
unsafe {
145+
let trail = first_avail();
146+
let ctr = counter;
147+
counter += 1;
148+
trails |= (1 << trail);
149+
let ret = D {
150+
name: name, i: i, log: log, uid: ctr, trail: trail
151+
};
152+
indent_println(trail, &format!("+-- Make {}", ret));
153+
ret
154+
}
155+
}
156+
pub fn incr(&self) -> D<'a> {
157+
D::new(self.name, self.i + 1, self.log)
158+
}
159+
}
160+
161+
impl<'a> Drop for D<'a> {
162+
fn drop(&mut self) {
163+
unsafe { trails &= !(1 << self.trail); };
164+
self.log.borrow_mut().push(self.uid);
165+
indent_println(self.trail, &format!("+-- Drop {}", self));
166+
indent_println(::PREF_INDENT, "");
167+
}
168+
}
169+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
// This is largely checking that we now accept code where temp values
12+
// are borrowing from the input parameters (the `foo` case below).
13+
//
14+
// Compare to run-pass/issue-23338-params-outlive-temps-of-body.rs
15+
//
16+
// (The `foo2` case is just for parity with the above test, which
17+
// shows what happens when you move the `y`-binding to the inside of
18+
// the inner block.)
19+
20+
use std::cell::RefCell;
21+
22+
fn foo(x: RefCell<String>) -> String {
23+
x.borrow().clone()
24+
}
25+
26+
fn foo2(x: RefCell<String>) -> String {
27+
let y = x;
28+
let ret = {
29+
y.borrow().clone()
30+
};
31+
ret
32+
}
33+
34+
pub fn main() {
35+
let r = RefCell::new(format!("data"));
36+
assert_eq!(foo(r), "data");
37+
let r = RefCell::new(format!("data"));
38+
assert_eq!(foo2(r), "data");
39+
}

0 commit comments

Comments
 (0)