fallow-engine 3.48.0

Typed analysis engine facade for fallow consumers
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
//! Consistent-identifier refinement of raw clone groups.
//!
//! With `normalization.ignoreIdentifiers`, every identifier hashes to one
//! value, so the matcher pairs `a = b + c` with `x = x + x`. With
//! `normalization.consistentIdentifiers`, this step checks each raw group
//! after the hash match: the names of each instance must map one-to-one to
//! the names of a reference instance.
//!
//! The rule has two parts:
//!
//! 1. Full-length classes. Instances that map one-to-one over the full
//!    length form one class. A one-to-one mapping is an equivalence
//!    relation, so the classes partition the instances. Each class with two
//!    or more instances stays a clone of the full length.
//! 2. Windows. When the group has more than one class, the step compares the
//!    first instance of each class with the first instance of each other
//!    class. For each pair it finds every maximal window that maps
//!    one-to-one: at a token that breaks the mapping, the next window starts
//!    one token after the last earlier occurrence of the conflicting name.
//!    Pairs of classes that share one window merge into one clone, because
//!    one-to-one mappings compose. Each window of `min_tokens` or more tokens
//!    stays a clone.
//!
//! Every emitted clone is consistent. The later subset removal drops a
//! window that a longer clone already covers. Above
//! `MAX_PAIRWISE_CLASSES` classes, the step compares each class only with
//! the first class, so the work stays bounded.

use rustc_hash::FxHashMap;

use super::FileData;
use super::extraction::RawGroup;
use crate::duplicates::tokenize::TokenKind;

/// Refine raw groups so that each emitted group has a one-to-one identifier
/// mapping between its instances.
pub(super) fn refine_groups(
    raw_groups: Vec<RawGroup>,
    files: &[FileData],
    min_tokens: usize,
) -> Vec<RawGroup> {
    let mut refined = Vec::with_capacity(raw_groups.len());
    for group in raw_groups {
        refine_group(&group, files, min_tokens, &mut refined);
    }
    refined
}

fn refine_group(group: &RawGroup, files: &[FileData], min_tokens: usize, out: &mut Vec<RawGroup>) {
    let names: Vec<Vec<Option<&str>>> = group
        .instances
        .iter()
        .map(|&(file_id, offset)| identifier_names(&files[file_id], offset, group.length))
        .collect();
    for planned in plan_groups(&names, group.length, min_tokens) {
        out.push(RawGroup {
            instances: planned
                .instances
                .iter()
                .map(|&index| {
                    let (file_id, offset) = group.instances[index];
                    (file_id, offset + planned.start)
                })
                .collect(),
            length: planned.length,
        });
    }
}

/// One consistent clone: instance indices into the raw group, and a token
/// range relative to the start of each instance.
#[derive(Debug, PartialEq, Eq)]
struct PlannedGroup {
    instances: Vec<usize>,
    start: usize,
    length: usize,
}

fn plan_groups(names: &[Vec<Option<&str>>], length: usize, min_tokens: usize) -> Vec<PlannedGroup> {
    let classes = full_length_classes(names);
    if classes.len() == 1 {
        return vec![PlannedGroup {
            instances: (0..names.len()).collect(),
            start: 0,
            length,
        }];
    }
    let mut planned = Vec::new();
    for class in &classes {
        if class.len() >= 2 {
            planned.push(PlannedGroup {
                instances: class.clone(),
                start: 0,
                length,
            });
        }
    }
    planned.extend(window_groups(names, &classes, min_tokens));
    planned
}

/// The identifier name at each position of one instance, `None` for other
/// tokens.
fn identifier_names(file: &FileData, offset: usize, length: usize) -> Vec<Option<&str>> {
    file.hashed_tokens[offset..offset + length]
        .iter()
        .map(
            |hashed| match &file.file_tokens.tokens[hashed.original_index].kind {
                TokenKind::Identifier(name) => Some(name.as_str()),
                _ => None,
            },
        )
        .collect()
}

/// A one-to-one mapping between the names of two instances.
#[derive(Default)]
struct Bijection<'a> {
    forward: FxHashMap<&'a str, &'a str>,
    backward: FxHashMap<&'a str, &'a str>,
}

impl<'a> Bijection<'a> {
    /// Add the pair `left -> right`. Returns `false` when the pair breaks
    /// the mapping.
    fn accept(&mut self, left: &'a str, right: &'a str) -> bool {
        match (self.forward.get(left), self.backward.get(right)) {
            (Some(&mapped), _) if mapped != right => false,
            (_, Some(&mapped)) if mapped != left => false,
            (Some(_), Some(_)) => true,
            _ => {
                self.forward.insert(left, right);
                self.backward.insert(right, left);
                true
            }
        }
    }

    fn clear(&mut self) {
        self.forward.clear();
        self.backward.clear();
    }
}

