From 2edeb31d21ed3641d6423c88d5a81190436906bd Mon Sep 17 00:00:00 2001 From: ychenfo Date: Thu, 31 Mar 2022 01:06:11 +0800 Subject: [PATCH] nac3core: do not get llvm value too eagerly for kernel invariant --- nac3core/src/codegen/expr.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index fa640cb86..33c0eec4e 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -1002,21 +1002,22 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>( let resolver = ctx.resolver.clone(); let val = resolver.get_symbol_value(*id, ctx).unwrap(); // if is option, need to cast pointer to handle None - match ( - &*ctx.unifier.get_ty(expr.custom.unwrap()), - val.to_basic_value_enum(ctx, generator)? - ) { - (TypeEnum::TObj { obj_id, params, .. }, BasicValueEnum::PointerValue(ptr)) + match &*ctx.unifier.get_ty(expr.custom.unwrap()) { + TypeEnum::TObj { obj_id, params, .. } if *obj_id == ctx.primitives.option.get_obj_id(&ctx.unifier) => { - let actual_ptr_ty = ctx.get_llvm_type( - generator, - *params.iter().next().unwrap().1, - ) - .ptr_type(AddressSpace::Generic); - ctx.builder.build_bitcast(ptr, actual_ptr_ty, "option_ptr_cast").into() + if let BasicValueEnum::PointerValue(ptr) = val.to_basic_value_enum(ctx, generator)? { + let actual_ptr_ty = ctx.get_llvm_type( + generator, + *params.iter().next().unwrap().1, + ) + .ptr_type(AddressSpace::Generic); + ctx.builder.build_bitcast(ptr, actual_ptr_ty, "option_ptr_cast").into() + } else { + unreachable!("option obj must be ptr") + } } - val => val.1.into(), + _ => val, } } },