pub struct MethodInference { /* private fields */ }Expand description
Type-inference results for one method.
Parameter and return types come from the method descriptor. Local-variable and instruction states are inferred from the method’s bytecode.
Implementations§
Source§impl MethodInference
impl MethodInference
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the method name as stored in the class file.
Examples found in repository?
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}More examples
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}Sourcepub const fn descriptor(&self) -> &MethodDescriptor
pub const fn descriptor(&self) -> &MethodDescriptor
Returns the parsed JVM method descriptor.
Examples found in repository?
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}More examples
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}Sourcepub const fn generic_signature(&self) -> Option<&GenericSignature>
pub const fn generic_signature(&self) -> Option<&GenericSignature>
Returns the method’s generic Signature attribute, when present.
Sourcepub const fn analysis_complete(&self) -> bool
pub const fn analysis_complete(&self) -> bool
Returns whether analysis reached a fixed point within configured limits.
Examples found in repository?
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}Sourcepub fn parameter_types(&self) -> &[InferredType]
pub fn parameter_types(&self) -> &[InferredType]
Returns the descriptor-derived parameter types in declaration order.
Sourcepub const fn return_type(&self) -> &ReturnType
pub const fn return_type(&self) -> &ReturnType
Returns the descriptor-derived method return type.
Examples found in repository?
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}Sourcepub const fn inferred_return_type(&self) -> Option<&InferredType>
pub const fn inferred_return_type(&self) -> Option<&InferredType>
Returns the inferred value type observed at reachable value-return instructions.
This is more precise than Self::return_type when a method declares a
broad reference type but consistently returns a narrower one. None
means the method is void, did not reach a value return, or did not reach
a complete, descriptor-compatible fixed point.
Examples found in repository?
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}More examples
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}Sourcepub const fn returned_parameter_index(&self) -> Option<usize>
pub const fn returned_parameter_index(&self) -> Option<usize>
Returns the parameter index returned unchanged by every reachable value return.
This enables direct-call inference to use an argument’s actual type. The
index is in declaration order and excludes an instance method’s implicit
this receiver. None means no single parameter source was proven.
Examples found in repository?
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}Sourcepub fn local_types(&self) -> &[InferredType]
pub fn local_types(&self) -> &[InferredType]
Returns inferred local-variable types indexed by JVM local slot.
Aggregates the types observed across the method, including separate lifetimes of a reused slot. Distinct reference candidates are retained rather than collapsed to a hierarchy-derived common supertype here.
Examples found in repository?
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}More examples
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}Sourcepub fn instructions(&self) -> &[InstructionInference]
pub fn instructions(&self) -> &[InstructionInference]
Returns type states for modeled bytecode instructions.
Examples found in repository?
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}More examples
5fn main() -> ExitCode {
6 let mut arguments = env::args_os().skip(1);
7 let Some(path) = arguments.next() else {
8 eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9 return ExitCode::FAILURE;
10 };
11
12 let mut hierarchy = ClassHierarchy::new();
13 while let Some(option) = arguments.next() {
14 if option != "--reference" {
15 eprintln!("unknown option: {}", option.to_string_lossy());
16 return ExitCode::FAILURE;
17 }
18 let Some(reference) = arguments.next() else {
19 eprintln!("--reference requires a JAR path");
20 return ExitCode::FAILURE;
21 };
22 if let Err(error) = hierarchy.insert_jar_file(&reference) {
23 eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24 return ExitCode::FAILURE;
25 }
26 }
27 let mut config = InferenceConfig::default();
28 if !hierarchy.is_empty() {
29 config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30 }
31 let inferer = match Inferer::new(config) {
32 Ok(inferer) => inferer,
33 Err(error) => {
34 eprintln!("failed to configure inference: {error}");
35 return ExitCode::FAILURE;
36 }
37 };
38
39 let path = Path::new(&path);
40 let bytes = match fs::read(path) {
41 Ok(bytes) => bytes,
42 Err(error) => {
43 eprintln!("failed to read {}: {error}", path.display());
44 return ExitCode::FAILURE;
45 }
46 };
47
48 let inference = match inferer.infer_class(&bytes) {
49 Ok(inference) => inference,
50 Err(error) => {
51 eprintln!("failed to infer {}: {error}", path.display());
52 return ExitCode::FAILURE;
53 }
54 };
55
56 println!("class: {}", inference.class_name().as_str());
57 println!("analysis complete: {}", inference.analysis_complete());
58
59 for method in inference.methods() {
60 println!("\nmethod: {}", method.name());
61 println!(" descriptor: {:?}", method.descriptor());
62 println!(" declared return type: {:?}", method.return_type());
63 println!(
64 " inferred return type: {:?}",
65 method.inferred_return_type()
66 );
67 println!(
68 " returned parameter index: {:?}",
69 method.returned_parameter_index()
70 );
71 println!(" analysis complete: {}", method.analysis_complete());
72 println!(" local types:");
73
74 for (slot, inferred_type) in method.local_types().iter().enumerate() {
75 println!(" {slot}: {inferred_type:?}");
76 }
77
78 for instruction in method.instructions() {
79 if instruction.operand_expectations().is_empty() {
80 continue;
81 }
82 println!(
83 " operand expectations at {}:",
84 instruction.bytecode_offset()
85 );
86 for expectation in instruction.operand_expectations() {
87 println!(
88 " stack[{}]: {:?}",
89 expectation.stack_index(),
90 expectation.constraint()
91 );
92 }
93 }
94 }
95
96 if inference.diagnostics().is_empty() {
97 println!("\ndiagnostics: none");
98 } else {
99 println!("\ndiagnostics:");
100 for diagnostic in inference.diagnostics() {
101 let location = diagnostic.location();
102 println!(
103 " {:?} {:?}: {}",
104 diagnostic.severity(),
105 diagnostic.kind(),
106 diagnostic.message()
107 );
108
109 if let Some(method_name) = location.method_name() {
110 println!(
111 " method: {}{}",
112 method_name,
113 location.method_descriptor().unwrap_or_default()
114 );
115 }
116 if let Some(offset) = location.bytecode_offset() {
117 println!(" bytecode offset: {offset}");
118 }
119 }
120 }
121
122 ExitCode::SUCCESS
123}