News for hackers. Stuff that matters.
Re:Rust to C compiler – 95.9% test pass rate, odd platforms by pornel on Saturday April 12, 2025 @05:52AM (#43662955)
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 would be UB otherwise. C doesn't have such thing, so only uses of UnsafeCell that are already allowed by C are safe.

Re:Rust to C compiler – 95.9% test pass rate, odd platforms by FractalFir on Saturday April 12, 2025 @06:12AM (#43663047)
I have workarounds for all "simple" cases of UB in C(this is partially what the talk is about). The test code is running with `-fsantize=undefined`, and triggers no UB checks.

There are also escape hatches for strict aliasing in the C standard - mainly using memcpy for all memory operations.

Re:Rust to C compiler – 95.9% test pass rate, odd platforms by bregma on Saturday April 12, 2025 @06:27AM (#43663129)
Wait until you find out how unsafe software written in the machine language that Rust usually transpiles to is.
Re:Rust to C compiler – 95.9% test pass rate, odd platforms by adwn on Saturday April 12, 2025 @07:07AM (#43663365)
That's not the same, and not what pornel is talking about. The x86 ADD instruction has a well-defined behavior on overflow, and i32 + i32 in Rust will usually be translated to an ADD instruction, same as int + int in C. But a C compiler is allowed to assume that a signed addition operation will never overflow (the dreaded Undefined Behavior), while a Rust compiler must not make that assumption. This means that i32 + i32 must not be translated to int + int.

For example, a C compiler is allowed to optimize the expression a+1<a 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 by cryptonector on Saturday April 12, 2025 @11:11AM (#43665066)
> 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.

Obviously, yes, but it could generate overflow checks.