Iter 14g: Term::If restored (revert of 14d)
Reconsidered 14d's removal of Term::If. The decision was wrong.
"No redundancies" requires judgment; reducibility (if -> match)
is not redundancy in the strong sense. Term::If is a primitive
control-flow shape; bool branching is the second most common
shape after sequencing, and removing it cost 3x tokens on every
branch site (`(if c a b)` 4 tokens vs the match-on-Bool form
12 tokens).
Meta-pattern fixed: I had been treating user observations as
directives. User said "if is a subset of match"; I jumped to
remove it citing CLAUDE.md, with no independent conviction.
The leak appeared in 14f's JOURNAL prose ("three lines for what
if used to do in one"), which read as regret. Two feedback
memories saved (memory/feedback_user_suggestions_not_directives,
memory/feedback_no_nostalgia_for_removed_features) to head this
off.
Implementation: mechanical reverse-application of 14d's diff at
every site (AST, check including the 14e tail-position arm,
codegen 4 sites, surface parser/printer, pretty, CLI walker,
e2e test mutation). Removed lower_bool_match helper — it existed
only because 14d's migration shape needed codegen for non-ptr
match scrutinees; with Term::If back, match-on-Bool returns to
its pre-14d unsupported state. Three fixtures (sum, sort, max3)
restored to pre-14d shape. gc_stress (added in 14f) also
migrated back to (if ...) since it was authored under the wrong
constraint.
14e (musttail) and 14f (GC_malloc) verified intact in IR.
Hashes restored to pre-14d values:
- sum.sum: db33f57cb329935e
- sort.insert: 697fcb9f30f8633a
- max3.max: 65c45d6a45dd0a72
- max3.max3: 624b14429bf302f5
All other defs across all 18 fixtures keep their post-14f
hashes. Tests 80/80 green; cargo doc 0 warnings. LOC delta
+265/-295 net -30.
DESIGN.md Decision 7 preserved with a "Status: REVERTED" header
for audit trail. Form-(A) `if-term` production restored.
Plan: back to 15a (std_maybe stdlib module).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -816,6 +816,87 @@ impl<'a> Emitter<'a> {
|
||||
self.locals.pop();
|
||||
r
|
||||
}
|
||||
Term::If { cond, then, else_ } => {
|
||||
let (cond_v, cond_ty) = self.lower_term(cond)?;
|
||||
if cond_ty != "i1" {
|
||||
return Err(CodegenError::Internal(format!(
|
||||
"if cond not i1: {cond_ty}"
|
||||
)));
|
||||
}
|
||||
let id = self.fresh_id();
|
||||
let then_lbl = format!("then.{id}");
|
||||
let else_lbl = format!("else.{id}");
|
||||
let join_lbl = format!("join.{id}");
|
||||
|
||||
self.body.push_str(&format!(
|
||||
" br i1 {cond_v}, label %{then_lbl}, label %{else_lbl}\n"
|
||||
));
|
||||
|
||||
self.start_block(&then_lbl);
|
||||
let (then_v, then_ty) = self.lower_term(then)?;
|
||||
// Iter 14e: a tail-call in this branch already terminated
|
||||
// its block; skip its branch to join and exclude from phi.
|
||||
let then_terminated = self.block_terminated;
|
||||
let then_block_end = self.current_block.clone();
|
||||
if !then_terminated {
|
||||
self.body.push_str(&format!(" br label %{join_lbl}\n"));
|
||||
}
|
||||
|
||||
self.start_block(&else_lbl);
|
||||
let (else_v, else_ty) = self.lower_term(else_)?;
|
||||
let else_terminated = self.block_terminated;
|
||||
let else_block_end = self.current_block.clone();
|
||||
if !then_terminated && !else_terminated && then_ty != else_ty {
|
||||
return Err(CodegenError::Internal(format!(
|
||||
"if branches type mismatch: {then_ty} vs {else_ty}"
|
||||
)));
|
||||
}
|
||||
if !else_terminated {
|
||||
self.body.push_str(&format!(" br label %{join_lbl}\n"));
|
||||
}
|
||||
|
||||
// Iter 14e: if both branches terminated, the whole `if` is
|
||||
// terminated and no join is reachable. Mark and bail.
|
||||
if then_terminated && else_terminated {
|
||||
self.block_terminated = true;
|
||||
return Ok(("0".into(), then_ty));
|
||||
}
|
||||
// If exactly one branch terminated, the join receives only
|
||||
// the other branch's value — no phi node is needed.
|
||||
if then_terminated {
|
||||
self.start_block(&join_lbl);
|
||||
return Ok((else_v, else_ty));
|
||||
}
|
||||
if else_terminated {
|
||||
self.start_block(&join_lbl);
|
||||
return Ok((then_v, then_ty));
|
||||
}
|
||||
|
||||
self.start_block(&join_lbl);
|
||||
let phi = self.fresh_ssa();
|
||||
self.body.push_str(&format!(
|
||||
" {phi} = phi {ty} [ {tv}, %{tlbl} ], [ {ev}, %{elbl} ]\n",
|
||||
ty = then_ty,
|
||||
tv = then_v,
|
||||
tlbl = then_block_end,
|
||||
ev = else_v,
|
||||
elbl = else_block_end,
|
||||
));
|
||||
// Iter 7: if both branches yield the same fn-pointer sig,
|
||||
// forward it to the phi SSA so subsequent indirect calls
|
||||
// can resolve.
|
||||
if then_ty == "ptr" {
|
||||
if let (Some(ts), Some(es)) =
|
||||
(self.ssa_fn_sigs.get(&then_v), self.ssa_fn_sigs.get(&else_v))
|
||||
{
|
||||
if ts.params == es.params && ts.ret == es.ret {
|
||||
let merged = ts.clone();
|
||||
self.ssa_fn_sigs.insert(phi.clone(), merged);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok((phi, then_ty))
|
||||
}
|
||||
Term::App { callee, args, tail } => {
|
||||
// Direct call when the callee is a `Var` referring to a
|
||||
// statically-known target (builtin, current-module fn,
|
||||
@@ -962,16 +1043,6 @@ impl<'a> Emitter<'a> {
|
||||
) -> Result<(String, String)> {
|
||||
let s_ail = self.synth_arg_type(scrutinee)?;
|
||||
let (s_val, s_ty) = self.lower_term(scrutinee)?;
|
||||
// Iter 14d: Bool scrutinee. `Term::If` was retired and migrated to
|
||||
// a `Match` of shape `(lit-bool true) -> A | _ -> B`. Recognise
|
||||
// exactly that two-arm pattern (the canonical migration target;
|
||||
// see DESIGN.md Decision 7) and emit the conditional-branch IR
|
||||
// the old `Term::If` arm used to emit. We do not generalise to
|
||||
// arbitrary primitive matches — only the shape that replaces
|
||||
// `If`.
|
||||
if s_ty == "i1" {
|
||||
return self.lower_bool_match(&s_val, arms);
|
||||
}
|
||||
if s_ty != "ptr" {
|
||||
return Err(CodegenError::Internal(format!(
|
||||
"match on non-ADT scrutinee (got {s_ty}); MVP supports only ADTs"
|
||||
@@ -1184,120 +1255,6 @@ impl<'a> Emitter<'a> {
|
||||
Ok((phi, rt))
|
||||
}
|
||||
|
||||
/// Iter 14d: lower a `Match` whose scrutinee is `Bool` (`i1`) and
|
||||
/// whose arms are exactly the canonical migration shape from
|
||||
/// retired `Term::If`:
|
||||
///
|
||||
/// 1. `(pat-lit (lit-bool C)) -> A`
|
||||
/// 2. `_ -> B`
|
||||
///
|
||||
/// Emits the same IR the old `Term::If` arm of `lower_term` did:
|
||||
/// `br i1` to a `then`/`else` pair joined by a phi. Both arms must
|
||||
/// produce the same LLVM type (the typechecker has unified them
|
||||
/// already; the assertion is a defence-in-depth check).
|
||||
fn lower_bool_match(
|
||||
&mut self,
|
||||
cond_v: &str,
|
||||
arms: &[Arm],
|
||||
) -> Result<(String, String)> {
|
||||
if arms.len() != 2 {
|
||||
return Err(CodegenError::Internal(format!(
|
||||
"Bool-scrutinee match: expected exactly 2 arms (lit-bool + wild), got {}",
|
||||
arms.len()
|
||||
)));
|
||||
}
|
||||
let (true_body, false_body) = match (&arms[0].pat, &arms[1].pat) {
|
||||
(
|
||||
Pattern::Lit { lit: Literal::Bool { value: true } },
|
||||
Pattern::Wild,
|
||||
) => (&arms[0].body, &arms[1].body),
|
||||
(
|
||||
Pattern::Lit { lit: Literal::Bool { value: false } },
|
||||
Pattern::Wild,
|
||||
) => (&arms[1].body, &arms[0].body),
|
||||
_ => {
|
||||
return Err(CodegenError::Internal(
|
||||
"Bool-scrutinee match: arms must be `(lit-bool true) -> A | _ -> B` \
|
||||
or `(lit-bool false) -> A | _ -> B` (the Iter 14d migration shape \
|
||||
for retired `Term::If`)".into(),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let id = self.fresh_id();
|
||||
let then_lbl = format!("then.{id}");
|
||||
let else_lbl = format!("else.{id}");
|
||||
let join_lbl = format!("join.{id}");
|
||||
|
||||
self.body.push_str(&format!(
|
||||
" br i1 {cond_v}, label %{then_lbl}, label %{else_lbl}\n"
|
||||
));
|
||||
|
||||
self.start_block(&then_lbl);
|
||||
let (then_v, then_ty) = self.lower_term(true_body)?;
|
||||
// Iter 14e: a tail-call in this arm already terminated its
|
||||
// block; skip its branch to join and exclude from phi.
|
||||
let then_terminated = self.block_terminated;
|
||||
let then_block_end = self.current_block.clone();
|
||||
if !then_terminated {
|
||||
self.body.push_str(&format!(" br label %{join_lbl}\n"));
|
||||
}
|
||||
|
||||
self.start_block(&else_lbl);
|
||||
let (else_v, else_ty) = self.lower_term(false_body)?;
|
||||
let else_terminated = self.block_terminated;
|
||||
let else_block_end = self.current_block.clone();
|
||||
if !then_terminated && !else_terminated && then_ty != else_ty {
|
||||
return Err(CodegenError::Internal(format!(
|
||||
"Bool-match arms type mismatch: {then_ty} vs {else_ty}"
|
||||
)));
|
||||
}
|
||||
if !else_terminated {
|
||||
self.body.push_str(&format!(" br label %{join_lbl}\n"));
|
||||
}
|
||||
|
||||
// Iter 14e: if both arms terminated, the whole match is
|
||||
// terminated and no join is reachable. Mark and bail.
|
||||
if then_terminated && else_terminated {
|
||||
self.block_terminated = true;
|
||||
return Ok(("0".into(), then_ty));
|
||||
}
|
||||
// If exactly one arm terminated, the join receives only the
|
||||
// other arm's value — no phi node is needed.
|
||||
if then_terminated {
|
||||
self.start_block(&join_lbl);
|
||||
return Ok((else_v, else_ty));
|
||||
}
|
||||
if else_terminated {
|
||||
self.start_block(&join_lbl);
|
||||
return Ok((then_v, then_ty));
|
||||
}
|
||||
|
||||
self.start_block(&join_lbl);
|
||||
let phi = self.fresh_ssa();
|
||||
self.body.push_str(&format!(
|
||||
" {phi} = phi {ty} [ {tv}, %{tlbl} ], [ {ev}, %{elbl} ]\n",
|
||||
ty = then_ty,
|
||||
tv = then_v,
|
||||
tlbl = then_block_end,
|
||||
ev = else_v,
|
||||
elbl = else_block_end,
|
||||
));
|
||||
// If both branches yield the same fn-pointer sig, forward it
|
||||
// to the phi SSA so subsequent indirect calls can resolve.
|
||||
if then_ty == "ptr" {
|
||||
if let (Some(ts), Some(es)) =
|
||||
(self.ssa_fn_sigs.get(&then_v), self.ssa_fn_sigs.get(&else_v))
|
||||
{
|
||||
if ts.params == es.params && ts.ret == es.ret {
|
||||
let merged = ts.clone();
|
||||
self.ssa_fn_sigs.insert(phi.clone(), merged);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok((phi, then_ty))
|
||||
}
|
||||
|
||||
fn lower_app(&mut self, name: &str, args: &[Term], tail: bool) -> Result<(String, String)> {
|
||||
// Built-in arithmetic / comparison.
|
||||
if let Some((instr, ret_ty)) = builtin_binop(name) {
|
||||
@@ -1874,6 +1831,11 @@ impl<'a> Emitter<'a> {
|
||||
bound.remove(name);
|
||||
}
|
||||
}
|
||||
Term::If { cond, then, else_ } => {
|
||||
Self::collect_captures(cond, bound, captures, captures_set, builtins, top_level);
|
||||
Self::collect_captures(then, bound, captures, captures_set, builtins, top_level);
|
||||
Self::collect_captures(else_, bound, captures, captures_set, builtins, top_level);
|
||||
}
|
||||
Term::Do { args, .. } => {
|
||||
for a in args {
|
||||
Self::collect_captures(a, bound, captures, captures_set, builtins, top_level);
|
||||
@@ -2214,6 +2176,7 @@ impl<'a> Emitter<'a> {
|
||||
new_extras.push((name.clone(), v_ail));
|
||||
self.synth_with_extras(body, &new_extras)
|
||||
}
|
||||
Term::If { then, .. } => self.synth_with_extras(then, extras),
|
||||
Term::Do { op, .. } => builtin_effect_op_ret(op).ok_or_else(|| {
|
||||
CodegenError::Internal(format!(
|
||||
"synth_arg_type: unknown effect op `{op}`"
|
||||
@@ -2520,6 +2483,11 @@ fn apply_subst_to_term(t: &Term, subst: &BTreeMap<String, Type>) -> Term {
|
||||
value: Box::new(apply_subst_to_term(value, subst)),
|
||||
body: Box::new(apply_subst_to_term(body, subst)),
|
||||
},
|
||||
Term::If { cond, then, else_ } => Term::If {
|
||||
cond: Box::new(apply_subst_to_term(cond, subst)),
|
||||
then: Box::new(apply_subst_to_term(then, subst)),
|
||||
else_: Box::new(apply_subst_to_term(else_, subst)),
|
||||
},
|
||||
Term::Do { op, args, tail } => Term::Do {
|
||||
op: op.clone(),
|
||||
args: args.iter().map(|a| apply_subst_to_term(a, subst)).collect(),
|
||||
|
||||
Reference in New Issue
Block a user