From 5aeeedbdce36db2fb3b257fa649c8cfdf68d64c2 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sat, 14 Dec 2024 15:19:02 +0100 Subject: [PATCH] fixing non pointer struct method receiver not counting as use forgot to strip pointer Signed-off-by: Jakub Doka --- lang/README.md | 31 ++++++++++++++++++- lang/src/backend/hbvm.rs | 8 ++++- lang/src/son.rs | 2 ++ .../son_tests_method_receiver_by_value.txt | 0 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 lang/tests/son_tests_method_receiver_by_value.txt diff --git a/lang/README.md b/lang/README.md index 80853dc2..1ed58ee5 100644 --- a/lang/README.md +++ b/lang/README.md @@ -692,7 +692,6 @@ modify := fn($num: ^uint): void { } ``` - #### fb_driver ```hb arm_fb_ptr := fn(): uint return 100 @@ -733,6 +732,36 @@ main := fn(): uint { ### Purely Testing Examples +#### method_receiver_by_value +```hb +$log := fn(ptr: ^u8): void return @eca(0, ptr) +Struct := struct { + ptr: ^u8, + print := fn(self: Self, rhs: Self): void { + log(self.ptr) + log(rhs.ptr) + } +} + +Struct2 := struct { + ptr: ^u8, + print2 := fn(self: ^Self, rhs: ^Self): void { + log(self.ptr) + log(rhs.ptr) + } +} + +main := fn(): void { + lhs := Struct.("Hello, World!\0") + rhs := Struct.("Goodbye, World!\0") + lhs.print(rhs) + + lhs2 := Struct2.("Hello, World!\0") + rhs2 := Struct2.("Goodbye, World!\0") + lhs2.print2(&rhs2) +} +``` + #### comparing_floating_points ```hb main := fn(): uint { diff --git a/lang/src/backend/hbvm.rs b/lang/src/backend/hbvm.rs index 0181eb06..b559b417 100644 --- a/lang/src/backend/hbvm.rs +++ b/lang/src/backend/hbvm.rs @@ -1121,7 +1121,7 @@ pub struct AbleOsExecutableHeader { #[cfg(test)] pub fn test_run_vm(out: &[u8], output: &mut String) { - use core::fmt::Write; + use core::{ffi::CStr, fmt::Write}; let mut stack = [0_u64; 1024 * 20]; @@ -1138,6 +1138,12 @@ pub fn test_run_vm(out: &[u8], output: &mut String) { match vm.run() { Ok(hbvm::VmRunOk::End) => break Ok(()), Ok(hbvm::VmRunOk::Ecall) => match vm.read_reg(2).0 { + 0 => writeln!( + output, + "{}", + unsafe { CStr::from_ptr(vm.read_reg(3).0 as _) }.to_str().unwrap() + ) + .unwrap(), 1 => writeln!(output, "ev: Ecall").unwrap(), // compatibility with a test 69 => { let [size, align] = [vm.read_reg(3).0 as usize, vm.read_reg(4).0 as usize]; diff --git a/lang/src/son.rs b/lang/src/son.rs index c38679eb..cd73d06d 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -4591,6 +4591,7 @@ impl<'a> Codegen<'a> { _ => {} } self.assert_ty(func.pos(), caller, ty, fa!("caller argument {}", carg.name)); + self.strip_ptr(caller); self.add_clobbers(*caller, &mut clobbered_aliases); self.ci.nodes.lock(caller.id); inps.push(caller.id); @@ -5987,6 +5988,7 @@ mod tests { fb_driver; // Purely Testing Examples; + method_receiver_by_value; comparing_floating_points; pointer_comparison; different_function_destinations; diff --git a/lang/tests/son_tests_method_receiver_by_value.txt b/lang/tests/son_tests_method_receiver_by_value.txt new file mode 100644 index 00000000..e69de29b