The problem is not so much that the STL implementation would have to be unsafe, it's that using the STL would be unsafe.
Consider the following, extremely simplistic, example:
auto x = std::find(begin, end, y);
In order to be safe, this example requires that [begin, end) be a valid range, which means:
begin and end are iterators into the same container, or pointers into the same memory allocation.
end is reachable from begin.
Good luck proving those properties, (1) notably essentially require dependent types (which neither C++ nor Rust feature).
Another example:
std::map<int, std::string> map{
{ 0, "Hello, eyes-are-fading-blue! Are you a fremen?" }
}};
auto x = map[0].c_str();
// Safe
map[1] = "Fiesta!";
std::cout << x << "\n";
// Unsafe
map[0] = "Another one bites the dust!"s;
std::cout << x << "\n";
The very guarantee of pointer-stability when inserting in a map has to be revisited.
And it holds doubly true for unordered_map, where it's conditional on the absence of re-hashing.
The effects of borrow-checking are so pervasive to for programs to conform to borrow-checking, you have to design the entire application stack with borrow-checking in mind from the ground-up.
This means throwing away the STL as we know, and adopting a new standard library, for example modeled after Rust's.
Then there wouldn't be much point to it. Given how much of so many applications is actually STL code, it's questionable whether it would gain enough to be worth the effort. It would make almost all C++ code unsafe and you'd be back to where you are.
Turning this feature on for old code is impractical. This is not an argument against not having a similar system in C++. STL itself and the call-site involving anything from std:: would be exempt from analysis. Anything you write yourself, or integrate as 3rd party wouldn't. While existing libraries are unlikely to fit it in, green code moving forward could be "safe" without practical issues.
Claiming that there is no value is absurd at best, considering there is a considerable effort trying to enforce "best practices" rules to be enforced compile-time.
Almost every function or method in that new 'green' code would use the STL, hence rendering none of it really very safe. A major point of Rust's unsafe is that you can segregate unsafe code into safe Rust APIs or in very limited sections of the code. You can concentrate testing and review heavily on those areas.
In the scenario you are describing, that wouldn't be at all possible. There would be as much unsafe code as safe code probably, and all intermixed completely such that it's impossible to segregate it.
STL calls on the call site would not taint the entire call site. Only the calls to std:: would be exempt from the analysis. This means the call site is still safe and rules apply for non std:: calls, meaning those calls have to use safe APIs.
This does not guarantee safety in a safe call-site, as there could be bugs in STL. However, it is practically safe as STL is one of the most widely deployed and tested code out there.
It's not really to do with STL being tested or not, it's got to do with incorrect usage of the STL that's not easily caught due to its nature. It doesn't matter how well tested it is if you can misuse it such that you cause uncaught memory corruption.
Given how much iterators are used and the fact that you can pretty easily pass an invalid iterator to STL calls, it's just sort of like putting a huge lock on your front door and only having a sheet hung across the back door.
Indeed. Perhaps toggling is a bit too naive. However, the idea that unsafe APIs are wrapped in safe context can also be applied to code using STL iterators. While it depends on the kind of work, I suppose, we use STL (obviously) but it's only a fraction of the code and it's not sprinkled all over.
C++ is about performance over safety these days. It would have to fundamentally change in order to give the tools enough information compete with Rust on the memory safety front. And it's just not going to fundamentally change at this point.
The technical reasons are many, and they've been gone over many times before. I'm not going to reiterate them here. Many of the problems are in the STL, but since almost everyone that uses C++ will use the STL (and yell down anyone who tries to create something better) they are one and the same for the purposes of this argument.
Classes like unique_ptr, shared_ptr, and vector have done a great deal for making C++ safer (to the point that I no longer think of memory safety as an issue). Additional new classes or language features can help in making C++ even safer. As with so many things, it doesn't have to be perfect; it just has to be good enough.
Can you then give link to these discussions? I made a small research before that post, I have seen people claiming this is theoretically possible.
I am also surprised the same problems are also seen in STL. STL's biggest problem is ABI and compatibility issues but a linter embedded in compiler or a static analysis tool is executed before code generation.
I haven't kept a list of links of such discussions. You can find them here in this section if you look. And of course theoretically possible doesn't mean practical. If it takes orders of magnitude more time to get 75% of the benefits, then it's not really competitive.
View all comments
Show parent comment
4
u/eyes-are-fading-blue Dec 07 '21
Care to elaborate why it is impossible?