`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>
225 lines
7.1 KiB
Rust
225 lines
7.1 KiB
Rust
#![allow(dead_code)]
|
|
//! Pins down that the ROOT state is targetable by the chart's own name.
|
|
//!
|
|
//! Spec section "What a chart contains":
|
|
//! State — a named node. The root state (the machine itself) is just a
|
|
//! state. Every state is just a state.
|
|
//!
|
|
//! Spec section "How transitions work":
|
|
//! The target can be any state in the entire chart. ... state names must
|
|
//! be unique across the chart.
|
|
//!
|
|
//! From these two together: `on(Trig) => <ChartName>;` is a valid transition
|
|
//! whose target is the root. From a leaf, this is an up-transition; from any
|
|
//! state inside the chart it's an up-transition (root is always on the active
|
|
//! path) — exiting only the states strictly between current and root.
|
|
|
|
#![cfg(all(feature = "tokio", feature = "journal"))]
|
|
#![allow(unexpected_cfgs)]
|
|
|
|
use hsmc::{statechart, ActionKind, TraceEvent, TransitionReason};
|
|
|
|
#[derive(Default)]
|
|
pub struct Ctx;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Ev {
|
|
UpToRoot,
|
|
Halt,
|
|
}
|
|
|
|
statechart! {
|
|
Sub {
|
|
context: Ctx;
|
|
events: Ev;
|
|
default(A);
|
|
terminate(Halt);
|
|
|
|
entry: r_in;
|
|
exit: r_out;
|
|
|
|
state A {
|
|
entry: a_in;
|
|
exit: a_out;
|
|
default(B);
|
|
state B {
|
|
entry: b_in;
|
|
exit: b_out;
|
|
on(UpToRoot) => Sub; // ← target is the chart name = root
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl SubActions for SubActionContext<'_> {
|
|
async fn r_in(&mut self) {}
|
|
async fn r_out(&mut self) {}
|
|
async fn a_in(&mut self) {}
|
|
async fn a_out(&mut self) {}
|
|
async fn b_in(&mut self) {}
|
|
async fn b_out(&mut self) {}
|
|
}
|
|
|
|
const SR: u16 = 0;
|
|
const SA: u16 = 1;
|
|
const SB: u16 = 2;
|
|
const A_R_IN: u16 = 0;
|
|
const A_R_OUT: u16 = 1;
|
|
const A_A_IN: u16 = 2;
|
|
const A_A_OUT: u16 = 3;
|
|
const A_B_IN: u16 = 4;
|
|
const A_B_OUT: u16 = 5;
|
|
const E_UPTOROOT: u16 = 0;
|
|
|
|
fn entry(state: u16, action: u16) -> TraceEvent {
|
|
TraceEvent::ActionInvoked {
|
|
state,
|
|
action,
|
|
kind: ActionKind::Entry,
|
|
}
|
|
}
|
|
fn exit_(state: u16, action: u16) -> TraceEvent {
|
|
TraceEvent::ActionInvoked {
|
|
state,
|
|
action,
|
|
kind: ActionKind::Exit,
|
|
}
|
|
}
|
|
|
|
#[tokio::test(flavor = "current_thread")]
|
|
async fn det_root_targetable_by_chart_name() {
|
|
// Chart compiles. Targeting root by chart name resolves correctly.
|
|
tokio::task::LocalSet::new()
|
|
.run_until(async {
|
|
let mut m = Sub::new(Ctx);
|
|
let _ = m.dispatch(Ev::UpToRoot).await;
|
|
let actual = m.take_journal();
|
|
|
|
// Up-transition to root from B: exit B and A. Root is target,
|
|
// not exited, not re-entered, no default-descent.
|
|
let expected = vec![
|
|
TraceEvent::Started {
|
|
chart_hash: Sub::<8>::CHART_HASH,
|
|
},
|
|
TraceEvent::EnterBegan { state: SR },
|
|
entry(SR, A_R_IN),
|
|
TraceEvent::Entered { state: SR },
|
|
// Root's `default(A)` fires.
|
|
TraceEvent::TransitionFired {
|
|
from: Some(SR),
|
|
to: SA,
|
|
reason: TransitionReason::Internal,
|
|
},
|
|
TraceEvent::EnterBegan { state: SA },
|
|
entry(SA, A_A_IN),
|
|
TraceEvent::Entered { state: SA },
|
|
TraceEvent::TransitionComplete {
|
|
from: Some(SR),
|
|
to: SA,
|
|
},
|
|
// A's `default(B)` fires.
|
|
TraceEvent::TransitionFired {
|
|
from: Some(SA),
|
|
to: SB,
|
|
reason: TransitionReason::Internal,
|
|
},
|
|
TraceEvent::EnterBegan { state: SB },
|
|
entry(SB, A_B_IN),
|
|
TraceEvent::Entered { state: SB },
|
|
TraceEvent::TransitionComplete {
|
|
from: Some(SA),
|
|
to: SB,
|
|
},
|
|
TraceEvent::EventReceived { event: E_UPTOROOT },
|
|
TraceEvent::EventDelivered {
|
|
handler_state: SB,
|
|
event: E_UPTOROOT,
|
|
},
|
|
TraceEvent::TransitionFired {
|
|
from: Some(SB),
|
|
to: SR,
|
|
reason: TransitionReason::Event { event: E_UPTOROOT },
|
|
},
|
|
TraceEvent::ExitBegan { state: SB },
|
|
exit_(SB, A_B_OUT),
|
|
TraceEvent::Exited { state: SB },
|
|
TraceEvent::ExitBegan { state: SA },
|
|
exit_(SA, A_A_OUT),
|
|
TraceEvent::Exited { state: SA },
|
|
TraceEvent::TransitionComplete {
|
|
from: Some(SB),
|
|
to: SR,
|
|
},
|
|
];
|
|
|
|
assert_eq!(
|
|
actual, expected,
|
|
"root targetable; up-transition to root exits A and B only"
|
|
);
|
|
})
|
|
.await;
|
|
}
|
|
|
|
#[tokio::test(flavor = "current_thread")]
|
|
async fn det_root_target_does_not_re_enter_root() {
|
|
// After the up-to-root, root must NOT have re-entered.
|
|
tokio::task::LocalSet::new()
|
|
.run_until(async {
|
|
let mut m = Sub::new(Ctx);
|
|
let _ = m.dispatch(Ev::UpToRoot).await;
|
|
let j = m.take_journal();
|
|
// After the EVENT-driven TransitionFired (the UpToRoot one),
|
|
// no Entered event should fire — the up-transition exits B
|
|
// and A but never re-enters root, and root has no default
|
|
// (so no internal default-fire follows either).
|
|
let split = j
|
|
.iter()
|
|
.position(|e| {
|
|
matches!(
|
|
e,
|
|
TraceEvent::TransitionFired {
|
|
reason: TransitionReason::Event { .. },
|
|
..
|
|
}
|
|
)
|
|
})
|
|
.unwrap();
|
|
let entered_after = j[split..]
|
|
.iter()
|
|
.filter(|e| matches!(e, TraceEvent::Entered { .. }))
|
|
.count();
|
|
assert_eq!(
|
|
entered_after, 0,
|
|
"up-to-root must not re-enter root or default-descend"
|
|
);
|
|
})
|
|
.await;
|
|
}
|
|
|
|
#[tokio::test(flavor = "current_thread")]
|
|
async fn det_root_target_then_terminate() {
|
|
// After up-to-root, root is the innermost active state. Terminate exits root only.
|
|
tokio::task::LocalSet::new()
|
|
.run_until(async {
|
|
let mut m = Sub::new(Ctx);
|
|
let _ = m.dispatch(Ev::UpToRoot).await;
|
|
let _ = m.dispatch(Ev::Halt).await;
|
|
let j = m.take_journal();
|
|
// After UpToRoot we're in root only. Halt exits root only.
|
|
let exits: Vec<u16> = j
|
|
.iter()
|
|
.filter_map(|e| match e {
|
|
TraceEvent::Exited { state } => Some(*state),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
assert_eq!(
|
|
exits,
|
|
vec![SB, SA, SR],
|
|
"up-to-root exits B, A; terminate exits root"
|
|
);
|
|
assert!(matches!(j.last(), Some(TraceEvent::Terminated)));
|
|
})
|
|
.await;
|
|
}
|