@@ -12,7 +12,7 @@ use crate::basic_block::BasicBlock;
12
12
use crate :: values:: { AggregateValue , AggregateValueEnum , AsValueRef , BasicValue , BasicValueEnum , PhiValue , FunctionValue , IntValue , PointerValue , VectorValue , InstructionValue , GlobalValue , IntMathValue , FloatMathValue , PointerMathValue , InstructionOpcode , CallSiteValue } ;
13
13
#[ llvm_versions( 3.9 ..=latest) ]
14
14
use crate :: values:: StructValue ;
15
- use crate :: types:: { AsTypeRef , BasicType , IntMathType , FloatMathType , PointerType , PointerMathType } ;
15
+ use crate :: types:: { AsTypeRef , BasicType , BasicTypeEnum , IntMathType , FloatMathType , PointerType , PointerMathType } ;
16
16
17
17
use std:: ffi:: CString ;
18
18
use std:: marker:: PhantomData ;
@@ -349,6 +349,19 @@ impl<'ctx> Builder<'ctx> {
349
349
350
350
// TODOC: Heap allocation
351
351
pub fn build_malloc < T : BasicType < ' ctx > > ( & self , ty : T , name : & str ) -> PointerValue < ' ctx > {
352
+ // LLVMBulidMalloc segfaults if ty is unsized
353
+ let is_sized = match ty. as_basic_type_enum ( ) {
354
+ BasicTypeEnum :: ArrayType ( ty) => ty. is_sized ( ) ,
355
+ BasicTypeEnum :: FloatType ( ty) => ty. is_sized ( ) ,
356
+ BasicTypeEnum :: IntType ( ty) => ty. is_sized ( ) ,
357
+ BasicTypeEnum :: PointerType ( ty) => ty. is_sized ( ) ,
358
+ BasicTypeEnum :: StructType ( ty) => ty. is_sized ( ) ,
359
+ BasicTypeEnum :: VectorType ( ty) => ty. is_sized ( ) ,
360
+ } ;
361
+ if !is_sized {
362
+ panic ! ( "Cannot build malloc call for an unsized type {:?}" , ty) ;
363
+ }
364
+
352
365
let c_string = CString :: new ( name) . expect ( "Conversion to CString failed unexpectedly" ) ;
353
366
354
367
let value = unsafe {
0 commit comments