()
| 150 | |
| 151 | #[test] |
| 152 | fn test_if_nil() { |
| 153 | let mut chunk = Chunk::new(); |
| 154 | // if nil { print("bad"); } else { print("nil"); } |
| 155 | chunk.write_code(OpCode::Nil, 1); // condition |
| 156 | chunk.write_code(OpCode::JumpPopIfFalse(4), 1); // skip true branch |
| 157 | chunk.write_code(OpCode::GetGlobal(0), 1); // true: print |
| 158 | chunk.write_code(OpCode::Constant(1), 1); // "bad" |
| 159 | chunk.write_code( |
| 160 | OpCode::Call { |
| 161 | positional_count: 1, |
| 162 | keyword_count: 0, |
| 163 | }, |
| 164 | 1, |
| 165 | ); // print("bad") |
| 166 | chunk.write_code(OpCode::Pop(1), 1); // cleanup |
| 167 | chunk.write_code(OpCode::Jump(1), 1); // skip else |
| 168 | chunk.write_code(OpCode::Pop(1), 1); // else: cleanup |
| 169 | chunk.write_code(OpCode::Nil, 1); // else: return nil |
| 170 | chunk.write_code(OpCode::Return, 1); // return |
| 171 | |
| 172 | let optimizer = DeadCodeEliminator; |
| 173 | assert!(!optimizer.optimize(&mut chunk)); // Should not optimize conditional code |
| 174 | assert_eq!(chunk.code.len(), 10); // Everything should be preserved |
| 175 | } |
| 176 | |
| 177 | // #[test] |
| 178 | // fn test_unconditional_dead_code() { |
nothing calls this directly
no test coverage detected