Deprecate uninitialized in favor of a new MaybeUninit type by canndrew · Pull Request #1892 · rust-lang/rfcs (original) (raw)
honestly I still do not understand why creating &mut to uninitialized memory is automatically undefined behaviour and not reading from it
Well, there are other people saying the exact opposite. ;) We have to make a choice either way, but for both cases we have people arguing that this is clearly the more intuitive option.
I don't see how we could allow &T to point to uninitialized memory without making a pretty big backwards incompatible language change: all code that expects a &T and doesn't guard against T potentially being uninitialized would be at risk at best. Could it become broken or incorrect? No idea.
No, there would be no incompatible change. As @ubsan mentioned, we have two invariants at play here: Which assumptions the compiler can make for its optimizations, and which assumptions safe code can make for values it sees. There is no a priori reason to think these are the same, and in fact I think it is rather impossible to make them the same. I have a blogpost upcoming for this that should hopefully be done no later than Monday...
But just one example: A safe higher-order function which takes an argument f: fn(&i32) -> &i32
can assume that f
can be called with any shared reference. So following your "violating safe code assumptions is insta-UB", it would be UB to have a function which does not have this property. On the other hand, many libraries have private functions not marked unsafe
that actually are de-facto unsafe
because they make extra assumptions that are guaranteed by the surrounding module. So your proposal makes all those libraries UB.
There are other problems as well. For example, the invariant that may be assumed by safe code is impossible to check for because it is frequently not computable (as in, would require solving the halting problem). I think we should have a definition of UB that can, at least in principle, be checked.