Fix typing of constants
This is an umbrella MR combining a few small changes:
- Fixes typing of constants.
If a typed
Constant
inside of an untypedConstantExpr
was encountered, the typification used to fail (see new test). - Strengthens
test_typify_integer_binops
now that arbitrary expressions can be deferred. - Increases the information content of two error messages.
- Fixes a typo.
Merge request reports
Activity
added Bug label
requested review from @da15siwa
assigned to @he66coqe
Thanks; this actually uncovers a more subtle bug in AST cloning: when cloning a
PsConstantExpr
, its wrapperPsConstant
is not cloned along with it, so both copies refer to the same constant. Then one of them gets typified, which unintentionally carries over to the other. Gonna fix that right away.
I admit that it is very non-obvious. The expression stems from one of our real applications which is how I found it.
It gets frozen to:
PsMul(PsSub(PsMul(Constant(-1: <untyped>), Symbol(PsSymbol(x, None))), Symbol(PsSymbol(y, None))), PsSub(PsMul(Constant(-1: <untyped>), Symbol(PsSymbol(x, None))), Symbol(PsSymbol(y, None))))
Now the typifier runs over this tree as follows (each line is the argument to
visit_expr
):PsMul(PsSub(PsMul(Constant(-1: <untyped>), Symbol(PsSymbol(x, None))), Symbol(PsSymbol(y, None))), PsSub(PsMul(Constant(-1: <untyped>), Symbol(PsSymbol(x, None))), Symbol(PsSymbol(y, None)))) PsSub(PsMul(Constant(-1: <untyped>), Symbol(PsSymbol(x, None))), Symbol(PsSymbol(y, None))) PsMul(Constant(-1: <untyped>), Symbol(PsSymbol(x, None))) Constant(-1: <untyped>) Symbol(PsSymbol(x, None)) Symbol(PsSymbol(y, None)) PsSub(PsMul(Constant(-1.0: const float), Symbol(PsSymbol(x, float))), Symbol(PsSymbol(y, float))) PsMul(Constant(-1.0: const float), Symbol(PsSymbol(x, float))) Constant(-1.0: const float)
Notice how
PsMul(Constant(-1: <untyped>), Symbol(PsSymbol(x, None)))
appears twice in the tree. I suspect that both occurences reference the samePsConstant
Python object. After typing the first expression, thedtype
of bothPsConstant
s changes from<untyped>
toconst float
. However, the secondPsCostantExpr
is not typed, yet. This happens next and the typifier was not able to handle an untypedPsConstantExpr
containing a typedPsConstant
.mentioned in commit d2cfa5a0