Skip to content

Auto-cleanup - #240

Merged
faiface merged 23 commits into
mainfrom
auto-close-drop-class
Aug 15, 2026
Merged

Auto-cleanup#240
faiface merged 23 commits into
mainfrom
auto-close-drop-class

Conversation

@faiface

@faiface faiface commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR introduces automatic cleanup of linear resources: the box constraint is split into drop and share, and linear resources can declare a cleanup branch that Par uses to dispose of them automatically.

Language & types

  • Split the old box constraint into two: drop (value may be left unused) and share (value may be copied, reused, and dropped). New constraint chain: signed -> number -> data -> share -> drop.
  • Cleanup branches: a choice/either type can mark exactly one branch with * (e.g. .release* => !) to declare it safe for automatic disposal; the marker also appears in the corresponding case construction to register the branch.
  • A choice satisfies drop when it has a * branch whose result also satisfies drop; drop propagates structurally through pairs, eithers, recursive types, and generics.
  • Automatic cleanup: when a droppable linear value is no longer referenced (unused, shadowed, or abandoned on an error path), Par disposes of it by following the type's shape. Pairs clean both parts, eithers clean the present payload, and choices select their marked branch and continue. Iterative values clean up one step, not recursively.
  • Marked cleanup branches remain ordinary operations, so explicit calls (e.g. writer.close.try to observe closing errors) still work.

Implementation

  • Compiler inserts structural disposal for unused drop values; choices expose a synthesized #close branch to the backend.
  • Flat runtime gains a Global::Close node with interaction rules for closing pairs, eithers, primitives, and choices.
  • Rust externals receive the synthesized cleanup branch, so external resources (files, sockets, etc.) clean up automatically.

Standard library

  • Migrated all constraints from box to drop/share across core and basic.
  • Marked cleanup branches on resources: Stream.cancel*, Sql.Transaction.rollback*, Console.close*, Bytes/String reader & writer .close*, Map.close (renamed from .list), and list builders.
  • Renamed Option.ToResult to Option.ToTry.

Examples & tests

  • Migrated examples to the new constraints and cleanup providers, simplifying error paths (CopyFile, Echo, WebServer, Downloader, MiniGrep, and others).
  • Added tests/src/CleanupBranch.par covering automatic and explicit cleanup, structural follow-through, and external cleanup aliases.

Book

  • New "Auto-Cleanup" chapter; rewrote the error-handling chapter around it; updated all other chapters and constraint docs to drop/share.

@faiface
faiface merged commit 56e5772 into main Aug 15, 2026
3 checks passed
@faiface
faiface deleted the auto-close-drop-class branch August 16, 2026 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant