infer_jrt/support/
comparison.rs1use std::collections::{BTreeMap, BTreeSet};
2
3use ferro_jtype::{ClassInferences, InferredType, IntegralTypeSet, ReferenceType};
4
5pub fn report(label: &str, result: &ClassInferences) {
6 let mut methods = 0;
7 let mut instructions = 0;
8 let mut diagnostics = 0;
9 let mut unknown = 0;
10 let mut conflict = 0;
11 let mut bottom = 0;
12 let mut alternatives = 0;
13 let mut integral_all = 0;
14 let mut expectations = 0;
15 for class in result.classes() {
16 diagnostics += class.diagnostics().len();
17 for method in class.methods() {
18 methods += 1;
19 for instruction in method.instructions() {
20 instructions += 1;
21 expectations += instruction.operand_expectations().len();
22 for value in instruction
23 .stack_before()
24 .iter()
25 .chain(instruction.stack_after())
26 {
27 unknown += usize::from(contains(
28 value,
29 &InferredType::Reference(ReferenceType::Unknown),
30 ));
31 conflict += usize::from(contains(value, &InferredType::Conflict));
32 bottom += usize::from(contains(value, &InferredType::Bottom));
33 alternatives += usize::from(matches!(value, InferredType::Alternatives(_)));
34 integral_all +=
35 usize::from(value.integral_types() == Some(IntegralTypeSet::ALL));
36 }
37 }
38 }
39 }
40 println!(
41 "{label}: classes={}, methods={methods}, instructions={instructions}, complete={}, diagnostics={diagnostics}, stack unknown={unknown}, conflict={conflict}, bottom={bottom}, alternatives={alternatives}",
42 result.len(),
43 result.analysis_complete()
44 );
45 println!(
46 " operand constraints={expectations}, stack values with all integral candidates={integral_all}"
47 );
48}
49
50fn contains(value: &InferredType, needle: &InferredType) -> bool {
51 value == needle
52 || matches!(value, InferredType::Alternatives(values) if values.iter().any(|value| contains(value, needle)))
53}
54
55pub fn compare(left: &ClassInferences, right: &ClassInferences) {
56 let mut changed = 0;
57 let mut stacks = 0;
58 let mut locals = 0;
59 let mut coverage = 0;
60 let mut returns = 0;
61 let mut method_locals = 0;
62 let mut constraints = 0;
63 assert_eq!(
64 left.len(),
65 right.len(),
66 "the same inputs must retain class metadata"
67 );
68 for (class, other) in left.classes().iter().zip(right.classes()) {
69 assert_eq!(class.class_name(), other.class_name());
70 assert_eq!(class.methods().len(), other.methods().len());
71 for (method, other) in class.methods().iter().zip(other.methods()) {
72 assert_eq!(method.name(), other.name());
73 assert_eq!(method.descriptor(), other.descriptor());
74 returns += usize::from(method.inferred_return_type() != other.inferred_return_type());
75 method_locals += usize::from(method.local_types() != other.local_types());
76 let before = method
77 .instructions()
78 .iter()
79 .map(|i| (i.bytecode_offset(), i))
80 .collect::<BTreeMap<_, _>>();
81 let after = other
82 .instructions()
83 .iter()
84 .map(|i| (i.bytecode_offset(), i))
85 .collect::<BTreeMap<_, _>>();
86 let offsets = before
87 .keys()
88 .chain(after.keys())
89 .copied()
90 .collect::<BTreeSet<_>>();
91 let mut method_changes = 0;
92 for offset in offsets {
93 let (Some(instruction), Some(other)) = (before.get(&offset), after.get(&offset))
94 else {
95 coverage += 1;
96 continue;
97 };
98 let local_changed = instruction.local_types() != other.local_types();
99 let stack_changed = instruction.stack_before() != other.stack_before()
100 || instruction.stack_after() != other.stack_after();
101 locals += usize::from(local_changed);
102 stacks += usize::from(stack_changed);
103 constraints +=
104 usize::from(instruction.operand_expectations() != other.operand_expectations());
105 if local_changed || stack_changed {
106 changed += 1;
107 method_changes += 1;
108 }
109 }
110 if method_changes != 0 {
111 println!(
112 " {}.{} {:?}: {method_changes} instruction states differ",
113 class.class_name(),
114 method.name(),
115 method.descriptor()
116 );
117 }
118 }
119 }
120 println!(
121 "Differences: instructions={changed}, stacks={stacks}, locals={locals}, reachability={coverage}, returns={returns}, method-local summaries={method_locals}, operand constraints={constraints}"
122 );
123 println!(
124 "Bottom in unused locals and category-two companion slots is not a missing type. Reference alternatives are retained evidence, not Unknown. Completion is not proof of recovered source types."
125 );
126}