Skip to content

Commit 3ab86fb

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

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/comp/middle/trans_ivec.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,16 @@ fn trans_add(cx: &@block_ctxt, vec_ty: ty::t, lhs: ValueRef,
494494
let lhs_len = lhs_len_and_data.len;
495495
let lhs_data = lhs_len_and_data.data;
496496
bcx = lhs_len_and_data.bcx;
497+
498+
lhs_len = alt ty::struct(bcx_tcx(bcx), vec_ty) {
499+
ty::ty_istr. {
500+
// Forget about the trailing null on the left side
501+
bcx.build.Sub(lhs_len, C_uint(1u))
502+
}
503+
ty::ty_vec(_) { lhs_len }
504+
_ { bcx_tcx(bcx).sess.bug("non-istr/ivec in trans_add") }
505+
};
506+
497507
let rhs_len_and_data = get_len_and_data(bcx, rhs, unit_ty);
498508
let rhs_len = rhs_len_and_data.len;
499509
let rhs_data = rhs_len_and_data.data;

src/test/run-pass/istr.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,39 @@ fn test_heap_lit() {
1212
}
1313

1414
fn test_heap_assign() {
15-
let s: istr;
16-
s = ~"AAAA";
15+
let s: istr = ~"a big ol' string";
16+
let t: istr = ~"a big ol' string";
17+
assert s == t;
18+
let u: istr = ~"a bad ol' string";
19+
assert s != u;
1720
}
1821

1922
fn test_heap_log() {
2023
let s = ~"a big ol' string";
2124
log s;
2225
}
2326

27+
fn test_stack_add() {
28+
assert ~"a" + ~"b" == ~"ab";
29+
let s: istr = ~"a";
30+
assert s + s == ~"aa";
31+
assert ~"" + ~"" == ~"";
32+
}
33+
34+
fn test_stack_heap_add() {
35+
assert ~"a" + ~"bracadabra" == ~"abracadabra";
36+
}
37+
38+
fn test_heap_add() {
39+
assert ~"this should" + ~" totally work" == ~"this should totally work";
40+
}
41+
2442
fn main() {
2543
test_stack_assign();
2644
test_heap_lit();
2745
test_heap_assign();
2846
test_heap_log();
47+
test_stack_add();
48+
test_stack_heap_add();
49+
test_heap_add();
2950
}

0 commit comments

Comments
 (0)