Skip to content

Commit 78f9e53

Browse files
committed
Return result from malloc builder functions
1 parent f9e23fc commit 78f9e53

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/builder.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl<'ctx> Builder<'ctx> {
348348
}
349349

350350
// TODOC: Heap allocation
351-
pub fn build_malloc<T: BasicType<'ctx>>(&self, ty: T, name: &str) -> PointerValue<'ctx> {
351+
pub fn build_malloc<T: BasicType<'ctx>>(&self, ty: T, name: &str) -> Result<PointerValue<'ctx>, &'static str> {
352352
// LLVMBulidMalloc segfaults if ty is unsized
353353
let is_sized = match ty.as_basic_type_enum() {
354354
BasicTypeEnum::ArrayType(ty) => ty.is_sized(),
@@ -359,7 +359,7 @@ impl<'ctx> Builder<'ctx> {
359359
BasicTypeEnum::VectorType(ty) => ty.is_sized(),
360360
};
361361
if !is_sized {
362-
panic!("Cannot build malloc call for an unsized type {:?}", ty);
362+
return Err("Cannot build malloc call for an unsized type");
363363
}
364364

365365
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
@@ -368,11 +368,16 @@ impl<'ctx> Builder<'ctx> {
368368
LLVMBuildMalloc(self.builder, ty.as_type_ref(), c_string.as_ptr())
369369
};
370370

371-
PointerValue::new(value)
371+
Ok(PointerValue::new(value))
372372
}
373373

374374
// TODOC: Heap allocation
375-
pub fn build_array_malloc<T: BasicType<'ctx>>(&self, ty: T, size: IntValue<'ctx>, name: &str) -> PointerValue<'ctx> {
375+
pub fn build_array_malloc<T: BasicType<'ctx>>(
376+
&self,
377+
ty: T,
378+
size: IntValue<'ctx>,
379+
name: &str
380+
) -> Result<PointerValue<'ctx>, &'static str> {
376381
// LLVMBulidArrayMalloc segfaults if ty is unsized
377382
let is_sized = match ty.as_basic_type_enum() {
378383
BasicTypeEnum::ArrayType(ty) => ty.is_sized(),
@@ -383,7 +388,7 @@ impl<'ctx> Builder<'ctx> {
383388
BasicTypeEnum::VectorType(ty) => ty.is_sized(),
384389
};
385390
if !is_sized {
386-
panic!("Cannot build malloc call for an unsized type {:?}", ty);
391+
return Err("Cannot build array malloc call for an unsized type");
387392
}
388393

389394
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
@@ -392,7 +397,7 @@ impl<'ctx> Builder<'ctx> {
392397
LLVMBuildArrayMalloc(self.builder, ty.as_type_ref(), size.as_value_ref(), c_string.as_ptr())
393398
};
394399

395-
PointerValue::new(value)
400+
Ok(PointerValue::new(value))
396401
}
397402

398403
// SubType: <P>(&self, ptr: PointerValue<P>) -> InstructionValue {

tests/all/test_values.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -928,17 +928,19 @@ fn test_allocations() {
928928

929929
let heap_ptr = builder.build_malloc(i32_type, "heap_ptr");
930930

931-
assert_eq!(*heap_ptr.get_type().print_to_string(), *CString::new("i32*").unwrap());
931+
assert!(heap_ptr.is_ok());
932+
assert_eq!(*heap_ptr.unwrap().get_type().print_to_string(), *CString::new("i32*").unwrap());
932933

933934
let heap_array = builder.build_array_malloc(i32_type, i32_three, "heap_array");
934935

935-
assert_eq!(*heap_array.get_type().print_to_string(), *CString::new("i32*").unwrap());
936+
assert!(heap_array.is_ok());
937+
assert_eq!(*heap_array.unwrap().get_type().print_to_string(), *CString::new("i32*").unwrap());
936938

937-
let bad_malloc_res = std::panic::catch_unwind(|| builder.build_malloc(unsized_type, ""));
939+
let bad_malloc_res = builder.build_malloc(unsized_type, "");
938940

939941
assert!(bad_malloc_res.is_err());
940942

941-
let bad_array_malloc_res = std::panic::catch_unwind(|| builder.build_array_malloc(unsized_type, i32_three, ""));
943+
let bad_array_malloc_res = builder.build_array_malloc(unsized_type, i32_three, "");
942944

943945
assert!(bad_array_malloc_res.is_err());
944946
}

0 commit comments

Comments
 (0)