5 ms·
I thought "contracts" were dropped out of C++20.[1] Entry and exit assertions, and invariants, are powerful, especially when coupled with a proof of correctnes
by Animats 4y ago
I thought "contracts" were dropped out of C++20.[1]
Entry and exit assertions, and invariants, are powerful, especially when coupled with a proof of correctness system. But they're a tough retrofit.
Invariants have some issues. The general idea is that the invariant has to be true when control is outside the object. This was once the core idea of the object concept, although it's been somewhat forgotten. A big issue is, when does control enter and exit the object? What if you call a public member function from inside the object? Did you re-enter? What if you call out of an object to something that calls back in? What about recursion? What if another thread enters? Objects need clarity on the inside/outside issue for invariants to work. This is quite possible but a tough retrofit.
Invariants need syntax for talking about arrays and parts therof. You need quantifiers, or something like them. Lambdas?
From back when I did this sort of thing, decades ago, a simple SAT solver can eliminate the need to check over 90% of assertions at run time. So you want something like that, rather than trying to check everything at run time. Otherwise, nobody will keep the checks turned on.
All this is quite do-able. Most attempts to do it have suffered from academic overreach - the technology is pushed by people in love with formal methods, and the result is too complicated for routine use.
[1] https://www.reddit.com/r/cpp/comments/cmk7ek/what_happened_to_c20_contracts/ https://www.reddit.com/r/cpp/comments/cmk7ek/what_happened_t...
- gavinray 4y ago> I thought "contracts" were dropped out of C++20.[1] You're right, but (I'm not an authority, someone from /r/cpp would be better to speak here probably) I think after the Kona meeting recently they were back on the charter for next iteration Specifically, GCC 13 has had support for Contracts in main since November: https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=ea63396f6b08f88f1cde827e6cab94cd488f7fa7 https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=ea63396f6b08f8... > A big issue is, when does control enter and exit the object? What if you call a public member function from inside the object? Did you re-enter? If you check the generated code from the end of the blogpost (see the bit that shows the Ghidra dissasembly) or the notes section, the pattern used in the plugin is: T function() { check_invariants(); // unless we're in constructor result = function_body(); check_invariants(); // unless we're in a destructor return result; } This does mean that a single member call can cause multiple cascading calls to check_invariants() If "this.foo()" calls "this.bar()" which calls "this.baz()", etc. But it's not avoidable, because in this open-world assumption we assume any member call can potentially mutate the state of the member.
- scatters 4y agoIf you're calling back into user code, you need to reestablish invariants first. As for threading, C++ distinguishes mutating from non mutating (const) methods. Usually, any number of threads can call const methods simultaneously, but only one thread can have access if mutating methods are called. This may require external synchronisation. The optimizer is often capable of inferring when invariants are maintained. One controversy is whether it should be permitted to use contracted invariants as axiomatic.
- nly 4y agoThe compiler can't reliably make assumptions about whether const methods in C++ cause mutation, because both const_cast and aliasing are permitted.
- layer8 4y ago> What if you call a public member function from inside the object? Did you re-enter? It's best to program as if every call to a public function is from client code (i.e. has the same pre/postconditions/invariants). If you need different semantics, it's straightforward to forward to a private or protected function to distinguish the two cases. This is similar to the Non-Virtual Interface (NVI) pattern.
- Animats 4y agoWhen did you leave the object? There should be a check of the invariant as control leaves the object. At what moment does this happen. The F# people struggled with this. In proof systems, you check invariants at exit, and get to assume them true at entrance. So you need to be very clear about entrance and exit.
- layer8 4y agoWhenever you return from a public member function. You might wish for more flexibility, but it's a simple rule that's not difficult to work with.
- Karliss 4y ago> I thought "contracts" were dropped out of C++20. C++ compilers often implement C++ features which haven't been fully standardized yet. Either to prepare for stuff that is very likely to be in the next version, or as experimental features so that people can try them out, find potential issues with proposal, and come to conclusion of what exactly should be standardized. Contracts were not dropped from C++20 because people didn't want the feature. But at least partially because people had very different opinions of what exactly should they do (runtime checks, documentation to library users, optimization annotation giving permission to UB if input requirements aren't satisfied, hint for optional static analyzers).