How is this issue big? One can implement wrapping signed integer arithmetic in plain C, so we would just need a support C library that provides wrapping_add and that’s what we would lower the Rust code to.
@comex
And it doesn’t address other cfg keys, such as target_os .
A C support library can do this as well. Rust doesn’t really do anything magical here.
Also, we could initially require transpiled C code to be compiled with gcc... -DRUST_TARGET_OS=darwin and call it a day.
But if you have portable Rust code that uses, say, libc , you can’t necessarily convert it to portable C code that uses libc. To do so, you’d need a sophisticated mechanism to allow for “constant expressions” that aren’t actually known at compile time, such as sizeof(some_libc_struct)
I don’t understand this point. libc could be converted to C code (its just extern functions), and if the values of the cfg() macros are know it should generate a C wrapper that is 100% ABI-identical with that of the platform; everything else is a bug in libc which is why this is actually tested by the ctest crate on every libc commit.
@josh
Rather than fixing this by transpiling to C, why not handle it by porting LLVM to those platforms?
Because porting LLVM to all platforms that every C compiler was ever able to target is a huge amount of work.
@matthieum
Translating Rust or MIR to C is going to probably run into very similar problems than translating LLVM-IR to C did.
IIRC the main problem is that MIR, LLVM-IR, etc. are all target dependent, which means that if the Rust toolchain generating the MIR or LLVM-IR does not support a particular target (or generates them for a slightly different target), the generated C code won’t probably compile with the C compiler that the end user wants to use.
I think this means that we cannot generate C from MIR because macros are expanded well before that. We would have to generate C directly from Rust, expanding Rust macros to C macros and maybe even expanding every “portable” operation to calls into a call into a C support library.
We don’t even have to provide this C support library, we can just transpile to C and expect the user to provide it for the platform they want to use. Such support libraries will probably be huge.