Yes, I would strongly suggest that. And let's be real. Rust doesn't even have a reliable picture of its abstract machine now. ^^
Okay. Well take for example the skeleton support for Rust that was just added to the Linux kernel. Let me ask you something. Do you think that every mutable pointer passed into Rust code there will be unaliased? Or is it C, and it's going to have pointers to that chunk flying around everywhere? Even if they go through manually and ensure that none of them alias, some dude is going to introduce a commit on the C side that breaks this, and it will be a subtle bug that will torment users for a year and a half until someone figures it out. Do you expect them to use raw pointers for everything in kernel drivers? If so, most of the point of Rust is lost.
I'm sorry, but I disagree. It can and should. Let's step for a moment back into the void, back into C-land.
Say you compile some library with aggressive strict aliasing optimizations, *restrict
pointers are everywhere, but your code uses -fno-strict-aliasing.
You have a sea of objects with properties and data that can be pointing almost anywhere. Because you're not watching the control flow (it's just too complicated, who knows where that pointer came from?), you don't notice that you're passing the same pointer twice, to a function that takes only 2*restrict
pointers.
What do you think will happen? Nothing good. Maybe nothing, depending on the code. Maybe a subtle bug that tortures you for months.
It's the same here. Unsafe code already comes with a responsibility and an intrinsic risk. You can blow your whole leg off. If you're passing aliased &mut pointers into Rust crates, you deserve whatever you get.
But there's another, rather easy solution here that you're not considering.
The problem stems from crates interacting badly with different aliasing rules.
So, just patch cargo too. Require an explicit option in Cargo.toml, only valid for the top-level crate (error out if it's only detected in a dependency), and which enables -fno-strict-aliasing on every dependency for that crate, whether they want it or not. Problem solved!
Oh, you're worried about std
and core
? If it's a problem, maybe just unconditionally build them with -fno-strict-aliasing. I doubt any performance lost there will be enough to really care about. Or, or, just have cargo download and build std and core like every other crate if that option is detected. There's probably other solutions there.
What I'm trying to do is remove undefined behavior in unsafe code. I'm not trying to create more UB or confusion or bugs, I'm asking for a compiler switch that will prevent them. That is what Rust's real purpose is.
What's the point of the borrow checker? To prevent accidental human error. What's the point of this flag? To prevent accidental human error, by disabling the rule that makes it an error in the first place.