Skip to content

Commit 06e261a

Browse files
author
Yuki Okushi
authored
Rollup merge of rust-lang#104045 - Ayush1325:type_array, r=nikic
Add type_array to BaseTypeMethods Moved `type_array` function to `rustc_codegen_ssa::BaseTypeMethods` trait. This allows using normal `alloca` function to create arrays as suggested in rust-lang#104022. Signed-off-by: Ayush Singh <[email protected]>
2 parents 19c780a + 299bc61 commit 06e261a

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

Diff for: compiler/rustc_codegen_gcc/src/type_.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,27 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
201201
fn val_ty(&self, value: RValue<'gcc>) -> Type<'gcc> {
202202
value.get_type()
203203
}
204+
205+
fn type_array(&self, ty: Type<'gcc>, mut len: u64) -> Type<'gcc> {
206+
if let Some(struct_type) = ty.is_struct() {
207+
if struct_type.get_field_count() == 0 {
208+
// NOTE: since gccjit only supports i32 for the array size and libcore's tests uses a
209+
// size of usize::MAX in test_binary_search, we workaround this by setting the size to
210+
// zero for ZSTs.
211+
// FIXME(antoyo): fix gccjit API.
212+
len = 0;
213+
}
214+
}
215+
216+
// NOTE: see note above. Some other test uses usize::MAX.
217+
if len == u64::MAX {
218+
len = 0;
219+
}
220+
221+
let len: i32 = len.try_into().expect("array len");
222+
223+
self.context.new_array_type(None, ty, len)
224+
}
204225
}
205226

206227
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
@@ -227,27 +248,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
227248
self.context.new_opaque_struct_type(None, name)
228249
}
229250

230-
pub fn type_array(&self, ty: Type<'gcc>, mut len: u64) -> Type<'gcc> {
231-
if let Some(struct_type) = ty.is_struct() {
232-
if struct_type.get_field_count() == 0 {
233-
// NOTE: since gccjit only supports i32 for the array size and libcore's tests uses a
234-
// size of usize::MAX in test_binary_search, we workaround this by setting the size to
235-
// zero for ZSTs.
236-
// FIXME(antoyo): fix gccjit API.
237-
len = 0;
238-
}
239-
}
240-
241-
// NOTE: see note above. Some other test uses usize::MAX.
242-
if len == u64::MAX {
243-
len = 0;
244-
}
245-
246-
let len: i32 = len.try_into().expect("array len");
247-
248-
self.context.new_array_type(None, ty, len)
249-
}
250-
251251
pub fn type_bool(&self) -> Type<'gcc> {
252252
self.context.new_type::<bool>()
253253
}

Diff for: compiler/rustc_codegen_llvm/src/type_.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ impl<'ll> CodegenCx<'ll, '_> {
127127
pub(crate) fn type_variadic_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
128128
unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, True) }
129129
}
130-
131-
pub(crate) fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
132-
unsafe { llvm::LLVMRustArrayType(ty, len) }
133-
}
134130
}
135131

136132
impl<'ll, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
@@ -231,6 +227,10 @@ impl<'ll, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
231227
fn val_ty(&self, v: &'ll Value) -> &'ll Type {
232228
common::val_ty(v)
233229
}
230+
231+
fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
232+
unsafe { llvm::LLVMRustArrayType(ty, len) }
233+
}
234234
}
235235

236236
impl Type {

Diff for: compiler/rustc_codegen_ssa/src/traits/type_.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub trait BaseTypeMethods<'tcx>: Backend<'tcx> {
2222
fn type_f32(&self) -> Self::Type;
2323
fn type_f64(&self) -> Self::Type;
2424

25+
fn type_array(&self, ty: Self::Type, len: u64) -> Self::Type;
2526
fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::Type;
2627
fn type_struct(&self, els: &[Self::Type], packed: bool) -> Self::Type;
2728
fn type_kind(&self, ty: Self::Type) -> TypeKind;

0 commit comments

Comments
 (0)