5 ms·
This is compiling C into very unsafe Rust as a target language: pub unsafe extern "C" fn insertion_sort(n: libc::c_int, p: *mut libc::c_int) -> () {
by Animats 8y ago
This is compiling C into very unsafe Rust as a target language:
pub unsafe extern "C" fn insertion_sort(n: libc::c_int, p: *mut libc::c_int)
-> () {
let mut i: libc::c_int = 1i32;
while i < n {
let tmp: libc::c_int = *p.offset(i as isize);
let mut j: libc::c_int = i;
while j > 0i32 && *p.offset((j - 1i32) as isize) > tmp {
*p.offset(j as isize) = *p.offset((j - 1i32) as isize);
j -= 1
}
*p.offset(j as isize) = tmp;
i += 1
};
}
The output is unmaintainable, like the output from a compiler. This isn't translation into usable Rust.
A C++ to Rust translator would be a good thing, but it has to be much smarter than this. This doesn't even comprehend arrays in C.
A good translator might need some human help to deal with C's ambiguities. Like "Is this an array? (Y or N)" How big is it? (Enter expression). Then you get a program that's real Rust. If the user is wrong about the size, safe Rust will either allocate too much space or get a subscript error.
- civilitty 8y agoThe problem is the type noise, which may or may not be avoidable. Most of the mess you see there is probably better expressed as std::mem::* function calls which give proper names to the operations (you can see one of them, .offset, used here but it's a very general function). I don't think it's a good idea to try to convert C code like this to more complex std::mem calls automatically but once you have the ugly Rust code, it becomes relatively easy to start porting it to be more Rustic. The bigger issue I see here is that this should be implemented as an iterator. I don't think there's any hope of automatically converting to those though - there's just no C equivalent except pointer arithmetic.
- wtetzner 8y agoEven though it's translating to unsafe Rust, it does still have some value. In particular, C libraries can be converted into Rust libraries that can be managed via Cargo, avoiding the need to deal with C tooling.
- smt88 8y agoIs that benefit worth the potential for inaccurate translation? Or is it possible to be 100% certain that the Rust functions identically to the C?
- staticassertion 8y agoWell if you translate it to safe rust at least the worst that could happen is a design flaw. And you can write oracle tests to catch those.
- smt88 8y ago> the worst that could happen is a design flaw That's still pretty bad, especially for low-level legacy libraries that aren't supposed to break userspace under any circumstances.
- staticassertion 8y agoHence the need for oracle tests.
- floatboth 8y agoAs a user of a C library in Cargo, you don't need to deal with C tooling, just have it installed.
- wtetzner 8y agoThat means anyone that wants to use a Rust crate that depends on a C library has to somehow install the C library. It's possible that's easy through a package manager, or maybe it's not, and you have to build the C library, and now you have to deal with C tooling.
- steveklabnik 8y agoSome packages give you the option to check for a preinstalled package, and build it if it’s not there. It works pretty cleanly, but it is true that one toolchain is easier than two.
- bluejekyll 8y ago> This is compiling C into very unsafe Rust as a target language It’s basically as safe as the original. > This isn't translation into usable Rust It’s slightly more usable, and easier, than FFI to raw C (see other comments about Cargo integration). I’ve been thinking about language rewrites like this, and generally I’m really torn. If it’s the intention to get rid of your C, it might in fact be easier to use bindgen for the C header translation, and create decent Rustful interfaces that abstract the C. After that maybe use this tool as a starting point to get Rust or possibly just rewrite the section by hand. In either case you’ll be rewriting the logic into safe Rust at some point, otherwise I don’t see a huge point in doing this (other than the Cargo integration, though there are other options for that).
- staticassertion 8y agoThe premise here must be that the translator (human) has the original C code. So answering questions like "is this an array" is as simple as reading the C code to determine that. Allocating too much space or getting a subscript error seems preferable to unsafety, and something one can hammer out with oracle testing. Actually, it would be cool if there were generated quickcheck-like oracle testcases alongside this so you could start your translation to safe rust and test along the way.
- Animats 8y agoSo answering questions like "is this an array" is as simple as reading the C code to determine that. If only. Here's the original function definition: void insertion_sort(int const n, int * const p) "p" is really an array. But C doesn't know that. Rust needs to know. Around 2010, I proposed an extension to C [1] in which you'd write that as void insertion_sort (int const n, int& p[n]) Instead of a pointer, you'd use a reference to an array. Same calling sequence - the compiler passes a pointer. But with enough info that the callee knows the array size. Rust needs that info to do its job. Somehow, you have to get that info into a C to Rust translator. [1] http://www.animats.com/papers/languages/safearraysforc43.pdf http://www.animats.com/papers/languages/safearraysforc43.pdf
- staticassertion 8y agoIt is as simple as the C code, whether that is simple or not.
- dbaupp 8y agoI know and agree that unsafe Rust is problematic, but that criticism of this is missing the forest for the trees. The original code is just as unsafe. Refactoring from unsafe Rust into safe Rust manually is likely to be easier than directly from C into Rust: one is only trying to deal with one small delta (like changing length/pointer arguments into a slice) rather than also having to deal with syntax changes and tooling issues. As others have said, it is essentially impossible to automatically translate to safe Rust without non-trivial annotation effort, since C the language and C in practice is very different to how Rust guarantees safety. It seems to me that that "annotation" effort is probably better spent going unsafe to safe Rust (where, subjectively, the language is nicer to use, like better enums and less error prone control flow) rather than annotating the original C.
- stefan_ 8y agoI think it is you missing the forest for the trees. The majority of C code does not in fact have unsafe memory accesses. So clearly we have a problem here with the translator being too stupid or the language; neither is something I look for in a tool I use.
- dbaupp 8y agoNo, all memory accesses in C are unsafe in the keyword-in-Rust sense: `unsafe` indicates that an API can be misused to cause undefined behaviour, contrasting to non-`unsafe` code where any potential undefined behaviours will be caught at compile time or runtime, no matter how incorrect the program/use of the API is. Just like C, it is an incorrect Rust program if undefined behaviour actually happens, meaning all uses of `unsafe` have to actually be safe, but it's up to the programmer to ensure this, with less compiler assistance than in normal Rust. Every single pointer access `*p` or `p[i]` in C has potential for undefined behaviour (e.g. p is NULL, points to an invalid object, or i is out of bounds), and thus the Rust equivalent is surfacing this risk in a more obvious way.
- dbaupp 8y ago(It would have been clearer if my original comment said: "The C and the Rust both risk undefined behaviour in the same ways", instead of using an overloaded meaning of unsafe in saying that the C is unsafe.)