At first I read it as C to rust compiler. What is the point of compiling rust to C?
I think there are probably C compilers for more platforms than there are rust compilers. So, if you want to compile your rust project on some obscure platform that doesn’t have a rust compiler for it yet, you could compile to C and then compile the resulting C code for that platform? Just a guess.
Rust to C compiler – 95.9% test pass rate, odd platforms
61–70 of 264 posts
Re: Rust to C compiler – 95.9% test pass rate, odd platforms
#62At first I read it as C to rust compiler. What is the point of compiling rust to C?
I think there are probably C compilers for more platforms than there are rust compilers. So, if you want to compile your rust project on some obscure platform that doesn’t have a rust compiler for it yet, you could compile to C and then compile the resulting C code for that platform? Just a guess.
Re: Rust to C compiler – 95.9% test pass rate, odd platforms
#63Earlier quoted context omitted.
Machine code is generally much safer than C - e.g. it usually lacks undefined behaviour. If you're unsure about how a given piece of machine code behaves, it's usually sufficient to test it empirically.
Not any different from C - a given C compiler + platform will behave completetly deterministically and you can test the output and see what it does, regardless of UB or not.
Sure[1], but that doesn't mean it's safe to publish that C code - the next version of that same compiler on that same platform might do something very different. With machine code (especially x86, with its very friendly memory model) that's unlikely.
(There are cases like unused instructions becoming used in never revisions of a processor - but you wouldn't be using those unused instructions in the first place. Whereas it's extremely common to have C code that looks like it's doing something useful, and is doing that useful thing when compiled with a particular compiler, but is nevertheless undefined behaviour that will do something different in a future version)
[1] Build nondeterminism does exist, but it's not my main concern
Re: Rust to C compiler – 95.9% test pass rate, odd platforms
#64Earlier quoted context omitted.
Not any different from C - a given C compiler + platform will behave completetly deterministically and you can test the output and see what it does, regardless of UB or not.
> a given C compiler + platform will behave completetly deterministically and you can test the output and see what it does, regardless of UB or not. Sure[1], but that doesn't mean it's safe to publish that C code - the next version of that same compiler on that same platform might do something very different. With machine code (especially x86, with its very friendly memory model) that's unlikely. (There are cases lik…
Re: Rust to C compiler – 95.9% test pass rate, odd platforms
#65Earlier quoted context omitted.
Not any different from C - a given C compiler + platform will behave completetly deterministically and you can test the output and see what it does, regardless of UB or not.
> a given C compiler + platform will behave completetly deterministically and you can test the output and see what it does, regardless of UB or not. Sure[1], but that doesn't mean it's safe to publish that C code - the next version of that same compiler on that same platform might do something very different. With machine code (especially x86, with its very friendly memory model) that's unlikely. (There are cases lik…
Re: Rust to C compiler – 95.9% test pass rate, odd platforms
#66Earlier quoted context omitted.
It could fail if the generated C code triggered Undefined Behavior. For example, signed overflow is UB in C, but defined in Rust. Generated code can't simply use the + operator. C has type-based alias analysis that makes some type casts illegal. Rust handles alias analysis through borrowing, so it's more forgiving about type casts. Rust has an UnsafeCell wrapper type for hacks that break the safe memory model and wou…
Wait until you find out how unsafe software written in the machine language that Rust usually transpiles to is.
For example, a C compiler is allowed to optimize the expression a+1 to false (if a is signed), but a Rust compiler isn't allowed to do this.
Re: Rust to C compiler – 95.9% test pass rate, odd platforms
#67Very cool. C to Rust would be fantastic.
> C to Rust would be fantastic. This would have to go into one big unsafe block for any nontrivial program. C doesn’t convey all of the explicit things you need to know about the code to make it even compile in Rust.
Re: Rust to C compiler – 95.9% test pass rate, odd platforms
#68Re: Rust to C compiler – 95.9% test pass rate, odd platforms
#69Earlier quoted context omitted.
Should be. The rust borrow checker has no runtime component. It checks the code as-is before (or during) compilation. Arguably it’s not the compiled binary that’s “safe”. It’s the code.
Borrow checker, and more generally any type checkers, are essentially terminating programs ran during compilation process, thus safety guarantees given by them are ensured before the code is transformed into some target language.
Re: Rust to C compiler – 95.9% test pass rate, odd platforms
#70Why would I use a tool that doesn't pass all tests?