Wado

Effects Through Closures and Iterators

Context

A closure's type carries the effects a call to it may perform (Closure Types), and an iterator may perform effects as it advances: a next that reads the next line of a stream does I/O. The compiler holds neither.

The standard library's other closure parameters (for_each, fold, sort_by, Option::map, …) declare no effects either, and rely on the same leak.

Decision

Mechanism

The elaborator cannot work out what a closure body performs: that needs every call in it resolved, which is the effect check's work, and the effect check runs on the whole program after reify. So the elaborator leaves a variable where an effect row is not yet known, and the effect check solves it.

Roadmap

  1. [x] A type parameter bounded by a function type is that signature in an impl as in a function: callable wherever a value of it is, a field among them. The impl matches a target whose argument is a closure of that signature, solving the parameters only the signature names, and an associated type naming one of them is what the target binds at the receiver. That last held of no impl parameter away from its declaration position, a nested one included.
  2. [x] An effect parameter is resolved from the effects a closure argument's body performs, inferred where the call is checked. The body is still checked against its enclosing function, so nothing that compiles today stops compiling.
  3. [x] Iterator and IntoIterator are open. An iterator whose next performs I/O compiles, and a loop over it, directly or through an adapter, demands its effects of the caller.
  4. [x] The eager functions taking a closure declare an effect parameter, and a function value declaring effects passes to one.
  5. [x] An effect parameter a call resolves is substituted in the types the call produces, as a type parameter is. The lazy adapters are generic over their closure's type, so an adapter built from a function value carries its effects.
  6. [x] A closure literal's type carries the effects inferred from its body. The body is checked against them, and an expected type without with admits none. An effectful closure through map demands its effects of whoever advances the adapter. error_closure_beyond_expected_fn_type.wado is rejected.
  7. [x] The specification states the rules above.
  8. [x] A function generic over an effect is taken as a value: the expected fn(...) type settles its effect parameters as it settles its type parameters. effect_generic_fn_as_value.wado holds it.

Known gaps