tldr-core 0.1.2

Core analysis engine for TLDR code analysis tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! HashKey - Structured Keys for GVN Hashing
//!
//! MIT-HASH-01b Mitigation: Use structured enum instead of string concatenation
//! to prevent collision issues like "binop:Add:ab:c" vs "binop:Add:a:bc".
//!
//! # Problem
//!
//! String-based hash keys like `format!("binop:{}:{}:{}", op, left, right)` can
//! cause false collisions when operand names contain the delimiter. For example:
//! - `binop:Add:a:bc` (a + bc)
//! - `binop:Add:ab:c` (ab + c)
//!
//! These would hash differently with strings but could collide if names are
//! manipulated incorrectly.
//!
//! # Solution
//!
//! Use a structured enum that derives Hash, ensuring each component is hashed
//! separately without delimiter confusion.

use std::cmp::Ordering;

/// Structured hash keys for GVN expressions.
///
/// Use this enum instead of string concatenation to ensure collision-free hashing.
/// The enum derives Hash, Eq, and PartialEq, so it can be used directly as a
/// HashMap key.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum HashKey {
    /// Constant value with type and representation
    Const {
        /// The type of the constant (e.g., "int", "str", "float").
        type_name: String,
        /// The string representation of the constant value (e.g., "42", "hello").
        repr: String,
    },

    /// Variable reference by value number (already resolved)
    VarVN {
        /// The GVN value number assigned to this variable.
        vn: usize,
    },

    /// Unresolved variable name (for parameters and initial references)
    Name {
        /// The source-level variable name before value numbering.
        name: String,
    },

    /// Binary operation with operator and operands
    /// If commutative=true, operands are normalized (sorted)
    BinOp {
        /// The binary operator (e.g., "Add", "Sub", "Mult").
        op: String,
        /// The left operand of the binary expression.
        left: Box<HashKey>,
        /// The right operand of the binary expression.
        right: Box<HashKey>,
        /// Whether the operands have been normalized for commutativity.
        commutative: bool,
    },

    /// Unary operation (e.g., -x, not x, ~x)
    UnaryOp {
        /// The unary operator (e.g., "USub", "Not", "Invert").
        op: String,
        /// The operand of the unary expression.
        operand: Box<HashKey>,
    },

    /// Boolean operation (and/or with multiple operands)
    BoolOp {
        /// The boolean operator ("And" or "Or").
        op: String,
        /// The operands of the boolean expression.
        operands: Vec<HashKey>,
    },

    /// Comparison expression (e.g., a < b < c)
    /// Parts are stored as strings for simplicity
    Compare {
        /// The comparison chain parts as strings (operators and operands interleaved).
        parts: Vec<String>,
    },

    /// Function/method call - always unique (conservative)
    /// Each call gets a unique ID since calls may have side effects
    Call {
        /// Monotonically increasing ID ensuring each call site is treated as unique.
        unique_id: usize,
    },

    /// Attribute access (obj.attr)
    Attribute {
        /// The base object expression being accessed.
        value: Box<HashKey>,
        /// The attribute name being accessed on the object.
        attr: String,
    },

    /// Subscript access (obj[key])
    Subscript {
        /// The base object expression being subscripted.
        value: Box<HashKey>,
        /// The subscript key expression.
        slice: Box<HashKey>,
    },

    /// Unique marker for expressions that should never be equivalent
    /// Used for depth-limited expressions and other special cases
    Unique {
        /// Monotonically increasing ID ensuring this expression never matches another.
        id: usize,
    },
}

// =============================================================================
// Commutativity Support
// =============================================================================

/// Returns true if the given binary operator is commutative.
///
/// Commutative operators have the property that `a op b == b op a`:
/// - Add: a + b == b + a
/// - Mult: a * b == b * a
/// - BitOr: a | b == b | a
/// - BitAnd: a & b == b & a
/// - BitXor: a ^ b == b ^ a
///
/// Non-commutative operators (Sub, Div, Mod, Pow, LShift, RShift, etc.)
/// do NOT satisfy this property.
pub fn is_commutative(op: &str) -> bool {
    matches!(
        op,
        // Python operator enum names (backward compat)
        "Add" | "Mult" | "BitOr" | "BitAnd" | "BitXor" |
        // Raw operator text (multi-language universal)
        "+" | "*" | "|" | "&" | "^" |
        // Equality (commutative across all languages)
        "==" | "!=" |
        // Boolean (Python-style)
        "and" | "or" |
        // Boolean (C-style)
        "&&" | "||"
    )
}

