3 ms·
> should be a trap representation In a new, green field programming language design: maybe. You will have FFI interop issues: what if some foreign gives you s
by kazinator 3y ago
> should be a trap representation
In a new, green field programming language design: maybe.
You will have FFI interop issues: what if some foreign gives you such a value, which is correct in its domain. It will have to be specially handled and widened/promoted to a type that can hold it. It looks like friction, though.
In an existing language, like C? Making currently correct programs blow up with a trap exception is a nonstarter.
I think there is no hardware support for treating 0x80... as a trap, so if you actually want it to trap, rather than leaving it at undefined behavior, your compiler has to emit check code, at least in code paths where it is not obvious whether the value is or isn't a trap.
Also, it would be strangely inconsistent for the 0x80... to predictably trap wherever it occurs, but arithmetic overflow to have undefined consequences. The two want to be tied together.
- owlbite 3y agoAlso bitfields / any application using the hi bit as a flag for something or other is going to break now.
- kelnos 3y ago> You will have FFI interop issues: what if some foreign gives you such a value, which is correct in its domain. It will have to be specially handled and widened/promoted to a type that can hold it. That's more or less what you have to do in Java if your FFI is passing you an unsigned type. You need to use a 64-bit signed long in Java to hold a uint32_t, and a BigInteger to hold a uint64_t. I mean, you can certainly store that unsigned 32-bit int from the FFI call in a 32-bit signed Java int, but then you have to use special methods on the Integer class to do unsigned comparisons and arithmetic. Agreed, this is a lot of friction, but there's long-standing precedent, at least.
- dkjaudyeqooe 3y agoThis is a simplistic view. I implement nil/null values everywhere including integers and at the external border there is a test for that special value. It's not like you're using this sort of thing for just another number so it's natural that there is an adaption.
- UncleMeat 3y agoNull is a valid value for pointers, but it is absolutely ordinary and normal for functions to refuse to accept null on the pain of crashing and burning.
- jcalvinowens 3y agoNo it isn't. In my experience, there are few higher quality signals a codebase is utter garbage than having `if (arg == NULL)` checks at the top of every function... Almost nothing in libc does that, it is entirely unnecessary. I realize that wasn't your point, but this is an enormous pet peeve of mine...
- UncleMeat 3y agoA dereference is an implicit "I will crash and burn if you hand me null" contract.
- jcalvinowens 3y agoYes, exactly. Nobody should be surprised that calling `strlen(NULL)` crashes. Checking for NULL at the top of strlen() would be stupid, which is why it doesn't do that. I've seen a lot of so-called "enterprise" code does that, and it's absolutely idiotic.
- UncleMeat 3y agoOkay. But that's totally unrelated to my original point, which is that we are already very used to functions that do not accept the full domain of a given type. "I don't accept nullptr" is not too fundamentally different from "I don't accept "0x8000..."
- kazinator 3y ago"I don't accept {nullptr, 0x8000}" is fundamentally different from "{nullptr, 0x8000} is a trap representation". If it's a trap representation, you can't even pass it to me so that I could accept or reject it.