Bugs and Implementation Errors with `CastFunc`
We have observed a number of bugs and implementation errors with CastFunc:
-
CastFunc, as well asTypeAtom, instances, cannot be unpickled becauseTypeAtominitializes its state in__init__, instead of__new__which SymPy requires. - When called with a
sp.Symbol,CastFuncturns itself into aBooleanCastFunc. TheBooleanCastFuncis not very well documented. As I understand it, it should be used when the cast expression will be used as a boolean. If this is the purpose, its implementation is completely wrong. Instead of what it is doing right now, it should check whether the target type is a boolean type, and only then reinterpret itself as a boolean. - The way
CastFuncexposes its assumptions to SymPy is inconsistent with the canonical way to implement these on custom function, see the sympy docs.
There's also some cosmetic and type safety issues:
- The name
CastFuncis quite clunky; why not justcastortype_cast? -
CastFuncallows arbitrary casts, but to improve stability in the code generator, we might want to restrict that, and disallow casts between non-related types (e.g. casting from pointers to integers, things like that) in the typifier.
Together, these issues warrant a profound refactoring of CastFunc.