/// Normalize a binary operation by sorting operands for commutative operators.
///
/// For commutative operations, we sort the operands to ensure that
/// `a + b` and `b + a` produce the same HashKey.
///
/// For non-commutative operations, operand order is preserved.
pub fn normalize_binop(op: &str, left: HashKey, right: HashKey) -> HashKey {
    let commutative = is_commutative(op);

    let (normalized_left, normalized_right) = if commutative {
        // Sort operands using a consistent ordering
        match compare_hash_keys(&left, &right) {
            Ordering::Greater => (right, left),
            _ => (left, right),
        }
    } else {
        (left, right)
    };

    HashKey::BinOp {
        op: op.to_string(),
        left: Box::new(normalized_left),
        right: Box::new(normalized_right),
        commutative,
    }
}

/// Compare two HashKeys for normalization ordering.
///
/// This provides a consistent total ordering for HashKey variants,
/// used to normalize commutative operations.
fn compare_hash_keys(a: &HashKey, b: &HashKey) -> Ordering {
    // Use discriminant first, then compare fields
    match (a, b) {
        (
            HashKey::Const {
                type_name: t1,
                repr: r1,
            },
            HashKey::Const {
                type_name: t2,
                repr: r2,
            },
        ) => t1.cmp(t2).then_with(|| r1.cmp(r2)),
        (HashKey::VarVN { vn: v1 }, HashKey::VarVN { vn: v2 }) => v1.cmp(v2),
        (HashKey::Name { name: n1 }, HashKey::Name { name: n2 }) => n1.cmp(n2),
        (
            HashKey::BinOp {
                op: o1,
                left: l1,
                right: r1,
                ..
            },
            HashKey::BinOp {
                op: o2,
                left: l2,
                right: r2,
                ..
            },
        ) => o1
            .cmp(o2)
            .then_with(|| compare_hash_keys(l1, l2))
            .then_with(|| compare_hash_keys(r1, r2)),
        (
            HashKey::UnaryOp {
                op: o1,
                operand: op1,
            },
            HashKey::UnaryOp {
                op: o2,
                operand: op2,
            },
        ) => o1.cmp(o2).then_with(|| compare_hash_keys(op1, op2)),
        (HashKey::Call { unique_id: u1 }, HashKey::Call { unique_id: u2 }) => u1.cmp(u2),
        (HashKey::Unique { id: u1 }, HashKey::Unique { id: u2 }) => u1.cmp(u2),
        // Fall back to discriminant ordering for different variants
        _ => discriminant_order(a).cmp(&discriminant_order(b)),
    }
}

