`default(...)` is now a transition with the same semantics as any
event-driven transition. It fires immediately after the declaring
state's entries finish and its durings start, applying the standard
exit-to-LCA / enter-to-target algorithm. Three behavioral changes
follow from this unification:
* Optional. A state with children is no longer required to declare a
`default(...)`. When omitted, the state itself is the resting
innermost active state; substates are reachable only via explicit
transitions. This is the "microwave Standby" pattern — Standby
has substates like LowPower that you enter only on demand, and
`=> Standby;` lands at Standby itself with no descent.
* Targets anywhere. The default target may be any state in the chart
— a child (the classic descent case), a sibling, an ancestor,
anywhere. `default(<sibling-or-ancestor>)` exits the just-entered
state as part of the default-fire transition; the same rule fires
whether you wrote it for descent, redirection, or up-bounce.
* Cycle-checked at compile time. The default-edge graph (out-degree
≤ 1 per node) is rejected if it contains any cycle, including
`default(self)`. `state A { default(A); }` and `A → B → A` chains
fail to compile with a pinpoint error message naming the cycle.
This guarantees default chains terminate at runtime.
Implementation:
* `validate.rs`: drop the "must be direct child" and "must have
children" restrictions; add `validate_default_graph` (iterative
three-color cycle detection via chain walk).
* `ir.rs`: keep the unresolved default ident through to
`resolve_transitions` so non-child targets resolve against the
global state-name map (the same map transition targets use).
* `codegen.rs`: precomputed `(from, to)` paths no longer fold default
descent into the enter slice — each path is LCA-relative
`(exit, enter)` ending exactly at the explicit target.
`descend_defaults` (a parent-pointer walk) is replaced by
`follow_defaults`, which fires real transitions through
`apply_path` and emits `TransitionFired{Internal}` /
`TransitionComplete` brackets per link. Up-transitions don't
trigger default-following (nothing was freshly entered, so no
state's default fires).
* `active_durings_per_innermost_state`: predicate widened to include
composites without a default (they can now be the innermost active
state, so any durings they declare must be raced when current).
BREAKING CHANGES:
* Journal format. Default-fires now emit `TransitionFired` (with
`reason: TransitionReason::Internal`) and `TransitionComplete`
brackets around their entry sequence. `Internal` was previously
reserved and never emitted; it is now active. Tests that
hand-build expected journal sequences must include these brackets
around every default-fire.
* Validation. `default(<non-child>)` and `default` on a childless
state previously raised compile-time errors. Both are now valid.
Code relying on these as guard rails must adapt.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
137 lines
4.3 KiB
Rust
137 lines
4.3 KiB
Rust
#![allow(dead_code)]
|
|
//! Deterministic-flow tests: `default(...)` targeting an ANCESTOR.
|
|
//! Inner's `default(Outer)` is a valid up-default. Inner is reachable
|
|
//! only via an explicit transition. When entered, its default fires
|
|
//! immediately, exiting Inner back to Outer (up-transition rule:
|
|
//! Outer is not re-entered).
|
|
|
|
#![cfg(all(feature = "tokio", feature = "journal"))]
|
|
#![allow(unexpected_cfgs)]
|
|
|
|
use hsmc::{statechart, TraceEvent};
|
|
|
|
#[derive(Default)]
|
|
pub struct Ctx;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Ev {
|
|
GoIn,
|
|
Halt,
|
|
}
|
|
|
|
statechart! {
|
|
AncestorDefault {
|
|
context: Ctx;
|
|
events: Ev;
|
|
default(Outer);
|
|
terminate(Halt);
|
|
|
|
state Outer {
|
|
entry: outer_in;
|
|
on(GoIn) => Inner;
|
|
state Inner {
|
|
entry: inner_in;
|
|
exit: inner_out;
|
|
// Inner's default redirects up to Outer. After entering
|
|
// Inner, this default fires as an up-transition that
|
|
// exits Inner. Outer is not re-entered.
|
|
default(Outer);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AncestorDefaultActions for AncestorDefaultActionContext<'_> {
|
|
async fn outer_in(&mut self) {}
|
|
async fn inner_in(&mut self) {}
|
|
async fn inner_out(&mut self) {}
|
|
}
|
|
|
|
const AD_ROOT: u16 = 0;
|
|
const AD_OUTER: u16 = 1;
|
|
const AD_INNER: u16 = 2;
|
|
|
|
#[tokio::test(flavor = "current_thread")]
|
|
async fn det_initial_lands_at_outer_no_default_descent() {
|
|
// Outer has no default of its own — initial entry stops at Outer.
|
|
tokio::task::LocalSet::new()
|
|
.run_until(async {
|
|
let mut m = AncestorDefault::new(Ctx);
|
|
let _ = m.step(hsmc::Duration::ZERO).await;
|
|
assert_eq!(m.current_state(), AncestorDefaultState::Outer);
|
|
let j = m.take_journal();
|
|
let entered: Vec<u16> = j
|
|
.iter()
|
|
.filter_map(|e| match e {
|
|
TraceEvent::Entered { state } => Some(*state),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
assert_eq!(entered, vec![AD_ROOT, AD_OUTER]);
|
|
})
|
|
.await;
|
|
}
|
|
|
|
#[tokio::test(flavor = "current_thread")]
|
|
async fn det_explicit_into_inner_bounces_back_via_up_default() {
|
|
// `=> Inner` enters Inner (entries fire), then Inner's default fires
|
|
// as an up-transition Inner → Outer. Outer is NOT re-entered (it
|
|
// was already on the active path).
|
|
tokio::task::LocalSet::new()
|
|
.run_until(async {
|
|
let mut m = AncestorDefault::new(Ctx);
|
|
let _ = m.dispatch(Ev::GoIn).await;
|
|
|
|
assert_eq!(
|
|
m.current_state(),
|
|
AncestorDefaultState::Outer,
|
|
"Inner's default bounces us back to Outer"
|
|
);
|
|
|
|
let j = m.take_journal();
|
|
|
|
// Inner was entered (its entries fired) and then exited
|
|
// (its default fired an up-transition).
|
|
let entered_states: Vec<u16> = j
|
|
.iter()
|
|
.filter_map(|e| match e {
|
|
TraceEvent::Entered { state } => Some(*state),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
// Across the full journal: Root, Outer, Inner. After Inner's
|
|
// up-default to Outer, Outer is NOT re-entered.
|
|
assert_eq!(entered_states, vec![AD_ROOT, AD_OUTER, AD_INNER]);
|
|
|
|
let exited_states: Vec<u16> = j
|
|
.iter()
|
|
.filter_map(|e| match e {
|
|
TraceEvent::Exited { state } => Some(*state),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
assert_eq!(
|
|
exited_states,
|
|
vec![AD_INNER],
|
|
"only Inner exits; Outer remains on the active path"
|
|
);
|
|
|
|
// Outer's entry actions ran exactly once (initial entry only).
|
|
let outer_entries = j
|
|
.iter()
|
|
.filter(|e| {
|
|
matches!(
|
|
e,
|
|
TraceEvent::ActionInvoked {
|
|
state: AD_OUTER,
|
|
kind: hsmc::ActionKind::Entry,
|
|
..
|
|
}
|
|
)
|
|
})
|
|
.count();
|
|
assert_eq!(outer_entries, 1, "Outer is entered once and stays put");
|
|
})
|
|
.await;
|
|
}
|