using cleanup on all exit paths + warning W3301
using cleanup on all exit paths + warning W3301
Section titled “using cleanup on all exit paths + warning W3301”The :service profile’s using statement (SPEC-019 §3.5) now guarantees that
the resource’s cleanup method runs on every exit path out of the block —
not just on fall-through.
All-exit-path cleanup
Section titled “All-exit-path cleanup”Cleanup (close() / deinit()) now fires on early return, try error
propagation, break, continue, and panic paths:
using file := try open("data.txt") do let header := try read_header(file) // on error: file.close() runs, then the error propagates if header.empty() do return // file.close() runs before the return end process(file)end // fall-through: file.close() runs as beforeBefore this release, cleanup on error and early-return paths was silently skipped — the file stayed open. This is now fixed.
Warning W3301 — using resource without a cleanup method
Section titled “Warning W3301 — using resource without a cleanup method”A using resource’s type must provide a cleanup method (close() or
deinit()). As of this release, when the resource’s type is inferred (no
explicit annotation) and resolves to a named struct with no cleanup method,
the compiler emits warning W3301 instead of silently falling back to a
runtime no-op:
warning: W3301: `using` resource of type 'Config' has no cleanup method (close/deinit); cleanup is a no-opThe full diagnostic behavior:
| Shape | Diagnostic |
|---|---|
using x: T := … (annotated) with no cleanup method | E3301 hard error (unchanged) |
| Inferred named struct with no cleanup method | W3301 warning (new) |
Inferred primitive resource (using f := open() where open() i64) | silent by design — primitives cannot have methods, so a diagnostic would be a false positive |
Fix: add a close() or deinit() method to the struct, or don’t wrap the
value in using if no cleanup is needed.