Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 28 additions & 45 deletions spec/checker/src/form/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2442,15 +2442,14 @@ impl Built {
// test's value (the Iterate passes the test's outputs to the body).
self.seed_flow(sc, test_stmts, rest, &[])?;
let vars = carried_vars(sc, &[test_stmts, rest]);
let thread = (sc.omega.is_some() || parent_omega.is_some())
&& (self.stmts_need_order(test_stmts)
|| self.stmts_need_order(rest)
|| sc.omega.is_some());
// A count Iterate may halt, so it has no applied form unless a region
// actually consumes ω (C7 drops a carried ω that is only passed through).
if !thread {
// A count may halt, so it carries order whenever the enclosing
// region has one. C7 keeps that order when neither region uses it
// (erratum 2026-11-13). The old refusal here blamed that removal.
// It is false when an order is in scope. When none is, the region
// still has no order to carry; that is the message below, not C7.
if sc.omega.is_none() && parent_omega.is_none() {
return Err(self.deny(
"bound: a count-bounded Iterate must carry ω, and C7 removes a carried ω that neither region uses, so this pure count loop has no applied form",
"omega: a count-bounded Iterate may halt, and this region has no ω to carry",
));
}
let mut test = self.open_region(sc, parent_omega, &vars, true)?;
Expand All @@ -2468,20 +2467,13 @@ impl Built {
};
let mut bod = self.open_region(sc, parent_omega, &vars, true)?;
let bw = bod.omega;
// A body break that names this loop is the flag. A pure count still
// has no applied form: the flag is a bool and is not a use of ω (O8).
// A body break that names this loop is the flag. The flag is a
// bool and is not a use of ω. The count still carries order, and
// C7 keeps that order when neither region uses it (erratum 2026-11-13).
let bflag = self.exec_body(rest, &mut bod, bw, ret_ty, brk)?;
if let Some(r) = bod.exit_residual {
sc.exit_residual = Some(r);
}
if !region_uses_order(&test) && !region_uses_order(&bod) {
// The body may have been read as a flag. A pure count is still
// outside Dom, so that reading is not an admission (O8).
BODY_BREAK.with(|c| c.set(false));
return Err(self.deny(
"bound: a count-bounded Iterate must carry ω, and C7 removes a carried ω that neither region uses, so this pure count loop has no applied form",
));
}
let test_b = Body {
nodes: std::mem::take(&mut test.nodes),
out: region_outs(&test, flag, &vars, true, None)
Expand Down Expand Up @@ -2765,23 +2757,10 @@ impl Built {
let mut bod = build_region(mid_body, &parts.flag1, &parts.sets1)?;
lose_outputs(sc, &test);
lose_outputs(sc, &bod);
// Both regions are given order together when the enclosing region
// has one, so a count does not reach a state where only one region
// carries it. The refusal for a count with no order at all is below.
let thread = test.omega.is_some() || bod.omega.is_some() || sc.omega.is_some();
if thread {
for region in [&mut test, &mut bod] {
if region.omega.is_none() {
let p = sc
.omega
.or(parent_omega)
.ok_or_else(|| self.deny("defect: a loop region has no ω"))?;
// inputs were built without ω; refuse rather than renumber
if !region.caps.is_empty() || !region.nodes.is_empty() {
return Err(self.deny("bound: a count-bounded Iterate must carry ω, and this loop's regions do not both receive it"));
}
region.caps.insert(0, p);
region.omega = Some(Src::In(0));
}
}
}
let omega_used = |region: &Scope| -> bool {
let Some(Src::In(0)) = region.omega else {
return region.omega.is_some();
Expand All @@ -2796,8 +2775,18 @@ impl Built {
}
touch(&region.nodes, Src::In(0)) || region.omega != Some(Src::In(0))
};
if count.is_some() && !omega_used(&test) && !omega_used(&bod) {
return Err(self.deny("bound: a count-bounded Iterate must carry ω, and C7 removes a carried ω that neither region uses, so this pure count loop has no applied form"));
// Both regions may only pass order through. C7 keeps that carried
// order on a count (erratum 2026-11-13), so the old refusal is
// false in that case. A count whose regions were not given an
// order still has none to carry.
if count.is_some()
&& !omega_used(&test)
&& !omega_used(&bod)
&& (test.omega.is_none() || bod.omega.is_none())
{
return Err(self.deny(
"omega: a count-bounded Iterate may halt, and this region has no ω to carry",
));
}
let finish = |region: &mut Scope, sets: &[(u32, Atom)]| -> Result<Body, Change> {
if let Some(h) = region.env.get(&u32::MAX).copied() {
Expand Down Expand Up @@ -2839,13 +2828,12 @@ impl Built {
out,
})
};
// Recompute outs with a consistent ω bit.
// A count whose regions were given order carries it even when
// neither region uses it (erratum 2026-11-13). A count with no
// order was already refused above.
let use_w = test.omega.is_some()
&& bod.omega.is_some()
&& (count.is_some() || omega_used(&test) || omega_used(&bod));
if count.is_some() && !use_w {
return Err(self.deny("bound: a count-bounded Iterate must carry ω, and C7 removes a carried ω that neither region uses, so this pure count loop has no applied form"));
}
let mut carried_ty = vec![];
if use_w {
carried_ty.push(BTy::Order);
Expand Down Expand Up @@ -3203,11 +3191,6 @@ fn bool_not(region: &mut Scope, s: Src) -> Src {
Src::Out(i, 0)
}

fn region_uses_order(region: &Scope) -> bool {
matches!(region.omega, Some(Src::Out(..)))
|| region.nodes.iter().any(|n| n.ins.contains(&Src::In(0)))
}

fn region_outs(
region: &Scope,
flag: Src,
Expand Down
9 changes: 8 additions & 1 deletion spec/checker/src/form/canon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ fn c7_hook(node: &mut Node, md: &dyn Fn(&Src) -> String) {
}

/// Remove the carried values of an Iterate that neither region uses and
/// both return unchanged; the Iterate's output for one becomes its initial value.
/// both return unchanged; the Iterate's output for one becomes its initial
/// value. A count-bounded Iterate keeps its carried order (erratum of
/// 2026-11-13): a count may halt, so that order is not an unused carrier.
fn c7_carried(b: &Body) -> Body {
let mut b = b.clone();
loop {
Expand All @@ -336,6 +338,11 @@ fn c7_carried(b: &Body) -> Body {
{
continue;
}
// Erratum 2026-11-13: a count may halt, so its carried order
// stays even when both regions only pass it through.
if matches!(&n.attrs, Attr::Count(ts) if ts.get(k) == Some(&BTy::Order)) {
continue;
}
let keep = n.regions.iter().any(|r| r.out.get(1 + k) != Some(&Src::In(k as u32)) || uses_input(r, k as u32, Some(1 + k)));
if !keep {
found = Some((i, k, first));
Expand Down
21 changes: 16 additions & 5 deletions spec/checker/tests/break_fg8_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,10 @@ fn shortcuts_that_admit_the_wrong_program_stay_refused() {
assert_eq!(z, Ok(Val::Int(0)));
assert_eq!(n4, Ok(Val::Int(1)), "the mutated end stops the next test");

// A pure count that breaks. The flag is not a use of ω. O8.
// A pure count that breaks. Before the erratum of 2026-11-13 the bridge
// refused this with tag `bound`, because C7 removed the carried order.
// The count now keeps that order. The flag is still not a use of ω.
// The value is the core's value.
let ct = build::count_t();
let mut b = FB::new(vec![ct.clone()], ct.clone());
let bound = b.arg(0);
Expand All @@ -1115,12 +1118,20 @@ fn shortcuts_that_admit_the_wrong_program_stay_refused() {
let rv = b.let_(Atom::V(i));
b.ret(rv);
let m = module("pure", b.finish());
let e = refuse(&m);
assert_eq!(e.tag(), "bound", "{}", e.line());
wf::check(&m).unwrap();
let f = bridge::construct(&m).unwrap_or_else(|e| panic!("pure count: {}", e.line()));
assert!(
!bridge::body_break_rewrite(),
"a refused pure count is not an admission"
f.defs.values().any(|g| g.fns.iter().any(|func| func.body.nodes.iter().any(|n| {
matches!(&n.attrs, fir::Attr::Count(ts) if ts.first() == Some(&fir::BTy::Order))
}))),
"the pure count dropped its carried order"
);
let (z, _) = core_eval(&m, vec![Val::Int(1)]);
assert_eq!(z, Ok(Val::Int(0)));
let got = feval::apply(&f, &form_key(&f, 1).0, form_key(&f, 1).1, vec![Val::Int(1)]).unwrap();
assert_eq!(got, vec![Val::Int(0)]);
let (halt, _) = core_eval(&m, vec![Val::Int(0)]);
assert!(halt.is_err(), "bound 0 did not halt: {halt:?}");

// A depth that names nothing. The core rejects it.
let mut b = FB::new(vec![], i64ty());
Expand Down
132 changes: 125 additions & 7 deletions spec/checker/tests/form_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,32 @@ fn owned_body_break_is_scheduled_once() {
assert_eq!(fuel_b, fuel_sb, "O7': fuel differs on the bound path");
}

fn count_carried(f: &fir::Module) -> Vec<Vec<fir::BTy>> {
let mut out = vec![];
fn walk(b: &fir::Body, out: &mut Vec<Vec<fir::BTy>>) {
for n in &b.nodes {
if let fir::Attr::Count(ts) = &n.attrs {
out.push(ts.clone());
}
for r in &n.regions {
walk(r, out);
}
}
}
for g in f.defs.values() {
for func in &g.fns {
walk(&func.body, &mut out);
}
}
out
}

#[test]
fn pure_count_break_stays_outside_dom() {
// O8. A break flag is a bool. It does not put a pure count into Dom.
fn pure_count_keeps_carried_order() {
// Erratum of 2026-11-13. Neither region uses ω. The old bridge refused
// with "C7 removes a carried ω that neither region uses". C7 now keeps
// that order, and the count has an applied form. The flag is still a
// bool, not a use of ω.
let ct = build::count_t();
let mut b = FB::new(vec![ct.clone()], ct.clone());
let bound = b.arg(0);
Expand All @@ -720,7 +743,6 @@ fn pure_count_break_stays_outside_dom() {
b.loop_(|b| {
b.count(cnt, bound)?;
let iv = b.let_(Atom::V(i));
// `int.eq` does not halt, so neither region uses ω. The flag is not a use of ω.
let stop = b.int_cmp("int.eq", iv, build::lit(0))?;
b.exit_if(stop)?;
Ok(())
Expand All @@ -730,12 +752,108 @@ fn pure_count_break_stays_outside_dom() {
b.ret(r);
let m = module("pure", b.finish());
wf::check(&m).unwrap_or_else(|e| panic!("{e}"));
let e = bridge::construct(&m).expect_err("pure count");
assert_eq!(e.tag(), "bound", "{}", e.line());
let f = bridge::construct(&m).unwrap_or_else(|e| panic!("{}", e.line()));
assert!(
!bridge::body_break_rewrite(),
"a refused pure count is not an admitted break rewrite"
bridge::body_break_rewrite(),
"the body break was not restructured into a flag"
);
let carried = count_carried(&f);
assert!(
carried.iter().any(|ts| ts.first() == Some(&fir::BTy::Order)),
"a count-bounded Iterate dropped its carried order: {carried:?}"
);
assert!(
flag_is_fresh_false(&f),
"the break path must return a literal false, not a carried bool"
);
let bytes = fir::encode(&f);
assert!(
matches!(form::check(bytes.as_bytes(), None), form::Outcome::Applied(_)),
"the pure count is not applied"
);
let s = bridge::schedule(&f).unwrap_or_else(|e| panic!("{}", e.line()));
wf::check(&s).unwrap_or_else(|e| panic!("schedule is not well formed: {e}"));
let back = bridge::construct(&s).unwrap_or_else(|e| panic!("construct of the schedule: {}", e.line()));
assert_eq!(fir::encode(&f), fir::encode(&back), "construct(schedule(F)) is not F");
// Bound 1: the body breaks on the first iteration, before the next bound check.
agree(&m, &f, &s, vec![Val::Int(1)]);
// Bound 0: the count halts before the body. The flag is not that halt.
agree(&m, &f, &s, vec![Val::Int(0)]);
}

const NO_ORDER: &str = "omega: a count-bounded Iterate may halt, and this region has no ω to carry";

/// A count bound whose trap is the bytes `loop bound` and a `break`, not a
/// `halt`, so the function is not given order.
fn count_without_halt(b: &mut FB, counter: u32, bound: Atom) {
let ct = build::count_t();
let nv = b.let_(Atom::V(counter));
let n1 = b.p("int.sadd", vec![ct.clone()], vec![nv, build::lit(1)]).unwrap();
b.set(counter, n1.clone());
let over = b.p("int.lt", vec![ct], vec![bound, n1]).unwrap();
b.open();
let _ = b.p("bytes.len", vec![], vec![Atom::bytes(b"loop bound")]).unwrap();
b.brk(0);
let arm = b.close();
b.push(Stmt::If(over, arm, vec![]));
}

#[test]
fn a_lowering_count_with_no_order_names_that_refusal() {
// `build_count`. The loop is the lowering's count shape, and nothing in
// the function takes order, so the count cannot carry it.
let ct = build::count_t();
let mut b = FB::new(vec![ct.clone()], ct.clone());
let bound = b.arg(0);
let cnt = b.var(ct.clone(), build::lit(0));
b.loop_(|b| {
count_without_halt(b, cnt, bound);
Ok(())
})
.unwrap();
b.ret(build::lit(0));
let m = module("bare", b.finish());
wf::check(&m).unwrap_or_else(|e| panic!("{e}"));
let e = bridge::construct(&m).expect_err("a count with no order");
assert_eq!(e.reason, NO_ORDER, "{}", e.line());
}

#[test]
fn a_schedule_count_with_no_order_names_that_refusal() {
// `loop_stmt`. Two breaks with the same carried store, and a count
// between them. The count's own statements are the bound, not part of
// either region. Nothing takes order.
let ct = build::count_t();
let mut b = FB::new(vec![ct.clone()], ct.clone());
let cnt = b.var(ct.clone(), build::lit(0));
let acc = b.var(ct.clone(), build::lit(0));
let cmp = |b: &mut FB| -> Atom {
b.p("int.eq", vec![ct.clone()], vec![build::lit(0), build::lit(0)]).unwrap()
};
let leave = |b: &mut FB, c: Atom| {
b.open();
let th = b.close();
b.open();
b.brk(0);
let el = b.close();
b.push(Stmt::If(c, th, el));
};
b.loop_(|b| {
let c = cmp(b);
b.set(acc, build::lit(0));
leave(b, c);
count_without_halt(b, cnt, build::lit(2));
let c = cmp(b);
b.set(acc, build::lit(1));
leave(b, c);
Ok(())
})
.unwrap();
b.ret(Atom::V(acc));
let m = module("sched", b.finish());
wf::check(&m).unwrap_or_else(|e| panic!("{e}"));
let e = bridge::construct(&m).expect_err("a scheduled count with no order");
assert_eq!(e.reason, NO_ORDER, "{}", e.line());
}

#[test]
Expand Down
9 changes: 4 additions & 5 deletions spec/checker/tests/form_bridge_census.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ whole-with-user 1

whole modules, top 10 by modules unblocked (one tag, or a pair)
unblocks set
14 break
15 break
14 round-trip
9 lowering
3 break,exit
2 defect
2 global
1 FG8
1 bound,break
1 break,catch
1 break,defect
1 catch,global

whole modules, every exact set
modules set
14 break
15 break
14 round-trip
9 lowering
3 break,exit
Expand All @@ -64,7 +64,6 @@ modules set
2 defect,exit,loop
2 global
1 FG8
1 bound,break
1 break,catch
1 break,catch,exit,loop
1 break,defect
Expand Down Expand Up @@ -133,7 +132,7 @@ module whole roots user global-sites
39_structural_recursion.rce lowering lowering lowering not covered by the lowering: a recursive type
40_extern.rce round-trip clear user
41_arena_bound_wrapping.rce round-trip round-trip user
42_f32.rce bound,break clear user
42_f32.rce break clear user
43_simd_lanes.rce FG8 round-trip user
44_device_region.rce break break user
45_byte_text.rce exit,global round-trip user check 7: global: g3 is not part of the working context ω holds
Expand Down
Loading
Loading