Skip to content

Commit 6f7e21d

Browse files
committed
rustc: Truncate or zero-extend indexes appropriately. Un-XFAIL integral-indexing.rs.
1 parent df3038e commit 6f7e21d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ TEST_XFAILS_RUSTC := $(addprefix test/run-pass/, \
456456
generic-recursive-tag.rs \
457457
generic-tag-alt.rs \
458458
generic-tag-values.rs \
459-
integral-indexing.rs \
460459
iter-range.rs \
461460
iter-ret.rs \
462461
lazychan.rs \

src/comp/middle/trans.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,10 +3209,23 @@ fn trans_index(@block_ctxt cx, &ast.span sp, @ast.expr base,
32093209
auto v = lv.val;
32103210
auto bcx = ix.bcx;
32113211

3212+
// Cast to an LLVM integer. Rust is less strict than LLVM in this regard.
3213+
auto ix_val;
3214+
auto ix_size = llsize_of_real(cx.fcx.ccx, val_ty(ix.val));
3215+
auto int_size = llsize_of_real(cx.fcx.ccx, T_int());
3216+
if (ix_size < int_size) {
3217+
ix_val = bcx.build.ZExt(ix.val, T_int());
3218+
} else if (ix_size > int_size) {
3219+
ix_val = bcx.build.Trunc(ix.val, T_int());
3220+
} else {
3221+
ix_val = ix.val;
3222+
}
3223+
32123224
auto llunit_ty = node_type(cx.fcx.ccx, ann);
32133225
auto unit_sz = size_of(bcx, node_ann_type(cx.fcx.ccx, ann));
32143226
bcx = unit_sz.bcx;
3215-
auto scaled_ix = bcx.build.Mul(ix.val, unit_sz.val);
3227+
3228+
auto scaled_ix = bcx.build.Mul(ix_val, unit_sz.val);
32163229

32173230
auto lim = bcx.build.GEP(v, vec(C_int(0), C_int(abi.vec_elt_fill)));
32183231
lim = bcx.build.Load(lim);
@@ -3229,7 +3242,7 @@ fn trans_index(@block_ctxt cx, &ast.span sp, @ast.expr base,
32293242
fail_res.bcx.build.Br(next_cx.llbb);
32303243

32313244
auto body = next_cx.build.GEP(v, vec(C_int(0), C_int(abi.vec_elt_data)));
3232-
auto elt = next_cx.build.GEP(body, vec(C_int(0), ix.val));
3245+
auto elt = next_cx.build.GEP(body, vec(C_int(0), ix_val));
32333246
ret lval_mem(next_cx, elt);
32343247
}
32353248

0 commit comments

Comments
 (0)