[issue42899] Is it legal to eliminate tests of a value, when that test has no effect on control flow?

Mark Shannon report at bugs.python.org
Tue Feb 2 07:08:18 EST 2021


Mark Shannon <mark at hotpy.org> added the comment:

Option 3 with what semantics exactly?

https://docs.python.org/3/reference/datamodel.html#object.__bool__ says that __bool__ should return True or False.


If we don't allow the optimizer the freedom to assume that __bool__ is self-consistent and has no side effects, then we need to define what happens for stuff like:

class FlipFlop:
    def __init__(self):
        self.val = random.choice((True, False))
    def __bool__(self):
        self.val = not self.val
        return self.val

Saying that only the second test can be removed is hard to define in a way that we can reliably implement.
For example, it makes the simplification of `x = True and y` to `x = y` problematic.
We routinely remove any tests of `if True:` or `while True:`, but the removal of `if True:` and the simplification of `x = True and y` to `x = y` is basically the same thing in the CFG.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42899>
_______________________________________


More information about the Python-bugs-list mailing list