From 31ae48845492c12f2745c7f4c0cd2ea448e0942a Mon Sep 17 00:00:00 2001 From: zinc Date: Thu, 15 Oct 2026 02:35:04 +0330 Subject: destructors: reassigning an m owner that still holds a value frees the old value first, running its drop (right side first, then the free, then the store), with hint: x: reassigning frees the old value; a new value that is part of the old one is an error; u, d and captured owners keep the leak error; the new value's size, leaf type and drop are recorded like a declaration's; spec 6.8.5, 6.15.2 --- tests/conformance/6-ownership/drop_reassign.zc | 25 ++++++++++++++++++++++ tests/conformance/6-ownership/overwrite_frees.zc | 6 ++++++ tests/conformance/6-ownership/overwrite_leaks.zc | 6 ------ .../6-ownership/reassign_moves_inner_owner.zc | 5 +++++ 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 tests/conformance/6-ownership/drop_reassign.zc create mode 100644 tests/conformance/6-ownership/overwrite_frees.zc delete mode 100644 tests/conformance/6-ownership/overwrite_leaks.zc create mode 100644 tests/conformance/6-ownership/reassign_moves_inner_owner.zc (limited to 'tests/conformance') diff --git a/tests/conformance/6-ownership/drop_reassign.zc b/tests/conformance/6-ownership/drop_reassign.zc new file mode 100644 index 0000000..89f233e --- /dev/null +++ b/tests/conformance/6-ownership/drop_reassign.zc @@ -0,0 +1,25 @@ +// expect: a drop1 b drop2 3 drop3 drop10 drop11 12 t:7 t:8 9 t:9 drop12 +// destructors 6.8.5, 6.15.2: reassigning an owner that still holds a value frees the old value first, running its drop; the right side is computed before the free +include rt; +struct t { int v; void drop() { rt.put_s("t:"); rt.put_n(this.v); rt.put_s(" "); } } +int* mk(int v) { return drop(heap(v), fn() { rt.put_s("drop"); rt.put_n(*this); rt.put_s(" "); }); } +fn main() { + int* p = heap(1); + p.drop = fn() { rt.put_s("drop"); rt.put_n(*this); rt.put_s(" "); }; + rt.put_s("a "); + p = heap(2); + p.drop = fn() { rt.put_s("drop"); rt.put_n(*this); rt.put_s(" "); }; + rt.put_s("b "); + p = mk(*p + 1); + rt.put_n(*p); rt.put_s(" "); + int i = 10; + while (i < 13) { p = mk(i); i++; } + rt.put_n(*p); rt.put_s(" "); + t* s = rt.new(t); s.v = 7; + s = rt.new(t); s.v = 8; + t* u = rt.new(t); u.v = 9; + s = u; + rt.put_n(s.v); rt.put_s(" "); + return 0; +} +// zc0: skip diff --git a/tests/conformance/6-ownership/overwrite_frees.zc b/tests/conformance/6-ownership/overwrite_frees.zc new file mode 100644 index 0000000..9ae60d0 --- /dev/null +++ b/tests/conformance/6-ownership/overwrite_frees.zc @@ -0,0 +1,6 @@ +// expect: 0/ +// spec 6.8.5: overwriting a variable that still owns frees the old object first +include rt; +void pn(int x) { rt.put_n(x); rt.put_c(10); } +fn main() { int* a = rt.new(1); a[0] = 1; a = rt.new(1); pn(a[0]); return 0; } +// zc0: skip diff --git a/tests/conformance/6-ownership/overwrite_leaks.zc b/tests/conformance/6-ownership/overwrite_leaks.zc deleted file mode 100644 index a9b0138..0000000 --- a/tests/conformance/6-ownership/overwrite_leaks.zc +++ /dev/null @@ -1,6 +0,0 @@ -// expect error: a: still owns an object; overwriting it would leak it -// spec 6.8.5 -include rt; -void pn(int x) { rt.put_n(x); rt.put_c(10); } -fn main() { int* a = rt.new(1); a[0] = 1; a = rt.new(1); pn(a[0]); return 0; } -// zc0: skip diff --git a/tests/conformance/6-ownership/reassign_moves_inner_owner.zc b/tests/conformance/6-ownership/reassign_moves_inner_owner.zc new file mode 100644 index 0000000..d66d886 --- /dev/null +++ b/tests/conformance/6-ownership/reassign_moves_inner_owner.zc @@ -0,0 +1,5 @@ +// expect: 1 2/ +// spec 6.8.5: r still owns; assigning n frees r's object and moves n's into r, so n's block end frees nothing +include rt; +fn main() { word r = rt.new(4); { word n = rt.new(8); n[0] = 1; r = n; } rt.put_n(r[0]); r[0] = 2; rt.put_c(32); rt.put_n(r[0]); rt.put_c(10); return 0; } +// zc0: skip -- cgit v1.2.3