4 ms·
Writing unsafe Rust is actually much harder than writing C or C++. C and C++ compilers apply certain optimizations quite conservatively (like anything involving
by kccqzy 5y ago
Writing unsafe Rust is actually much harder than writing C or C++. C and C++ compilers apply certain optimizations quite conservatively (like anything involving aliasing) because the language doesn't assume a bunch of things like two mutable references never alias. Rust is a lot more aggressive.
If I was asked to rank them in terms of number of things you need to know to write fully correct (no UB) code, I would have: safe Rust < C++ < unsafe Rust
Here's a nice blog post that really resonated with me: https://lucumr.pocoo.org/2022/1/30/unsafe-rust/ https://lucumr.pocoo.org/2022/1/30/unsafe-rust/
- deleted 5y ago[deleted]
- Filligree 5y agoDoes Rust apply aliasing optimisations now? Last I heard of that, they kept having to turn it back off because of LLVM bugs concerning aliasing.
- kzrdude 5y agoRust does not use type-based alias analysis (the "strict aliasing" problem), however.
- coolreader18 5y agoIn any case, it's still UB under the language guidelines, it's just not exploited yet.
- oconnor663 5y agoI believe since the last time they were turned on they have stayed on, yes.
- pcwalton 5y agoRust (and, for that matter, C++ compilers) have always applied optimizations that rely on alias analysis. What I assume this thread is referring to is the noalias LLVM attribute on &mut function parameters. Rust now applies this.
- oconnor663 5y agoOk I was going to try to be pedantic and say that if you could only use unsafe pointer operations, the aliasing issues don't apply. But the first example I tried to put together failed Miri, cause I was creating temporary &mut references to cast to *mut. So yeah...egg on face. Edited.