diff --git a/spec/checker/src/form/bridge.rs b/spec/checker/src/form/bridge.rs index 1d4bc0e0..45f0ad7a 100644 --- a/spec/checker/src/form/bridge.rs +++ b/spec/checker/src/form/bridge.rs @@ -459,6 +459,10 @@ struct Scope { /// Raw break depth a frame has not crossed yet. A block or the loop /// decrements it once. An `if` does not. pending_break: Option, + /// A return that left one arm of a join in this region. + /// The flag is true when that path continues, and false when it returns. + /// The value is the return. This is not the loop flag of a break. + early: Option<(Src, Src)>, /// The output list inside ω (rce-form.md §5). `Fresh` is the empty list /// a fresh ω holds. `Lost` means a branch changed the list and this /// scope did not join that change, so a later read requires change. @@ -478,6 +482,23 @@ enum OutList { Lost, } +fn blank_scope() -> Scope { + Scope { + nodes: vec![], + env: HashMap::new(), + omega: None, + caps: vec![], + assigned: BTreeSet::new(), + set_at: vec![], + lits: vec![], + exit_flag: None, + exit_residual: None, + pending_break: None, + early: None, + outputs: OutList::Fresh, + } +} + fn inherited_outputs(parent: &Scope) -> OutList { match parent.outputs { OutList::Fresh | OutList::Empty(_) => OutList::Fresh, @@ -612,6 +633,49 @@ impl Built { } } + /// A value of the body's return type. The arm that falls through holds + /// it so the return channel has a source. The continue flag selects the + /// other arm's return, so this value is not the function result. + fn lit_bty(&self, sc: &mut Scope, t: &BTy) -> Result { + let ty = cty(t).map_err(|e| self.deny(format!("defect: {e}")))?; + self.dummy_src(sc, &ty) + } + + /// The loop continues on this path. A join that returned yields the + /// flag of that join, which is false on the returning arm. A path with + /// no return yields true. A break does not use this flag. + fn continue_flag(&self, sc: &mut Scope) -> Src { + if let Some((cont, _)) = sc.early { + return cont; + } + lit_bool(sc, true) + } + + /// `flag` true yields `yes`, and false yields `no`. Both arms return, + /// so the branch result is the selected return. + fn select_return( + &self, + sc: &mut Scope, + flag: Src, + yes: Src, + no: Src, + ret_ty: &BTy, + ) -> Result { + let caps = if yes == no { vec![yes] } else { vec![yes, no] }; + let arm = |which: Src| { + let mut a = blank_scope(); + a.caps = caps.clone(); + let j = caps.iter().position(|s| *s == which).expect("a captured return") as u32; + (End::Ret(Src::In(j)), a) + }; + let (e1, a1) = arm(yes); + let (e0, a0) = arm(no); + match self.finish_if(sc, flag, a0, e0, a1, e1, None, ret_ty, false)? { + End::Ret(s) => Ok(s), + _ => Err(self.deny("defect: a return was not the branch result")), + } + } + /// Locals the test binds on every path and the body uses, but the parent /// does not have yet. Each gets a dummy initial so it can be carried. fn seed_flow( @@ -1302,7 +1366,10 @@ impl Built { i += 1; } Stmt::Ret(a) => { - let src = self.src_atom(sc, a)?; + let mut src = self.src_atom(sc, a)?; + if let Some((cont, val)) = sc.early.take() { + src = self.select_return(sc, cont, src, val, ret_ty)?; + } return Ok(End::Ret(src)); } Stmt::Halt(a) => { @@ -1494,6 +1561,7 @@ impl Built { exit_flag: None, exit_residual: None, pending_break: None, + early: None, outputs: inherited_outputs(parent), }; for (j, l) in seed.iter().enumerate() { @@ -1642,9 +1710,13 @@ impl Built { "branch: the arms capture different values, and this slice does not reconcile them", )); } - // outputs: vars assigned in either arm, then ω + // Carried vars come from arms that fall through. A returning arm + // contributes none of them: its return is a separate channel. let mut vars: BTreeSet = BTreeSet::new(); - for arm in [&a0, &a1] { + for (arm, e) in [(&a0, &e0), (&a1, &e1)] { + if matches!(e, End::Ret(_)) { + continue; + } for v in &arm.assigned { if sc.env.contains_key(v) || self.info.loc.get(v).is_some_and(|l| l.var) && sc.env.contains_key(v) @@ -1679,6 +1751,12 @@ impl Built { a0.env.contains_key(&v) } else if yields(&e1) && matches!(e0, End::Halt(_)) { a1.env.contains_key(&v) + } else if yields(&e0) && matches!(e1, End::Ret(_)) { + // The returning arm passes the value through. It does not + // define a new one, and it must already hold the capture. + a0.env.contains_key(&v) && a1.env.contains_key(&v) + } else if yields(&e1) && matches!(e0, End::Ret(_)) { + a1.env.contains_key(&v) && a0.env.contains_key(&v) } else { false }; @@ -1739,6 +1817,23 @@ impl Built { // in the arm, not a carried input, so C7 does not drop it (B4). ts.insert(0, BTy::Bool); } + // One arm returns and the other falls through. The return is two + // outputs after the carried vars: a continue flag that is false on + // the returning arm, and the return value. The returning arm does + // not put that value into the carried vars. A break's flag is the + // join_flag output, and that path stays refused above. + let mixed = matches!((&e0, &e1), (End::Ret(_), End::Fall | End::Exit)) + || matches!((&e0, &e1), (End::Fall | End::Exit, End::Ret(_))); + // A return nested in an arm that itself falls through. The channel + // leaves this join too. A halt arm does not continue, so it does not + // define the channel; the Halt node still has a slot of each output. + let nested = !mixed + && (matches!(e0, End::Fall | End::Exit) && a0.early.is_some() + || matches!(e1, End::Fall | End::Exit) && a1.early.is_some()); + if mixed || nested { + ts.push(BTy::Bool); + ts.push(ret_ty.clone()); + } if needs_w { ts.push(BTy::Order); } @@ -1791,7 +1886,20 @@ impl Built { ))); } End::Ret(src) => { - let mut o = vec![src]; + let mut o = vec![]; + if mixed { + for v in &vars { + o.push(arm.env.get(v).copied().ok_or_else(|| { + self.deny("defect: a joined var is not in the arm") + })?); + } + // False: this arm does not continue. The return value + // follows the flag. Neither is a carried var. + o.push(lit_bool(arm, false)); + o.push(src); + } else { + o.push(src); + } if needs_w { o.push( arm.omega @@ -1805,7 +1913,7 @@ impl Built { if join_flag { o.push(arm.exit_flag.ok_or_else(|| self.deny("defect: a break arm has no flag"))?); } - if vars.is_empty() && ts.first() == Some(ret_ty) && !join_flag { + if vars.is_empty() && ts.first() == Some(ret_ty) && !join_flag && !mixed { return Err( self.deny("exit: a branch arm falls through where the other returns") ); @@ -1820,6 +1928,15 @@ impl Built { s.ok_or_else(|| self.deny("defect: a joined var is not in the arm"))?, ); } + if mixed || nested { + if let Some((c, v)) = arm.early.take() { + o.push(c); + o.push(v); + } else { + o.push(lit_bool(arm, true)); + o.push(self.lit_bty(arm, ret_ty)?); + } + } if needs_w { o.push(arm.omega.ok_or_else(|| { self.deny("defect: an arm of an effectful branch has no ω") @@ -1888,6 +2005,10 @@ impl Built { if join_flag { sc.exit_flag = Some(Src::Out(id, 0)); } + if mixed || nested { + let at = u32::from(join_flag) + vars.len() as u32; + sc.early = Some((Src::Out(id, at), Src::Out(id, at + 1))); + } if both_term { return Ok(End::Ret(Src::Out(id, 0))); } @@ -1937,7 +2058,7 @@ impl Built { sc.exit_residual = Some(d - 1); Ok(lit_bool(sc, false)) } - None if virtual_consumed => Ok(lit_bool(sc, true)), + None if virtual_consumed => Ok(self.continue_flag(sc)), None => Ok(flag), } } @@ -1967,7 +2088,7 @@ impl Built { } if fall.is_empty() { if far.is_empty() { - return Ok(lit_bool(sc, true)); + return Ok(self.continue_flag(sc)); } return self.exec_body_seq(far, sc, parent_omega, ret_ty, far_names, &[], 0, &[], 0); } @@ -2019,7 +2140,7 @@ impl Built { let run = |sc: &mut Scope, tail: &[&Stmt], nm: u32, next: &[&Stmt], nn: u32| { if tail.is_empty() { if next.is_empty() { - Ok(lit_bool(sc, true)) + Ok(self.continue_flag(sc)) } else { self.exec_body_seq(next, sc, parent_omega, ret_ty, nn, &[], 0, &[], 0) } @@ -2077,7 +2198,7 @@ impl Built { return Ok(mid); } } else if fall.is_empty() { - return Ok(lit_bool(sc, true)); + return Ok(self.continue_flag(sc)); } return self.exec_body_seq( fall, sc, parent_omega, ret_ty, fall_names, &[], 0, &[], 0, @@ -2140,6 +2261,7 @@ impl Built { exit_flag: None, exit_residual: None, pending_break: None, + early: None, outputs: inherited_outputs(parent), }; for (j, l) in seed.iter().enumerate() { @@ -2401,6 +2523,7 @@ impl Built { exit_flag: None, exit_residual: None, pending_break: None, + early: None, outputs: inherited_outputs(sc), }; let mut ix = 0u32; @@ -2482,16 +2605,10 @@ impl Built { "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) - .map_err(|m| self.deny(format!("loop: {m}")))?, - }; - let body_b = Body { - nodes: std::mem::take(&mut bod.nodes), - out: region_outs(&bod, bflag, &vars, true, None) - .map_err(|m| self.deny(format!("loop: {m}")))?, - }; + let mut test_out = region_outs(&test, flag, &vars, true, None) + .map_err(|m| self.deny(format!("loop: {m}")))?; + let mut body_out = region_outs(&bod, bflag, &vars, true, None) + .map_err(|m| self.deny(format!("loop: {m}")))?; lose_outputs(sc, &test); lose_outputs(sc, &bod); let bound = self.src_atom(sc, &count.bound)?; @@ -2506,11 +2623,33 @@ impl Built { ins.push(sc.env[v]); carried_ty.push(bty(&self.loc_ty(*v)?).map_err(|e| self.deny(format!("defect: {e}")))?); } - let id = sc.push(Attr::Count(carried_ty), ins, vec![test_b, body_b]); + let carried_return = if let Some(early) = bod.early.take() { + let init_flag = lit_bool(sc, true); + let init_val = self.lit_bty(sc, ret_ty)?; + append_early( + Some(early), &mut test_out, &mut body_out, &mut ins, &mut carried_ty, init_flag, + init_val, ret_ty, + ) + } else { + false + }; + let test_b = Body { + nodes: std::mem::take(&mut test.nodes), + out: test_out, + }; + let body_b = Body { + nodes: std::mem::take(&mut bod.nodes), + out: body_out, + }; + let id = sc.push(Attr::Count(carried_ty.clone()), ins, vec![test_b, body_b]); sc.omega = Some(Src::Out(id, 0)); for (k, v) in vars.iter().enumerate() { sc.env.insert(*v, Src::Out(id, 1 + k as u32)); } + if carried_return { + let n = carried_ty.len() as u32; + sc.early = Some((Src::Out(id, n - 2), Src::Out(id, n - 1))); + } Ok(()) } @@ -2601,16 +2740,10 @@ impl Built { sc.exit_residual = Some(r); } let lit_slot = matches!(spec.end, Atom::K(_, _)); - let test_b = Body { - nodes: std::mem::take(&mut test.nodes), - out: region_outs(&test, flag, &vars, thread, lit_slot.then_some(j_in)) - .map_err(|m| self.deny(format!("loop: {m}")))?, - }; - let body_b = Body { - nodes: std::mem::take(&mut bod.nodes), - out: region_outs(&bod, bflag, &vars, thread, lit_slot.then_some(j_in)) - .map_err(|m| self.deny(format!("loop: {m}")))?, - }; + let mut test_out = region_outs(&test, flag, &vars, thread, lit_slot.then_some(j_in)) + .map_err(|m| self.deny(format!("loop: {m}")))?; + let mut body_out = region_outs(&bod, bflag, &vars, thread, lit_slot.then_some(j_in)) + .map_err(|m| self.deny(format!("loop: {m}")))?; lose_outputs(sc, &test); lose_outputs(sc, &bod); let mut ins = vec![]; @@ -2631,7 +2764,25 @@ impl Built { ins.push(self.lit_node(sc, t, l)?); carried_ty.push(bty(t).map_err(|e| self.deny(format!("defect: {e}")))?); } - let id = sc.push(Attr::Measure(i_in, j_in, carried_ty), ins, vec![ + let carried_return = if let Some(early) = bod.early.take() { + let init_flag = lit_bool(sc, true); + let init_val = self.lit_bty(sc, ret_ty)?; + append_early( + Some(early), &mut test_out, &mut body_out, &mut ins, &mut carried_ty, init_flag, + init_val, ret_ty, + ) + } else { + false + }; + let test_b = Body { + nodes: std::mem::take(&mut test.nodes), + out: test_out, + }; + let body_b = Body { + nodes: std::mem::take(&mut bod.nodes), + out: body_out, + }; + let id = sc.push(Attr::Measure(i_in, j_in, carried_ty.clone()), ins, vec![ test_b, body_b, ]); let base = u32::from(thread); @@ -2641,6 +2792,10 @@ impl Built { if thread { sc.omega = Some(Src::Out(id, 0)); } + if carried_return { + let n = carried_ty.len() as u32; + sc.early = Some((Src::Out(id, n - 2), Src::Out(id, n - 1))); + } Ok(()) } @@ -2688,6 +2843,7 @@ impl Built { exit_flag: None, exit_residual: None, pending_break: None, + early: None, outputs: inherited_outputs(sc), }; let mut ix = 0u32; @@ -2895,34 +3051,43 @@ impl Built { }) }; let _ = finish; - let test_b = body_of(&mut test, &parts.sets0)?; - let body_b = body_of(&mut bod, &parts.sets1)?; - let (attrs, mut ins) = if let Some(c) = count { + let early = bod.early.take(); + let mut test_b = body_of(&mut test, &parts.sets0)?; + let mut body_b = body_of(&mut bod, &parts.sets1)?; + let mut ins_tail = vec![]; + if use_w { + let w = if count.is_some() { + sc.omega + .or(parent_omega) + .ok_or_else(|| self.deny("defect: count has no ω"))? + } else { + sc.omega.unwrap() + }; + ins_tail.push(w); + } + for v in &carried { + ins_tail.push(*sc.env.get(v).ok_or_else(|| self.deny("defect: carried"))?); + } + let carried_return = if let Some(pair) = early { + let init_flag = lit_bool(sc, true); + let init_val = self.lit_bty(sc, ret_ty)?; + append_early( + Some(pair), &mut test_b.out, &mut body_b.out, &mut ins_tail, &mut carried_ty, + init_flag, init_val, ret_ty, + ) + } else { + false + }; + let (attrs, ins) = if let Some(c) = count { let bound = self.src_atom(sc, &c.bound)?; let mut ins = vec![bound]; - if use_w { - ins.push( - sc.omega - .or(parent_omega) - .ok_or_else(|| self.deny("defect: count has no ω"))?, - ); - } - for v in &carried { - ins.push(*sc.env.get(v).ok_or_else(|| self.deny("defect: carried"))?); - } + ins.extend(ins_tail); (Attr::Count(carried_ty.clone()), ins) } else { - let mut ins = vec![]; - if use_w { - ins.push(sc.omega.unwrap()); - } - for v in &carried { - ins.push(sc.env[v]); - } let (i, j) = measure_ij(&test_b).map_err(|m| self.deny(m))?; - (Attr::Measure(i, j, carried_ty.clone()), ins) + (Attr::Measure(i, j, carried_ty.clone()), ins_tail) }; - let id = sc.push(attrs, std::mem::take(&mut ins), vec![test_b, body_b]); + let id = sc.push(attrs, ins, vec![test_b, body_b]); let base = u32::from(use_w); for (k, v) in carried.iter().enumerate() { sc.env.insert(*v, Src::Out(id, base + k as u32)); @@ -2930,6 +3095,10 @@ impl Built { if use_w { sc.omega = Some(Src::Out(id, 0)); } + if carried_return { + let n = carried_ty.len() as u32; + sc.early = Some((Src::Out(id, n - 2), Src::Out(id, n - 1))); + } Ok(()) } } @@ -3208,6 +3377,34 @@ fn region_uses_order(region: &Scope) -> bool { || region.nodes.iter().any(|n| n.ins.contains(&Src::In(0))) } +/// Carry a return out of an Iterate. The test passes the flag and the value +/// through. The body returns the join's flag and value. The flag starts +/// true, so a loop that never returns keeps the caller's own return. +fn append_early( + early: Option<(Src, Src)>, + test_out: &mut Vec, + body_out: &mut Vec, + ins: &mut Vec, + carried_ty: &mut Vec, + init_flag: Src, + init_val: Src, + ret_ty: &BTy, +) -> bool { + let Some((flag, val)) = early else { + return false; + }; + let k = carried_ty.len() as u32; + test_out.push(Src::In(k)); + test_out.push(Src::In(k + 1)); + body_out.push(flag); + body_out.push(val); + ins.push(init_flag); + ins.push(init_val); + carried_ty.push(BTy::Bool); + carried_ty.push(ret_ty.clone()); + true +} + fn region_outs( region: &Scope, flag: Src, @@ -3664,6 +3861,7 @@ pub fn construct(m: &core::Module) -> Result { exit_flag: None, exit_residual: None, pending_break: None, + early: None, outputs: OutList::Fresh, }; let mut ins = me.params.clone(); @@ -5075,12 +5273,25 @@ fn schedule_branch( }; let omega_at = ts.iter().position(|t| *t == BTy::Order); let mut vars = vec![]; + // A break flag is the first output, and the branch carries something + // beside it. That dummy stays a literal false. A bool that is the whole + // result, and a return flag after the carried values, is a literal the + // arm builds. Sharing the dummy would make that literal a capture. + let carried_beside = ts.iter().filter(|t| **t != BTy::Order).count() > 1; for (p, t) in ts.iter().enumerate() { if Some(p) == omega_at { continue; } let ct = cty(t).map_err(|_| sch.deny("defect: a branch output the core cannot hold"))?; - let init = dummy(sch, t)?; + let break_flag = p == 0 && carried_beside; + let init = if *t == BTy::Bool + && !break_flag + && n.regions.iter().any(|r| output_is_lit(r, p, &Lit::Bool(false))) + { + neutral_bool(sch, &n.regions)? + } else { + dummy(sch, t)? + }; sch.consume(&init)?; sch.stmts.push(Stmt::Var(ct.clone(), init)); let id = sch.fresh(ct, true); @@ -5095,6 +5306,35 @@ fn schedule_branch( // keeps a capture only when every arm that falls through still holds it. let mut arm_live = [saved_live.clone(), saved_live.clone()]; let mut arm_falls = [false, false]; + // A capture of a literal the parent built stays a capture. Inlining the + // bits would build the literal in the arm, and construct(schedule(F)) + // would differ. An arm that already builds that literal keeps its own node. + let mut hoisted: HashMap<(Ty, Lit), u32> = HashMap::new(); + let contains_lit = |lit: &Lit| { + fn walk(b: &Body, lit: &Lit) -> bool { + b.nodes.iter().any(|n| { + matches!(&n.attrs, Attr::Lit(_, l) if l == lit) + || n.regions.iter().any(|r| walk(r, lit)) + }) + } + n.regions.iter().any(|r| walk(r, lit)) + }; + for region in &n.regions { + for src in ®ion.out { + let Src::In(j) = *src else { continue }; + let Some(parent) = n.ins.get(1 + j as usize) else { continue }; + let Some(Av::A(Atom::K(t, l))) = env.get(parent) else { continue }; + if !matches!(t, Ty::Bool | Ty::Int(..)) || contains_lit(l) { + continue; + } + let key = (t.clone(), l.clone()); + if hoisted.contains_key(&key) { + continue; + } + let id = sch.let_atom(Atom::K(t.clone(), l.clone()), t.clone())?; + hoisted.insert(key, id); + } + } let mut arm = |sch: &mut Sch, region: &Body, dom: Option<&Dom>, slot: usize| -> Result, Change> { sch.live.clone_from(&saved_live); let saved = std::mem::take(&mut sch.stmts); @@ -5117,7 +5357,12 @@ fn schedule_branch( for (p, var) in &vars { let src = region.out[*p]; match sch.val(&en, src)? { - Av::A(a) => { + Av::A(mut a) => { + if let (Src::In(_), Atom::K(t, l)) = (src, &a) + && let Some(id) = hoisted.get(&(t.clone(), l.clone())) + { + a = Atom::V(*id); + } if let Atom::V(id) = &a { keep.insert(*id); } @@ -5238,6 +5483,70 @@ impl Sch { } } +/// Whether output `port` of `region` is a literal, including one an arm of a +/// nested branch builds and this region returns as that same port. +fn output_is_lit(region: &Body, port: usize, lit: &Lit) -> bool { + let Some(src) = region.out.get(port).copied() else { + return false; + }; + source_is_lit(region, src, lit) +} + +fn source_is_lit(region: &Body, src: Src, lit: &Lit) -> bool { + let Src::Out(i, p) = src else { + return false; + }; + let Some(n) = region.nodes.get(i as usize) else { + return false; + }; + match &n.attrs { + Attr::Lit(_, l) if p == 0 && l == lit => true, + Attr::Branch(_, _) => n.regions.iter().any(|arm| { + arm.out + .get(p as usize) + .is_some_and(|s| source_is_lit(arm, *s, lit)) + }), + _ => false, + } +} + +/// A bool that is not a literal. The arms keep the literals they build. +/// The two integers are ones no arm already uses, so this test does not +/// become a capture of an arm's own literal. Nothing reads the result: +/// every arm stores the output, and the canonical passes drop the test. +fn neutral_bool(sch: &mut Sch, regions: &[Body]) -> Result { + let mut used = BTreeSet::new(); + fn walk(b: &Body, used: &mut BTreeSet) { + for n in &b.nodes { + if let Attr::Lit(_, Lit::Int(v)) = &n.attrs { + used.insert(*v); + } + for r in &n.regions { + walk(r, used); + } + } + } + for r in regions { + walk(r, &mut used); + } + let mut a = 0i128; + while used.contains(&a) { + a += 1; + } + let mut b = a + 1; + while used.contains(&b) { + b += 1; + } + let ty = Ty::Int(a.min(b), a.max(b)); + sch.stmts.push(Stmt::Prim( + "int.eq".into(), + vec![ty], + vec![build::lit(a), build::lit(b)], + )); + let id = sch.fresh(Ty::Bool, false); + Ok(Atom::V(id)) +} + fn dummy(sch: &mut Sch, t: &BTy) -> Result { Ok(match t { BTy::Bool => Atom::bool(false), diff --git a/spec/checker/tests/break_fg8_witness.rs b/spec/checker/tests/break_fg8_witness.rs index 872fb4e3..131606a7 100644 --- a/spec/checker/tests/break_fg8_witness.rs +++ b/spec/checker/tests/break_fg8_witness.rs @@ -255,13 +255,14 @@ fn corpus_baseline_on_50294e05() { // the rest. A program root's first refusal is the file's root tag, as // in examples_are_covered_or_require_change. // - // Counting `construct` alone, with no schedule, accepts 18 modules on - // this commit. That is not the census `whole` of 2. One of the 18, - // `09_generics.rce`, is entry-only. The other 17 contain a user - // function, and 16 of them do not schedule back to the same bytes. + // Counting `construct` alone, with no schedule, accepts 19 modules on + // this commit. That is not the census `whole` of 2. One of the 19, + // `09_generics.rce`, is entry-only. The other 18 contain a user + // function, and 17 of them do not schedule back to the same bytes. // The 18 entry-only cores in the #158 note were the count before - // library functions were kept in the core. This 18 is the gate - // accepting the Form after that change. + // library functions were kept in the core. This 19 is the gate + // accepting the Form after a returning arm stopped adding its value + // to the join. `33_in_operator.rce` constructs, and its schedule is FG8. let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples"); let mut files: Vec<_> = std::fs::read_dir(&dir) .unwrap() @@ -380,6 +381,7 @@ fn corpus_baseline_on_50294e05() { "25_labels.rce", "27_bitops.rce", "28_slices.rce", + "33_in_operator.rce", "35_nested_if.rce", "36_multiline_brackets.rce", "40_extern.rce", @@ -436,12 +438,17 @@ fn corpus_baseline_on_50294e05() { names("catch") ); assert_eq!(names("global"), ["32_comptime_block.rce"]); - assert_eq!(names("loop"), ["50_iterators.rce", "55_rcedb.rce"]); - assert_eq!(names("exit"), ["02_hello_main.rce", "31_inventory.rce"]); assert_eq!( - names("defect"), - ["06_option.rce", "07_error_union.rce", "20_strings.rce"] + names("loop"), + [ + "06_option.rce", + "20_strings.rce", + "50_iterators.rce", + "55_rcedb.rce", + ] ); + assert_eq!(names("exit"), ["02_hello_main.rce", "31_inventory.rce"]); + assert_eq!(names("defect"), ["07_error_union.rce"]); assert_eq!( names("round-trip"), [ @@ -522,7 +529,7 @@ fn corpus_baseline_on_50294e05() { ); assert_eq!( tagged("defect"), - ["07_error_union.rce", "20_strings.rce", "33_in_operator.rce"] + ["07_error_union.rce", "20_strings.rce"] ); assert_eq!(tagged("exit"), ["02_hello_main.rce", "31_inventory.rce"]); assert_eq!( @@ -537,7 +544,10 @@ fn corpus_baseline_on_50294e05() { ); assert_eq!(tagged("loop"), ["55_rcedb.rce"]); assert_eq!(tagged("schedule defect"), ["22_tuples.rce"]); - assert_eq!(tagged("schedule FG8"), ["43_simd_lanes.rce"]); + assert_eq!( + tagged("schedule FG8"), + ["33_in_operator.rce", "43_simd_lanes.rce"] + ); assert_eq!( tagged("round-trip"), [ @@ -951,9 +961,9 @@ fn form_key(m: &fir::Module, nargs: usize) -> (String, u32) { } #[test] -fn a_return_inside_a_loop_is_a_defect_of_the_arm_widths() { - // Contract section 3.3. The bridge reports a defect of the arm widths. - // A return is not the loop flag, and the core results stay the contract. +fn a_return_inside_a_loop_is_the_regions_result() { + // Contract section 3.3. A return inside the loop is the region's + // result. It is not the continue flag. The core results stay 7, 4, and 0. let t = Ty::u64(); let mut b = FB::new(vec![t.clone(), Ty::Bool], t.clone()); let n = b.arg(0); @@ -975,20 +985,184 @@ fn a_return_inside_a_loop_is_a_defect_of_the_arm_widths() { let rv = b.let_(Atom::V(i)); b.ret(rv); let m = module("retloop", b.finish()); - let e = refuse(&m); - assert_eq!(e.tag(), "defect", "{}", e.line()); - assert_eq!( - e.reason, - "defect: branch arms return 1, 2 values for 1 outputs", - "{}", - e.line() - ); + wf::check(&m).unwrap(); + let f = bridge::construct(&m).unwrap_or_else(|e| panic!("{}", e.line())); + assert!(!bridge::body_break_rewrite(), "a return was restructured as a break"); + assert_applied(&f); let (stop_v, _) = core_eval(&m, vec![Val::Int(4), Val::Bool(true)]); let (run, _) = core_eval(&m, vec![Val::Int(4), Val::Bool(false)]); let (z, _) = core_eval(&m, vec![Val::Int(0), Val::Bool(true)]); assert_eq!(stop_v, Ok(Val::Int(7))); assert_eq!(run, Ok(Val::Int(4))); assert_eq!(z, Ok(Val::Int(0))); + assert_values(&m, &f, vec![Val::Int(4), Val::Bool(true)]); + assert_values(&m, &f, vec![Val::Int(4), Val::Bool(false)]); + assert_values(&m, &f, vec![Val::Int(0), Val::Bool(true)]); + assert_schedule(&f); +} + +#[test] +fn every_arm_of_a_join_may_return() { + // Both arms return. The join's result is the return. There is no + // fall-through carried state. + let mut b = FB::new(vec![Ty::Bool], i64ty()); + let c = b.arg(0); + b.if_( + c, + |b| { + b.ret(build::lit(1)); + Ok(()) + }, + |b| { + b.ret(build::lit(0)); + Ok(()) + }, + ) + .unwrap(); + let m = module("both", b.finish()); + wf::check(&m).unwrap(); + let f = bridge::construct(&m).unwrap_or_else(|e| panic!("{}", e.line())); + assert!(!bridge::body_break_rewrite()); + assert_applied(&f); + let s = bridge::schedule(&f).unwrap_or_else(|e| panic!("{}", e.line())); + let back = bridge::construct(&s).unwrap_or_else(|e| panic!("{}", e.line())); + assert_eq!(fir::encode(&f), fir::encode(&back)); + assert_values(&m, &f, vec![Val::Bool(true)]); + assert_values(&m, &f, vec![Val::Bool(false)]); +} + +#[test] +fn a_returning_arm_and_a_falling_arm_keep_their_results() { + // One arm returns. The other falls through and stores. The join's + // carried state is the store. The return is not that store. + let t = Ty::u64(); + let mut b = FB::new(vec![Ty::Bool], t.clone()); + let stop = b.arg(0); + let i = b.var(t.clone(), build::lit(1)); + b.if_( + stop, + |b| { + b.ret(build::lit(7)); + Ok(()) + }, + |b| { + b.set(i, build::lit(4)); + Ok(()) + }, + ) + .unwrap(); + let rv = b.let_(Atom::V(i)); + b.ret(rv); + let m = module("mix", b.finish()); + wf::check(&m).unwrap(); + let f = bridge::construct(&m).unwrap_or_else(|e| panic!("{}", e.line())); + assert!(!bridge::body_break_rewrite()); + assert_applied(&f); + assert_values(&m, &f, vec![Val::Bool(true)]); + assert_values(&m, &f, vec![Val::Bool(false)]); + let (yes, _) = core_eval(&m, vec![Val::Bool(true)]); + let (no, _) = core_eval(&m, vec![Val::Bool(false)]); + assert_eq!(yes, Ok(Val::Int(7))); + assert_eq!(no, Ok(Val::Int(4))); + assert_schedule(&f); +} + +/// `construct(schedule(F))` is `F`. Section 13: the schedule of an admitted +/// form constructs back to the same bytes. +fn assert_schedule(f: &fir::Module) { + let s = bridge::schedule(f).unwrap_or_else(|e| panic!("{}", e.line())); + let back = bridge::construct(&s).unwrap_or_else(|e| panic!("{}", e.line())); + assert_eq!(fir::encode(f), fir::encode(&back)); +} + +fn runtime_closure(file: &str, name: &str) -> (CoreModule, fir::Module) { + let core_m = load_example(file); + let sub = bridge::closure(&core_m, name).unwrap_or_else(|| panic!("{file} has no {name}")); + let f = bridge::construct(&sub).unwrap_or_else(|e| panic!("{name}: {}", e.line())); + assert_applied(&f); + (sub, f) +} + +#[test] +fn rt_text_isdigit_returns_from_the_scan() { + // The shape in `07_error_union.rce`: `rt.text.isdigit` returns false + // from the arm that sees a non-digit, and true when the scan falls + // through. The returning arm adds nothing to the scan's carried index. + let (m, f) = runtime_closure("07_error_union.rce", "rt.text.isdigit"); + for (text, bit) in [(b"" as &[u8], false), (b"12", true), (b"12a", false), (b"0", true)] { + let args = vec![Val::bytes(text)]; + let (core_v, _) = core_eval(&m, args.clone()); + assert_eq!(core_v, Ok(Val::Bool(bit)), "{text:?}"); + assert_values(&m, &f, args); + } + assert_schedule(&f); +} + +#[test] +fn rt_text_find_returns_from_the_scan() { + // The shape in `33_in_operator.rce`: `rt.text.find` returns the index + // from the arm that matches, and -1 when the scan falls through. + let (m, f) = runtime_closure("33_in_operator.rce", "rt.text.find"); + let cases = [ + (b"abc".as_slice(), b"b".as_slice(), 0, 1), + (b"abc", b"z", 0, -1), + (b"abc", b"abc", 0, 0), + (b"abc", b"bc", 1, 1), + (b"", b"a", 0, -1), + ]; + for (text, needle, from, want) in cases { + let args = vec![Val::bytes(text), Val::bytes(needle), Val::Int(from)]; + let (core_v, _) = core_eval(&m, args.clone()); + assert_eq!(core_v, Ok(Val::Int(want)), "{text:?} {needle:?} {from}"); + assert_values(&m, &f, args); + } + assert_schedule(&f); +} + +#[test] +fn a_nested_loop_return_stays_distinct_from_continue() { + // The inner loop continues by raising its index, and one arm returns. + // The outer loop continues on the path that does not return. The + // return is not that continue. + let t = Ty::u64(); + let mut b = FB::new(vec![Ty::Bool], t.clone()); + let stop = b.arg(0); + let j = b.var(t.clone(), build::lit(0)); + b.loop_(|b| { + let jv = b.let_(Atom::V(j)); + let more = b.int_cmp("int.lt", jv.clone(), build::lit(2))?; + b.exit_unless(more)?; + let i = b.var(t.clone(), build::lit(0)); + b.loop_(|b| { + let iv = b.let_(Atom::V(i)); + let more = b.int_cmp("int.lt", iv.clone(), build::lit(3))?; + b.exit_unless(more)?; + b.when(stop.clone(), |b| { + b.ret(build::lit(7)); + Ok(()) + })?; + let i1 = b.add_in(iv, build::lit(1), t.clone())?; + b.set(i, i1); + Ok(()) + })?; + let j1 = b.add_in(jv, build::lit(1), t.clone())?; + b.set(j, j1); + Ok(()) + }) + .unwrap(); + let rv = b.let_(Atom::V(j)); + b.ret(rv); + let m = module("nest", b.finish()); + wf::check(&m).unwrap(); + let f = bridge::construct(&m).unwrap_or_else(|e| panic!("{}", e.line())); + assert_applied(&f); + let (stop_v, _) = core_eval(&m, vec![Val::Bool(true)]); + let (run, _) = core_eval(&m, vec![Val::Bool(false)]); + assert_eq!(stop_v, Ok(Val::Int(7))); + assert_eq!(run, Ok(Val::Int(2))); + assert_values(&m, &f, vec![Val::Bool(true)]); + assert_values(&m, &f, vec![Val::Bool(false)]); + assert_schedule(&f); } #[test] diff --git a/spec/checker/tests/form_bridge_census.txt b/spec/checker/tests/form_bridge_census.txt index f8bf71bb..3af14a52 100644 --- a/spec/checker/tests/form_bridge_census.txt +++ b/spec/checker/tests/form_bridge_census.txt @@ -44,32 +44,31 @@ unblocks set 14 break 14 round-trip 9 lowering -3 break,exit -2 defect +4 break,exit +2 FG8 2 global -1 FG8 1 bound,break 1 break,catch -1 break,defect +1 break,loop +1 catch,global whole modules, every exact set modules set 14 break 14 round-trip 9 lowering -3 break,exit +4 break,exit +2 FG8 2 break,catch,exit 2 clear -2 defect 2 defect,exit,loop 2 global -1 FG8 1 bound,break 1 break,catch 1 break,catch,exit,loop -1 break,defect -1 break,defect,exit +1 break,loop 1 catch,global +1 defect 1 exit 1 exit,global 1 global,loop @@ -80,12 +79,13 @@ modules set 16 round-trip 9 lowering 5 clear -2 defect 2 exit 2 no-root -1 FG8,defect -1 break,defect +1 FG8 +1 break,loop 1 break,round-trip +1 defect +1 defect,loop 1 defect,round-trip 1 global 1 global,loop @@ -98,7 +98,7 @@ module whole roots user global-sites 03_fib_loop.rce round-trip round-trip user 04_bank_account.rce break break user 05_kind_match.rce break break user -06_option.rce break,defect break,defect user +06_option.rce break,loop break,loop user 07_error_union.rce defect,exit,loop defect user 08_traits.rce round-trip round-trip user 09_generics.rce clear no-root entry-only @@ -112,7 +112,7 @@ module whole roots user global-sites 17_traffic_light.rce break break user 18_while_bound.rce round-trip round-trip user 19_floats.rce break round-trip user -20_strings.rce defect,exit,loop defect user +20_strings.rce defect,exit,loop defect,loop user 21_maps.rce lowering lowering lowering not covered by the lowering: an integer operation checked against ranges that differ at run time (["lit", "u32"]) 22_tuples.rce defect defect,round-trip user 23_patterns.rce break break user @@ -124,7 +124,7 @@ module whole roots user global-sites 30_state_machine_guarded.rce break break user 31_inventory.rce break,exit exit user 32_comptime_block.rce global global user fn table_size: global: g3 is not part of the working context ω holds -33_in_operator.rce defect FG8,defect user +33_in_operator.rce FG8 FG8 user 34_chan_types.rce lowering lowering lowering not covered by the lowering: the type chan (u64, text) 35_nested_if.rce clear clear user 36_multiline_brackets.rce round-trip round-trip user @@ -142,7 +142,7 @@ module whole roots user global-sites 48_linear_handle.rce round-trip clear user 49_loop_limit.rce round-trip round-trip user 50_iterators.rce global,loop global,loop user check 27: global: g3 is not part of the working context ω holds | entry: global: g3 is not part of the working context ω holds | gen iter evens(int{u64}[0, 18446744073709551615]): global: g3 is not part of the working context ω holds | gen iter naturals(): global: g3 is not part of the working context ω holds -51_refined_input.rce break,defect,exit break user +51_refined_input.rce break,exit break user 52_context_text.rce break,exit break user 53_capability.rce break break user 54_secret.rce break,exit break user