mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Do not pollute stack when if-expression condition evaluated to False
This commit is contained in:
1 parent
4a108c1ef5
commit
8162b87a00
2 files changed
+31
-1
No files matched your search
@@ -1426,11 +1426,15 @@ impl Compiler {
|
||||
ast::Expression::IfExpression { test, body, orelse } => {
|
||||
let no_label = self.new_label();
|
||||
let end_label = self.new_label();
|
||||
self.compile_test(test, None, Some(no_label), EvalContext::Expression)?;
|
||||
self.compile_test(test, None, None, EvalContext::Expression)?;
|
||||
self.emit(Instruction::JumpIfFalse { target: no_label });
|
||||
// True case
|
||||
self.compile_expression(body)?;
|
||||
self.emit(Instruction::Jump { target: end_label });
|
||||
// False case
|
||||
self.set_label(no_label);
|
||||
self.compile_expression(orelse)?;
|
||||
// End
|
||||
self.set_label(end_label);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
def ret(expression):
|
||||
return expression
|
||||
|
||||
|
||||
assert ret("0" if True else "1") == "0"
|
||||
assert ret("0" if False else "1") == "1"
|
||||
|
||||
assert ret("0" if False else ("1" if True else "2")) == "1"
|
||||
assert ret("0" if False else ("1" if False else "2")) == "2"
|
||||
|
||||
assert ret(("0" if True else "1") if True else "2") == "0"
|
||||
assert ret(("0" if False else "1") if True else "2") == "1"
|
||||
|
||||
a = True
|
||||
b = False
|
||||
assert ret("0" if a or b else "1") == "0"
|
||||
assert ret("0" if a and b else "1") == "1"
|
||||
|
||||
|
||||
def func1():
|
||||
return 0
|
||||
|
||||
def func2():
|
||||
return 20
|
||||
|
||||
assert ret(func1() or func2()) == 20
|
||||
Reference in new issue
Block a user