// Early `return` (ADR 0037): what it returns, and where it can't be. fn checked :: n: Int -> Int { if n < 0 { return "negative" //~ ERROR E0202 expected `Int`, found `String` } n } fn unit_only :: n: Int -> Int { if n < 0 { return //~ ERROR E0202 expected `Int`, found `()` } n } /// `return` has type `Never`: it fits where any value is expected. fn fits :: n: Int -> String { let label: String = if n > 0 { "positive" } else { return "other" } label } fn in_a_lambda :: xs: List -> List { list.map xs fn x { if x < 0 { return 0 //~ ERROR E0119 from a lambda } x } } fn in_a_capture :: n: Int -> fn(Int) -> Int { add _ (return 1) //~ ERROR E0119 from a capture } fn add :: a: Int b: Int -> Int { a + b } effect ask { fn ask :: -> Int } fn in_a_clause :: -> Int { handle { ask () } with { ask -> return 1 //~ ERROR E0119 from a handler clause } } fn in_a_cleanup :: -> Int / //~ WARN W0501 never performed { defer { return 1 //~ ERROR E0119 from a `defer` cleanup } 2 } import std/list /// A callback that could still `return` can't outlive its `use`. fn keep :: f: fn() -> a / -> fn() -> a / { f } fn escapes :: -> Int { let later = { use <- keep //~ ERROR E0703 would escape return 1 } 0 } /// A callee whose callback's effects are fixed can't let it `return`. fn total :: f: fn() -> Int -> Int { f () } fn fixed :: -> Int { use <- total return 1 //~ ERROR E0502 can't leave the function from this callback } /// Nor can an actor's body: it runs in another actor. fn spawned :: -> Int / { let _ = process.spawn fn { return 1 } //~ ERROR E0119 from a lambda { use <- process.spawn return 2 //~ ERROR E0502 can't leave the function from this callback } 0 } import std/process.{type proc}