|
| 1 | +//! Return statement handling |
| 2 | +//! |
| 3 | +//! Processes `return expr` by connecting the expression's vertex |
| 4 | +//! to the enclosing method's merge vertex. |
| 5 | +
|
| 6 | +use crate::env::{GlobalEnv, LocalEnv}; |
| 7 | +use crate::graph::{ChangeSet, VertexId}; |
| 8 | + |
| 9 | +use super::install::install_node; |
| 10 | + |
| 11 | +/// Process ReturnNode: connect return value to method's merge vertex |
| 12 | +pub(crate) fn process_return_node( |
| 13 | + genv: &mut GlobalEnv, |
| 14 | + lenv: &mut LocalEnv, |
| 15 | + changes: &mut ChangeSet, |
| 16 | + source: &str, |
| 17 | + return_node: &ruby_prism::ReturnNode, |
| 18 | +) -> Option<VertexId> { |
| 19 | + // Process return value (first argument only; multi-value return not yet supported) |
| 20 | + let value_vtx = if let Some(arguments) = return_node.arguments() { |
| 21 | + arguments |
| 22 | + .arguments() |
| 23 | + .iter() |
| 24 | + .next() |
| 25 | + .and_then(|arg| install_node(genv, lenv, changes, source, &arg)) |
| 26 | + } else { |
| 27 | + // `return` without value → nil |
| 28 | + Some(genv.new_source(crate::types::Type::Nil)) |
| 29 | + }; |
| 30 | + |
| 31 | + // Connect return value to method's merge vertex |
| 32 | + if let Some(vtx) = value_vtx { |
| 33 | + if let Some(merge_vtx) = genv.scope_manager.current_method_return_vertex() { |
| 34 | + genv.add_edge(vtx, merge_vtx); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + None |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +mod tests { |
| 43 | + use crate::env::{GlobalEnv, LocalEnv}; |
| 44 | + use crate::graph::ChangeSet; |
| 45 | + use crate::parser::ParseSession; |
| 46 | + use crate::types::Type; |
| 47 | + |
| 48 | + fn setup_and_infer(source: &str) -> GlobalEnv { |
| 49 | + let session = ParseSession::new(); |
| 50 | + let parse_result = session.parse_source(source, "test.rb").unwrap(); |
| 51 | + let root = parse_result.node(); |
| 52 | + let program = root.as_program_node().unwrap(); |
| 53 | + |
| 54 | + let mut genv = GlobalEnv::new(); |
| 55 | + let mut lenv = LocalEnv::new(); |
| 56 | + let mut changes = ChangeSet::new(); |
| 57 | + |
| 58 | + for stmt in &program.statements().body() { |
| 59 | + crate::analyzer::install::install_node( |
| 60 | + &mut genv, &mut lenv, &mut changes, source, &stmt, |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + genv.apply_changes(changes); |
| 65 | + genv.run_all(); |
| 66 | + genv |
| 67 | + } |
| 68 | + |
| 69 | + fn get_return_type(genv: &GlobalEnv, class_name: &str, method_name: &str) -> String { |
| 70 | + let info = genv |
| 71 | + .resolve_method(&Type::instance(class_name), method_name) |
| 72 | + .unwrap_or_else(|| panic!("{}#{} should be registered", class_name, method_name)); |
| 73 | + let vtx = info |
| 74 | + .return_vertex |
| 75 | + .expect("return_vertex should be Some"); |
| 76 | + |
| 77 | + if let Some(source) = genv.get_source(vtx) { |
| 78 | + source.ty.show() |
| 79 | + } else if let Some(vertex) = genv.get_vertex(vtx) { |
| 80 | + vertex.show() |
| 81 | + } else { |
| 82 | + panic!("return_vertex not found"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_simple_return() { |
| 88 | + let source = r#" |
| 89 | +class Foo |
| 90 | + def bar |
| 91 | + return "hello" |
| 92 | + end |
| 93 | +end |
| 94 | +"#; |
| 95 | + let genv = setup_and_infer(source); |
| 96 | + assert_eq!(get_return_type(&genv, "Foo", "bar"), "String"); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_return_with_implicit_return_union() { |
| 101 | + let source = r#" |
| 102 | +class Foo |
| 103 | + def bar |
| 104 | + return "hello" if true |
| 105 | + 42 |
| 106 | + end |
| 107 | +end |
| 108 | +"#; |
| 109 | + let genv = setup_and_infer(source); |
| 110 | + let ty = get_return_type(&genv, "Foo", "bar"); |
| 111 | + assert!(ty.contains("Integer"), "should contain Integer, got: {}", ty); |
| 112 | + assert!(ty.contains("String"), "should contain String, got: {}", ty); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn test_multiple_returns() { |
| 117 | + let source = r#" |
| 118 | +class Foo |
| 119 | + def bar |
| 120 | + return "a" if true |
| 121 | + return :b if false |
| 122 | + 42 |
| 123 | + end |
| 124 | +end |
| 125 | +"#; |
| 126 | + let genv = setup_and_infer(source); |
| 127 | + let ty = get_return_type(&genv, "Foo", "bar"); |
| 128 | + assert!(ty.contains("Integer"), "should contain Integer, got: {}", ty); |
| 129 | + assert!(ty.contains("String"), "should contain String, got: {}", ty); |
| 130 | + assert!(ty.contains("Symbol"), "should contain Symbol, got: {}", ty); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_return_without_value() { |
| 135 | + let source = r#" |
| 136 | +class Foo |
| 137 | + def bar |
| 138 | + return if true |
| 139 | + 42 |
| 140 | + end |
| 141 | +end |
| 142 | +"#; |
| 143 | + let genv = setup_and_infer(source); |
| 144 | + let ty = get_return_type(&genv, "Foo", "bar"); |
| 145 | + assert!(ty.contains("Integer"), "should contain Integer, got: {}", ty); |
| 146 | + assert!(ty.contains("nil"), "should contain nil, got: {}", ty); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn test_no_return_backward_compat() { |
| 151 | + let source = r#" |
| 152 | +class Foo |
| 153 | + def bar |
| 154 | + "hello" |
| 155 | + end |
| 156 | +end |
| 157 | +"#; |
| 158 | + let genv = setup_and_infer(source); |
| 159 | + assert_eq!(get_return_type(&genv, "Foo", "bar"), "String"); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_return_only_method() { |
| 164 | + let source = r#" |
| 165 | +class Foo |
| 166 | + def bar |
| 167 | + return "hello" |
| 168 | + end |
| 169 | +end |
| 170 | +"#; |
| 171 | + let genv = setup_and_infer(source); |
| 172 | + assert_eq!(get_return_type(&genv, "Foo", "bar"), "String"); |
| 173 | + } |
| 174 | + |
| 175 | + #[test] |
| 176 | + fn test_return_dead_code_over_approximation() { |
| 177 | + let source = r#" |
| 178 | +class Foo |
| 179 | + def bar |
| 180 | + return "hello" |
| 181 | + 42 |
| 182 | + end |
| 183 | +end |
| 184 | +"#; |
| 185 | + let genv = setup_and_infer(source); |
| 186 | + let ty = get_return_type(&genv, "Foo", "bar"); |
| 187 | + // Dead code after return is still processed (over-approximation) |
| 188 | + assert!(ty.contains("Integer"), "should contain Integer (dead code), got: {}", ty); |
| 189 | + assert!(ty.contains("String"), "should contain String, got: {}", ty); |
| 190 | + } |
| 191 | +} |
0 commit comments