Skip to content

Commit 386d329

Browse files
committed
rollup merge of #22437: dotdash/fix_array_type
In trans_slice_vec we currently use arrayalloca, which gives us a pointer to the element type with enough memory allocated for the requested number of elements. This works, but everywhere else we use the [n x T] type for fixed size arrays and so we have to bitcast the pointer here. Let's directly use the proper type for the allocation and remove some code duplication along the way.
2 parents 754db0f + 0fe880b commit 386d329

File tree

2 files changed

+11
-30
lines changed

2 files changed

+11
-30
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,19 +1219,6 @@ pub fn alloca_zeroed<'blk, 'tcx>(cx: Block<'blk, 'tcx>, ty: Ty<'tcx>,
12191219
p
12201220
}
12211221

1222-
pub fn arrayalloca(cx: Block, ty: Type, v: ValueRef) -> ValueRef {
1223-
let _icx = push_ctxt("arrayalloca");
1224-
if cx.unreachable.get() {
1225-
unsafe {
1226-
return llvm::LLVMGetUndef(ty.to_ref());
1227-
}
1228-
}
1229-
debuginfo::clear_source_location(cx.fcx);
1230-
let p = ArrayAlloca(cx, ty, v);
1231-
call_lifetime_start(cx, p);
1232-
p
1233-
}
1234-
12351222
// Creates the alloca slot which holds the pointer to the slot for the final return value
12361223
pub fn make_return_slot_pointer<'a, 'tcx>(fcx: &FunctionContext<'a, 'tcx>,
12371224
output_type: Ty<'tcx>) -> ValueRef {

src/librustc_trans/trans/tvec.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,33 +171,27 @@ pub fn trans_slice_vec<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
171171
let vt = vec_types_from_expr(bcx, content_expr);
172172
let count = elements_required(bcx, content_expr);
173173
debug!(" vt={}, count={}", vt.to_string(ccx), count);
174-
let llcount = C_uint(ccx, count);
175174

176175
let fixed_ty = ty::mk_vec(bcx.tcx(),
177176
vt.unit_ty,
178177
Some(count));
179-
let llfixed_ty = type_of::type_of(bcx.ccx(), fixed_ty).ptr_to();
178+
let llfixed_ty = type_of::type_of(bcx.ccx(), fixed_ty);
180179

181-
let llfixed = if count == 0 {
182-
// Just create a zero-sized alloca to preserve
183-
// the non-null invariant of the inner slice ptr
184-
let llfixed = base::arrayalloca(bcx, vt.llunit_ty, llcount);
185-
BitCast(bcx, llfixed, llfixed_ty)
186-
} else {
187-
// Make a fixed-length backing array and allocate it on the stack.
188-
let llfixed = base::arrayalloca(bcx, vt.llunit_ty, llcount);
180+
// Always create an alloca even if zero-sized, to preserve
181+
// the non-null invariant of the inner slice ptr
182+
let llfixed = base::alloca(bcx, llfixed_ty, "");
189183

184+
if count > 0 {
190185
// Arrange for the backing array to be cleaned up.
191-
let llfixed_casted = BitCast(bcx, llfixed, llfixed_ty);
192186
let cleanup_scope = cleanup::temporary_scope(bcx.tcx(), content_expr.id);
193-
fcx.schedule_lifetime_end(cleanup_scope, llfixed_casted);
194-
fcx.schedule_drop_mem(cleanup_scope, llfixed_casted, fixed_ty);
187+
fcx.schedule_lifetime_end(cleanup_scope, llfixed);
188+
fcx.schedule_drop_mem(cleanup_scope, llfixed, fixed_ty);
195189

196190
// Generate the content into the backing array.
197-
bcx = write_content(bcx, &vt, slice_expr,
198-
content_expr, SaveIn(llfixed));
199-
200-
llfixed_casted
191+
// llfixed has type *[T x N], but we want the type *T,
192+
// so use GEP to convert
193+
bcx = write_content(bcx, &vt, slice_expr, content_expr,
194+
SaveIn(GEPi(bcx, llfixed, &[0, 0])));
201195
};
202196

203197
immediate_rvalue_bcx(bcx, llfixed, vec_ty).to_expr_datumblock()

0 commit comments

Comments
 (0)