Skip to content

Commit ac91219

Browse files
fix(comprehensions): Push None for void elements to avoid stack underflow.
1 parent b4c7d8e commit ac91219

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

compiler/src/modules/parser/literals.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
156156
};
157157
self.chunk.instructions.push(Instruction { opcode: ins.opcode, operand });
158158
}
159+
// Void element (`print(...)` -> CallPrint) leaves no value; push None so append has one.
160+
if matches!(body.last().map(|i| i.opcode), Some(OpCode::CallPrint)) {
161+
self.emit_const(Value::None);
162+
}
159163
}
160164
self.chunk.emit(append_op, 0);
161165

compiler/tests/cases/vm.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
{"src": "y = 100\nprint([x + y for x in range(3)])", "output": ["[100, 101, 102]"]},
192192
{"src": "print([len(s) for s in ['a', 'bb', 'ccc']])", "output": ["[1, 2, 3]"]},
193193
{"src": "print([x for x in []])", "output": ["[]"]},
194+
{"src": "print([print(x) for x in range(2)])", "output": ["0", "1", "[None, None]"]},
194195
{"src": "print(sum([x for x in range(101)]))", "output": ["5050"]},
195196
{"src": "print({k: k * k for k in range(4)})", "output": ["{0: 0, 1: 1, 2: 4, 3: 9}"]},
196197
{"src": "pairs = [(1, 'a'), (2, 'b'), (3, 'c')]\nprint({k: v for k, v in pairs})", "output": ["{1: 'a', 2: 'b', 3: 'c'}"]},

0 commit comments

Comments
 (0)