That may be true over the set of all programs, but for most programs the compiler can trace flow paths well enough to determine that code is unreachable, so similarly can determine that use can occur without initialization. The problem with UB, which is a compiler concept, is that it gives the optimization processes within the compiler freedom to make any consistent assumptions about the undefined variable(s) and then prune code based on the result.
No, this is the crucial point about UB - the compiler can assume that it never occurs. LLVM’s optimizations will assume that UB never happens, and are only correct without the presence of UB (or rather, the program is only correct without the presence of UB).
This isn’t necessarily true (the C code can be compiled with clang), and what matters here is whatever the C standard says is UB. This is complicated and depends on the standard and whether you’re using C or C++.
I don’t know what exactly you mean here. How would you copy the data out of the Vec<T>? As long as you use safe Rust, you cannot cause UB. If you want to use ptr::copy, then AFAIK the source must not contain any padding bytes, as reading those is instant UB.
You can get around this by declaring the padding yourself instead of having the compiler do it (by inserting correctly-sized padding fields between the “real” fields and making the struct #[repr(C)]).
If the compiler knows code is trying to allow an uninitialized read then you have UB even in unsafe block.
From reading a few of the messages; It also might be better of thinking of the compiler as creating machine instructions (rather than in terms of reads.) If something is creating UB the compiler is free to do what it likes. GIGO garbage in garbage out.
unsafe does not increase the set of things that you are permitted to do. Instead it tells the compiler to trust that you are obeying it’s rules, even if the compiler itself is not able to prove to itself that you are complying with those rules.
It’s true that the halting problem means that there are many reads where the compiler cannot know. However, there are a great many cases where it can, particularly once inlining gets involved. And just because the compiler doesn’t know today doesn’t make it sound – the terrible thing about UB is that it often seems to work initially, then breaks later when you change something unrelated.