Skip to content

Commit d6e4fa6

Browse files
committed
Teach rustc to append istrs. Issue #855
1 parent 3ab86fb commit d6e4fa6

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/comp/middle/trans_ivec.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,6 @@ fn trans_append(cx: &@block_ctxt, t: ty::t, orig_lhs: ValueRef,
354354

355355
let unit_ty = ty::sequence_element_type(bcx_tcx(cx), t);
356356
let llunitty = type_of_or_i8(cx, unit_ty);
357-
alt ty::struct(bcx_tcx(cx), t) {
358-
ty::ty_istr. { }
359-
ty::ty_vec(_) { }
360-
_ { bcx_tcx(cx).sess.bug("non-istr/ivec in trans_append"); }
361-
}
362357

363358
let rs = size_of(cx, unit_ty);
364359
let bcx = rs.bcx;
@@ -381,10 +376,31 @@ fn trans_append(cx: &@block_ctxt, t: ty::t, orig_lhs: ValueRef,
381376
let rhs_len = rhs_len_and_data.len;
382377
let rhs_data = rhs_len_and_data.data;
383378
bcx = rhs_len_and_data.bcx;
384-
rs = reserve_space(bcx, llunitty, lhs, rhs_len);
385-
let lhs_data = rs.val;
379+
380+
let have_istrs = alt ty::struct(bcx_tcx(cx), t) {
381+
ty::ty_istr. { true }
382+
ty::ty_vec(_) { false }
383+
_ { bcx_tcx(cx).sess.bug("non-istr/ivec in trans_append"); }
384+
};
385+
386+
let extra_len = if have_istrs {
387+
// Only need one of the nulls
388+
bcx.build.Sub(rhs_len, C_uint(1u))
389+
} else { rhs_len };
390+
391+
rs = reserve_space(bcx, llunitty, lhs, extra_len);
386392
bcx = rs.bcx;
387393

394+
let lhs_data = if have_istrs {
395+
let lhs_data = rs.val;
396+
let lhs_data_without_null_ptr = alloca(bcx, T_ptr(llunitty));
397+
incr_ptr(bcx, lhs_data, C_int(-1),
398+
lhs_data_without_null_ptr);
399+
bcx.build.Load(lhs_data_without_null_ptr)
400+
} else {
401+
rs.val
402+
};
403+
388404
// If rhs is lhs then our rhs pointer may have changed
389405
rhs_len_and_data = get_len_and_data(bcx, rhs, unit_ty);
390406
rhs_data = rhs_len_and_data.data;

src/test/run-pass/istr.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ fn test_heap_add() {
3939
assert ~"this should" + ~" totally work" == ~"this should totally work";
4040
}
4141

42+
fn test_append() {
43+
let s = ~"";
44+
s += ~"a";
45+
assert s == ~"a";
46+
47+
let s = ~"a";
48+
s += ~"b";
49+
log s;
50+
assert s == ~"ab";
51+
52+
let s = ~"c";
53+
s += ~"offee";
54+
assert s == ~"coffee";
55+
56+
s += ~"&tea";
57+
assert s == ~"coffee&tea";
58+
}
59+
4260
fn main() {
4361
test_stack_assign();
4462
test_heap_lit();
@@ -47,4 +65,5 @@ fn main() {
4765
test_stack_add();
4866
test_stack_heap_add();
4967
test_heap_add();
68+
test_append();
5069
}

0 commit comments

Comments
 (0)