From c622d59c93ee7aa789a65ae049a5557bc439f8e8 Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 23 Jul 2025 17:52:28 +0300 Subject: [PATCH] Fix borrow derefs --- examples/array_structs.reid | 26 ++++++++++---------------- reid-llvm-lib/src/builder.rs | 1 + reid/src/codegen.rs | 9 +++++++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/array_structs.reid b/examples/array_structs.reid index de7d6f4..91b24dd 100644 --- a/examples/array_structs.reid +++ b/examples/array_structs.reid @@ -1,30 +1,24 @@ // Arithmetic, function calls and imports! struct Test { - field: i32, - second: [u32; 4] + field: i32, + second: [u32; 4], } fn test() -> Test { - let value = Test { - field: 5, - second: [6, 3, 4, 8], - }; - return value; + let value = Test { + field: 5, + second: [6, 3, 4, 8], + }; + return value; } fn main() -> u32 { - let mut value = test(); + let mut value = test(); - let mut a = &mut value; + let mut a = &mut value; - *a.second[2] = 15; + let test = *a.field; - let b = 4; - if value.field < b { return 0; - } - - - return value.second[2]; } diff --git a/reid-llvm-lib/src/builder.rs b/reid-llvm-lib/src/builder.rs index 67de72b..949a147 100644 --- a/reid-llvm-lib/src/builder.rs +++ b/reid-llvm-lib/src/builder.rs @@ -440,6 +440,7 @@ impl Builder { } Instr::GetStructElemPtr(ptr_val, idx) => { let ptr_ty = ptr_val.get_type(&self)?; + dbg!(&ptr_ty); if let Type::Ptr(ty) = ptr_ty { if let Type::CustomType(val) = *ty { match self.type_data(&val).kind { diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index dfcc59e..6abc671 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -256,7 +256,6 @@ impl mir::Module { let mut typedefs = self.typedefs.clone(); typedefs.sort_by(|a, b| b.source_module.cmp(&a.source_module)); - dbg!(&self.module_id, &typedefs); for typedef in typedefs { let type_key = CustomTypeKey(typedef.name.clone(), typedef.source_module); let type_value = match &typedef.kind { @@ -1001,6 +1000,7 @@ impl mir::Expression { let TypeKind::CodegenPtr(inner) = &struct_val.1 else { panic!("tried accessing non-pointer"); }; + dbg!(&inner); let TypeKind::CustomType(key) = *inner.clone() else { panic!("tried accessing non-custom-type"); }; @@ -1097,9 +1097,14 @@ impl mir::Expression { .stack_values .get(&varref.1) .expect("Variable reference not found?!"); + + let TypeKind::CodegenPtr(ptr_inner) = &v.1 else { + panic!(); + }; + Some(StackValue( StackValueKind::mutable(*mutable, v.0.instr()), - TypeKind::Borrow(Box::new(v.1.clone()), *mutable), + TypeKind::Borrow(Box::new(*ptr_inner.clone()), *mutable), )) } mir::ExprKind::Deref(varref) => {