fn accept_position<'a>(
    mapping: &mut Bijection<'a>,
    reference: Option<&'a str>,
    other: Option<&'a str>,
) -> bool {
    match (reference, other) {
        (Some(left), Some(right)) => mapping.accept(left, right),
        _ => true,
    }
}

fn maps_one_to_one(reference: &[Option<&str>], other: &[Option<&str>]) -> bool {
    let mut mapping = Bijection::default();
    reference
        .iter()
        .zip(other)
        .all(|(&left, &right)| accept_position(&mut mapping, left, right))
}

/// Partition instance indices into classes that map one-to-one over the full
/// length.
fn full_length_classes(names: &[Vec<Option<&str>>]) -> Vec<Vec<usize>> {
    let mut classes: Vec<Vec<usize>> = Vec::new();
    for index in 0..names.len() {
        match classes
            .iter_mut()
            .find(|class| maps_one_to_one(&names[class[0]], &names[index]))
        {
            Some(class) => class.push(index),
            None => classes.push(vec![index]),
        }
    }
    classes
}

/// Above this number of classes, each class is compared only with the first
/// class instead of with every other class.
const MAX_PAIRWISE_CLASSES: usize = 32;

/// Clones of the windows that two or more classes share.
fn window_groups(
    names: &[Vec<Option<&str>>],
    classes: &[Vec<usize>],
    min_tokens: usize,
) -> Vec<PlannedGroup> {
    let mut pairs_by_window: FxHashMap<(usize, usize), Vec<(usize, usize)>> = FxHashMap::default();
    let firsts = if classes.len() > MAX_PAIRWISE_CLASSES {
        1
    } else {
        classes.len()
    };
    for left in 0..firsts {
        for right in left + 1..classes.len() {
            let windows = maximal_windows(&names[classes[left][0]], &names[classes[right][0]]);
            for window in windows {
                if window.1 - window.0 >= min_tokens {
                    pairs_by_window
                        .entry(window)
                        .or_default()
                        .push((left, right));
                }
            }
        }
    }
    let mut windows: Vec<_> = pairs_by_window.into_iter().collect();
    windows.sort_unstable_by_key(|&(window, _)| window);
    let mut planned = Vec::new();
    for ((start, end), pairs) in windows {
        for component in connected_classes(classes.len(), &pairs) {
            let mut instances: Vec<usize> = component
                .iter()
                .flat_map(|&class| classes[class].iter().copied())
                .collect();
            instances.sort_unstable();
            planned.push(PlannedGroup {
                instances,
                start,
                length: end - start,
            });
        }
    }
    planned
}

/// Connected components of the classes that the pairs link, in order of
/// their smallest class. Classes without a pair are left out.
fn connected_classes(class_count: usize, pairs: &[(usize, usize)]) -> Vec<Vec<usize>> {
    let mut parent: Vec<usize> = (0..class_count).collect();
    let find = |parent: &mut Vec<usize>, mut class: usize| {
        while parent[class] != class {
            parent[class] = parent[parent[class]];
            class = parent[class];
        }
        class
    };
    for &(left, right) in pairs {
        let (left, right) = (find(&mut parent, left), find(&mut parent, right));
        parent[left.max(right)] = left.min(right);
    }
    let mut linked = vec![false; class_count];
    for &(left, right) in pairs {
        linked[left] = true;
        linked[right] = true;
    }
    let mut components: Vec<Vec<usize>> = Vec::new();
    let mut component_of_root: FxHashMap<usize, usize> = FxHashMap::default();
    for class in (0..class_count).filter(|&class| linked[class]) {
        let root = find(&mut parent, class);
        let index = *component_of_root.entry(root).or_insert_with(|| {
            components.push(Vec::new());
            components.len() - 1
        });
        components[index].push(class);
    }
    components
}

