Skip to main content

lex_vcs/
compute_diff.rs

1//! AST-level structural diff between two sets of `FnDecl`s.
2//!
3//! Moved from `lex-cli/src/diff.rs` so both the CLI (`lex diff`
4//! command) and the HTTP API (`lex serve`) can compute a [`DiffReport`]
5//! without introducing a circular dependency. `lex-vcs` is the right
6//! home because it already owns `DiffReport` and `diff_to_ops`.
7
8use crate::diff_report::{
9    AddRemove, BodyPatch, DiffReport, EffectChanges, Modified, Renamed,
10};
11use lex_ast::{stage_canonical_hash_hex, CExpr, Effect, EffectArg, FnDecl, Stage, TypeExpr};
12use std::collections::{BTreeMap, BTreeSet, HashMap};
13
14/// Compute a structural diff between two named fn-decl maps.
15///
16/// `body_patches` controls whether body-level expression diffs are
17/// emitted inside each `Modified` entry. Pass `true` for rich output
18/// (CLI / review); `false` for a signature-only diff (faster).
19pub fn compute_diff(
20    a: &BTreeMap<String, FnDecl>,
21    b: &BTreeMap<String, FnDecl>,
22    body_patches: bool,
23) -> DiffReport {
24    let mut report = DiffReport::default();
25    let names_a: BTreeSet<&String> = a.keys().collect();
26    let names_b: BTreeSet<&String> = b.keys().collect();
27
28    let only_a: Vec<&String> = names_a.difference(&names_b).copied().collect();
29    let only_b: Vec<&String> = names_b.difference(&names_a).copied().collect();
30
31    // Detect renames: for each name only-in-A, check if any only-in-B
32    // has a body whose canonical-AST hash matches (modulo the fn
33    // name itself). sig_id over the FnDecl with name normalized
34    // serves as the structural-identity key.
35    //
36    // `body_hash` clones the FnDecl and does a full canonical
37    // serialize + SHA-256 — not O(1). The naive nested loop below
38    // used to recompute `body_hash(fb)` on every outer (`only_a`)
39    // iteration even though `fb` never changes, making this
40    // O(|only_a| * |only_b|) hashes instead of O(|only_a| + |only_b|).
41    // Harmless at the tiny scale these sets used to have, but
42    // `pkg_publish_handler` calls this once per uploaded file with
43    // `only_a` sized to the *entire* tenant's historical function
44    // set — that combination hung the server for 38+ minutes on a
45    // real publish (alpibrusl/lex-lang#813). Precomputing each
46    // `only_b` hash once turns the nested loop into O(1) lookups.
47    let mut hash_to_bs: HashMap<String, Vec<&String>> = HashMap::new();
48    for &bn in &only_b {
49        hash_to_bs.entry(body_hash(&b[bn])).or_default().push(bn);
50    }
51    let mut renamed_pairs: Vec<(String, String)> = Vec::new();
52    let mut consumed_a: BTreeSet<String> = BTreeSet::new();
53    let mut consumed_b: BTreeSet<String> = BTreeSet::new();
54    for &an in &only_a {
55        let fa = &a[an];
56        let fa_norm_id = body_hash(fa);
57        let Some(candidates) = hash_to_bs.get(&fa_norm_id) else { continue };
58        let Some(&bn) = candidates.iter().find(|bn| !consumed_b.contains(**bn)) else { continue };
59        renamed_pairs.push((an.clone(), bn.clone()));
60        consumed_a.insert(an.clone());
61        consumed_b.insert(bn.clone());
62    }
63
64    for &n in &only_a {
65        if consumed_a.contains(n) { continue; }
66        let fd = &a[n];
67        report.removed.push(AddRemove {
68            name: n.clone(),
69            signature: render_signature(fd),
70        });
71    }
72    for &n in &only_b {
73        if consumed_b.contains(n) { continue; }
74        let fd = &b[n];
75        report.added.push(AddRemove {
76            name: n.clone(),
77            signature: render_signature(fd),
78        });
79    }
80    for (an, bn) in &renamed_pairs {
81        let fd = &b[bn];
82        report.renamed.push(Renamed {
83            from: an.clone(),
84            to: bn.clone(),
85            signature: render_signature(fd),
86        });
87    }
88
89    // Modified: same name on both sides; compare bodies.
90    for n in names_a.intersection(&names_b) {
91        let fa = &a[*n];
92        let fb = &b[*n];
93        let sig_a = render_signature(fa);
94        let sig_b = render_signature(fb);
95        if body_hash(fa) == body_hash(fb) && sig_a == sig_b { continue; }
96
97        let patches = if body_patches {
98            let mut patches = Vec::new();
99            diff_expr(&fa.body, &fb.body, "body", &mut patches, 4);
100            patches
101        } else { Vec::new() };
102
103        let effect_changes = effect_diff(&fa.effects, &fb.effects);
104        report.modified.push(Modified {
105            name: (*n).clone(),
106            signature_before: sig_a.clone(),
107            signature_after: sig_b.clone(),
108            signature_changed: sig_a != sig_b,
109            effect_changes,
110            body_patches: patches,
111        });
112    }
113    report
114}
115
116/// Hash of the function's structural identity, used for rename
117/// detection. Excludes the function's name (so `fn foo -> Int { 1 }`
118/// and `fn bar -> Int { 1 }` share a hash) but includes everything
119/// else: params, effects, return type, body.
120fn body_hash(fd: &FnDecl) -> String {
121    let mut anon = fd.clone();
122    anon.name = String::new();
123    let stage = Stage::FnDecl(anon);
124    stage_canonical_hash_hex(&stage)
125}
126
127/// Walk two CExprs in parallel; record the first divergence at each
128/// child position. `depth` caps recursion so a tiny per-fn diff
129/// doesn't degenerate into hundreds of micro-changes.
130fn diff_expr(a: &CExpr, b: &CExpr, path: &str, out: &mut Vec<BodyPatch>, depth: u32) {
131    if depth == 0 { return; }
132    let kind_a = node_kind(a);
133    let kind_b = node_kind(b);
134    if kind_a != kind_b {
135        out.push(BodyPatch {
136            op: "Replace".into(), node_path: path.into(),
137            from_kind: kind_a.into(), to_kind: kind_b.into(),
138        });
139        return;
140    }
141    // Same kind: recurse into structurally-equivalent children.
142    match (a, b) {
143        (CExpr::Literal { value: la }, CExpr::Literal { value: lb }) => {
144            if la != lb {
145                out.push(BodyPatch {
146                    op: "Replace".into(), node_path: path.into(),
147                    from_kind: "Literal".into(), to_kind: "Literal".into(),
148                });
149            }
150        }
151        (CExpr::Var { name: na }, CExpr::Var { name: nb }) => {
152            if na != nb {
153                out.push(BodyPatch {
154                    op: "Replace".into(), node_path: path.into(),
155                    from_kind: format!("Var({na})"), to_kind: format!("Var({nb})"),
156                });
157            }
158        }
159        (CExpr::Call { callee: ca, args: aa },
160         CExpr::Call { callee: cb, args: ab }) => {
161            diff_expr(ca, cb, &format!("{path}.callee"), out, depth - 1);
162            diff_args(aa, ab, &format!("{path}.args"), out, depth);
163        }
164        (CExpr::Let { name: na, value: va, body: ba, .. },
165         CExpr::Let { name: nb, value: vb, body: bb, .. }) => {
166            if na != nb {
167                out.push(BodyPatch {
168                    op: "Replace".into(),
169                    node_path: format!("{path}.name"),
170                    from_kind: format!("Let({na})"),
171                    to_kind:   format!("Let({nb})"),
172                });
173            }
174            diff_expr(va, vb, &format!("{path}.value"), out, depth - 1);
175            diff_expr(ba, bb, &format!("{path}.body"),  out, depth - 1);
176        }
177        (CExpr::Match { scrutinee: sa, arms: ams },
178         CExpr::Match { scrutinee: sb, arms: bms }) => {
179            diff_expr(sa, sb, &format!("{path}.scrutinee"), out, depth - 1);
180            let n = ams.len().max(bms.len());
181            for i in 0..n {
182                let p = format!("{path}.arms[{i}]");
183                match (ams.get(i), bms.get(i)) {
184                    (Some(a), Some(b)) =>
185                        diff_expr(&a.body, &b.body, &p, out, depth - 1),
186                    (Some(_), None) => out.push(BodyPatch {
187                        op: "Deleted".into(), node_path: p,
188                        from_kind: "MatchArm".into(), to_kind: "(removed)".into(),
189                    }),
190                    (None, Some(_)) => out.push(BodyPatch {
191                        op: "Inserted".into(), node_path: p,
192                        from_kind: "(none)".into(), to_kind: "MatchArm".into(),
193                    }),
194                    (None, None) => break,
195                }
196            }
197        }
198        (CExpr::Block { statements: sa, result: ra },
199         CExpr::Block { statements: sb, result: rb }) => {
200            diff_args(sa, sb, &format!("{path}.statements"), out, depth);
201            diff_expr(ra, rb, &format!("{path}.result"), out, depth - 1);
202        }
203        (CExpr::FieldAccess { value: va, field: fa },
204         CExpr::FieldAccess { value: vb, field: fb }) => {
205            diff_expr(va, vb, &format!("{path}.value"), out, depth - 1);
206            if fa != fb {
207                out.push(BodyPatch {
208                    op: "Replace".into(), node_path: format!("{path}.field"),
209                    from_kind: format!("Field({fa})"), to_kind: format!("Field({fb})"),
210                });
211            }
212        }
213        (CExpr::Lambda { body: ba, .. }, CExpr::Lambda { body: bb, .. }) => {
214            diff_expr(ba, bb, &format!("{path}.body"), out, depth - 1);
215        }
216        // For shapes we don't unfold further, mark the node itself
217        // as edited (same kind, content differs) — finer detail can
218        // come in a follow-up.
219        _ => {
220            out.push(BodyPatch {
221                op: "Replace".into(), node_path: path.into(),
222                from_kind: kind_a.into(), to_kind: kind_b.into(),
223            });
224        }
225    }
226}
227
228fn diff_args(a: &[CExpr], b: &[CExpr], path: &str, out: &mut Vec<BodyPatch>, depth: u32) {
229    let n = a.len().max(b.len());
230    for i in 0..n {
231        let p = format!("{path}[{i}]");
232        match (a.get(i), b.get(i)) {
233            (Some(x), Some(y)) => diff_expr(x, y, &p, out, depth - 1),
234            (Some(x), None) => out.push(BodyPatch {
235                op: "Deleted".into(), node_path: p,
236                from_kind: node_kind(x).into(), to_kind: "(removed)".into(),
237            }),
238            (None, Some(y)) => out.push(BodyPatch {
239                op: "Inserted".into(), node_path: p,
240                from_kind: "(none)".into(), to_kind: node_kind(y).into(),
241            }),
242            (None, None) => break,
243        }
244    }
245}
246
247fn node_kind(e: &CExpr) -> &'static str {
248    match e {
249        CExpr::Literal { .. }     => "Literal",
250        CExpr::Var { .. }         => "Var",
251        CExpr::Call { .. }        => "Call",
252        CExpr::Let { .. }         => "Let",
253        CExpr::Match { .. }       => "Match",
254        CExpr::Block { .. }       => "Block",
255        CExpr::Constructor { .. } => "Constructor",
256        CExpr::RecordLit { .. }   => "RecordLit",
257        CExpr::TupleLit { .. }    => "TupleLit",
258        CExpr::ListLit { .. }     => "ListLit",
259        CExpr::FieldAccess { .. } => "FieldAccess",
260        CExpr::Lambda { .. }      => "Lambda",
261        CExpr::BinOp { .. }       => "BinOp",
262        CExpr::UnaryOp { .. }     => "UnaryOp",
263        CExpr::Return { .. }      => "Return",
264    }
265}
266
267pub fn render_signature(fd: &FnDecl) -> String {
268    let params: Vec<String> = fd.params.iter()
269        .map(|p| format!("{} :: {}", p.name, render_type(&p.ty))).collect();
270    let eff = if fd.effects.is_empty() { String::new() } else {
271        let labels: Vec<String> = fd.effects.iter().map(effect_label).collect();
272        format!("[{}] ", labels.join(", "))
273    };
274    format!("fn {}({}) -> {}{}", fd.name, params.join(", "),
275        eff, render_type(&fd.return_type))
276}
277
278/// Render an effect with its arg if present: `fs_read("/tmp")`,
279/// `net("api.example.com")`, or just `io`. Used by both signature
280/// rendering and effect-diff so the same string identifies the
281/// same effect in either context.
282pub fn effect_label(e: &Effect) -> String {
283    match &e.arg {
284        Some(EffectArg::Str { value })   => format!("{}({:?})", e.name, value),
285        Some(EffectArg::Int { value })   => format!("{}({})",   e.name, value),
286        Some(EffectArg::Ident { value }) => format!("{}({})",   e.name, value),
287        None => e.name.clone(),
288    }
289}
290
291/// Set-style diff over two effect lists. Order-insensitive within
292/// the lists; ordering of the output is sorted-by-label so the
293/// JSON is stable.
294fn effect_diff(a: &[Effect], b: &[Effect]) -> EffectChanges {
295    let labels_a: BTreeSet<String> = a.iter().map(effect_label).collect();
296    let labels_b: BTreeSet<String> = b.iter().map(effect_label).collect();
297    let added:   Vec<String> = labels_b.difference(&labels_a).cloned().collect();
298    let removed: Vec<String> = labels_a.difference(&labels_b).cloned().collect();
299    EffectChanges {
300        before:  labels_a.into_iter().collect(),
301        after:   labels_b.into_iter().collect(),
302        added,
303        removed,
304    }
305}
306
307fn render_type(t: &TypeExpr) -> String {
308    match t {
309        TypeExpr::Named { name, args } => {
310            if args.is_empty() { name.clone() }
311            else {
312                let parts: Vec<String> = args.iter().map(render_type).collect();
313                format!("{name}[{}]", parts.join(", "))
314            }
315        }
316        TypeExpr::Tuple { items } => {
317            let parts: Vec<String> = items.iter().map(render_type).collect();
318            format!("({})", parts.join(", "))
319        }
320        TypeExpr::Record { fields } => {
321            let parts: Vec<String> = fields.iter()
322                .map(|f| format!("{} :: {}", f.name, render_type(&f.ty))).collect();
323            format!("{{ {} }}", parts.join(", "))
324        }
325        TypeExpr::RecordWithSpreads { spreads, fields } => {
326            let mut parts: Vec<String> = spreads.iter().map(|s| format!("...{}", s)).collect();
327            parts.extend(fields.iter().map(|f| format!("{} :: {}", f.name, render_type(&f.ty))));
328            format!("{{ {} }}", parts.join(", "))
329        }
330        TypeExpr::Function { params, effects, effect_row_var, ret } => {
331            let parts: Vec<String> = params.iter().map(render_type).collect();
332            let eff = if effects.is_empty() && effect_row_var.is_none() { String::new() } else {
333                let mut names: Vec<String> = effects.iter().map(|e| e.name.clone()).collect();
334                if let Some(v) = effect_row_var { names.push(format!("| {}", v)); }
335                format!("[{}] ", names.join(", "))
336            };
337            format!("({}) -> {}{}", parts.join(", "), eff, render_type(ret))
338        }
339        TypeExpr::Union { variants } => variants.iter().map(|v| match &v.payload {
340            Some(p) => format!("{}({})", v.name, render_type(p)),
341            None => v.name.clone(),
342        }).collect::<Vec<_>>().join(" | "),
343        TypeExpr::Refined { base, binding, .. } => {
344            // Render compactly: `Base{x | …}`. The full predicate is
345            // captured in the canonical AST and contributes to
346            // OpId hashing via lex-vcs's content-addressing — this
347            // string is for diagnostics only. (#209 slice 1)
348            format!("{}{{{} | …}}", render_type(base), binding)
349        }
350    }
351}