Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I was always told rust uses llvm tokens not produceable by c code to do its magic. Was I informed wrong?


You probably misunderstood. C can represent any program's semantics, since it's Turing-complete (modulo finite memory). C can't encode the lifetimes Rust uses, but those get erased during compilation to MIR. This takes MIR from rustc (where borrow checking has been completed and lifetime annotations erased) and outputs C with the same semantics. LLVM doesn't use tokens not produceable by C, but rustc does.


C still has lifetime rules. It just has no syntax for them. What people are bitten with is that they violate the lifetime rules, but their compiler doesn't tell them.


I think it's a reference to certain optimizations possible due to aliasing rules in Rust that are not possible (or maybe only "not straight forward", I'm not sure) in C. So a transpiled program while keeping its semantics might not still compile to equally optimized assembly.


IIRC C can do the same things with correct usage of `restrict`, but that's extremely difficult by hand. So difficult that LLVM's `restrict` support was very buggy when Rust first started using the capabilities. Those bugs got fixed, but it's still impractical to use in handwritten C.


There are multiple stages of IR in the compiler, basically




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: