Hacker News new | ask | show | jobs
by leni536 10 days ago
Contracts have nothing to do with memory safety.
1 comments

Contracts have plenty to do with memory safety; the notion of 'hardened implementations' and bound-checked vector access ( 2 enormously popular features) rely on the contracts model.
Contracts as originally designed by the committee were explicitly not meant to enforce memory safety in that way. You were not supposed to be able to rely on the existence of any particular contract check at runtime. This recent change was something that the committee had rejected earlier, and probably begrudgingly accepted this time around as a compromise to try to ensure the NB vote passes

The two features you list don't have to depend on contracts, and in fact didn't do so at first. The dependency was only added later, and there were efforts to undo such a change right to the end.

Furthermore, IIRC hardening only relies on the contracts system to report precondition violations. The actual checks are intentionally not contract assertions and are intentionally not controlled via the same system as other contract assertions (i.e., hardened checks remain even if you use "ignore" as your contract evaluation semantic).

The reason for this is that C++ has, by design, avoided the use of runtime checks. But I think this is ill advised from now on, as we need to enforce safety behaviors beyond what the type system can do.
Assertions were always an option. Unless I'm missing some new development, contracts are just a new way to spell assertions. That's good, but it doesn't move the needle at all in terms of memory safety.
making memory safe patterns easier to implement makes for more memory safety:)