/// Get a numeric discriminant for ordering different HashKey variants
fn discriminant_order(key: &HashKey) -> u8 {
    match key {
        HashKey::Const { .. } => 0,
        HashKey::VarVN { .. } => 1,
        HashKey::Name { .. } => 2,
        HashKey::BinOp { .. } => 3,
        HashKey::UnaryOp { .. } => 4,
        HashKey::BoolOp { .. } => 5,
        HashKey::Compare { .. } => 6,
        HashKey::Call { .. } => 7,
        HashKey::Attribute { .. } => 8,
        HashKey::Subscript { .. } => 9,
        HashKey::Unique { .. } => 10,
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // =========================================================================
    // Commutativity Tests
    // =========================================================================

    #[test]
    fn test_is_commutative_add() {
        assert!(is_commutative("Add"));
    }

    #[test]
    fn test_is_commutative_mult() {
        assert!(is_commutative("Mult"));
    }

    #[test]
    fn test_is_commutative_bitor() {
        assert!(is_commutative("BitOr"));
    }

    #[test]
    fn test_is_commutative_bitand() {
        assert!(is_commutative("BitAnd"));
    }

    #[test]
    fn test_is_commutative_bitxor() {
        assert!(is_commutative("BitXor"));
    }

    #[test]
    fn test_is_not_commutative_sub() {
        assert!(!is_commutative("Sub"));
    }

    #[test]
    fn test_is_not_commutative_div() {
        assert!(!is_commutative("Div"));
    }

    #[test]
    fn test_is_not_commutative_mod() {
        assert!(!is_commutative("Mod"));
    }

    #[test]
    fn test_is_not_commutative_pow() {
        assert!(!is_commutative("Pow"));
    }

    #[test]
    fn test_is_not_commutative_lshift() {
        assert!(!is_commutative("LShift"));
    }

    #[test]
    fn test_is_not_commutative_rshift() {
        assert!(!is_commutative("RShift"));
    }

    // =========================================================================
    // P2: Raw Operator Text Commutativity Tests
    // =========================================================================

    #[test]
    fn test_is_commutative_raw_plus() {
        assert!(is_commutative("+"));
    }

    #[test]
    fn test_is_commutative_raw_star() {
        assert!(is_commutative("*"));
    }

    #[test]
    fn test_is_commutative_raw_eq() {
        assert!(is_commutative("=="));
    }

    #[test]
    fn test_is_commutative_raw_neq() {
        assert!(is_commutative("!="));
    }

    #[test]
    fn test_is_commutative_raw_pipe() {
        assert!(is_commutative("|"));
    }

    #[test]
    fn test_is_commutative_raw_ampersand() {
        assert!(is_commutative("&"));
    }

    #[test]
    fn test_is_commutative_raw_caret() {
        assert!(is_commutative("^"));
    }

    #[test]
    fn test_is_commutative_c_style_and() {
        assert!(is_commutative("&&"));
    }

    #[test]
    fn test_is_commutative_c_style_or() {
        assert!(is_commutative("||"));
    }

    #[test]
    fn test_is_not_commutative_raw_minus() {
        assert!(!is_commutative("-"));
    }

    #[test]
    fn test_is_not_commutative_raw_slash() {
        assert!(!is_commutative("/"));
    }

    #[test]
    fn test_is_not_commutative_raw_percent() {
        assert!(!is_commutative("%"));
    }

    // =========================================================================
    // Normalization Tests
    // =========================================================================

    #[test]
    fn test_hash_key_commutative_normalization_add() {
        // x + y and y + x should produce the same HashKey
        let x = HashKey::Name {
            name: "x".to_string(),
        };
        let y = HashKey::Name {
            name: "y".to_string(),
        };

        let xy = normalize_binop("Add", x.clone(), y.clone());
        let yx = normalize_binop("Add", y.clone(), x.clone());

        assert_eq!(xy, yx, "x + y and y + x should be equal");
    }

    #[test]
    fn test_hash_key_commutative_normalization_mult() {
        let a = HashKey::Name {
            name: "a".to_string(),
        };
        let b = HashKey::Name {
            name: "b".to_string(),
        };

        let ab = normalize_binop("Mult", a.clone(), b.clone());
        let ba = normalize_binop("Mult", b.clone(), a.clone());

        assert_eq!(ab, ba, "a * b and b * a should be equal");
    }

    #[test]
    fn test_hash_key_commutative_normalization_bitor() {
        let a = HashKey::Name {
            name: "a".to_string(),
        };
        let b = HashKey::Name {
            name: "b".to_string(),
        };

        let ab = normalize_binop("BitOr", a.clone(), b.clone());
        let ba = normalize_binop("BitOr", b.clone(), a.clone());

        assert_eq!(ab, ba, "a | b and b | a should be equal");
    }

    #[test]
    fn test_hash_key_commutative_normalization_bitand() {
        let a = HashKey::Name {
            name: "a".to_string(),
        };
        let b = HashKey::Name {
            name: "b".to_string(),
        };

        let ab = normalize_binop("BitAnd", a.clone(), b.clone());
        let ba = normalize_binop("BitAnd", b.clone(), a.clone());

        assert_eq!(ab, ba, "a & b and b & a should be equal");
    }

    #[test]
    fn test_hash_key_commutative_normalization_bitxor() {
        let a = HashKey::Name {
            name: "a".to_string(),
        };
        let b = HashKey::Name {
            name: "b".to_string(),
        };

        let ab = normalize_binop("BitXor", a.clone(), b.clone());
        let ba = normalize_binop("BitXor", b.clone(), a.clone());

        assert_eq!(ab, ba, "a ^ b and b ^ a should be equal");
    }

    #[test]
    fn test_hash_key_non_commutative_order_preserved_sub() {
        // a - b and b - a should be DIFFERENT
        let a = HashKey::Name {
            name: "a".to_string(),
        };
        let b = HashKey::Name {
            name: "b".to_string(),
        };

        let ab = normalize_binop("Sub", a.clone(), b.clone());
        let ba = normalize_binop("Sub", b.clone(), a.clone());

        assert_ne!(ab, ba, "a - b and b - a should be different");
    }

    #[test]
    fn test_hash_key_non_commutative_order_preserved_div() {
        let a = HashKey::Name {
            name: "a".to_string(),
        };
        let b = HashKey::Name {
            name: "b".to_string(),
        };

        let ab = normalize_binop("Div", a.clone(), b.clone());
        let ba = normalize_binop("Div", b.clone(), a.clone());

        assert_ne!(ab, ba, "a / b and b / a should be different");
    }

    // =========================================================================
    // HashKey Equality Tests
    // =========================================================================

    #[test]
    fn test_hash_key_const_equality() {
        let c1 = HashKey::Const {
            type_name: "int".to_string(),
            repr: "42".to_string(),
        };
        let c2 = HashKey::Const {
            type_name: "int".to_string(),
            repr: "42".to_string(),
        };
        let c3 = HashKey::Const {
            type_name: "int".to_string(),
            repr: "43".to_string(),
        };

        assert_eq!(c1, c2);
        assert_ne!(c1, c3);
    }

    #[test]
    fn test_hash_key_var_vn_equality() {
        let v1 = HashKey::VarVN { vn: 1 };
        let v2 = HashKey::VarVN { vn: 1 };
        let v3 = HashKey::VarVN { vn: 2 };

        assert_eq!(v1, v2);
        assert_ne!(v1, v3);
    }

    #[test]
    fn test_hash_key_name_equality() {
        let n1 = HashKey::Name {
            name: "x".to_string(),
        };
        let n2 = HashKey::Name {
            name: "x".to_string(),
        };
        let n3 = HashKey::Name {
            name: "y".to_string(),
        };

        assert_eq!(n1, n2);
        assert_ne!(n1, n3);
    }

    #[test]
    fn test_hash_key_call_always_different() {
        // Calls with different unique_ids should be different
        let c1 = HashKey::Call { unique_id: 1 };
        let c2 = HashKey::Call { unique_id: 2 };
        let c3 = HashKey::Call { unique_id: 1 };

        assert_ne!(c1, c2);
        assert_eq!(c1, c3);
    }

    #[test]
    fn test_hash_key_unique_always_different() {
        let u1 = HashKey::Unique { id: 1 };
        let u2 = HashKey::Unique { id: 2 };

        assert_ne!(u1, u2);
    }

    // =========================================================================
    // Complex Expression Tests
    // =========================================================================

    #[test]
    fn test_nested_commutative_normalization() {
        // (a + b) + c and (b + a) + c should be equal
        let a = HashKey::Name {
            name: "a".to_string(),
        };
        let b = HashKey::Name {
            name: "b".to_string(),
        };
        let c = HashKey::Name {
            name: "c".to_string(),
        };

        let ab = normalize_binop("Add", a.clone(), b.clone());
        let ba = normalize_binop("Add", b.clone(), a.clone());

        let abc = normalize_binop("Add", ab.clone(), c.clone());
        let bac = normalize_binop("Add", ba.clone(), c.clone());

        // ab and ba are normalized to the same value, so abc == bac
        assert_eq!(abc, bac, "Nested commutative expressions should normalize");
    }

    #[test]
    fn test_different_operators_not_equal() {
        let a = HashKey::Name {
            name: "a".to_string(),
        };
        let b = HashKey::Name {
            name: "b".to_string(),
        };

        let add = normalize_binop("Add", a.clone(), b.clone());
        let sub = normalize_binop("Sub", a.clone(), b.clone());
        let mult = normalize_binop("Mult", a.clone(), b.clone());

        assert_ne!(add, sub);
        assert_ne!(add, mult);
        assert_ne!(sub, mult);
    }

    #[test]
    fn test_hash_key_as_hashmap_key() {
        use std::collections::HashMap;

        let mut map: HashMap<HashKey, usize> = HashMap::new();

        let key1 = HashKey::Name {
            name: "x".to_string(),
        };
        let key2 = HashKey::Name {
            name: "x".to_string(),
        };

        map.insert(key1, 1);

        // Same key should retrieve the same value
        assert_eq!(map.get(&key2), Some(&1));
    }

    #[test]
    fn test_commutative_keys_hash_same() {
        use std::collections::HashMap;

        let mut map: HashMap<HashKey, usize> = HashMap::new();

        let a = HashKey::Name {
            name: "a".to_string(),
        };
        let b = HashKey::Name {
            name: "b".to_string(),
        };

        let ab = normalize_binop("Add", a.clone(), b.clone());
        let ba = normalize_binop("Add", b.clone(), a.clone());

        map.insert(ab, 1);

        // ba should find the same entry as ab
        assert_eq!(map.get(&ba), Some(&1));
    }
}