Skip to content

Commit 932d7b9

Browse files
committed
auto merge of #8780 : brson/rust/from_elem, r=thestinger
2 parents d9b0d4b + 063d9ca commit 932d7b9

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/libstd/vec.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,14 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] {
125125
let mut v = with_capacity(n_elts);
126126
let p = raw::to_mut_ptr(v);
127127
let mut i = 0u;
128-
while i < n_elts {
129-
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i as int)), t.clone());
130-
i += 1u;
128+
do (|| {
129+
while i < n_elts {
130+
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i as int)), t.clone());
131+
i += 1u;
132+
}
133+
}).finally {
134+
raw::set_len(&mut v, i);
131135
}
132-
raw::set_len(&mut v, n_elts);
133136
v
134137
}
135138
}
@@ -3134,6 +3137,29 @@ mod tests {
31343137
};
31353138
}
31363139

3140+
#[test]
3141+
#[should_fail]
3142+
fn test_from_elem_fail() {
3143+
use cast;
3144+
3145+
struct S {
3146+
f: int,
3147+
boxes: (~int, @int)
3148+
}
3149+
3150+
impl Clone for S {
3151+
fn clone(&self) -> S {
3152+
let s = unsafe { cast::transmute_mut(self) };
3153+
s.f += 1;
3154+
if s.f == 10 { fail!() }
3155+
S { f: s.f, boxes: s.boxes.clone() }
3156+
}
3157+
}
3158+
3159+
let s = S { f: 0, boxes: (~0, @0) };
3160+
let _ = from_elem(100, s);
3161+
}
3162+
31373163
#[test]
31383164
#[should_fail]
31393165
fn test_build_fail() {

0 commit comments

Comments
 (0)