Consider the following C# snippet:
Object bad= null;
This is valid C#. It will raise a
NullReferenceException which can be caught and handled. On the other hand, the most obvious equivalent code in C or C++ (dereferencing a null reference or pointer) corresponds not to the defined behavior of throwing an exception, but to undefined behavior.
How, generally, are such situations handled when transpiling code with defined behavior in one language into another language in which a close literal translation of the code would result in undefined behavior?
The following seem like reasonable options:
- Refuse to translate.
- Generate code to raise an exception, if the target language has exception handling structures.
- Substitute some type of zero behavior, such as performing a
0or an empty string as applicable.
- Do anything - the behavior of the transpiler is undefined. The transpiled code in the target language might erase the developer's hard disk, spam naughty pictures all over the Stack Exchange Network, hack the Pentagon, or raise the dead.
Is there a standard practice?
A similar situation could happen when transpiling from a language in which integer overflow results in odometer wraparound (and thus might be a common practice or shortcut) into a language in which integer overflow is undefined behavior. Would a transpiler be expected to implement odometer wraparound checks and handling into the transpiled code, or would raising an error or terminating the program on wraparound be acceptable?