From 000b128551c99eb61889f2e5a85f641234d82b35 Mon Sep 17 00:00:00 2001 From: ychenfo Date: Sat, 26 Mar 2022 23:32:50 +0800 Subject: [PATCH] nac3artiq: cast none to correct ptr type (#241) --- nac3artiq/src/symbol_resolver.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/nac3artiq/src/symbol_resolver.rs b/nac3artiq/src/symbol_resolver.rs index b2aa1ddf..213c12d3 100644 --- a/nac3artiq/src/symbol_resolver.rs +++ b/nac3artiq/src/symbol_resolver.rs @@ -917,9 +917,24 @@ impl InnerResolver { if let TopLevelDef::Class { fields, .. } = &*definition { let values: Result>, _> = fields .iter() - .map(|(name, _, _)| { - self.get_obj_value(py, obj.getattr(&name.to_string())?, ctx, generator).map_err(|e| - super::CompileError::new_err(format!("Error getting field {}: {}", name, e))) + .map(|(name, ty, _)| { + let v = self.get_obj_value(py, obj.getattr(&name.to_string())?, ctx, generator) + .map_err(|e| super::CompileError::new_err(format!("Error getting field {}: {}", name, e))); + match (v, ctx.unifier.get_ty_immutable(*ty).as_ref()) { + (Ok(Some(v)), 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); + Ok(Some(ctx.builder.build_bitcast( + v, + actual_ptr_ty, + "option_none_ptr_cast", + ))) + } + (v, _) => v, + } }) .collect(); let values = values?;