From abd56ef20bc8061bbecec29edd1301d324c2cb34 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 17 Oct 2026 08:34:42 +0000 Subject: feat(classes): destroy stack objects at scope exit Co-Authored-By: Claude Opus 5.5 --- tests/i/class-delete/expected.txt | 12 ++++++++++ tests/i/class-delete/notes.md | 2 ++ tests/i/class-raii/case.json | 1 + tests/i/class-raii/expected.txt | 12 ++++++++++ tests/i/class-raii/notes.md | 5 ++++ tests/i/class-raii/program.cps | 49 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+) create mode 100644 tests/i/class-raii/case.json create mode 100644 tests/i/class-raii/expected.txt create mode 100644 tests/i/class-raii/notes.md create mode 100644 tests/i/class-raii/program.cps (limited to 'tests/i') diff --git a/tests/i/class-delete/expected.txt b/tests/i/class-delete/expected.txt index 85c4753..c7e7246 100644 --- a/tests/i/class-delete/expected.txt +++ b/tests/i/class-delete/expected.txt @@ -8,3 +8,15 @@ double caught [Fatal] line 30: Invalid memory address (not an active allocation) stack caught leaf:2 +pair +leaf:2 +leaf:1 +pair +leaf:2 +leaf:1 +pair +leaf:2 +leaf:1 +pair +leaf:2 +leaf:1 diff --git a/tests/i/class-delete/notes.md b/tests/i/class-delete/notes.md index c4e2638..efe1e52 100644 --- a/tests/i/class-delete/notes.md +++ b/tests/i/class-delete/notes.md @@ -3,3 +3,5 @@ Human review: pending. Expected output was independently traced by AI. Explicit calls print only the outer method. Keyword DELETE prints outer, right, left. Invalid stack and stale heap addresses throw before any method runs. The array element uses an ordinary explicit method call. + +Normal main exit destroys Items[2], Items[1], Stack and the original Obj storage; each prints pair, leaf:2, leaf:1. diff --git a/tests/i/class-raii/case.json b/tests/i/class-raii/case.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/i/class-raii/case.json @@ -0,0 +1 @@ +{} diff --git a/tests/i/class-raii/expected.txt b/tests/i/class-raii/expected.txt new file mode 100644 index 0000000..532f9db --- /dev/null +++ b/tests/i/class-raii/expected.txt @@ -0,0 +1,12 @@ +destroy:31 +destroy:32 +destroy:20 +destroy:21 +destroy:50 +destroy:42 +destroy:41 +destroy:33 +destroy:2 +destroy:1 +main end +destroy:100 diff --git a/tests/i/class-raii/notes.md b/tests/i/class-raii/notes.md new file mode 100644 index 0000000..336e5cb --- /dev/null +++ b/tests/i/class-raii/notes.md @@ -0,0 +1,5 @@ +# Stack lifetimes + +Human review: pending. Expected output was independently traced by AI. + +Loop re-declarations destroy 31 and 32 before reinitialization. Copy destroys local 20 then its shallow parameter copy 21. Early RETURN destroys record field 50, array elements 42 and 41, loop local 33, original rebound storage 2 and first local 1. Heap object 99 stays alive. Never is not constructed. Main destroys 100 after its final output. diff --git a/tests/i/class-raii/program.cps b/tests/i/class-raii/program.cps new file mode 100644 index 0000000..ff350f9 --- /dev/null +++ b/tests/i/class-raii/program.cps @@ -0,0 +1,49 @@ +CLASS Traced + Id : INTEGER + PROCEDURE NEW() + Id <- 0 + ENDPROCEDURE + PROCEDURE DELETE() + OUTPUT "destroy:", Id + ENDPROCEDURE +ENDCLASS +TYPE Wrapped + DECLARE Item : Traced +ENDTYPE +PROCEDURE Copy(Value : Traced) + DECLARE Local : Traced + Local.Id <- 20 + Value.Id <- 21 +ENDPROCEDURE +PROCEDURE Work() + DECLARE First : Traced + First.Id <- 1 + DECLARE Rebound : Traced + Rebound.Id <- 2 + Rebound <- NEW Traced() + Rebound.Id <- 99 + DECLARE I : INTEGER + FOR I <- 1 TO 3 + DECLARE Again : Traced + Again.Id <- 3 + Again.Id <- I + 30 + NEXT I + DECLARE Arr : ARRAY[1:2] OF Plain + Arr[1].Item.Id <- 41 + Arr[2].Item.Id <- 42 + DECLARE Rec : Wrapped + Rec.Item.Id <- 50 + CALL Copy(First) + IF TRUE THEN + RETURN + ENDIF + DECLARE Never : Traced + Never.Id <- 77 +ENDPROCEDURE +CLASS Plain + Item : Traced +ENDCLASS +DECLARE Global : Traced + Global.Id <- 100 +CALL Work() +OUTPUT "main end" -- cgit v1.3.1-4-g156e