The key change is that we now treat F_type_error like F_dispatch_error. This entails replicating the arguments of a function to F_type_error in case the type error replaces the function call in wrapper functions. That way, there will be no reference-counting induced elimination of global objects which tridggered this bug.
The initial change relates to the actual wrapper code generation in new_types.c. Once this was done, we needed to make sure such type-error arguments survive subsequent type inferences, more precisely: subsequent calls to eliminate-bottom-types. Finally, the reference counting had to learn that F_type_error like F_dispatch_error conceptually consumes all its arguments.
Finally, I added a test case.
Fixes #2294 (closed)