Classifying statements
classify(stmt, scope, relaxed) (classify.rs) decides what each statement is. The result is
either a Node, which carries everything the scheduler needs, or a Barrier with an optional
reason for the report.
What a node records
pub(crate) struct Node {
pub stmt: Stmt, // the statement as written
pub defs: Vec<(Ident, bool)>, // bindings it introduces, with their `mut` flag
pub acc: Access, // what it reads and writes (see the next chapter)
pub awaits: bool, // awaits outside nested closures and async blocks
pub effect: bool, // an ordinary statement: keeps its order with other effects
pub exits: bool, // contains ?, return, break or continue that leave it
pub label: String, // for reports
pub line: usize,
}
Finding awaits and exits
The Exits visitor walks an expression. It does not enter closures, async blocks or nested items,
because their awaits and exits belong to them. It records:
awaits, and the names of the awaited callees (methodforx.method(),Type::ffor a path call). A second list,awaited_unscoped, holds only the awaits outside any nested block. It is used to tell a guard that is kept from one that is dropped inside the statement.exits: any?,return, orbreak/continuethat leaves the statement. Loops and labeled blocks inside the expression are tracked, so abreakaimed at an inner loop does not count.- a
blocker: something psync cannot rewrite. That is an exit hidden in macro tokens,bail!-style macros,yield, orbreakwith a value.
The decision
In order, a statement is a barrier if:
- it is a
letwithout an initializer, or an item; - it carries
#[psync::seq]or#[psync::fresh]; Exitsfound a blocker;- it calls a clock function (
now,elapsed,duration_since) outside closures and async blocks; - its pattern binds by
ref; - it awaits a delay (
sleep,sleep_until,tick,yield_now,delay_for), or awaits a guard (lock,lock_owned,read,write,acquire,acquire_owned,acquire_many,begin,transaction) outside a nested block.
Otherwise, it is a node. It is an effect unless it is shaped (a let, an await expression
statement, or a logging macro) and either awaits or binds a pure value. The block’s last
expression, its value, is always made a barrier by the caller.
relaxed is used for a loop’s prefix. It admits bindings of non-pure values as movable, because
the loop code has already confined the prefix to per-iteration data. An ordinary statement may
join a prefix only if it writes nothing, for example a guard like if done { continue; }.
What is pure
is_pure accepts expressions with no side effects that are cheap to move:
- literals, paths, closures and ranges;
- field access,
&(not&mut), unary and binary operators, casts, indexing, tuples, arrays and struct literals built from pure parts; - method calls from a fixed list of read-only methods (
clone,to_string,len,iter,is_some, …); - constructor calls:
T::new,T::from,T::default,with_capacity,Some,Ok,Err, and tuple structs; format!,vec!,concat!andstringify!with pure arguments.
Everything else that does not await is an ordinary statement, an effect, and keeps its order.
Labels
Report labels are made from the bindings and the first awaited callee (user = user()?). A
non-awaited binding shows the first 23 characters of its value. A trailing ? means the node can
exit.