import std/io import std/process.{type proc} import std/ref // `defer` cleanups (ADR 0016) may use effects whose operations can neither // unwind nor suspend: `io`, `ffi` and references. effect log { fn log :: message: String -> () } effect gen { ctl yield :: x: Int -> () } effect fail { final ctl fail :: reason: String -> Never } pub fn prints :: -> Int / { defer io.println "bye" 1 } pub fn restores :: -> Int { region { let r = ref.new 0 defer ref.set r 0 ref.set r 1 ref.get r } } // The block's value is the value of what follows the `defer`. pub fn value :: -> String / { defer io.println "bye" "kept" } //~^^^ TYPE value: fn() -> String / pub fn may_panic :: -> Int { defer panic "allowed: panics unwind anyway" 1 } pub fn logs :: -> () / { defer log "bye" //~ ERROR E0506 cannot perform `log` () } pub fn generates :: -> () / { defer yield 1 //~ ERROR E0506 cannot perform `gen` () } pub fn fails :: -> () / { defer fail "again" //~ ERROR E0506 cannot perform `fail` () } pub fn messages :: -> () / { defer process.sleep 1 //~ ERROR E0506 cannot perform `proc` () } // A callback could do anything: its effects must be known (as in a // lambda, a declared row variable is not allowed here). pub fn calls :: action: fn() -> () / -> () / { defer action () //~ ERROR E0506 cannot call code with unknown effects () } // An inferred callback becomes one without effects. fn twice :: cleanup { defer cleanup () defer cleanup () () } //~^^^^^ TYPE twice: fn(fn() -> ()) -> () // The cleanup is unit. pub fn returns_unit :: -> () { defer 1 //~ ERROR E0202 () }