@@ -348,7 +348,7 @@ impl<'ctx> Builder<'ctx> {
348
348
}
349
349
350
350
// 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 > {
352
352
// LLVMBulidMalloc segfaults if ty is unsized
353
353
let is_sized = match ty. as_basic_type_enum ( ) {
354
354
BasicTypeEnum :: ArrayType ( ty) => ty. is_sized ( ) ,
@@ -359,7 +359,7 @@ impl<'ctx> Builder<'ctx> {
359
359
BasicTypeEnum :: VectorType ( ty) => ty. is_sized ( ) ,
360
360
} ;
361
361
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" ) ;
363
363
}
364
364
365
365
let c_string = CString :: new ( name) . expect ( "Conversion to CString failed unexpectedly" ) ;
@@ -368,11 +368,16 @@ impl<'ctx> Builder<'ctx> {
368
368
LLVMBuildMalloc ( self . builder , ty. as_type_ref ( ) , c_string. as_ptr ( ) )
369
369
} ;
370
370
371
- PointerValue :: new ( value)
371
+ Ok ( PointerValue :: new ( value) )
372
372
}
373
373
374
374
// 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 > {
376
381
// LLVMBulidArrayMalloc segfaults if ty is unsized
377
382
let is_sized = match ty. as_basic_type_enum ( ) {
378
383
BasicTypeEnum :: ArrayType ( ty) => ty. is_sized ( ) ,
@@ -383,7 +388,7 @@ impl<'ctx> Builder<'ctx> {
383
388
BasicTypeEnum :: VectorType ( ty) => ty. is_sized ( ) ,
384
389
} ;
385
390
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" ) ;
387
392
}
388
393
389
394
let c_string = CString :: new ( name) . expect ( "Conversion to CString failed unexpectedly" ) ;
@@ -392,7 +397,7 @@ impl<'ctx> Builder<'ctx> {
392
397
LLVMBuildArrayMalloc ( self . builder , ty. as_type_ref ( ) , size. as_value_ref ( ) , c_string. as_ptr ( ) )
393
398
} ;
394
399
395
- PointerValue :: new ( value)
400
+ Ok ( PointerValue :: new ( value) )
396
401
}
397
402
398
403
// SubType: <P>(&self, ptr: PointerValue<P>) -> InstructionValue {
0 commit comments