/// Every maximal `[start, end)` window in which the names of two instances
/// map one-to-one.
fn maximal_windows(left: &[Option<&str>], right: &[Option<&str>]) -> Vec<(usize, usize)> {
    let mut windows = Vec::new();
    let mut mapping = Bijection::default();
    let mut last_left: FxHashMap<&str, usize> = FxHashMap::default();
    let mut last_right: FxHashMap<&str, usize> = FxHashMap::default();
    let mut start = 0;
    for position in 0..left.len() {
        let (Some(left_name), Some(right_name)) = (left[position], right[position]) else {
            continue;
        };
        if mapping.accept(left_name, right_name) {
            last_left.insert(left_name, position);
            last_right.insert(right_name, position);
            continue;
        }
        windows.push((start, position));
        let left_conflict = mapping
            .forward
            .get(left_name)
            .is_some_and(|&mapped| mapped != right_name);
        let right_conflict = mapping
            .backward
            .get(right_name)
            .is_some_and(|&mapped| mapped != left_name);
        if left_conflict {
            start = start.max(last_left[left_name] + 1);
        }
        if right_conflict {
            start = start.max(last_right[right_name] + 1);
        }
        mapping.clear();
        last_left.clear();
        last_right.clear();
        for earlier in start..=position {
            if let (Some(left_name), Some(right_name)) = (left[earlier], right[earlier]) {
                mapping.accept(left_name, right_name);
                last_left.insert(left_name, earlier);
                last_right.insert(right_name, earlier);
            }
        }
    }
    windows.push((start, left.len()));
    windows
}

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

    fn names<'a>(tokens: &[&'a str]) -> Vec<Option<&'a str>> {
        tokens
            .iter()
            .map(|&token| (token != "+" && token != "=").then_some(token))
            .collect()
    }

    fn repeated(
        head: &'static str,
        tail: &'static str,
        body: &[&'static str],
    ) -> Vec<Option<&'static str>> {
        let mut tokens = vec![Some(head)];
        tokens.extend((0..30).map(|index| Some(body[index % body.len()])));
        tokens.push(Some(tail));
        tokens.extend((0..30).map(|index| Some(body[(index + 1) % body.len()])));
        tokens
    }

    fn group(instances: &[usize], start: usize, length: usize) -> PlannedGroup {
        PlannedGroup {
            instances: instances.to_vec(),
            start,
            length,
        }
    }

    #[test]
    fn window_after_a_conflict_starts_after_the_conflicting_name() {
        // `a` maps to `x` at 0 and to `z` at 31. `[1, 62)` maps one-to-one.
        let all = [repeated("a", "a", &["b"]), repeated("x", "z", &["y"])];
        let planned = plan_groups(&all, 62, 50);
        assert_eq!(planned, vec![group(&[0, 1], 1, 61)]);
    }

    #[test]
    fn pair_keeps_its_window_when_a_third_instance_breaks_often() {
        let all = [
            repeated("a", "a", &["b"]),
            repeated("x", "z", &["y"]),
            repeated("p", "p", &["q", "r", "q", "s"]),
        ];
        let planned = plan_groups(&all, 62, 50);
        assert!(planned.contains(&group(&[0, 1], 1, 61)), "{planned:?}");
        assert!(
            planned
                .iter()
                .all(|planned| !planned.instances.contains(&2)),
            "{planned:?}"
        );
    }

    #[test]
    fn three_instances_share_one_window() {
        let all = [
            repeated("a", "a", &["b"]),
            repeated("x", "z", &["y"]),
            repeated("p", "r", &["q"]),
        ];
        // The last two instances map one-to-one over the full length.
        assert_eq!(
            plan_groups(&all, 62, 50),
            vec![group(&[1, 2], 0, 62), group(&[0, 1, 2], 1, 61)]
        );
    }

    #[test]
    fn consistent_pair_of_three_instances_keeps_the_full_length() {
        let all = [
            repeated("a", "c", &["b"]),
            repeated("x", "w", &["y"]),
            repeated("p", "p", &["q"]),
        ];
        let planned = plan_groups(&all, 62, 50);
        assert!(planned.contains(&group(&[0, 1], 0, 62)), "{planned:?}");
    }

    #[test]
    fn short_windows_of_three_instances_are_dropped() {
        let all = [
            repeated("a", "a", &["b", "c"]),
            repeated("x", "x", &["y", "y", "z"]),
            repeated("p", "p", &["q", "r", "q", "s"]),
        ];
        assert_eq!(plan_groups(&all, 62, 10), vec![]);
    }

    #[test]
    fn bijection_rejects_two_names_for_one() {
        let mut mapping = Bijection::default();
        assert!(mapping.accept("a", "x"));
        assert!(mapping.accept("b", "y"));
        assert!(mapping.accept("a", "x"));
        assert!(!mapping.accept("c", "x"), "x already stands for a");
        assert!(!mapping.accept("a", "z"), "a already maps to x");
    }

    #[test]
    fn consistent_rename_is_one_class() {
        let reference = names(&["a", "=", "b", "+", "c"]);
        let renamed = names(&["x", "=", "y", "+", "z"]);
        assert_eq!(full_length_classes(&[reference, renamed]), vec![vec![0, 1]]);
    }

    #[test]
    fn inconsistent_rename_gives_maximal_windows() {
        let reference = names(&["a", "=", "b", "+", "c"]);
        let collapsed = names(&["x", "=", "x", "+", "x"]);
        assert_eq!(
            full_length_classes(&[reference.clone(), collapsed.clone()]).len(),
            2
        );
        assert_eq!(
            maximal_windows(&reference, &collapsed),
            vec![(0, 2), (1, 4), (3, 5)]
        );
    }

    #[test]
    fn window_covers_the_full_length_without_conflict() {
        let reference = names(&["a", "+", "a"]);
        let renamed = names(&["b", "+", "b"]);
        assert_eq!(maximal_windows(&reference, &renamed), vec![(0, 3)]);
    }
}